rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Darin Petkov | 47974d4 | 2011-03-03 15:55:04 -0800 | [diff] [blame] | 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # Script to enter the chroot environment |
| 8 | |
J. Richard Barnette | 8e6750d | 2011-08-22 12:35:52 -0700 | [diff] [blame] | 9 | SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..) |
| 10 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 11 | |
Brian Harring | feb04f7 | 2012-02-03 21:22:50 -0800 | [diff] [blame] | 12 | enable_strict_sudo |
| 13 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 14 | # Script must be run outside the chroot and as a regular user. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 15 | assert_outside_chroot |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 16 | assert_not_root_user |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 17 | |
| 18 | # Define command line flags |
| 19 | # See http://code.google.com/p/shflags/wiki/Documentation10x |
| 20 | DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \ |
| 21 | "The destination dir for the chroot environment." "d" |
| 22 | DEFINE_string trunk "$GCLIENT_ROOT" \ |
| 23 | "The source trunk to bind mount within the chroot." "s" |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 24 | DEFINE_string build_number "" \ |
| 25 | "The build-bot build number (when called by buildbot only)." "b" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 26 | DEFINE_string chrome_root "" \ |
| 27 | "The root of your chrome browser source. Should contain a 'src' subdir." |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 28 | DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \ |
| 29 | "The mount point of the chrome broswer source in the chroot." |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 30 | DEFINE_string distfiles "" \ |
| 31 | "Override the destination dir used for distfiles." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 32 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 33 | DEFINE_boolean official_build $FLAGS_FALSE \ |
| 34 | "Set CHROMEOS_OFFICIAL=1 for release builds." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 35 | DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts." |
| 36 | DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts." |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 37 | DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent." |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 38 | DEFINE_boolean early_make_chroot $FLAGS_FALSE \ |
| 39 | "Internal flag. If set, the command is run as root without sudo." |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 40 | DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 41 | |
| 42 | # More useful help |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 43 | FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...] |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 44 | |
| 45 | One or more VAR=value pairs can be specified to export variables into |
| 46 | the chroot environment. For example: |
| 47 | |
| 48 | $0 FOO=bar BAZ=bel |
| 49 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 50 | If [-- command] is present, runs the command inside the chroot, |
| 51 | after changing directory to /$USER/trunk/src/scripts. Note that neither |
| 52 | the command nor args should include single quotes. For example: |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 53 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 54 | $0 -- ./build_platform_packages.sh |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 55 | |
| 56 | Otherwise, provides an interactive shell. |
| 57 | " |
| 58 | |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 59 | CROS_LOG_PREFIX=cros_sdk:enter_chroot |
| 60 | |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 61 | # Version of info from common.sh that only echos if --verbose is set. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 62 | debug() { |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 63 | if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 64 | info "$*" |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 65 | fi |
| 66 | } |
| 67 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 68 | # Parse command line flags |
| 69 | FLAGS "$@" || exit 1 |
| 70 | eval set -- "${FLAGS_ARGV}" |
| 71 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 72 | if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 73 | CHROMEOS_OFFICIAL=1 |
| 74 | fi |
| 75 | |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 76 | [ -z "${FLAGS_distfiles}" ] && \ |
| 77 | FLAGS_distfiles="${FLAGS_trunk}/distfiles" |
| 78 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 79 | # Only now can we die on error. shflags functions leak non-zero error codes, |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 80 | # so will die prematurely if 'switch_to_strict_mode' is specified before now. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 81 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 82 | switch_to_strict_mode |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 83 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 84 | # These config files are to be copied into chroot if they exist in home dir. |
| 85 | FILES_TO_COPY_TO_CHROOT=( |
| 86 | .gdata_cred.txt # User/password for Google Docs on chromium.org |
| 87 | .gdata_token # Auth token for Google Docs on chromium.org |
| 88 | .disable_build_stats_upload # Presence of file disables command stats upload |
| 89 | ) |
| 90 | |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 91 | INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 92 | CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 93 | INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 94 | FUSE_DEVICE="/dev/fuse" |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 95 | AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount" |
| 96 | SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 97 | |
Mike Frysinger | 8264917 | 2011-09-21 00:18:14 -0400 | [diff] [blame] | 98 | # Avoid the sudo call if possible since it is a little slow. |
| 99 | if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then |
| 100 | sudo chmod 0777 "$FLAGS_chroot/var/lock" |
| 101 | fi |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 102 | |
| 103 | LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 104 | SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 105 | |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 106 | |
Mike Frysinger | 0a0e6ec | 2011-09-28 11:57:21 -0400 | [diff] [blame] | 107 | MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 108 | mount_queue_init() { |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 109 | MOUNT_QUEUE=() |
| 110 | } |
| 111 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 112 | queue_mount() { |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 113 | # If necessary, mount $source in the host FS at $target inside the |
| 114 | # chroot directory with $mount_args. |
| 115 | local source="$1" |
| 116 | local mount_args="$2" |
| 117 | local target="$3" |
| 118 | |
Mike Frysinger | 0a0e6ec | 2011-09-28 11:57:21 -0400 | [diff] [blame] | 119 | local mounted_path="${MOUNTED_PATH}$target" |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 120 | |
Mike Frysinger | 9e5b0a4 | 2012-05-16 17:30:24 -0400 | [diff] [blame] | 121 | case " ${MOUNT_CACHE} " in |
| 122 | *" ${mounted_path} "*) |
Mike Frysinger | 2a97059 | 2011-09-20 22:49:01 -0400 | [diff] [blame] | 123 | # Already mounted! |
| 124 | ;; |
| 125 | *) |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 126 | MOUNT_QUEUE+=( "mkdir -p '${mounted_path}'" ) |
| 127 | # The args are left unquoted on purpose. |
| 128 | if [[ -n ${source} ]]; then |
| 129 | MOUNT_QUEUE+=( "mount ${mount_args} '${source}' '${mounted_path}'" ) |
| 130 | else |
| 131 | MOUNT_QUEUE+=( "mount ${mount_args} '${mounted_path}'" ) |
| 132 | fi |
Mike Frysinger | 2a97059 | 2011-09-20 22:49:01 -0400 | [diff] [blame] | 133 | ;; |
| 134 | esac |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 137 | process_mounts() { |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 138 | if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then |
| 139 | return 0 |
| 140 | fi |
| 141 | sudo_multi "${MOUNT_QUEUE[@]}" |
| 142 | mount_queue_init |
| 143 | } |
| 144 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 145 | env_sync_proc() { |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 146 | # This function runs and performs periodic updates to the chroot env, if |
| 147 | # necessary. |
| 148 | |
| 149 | local poll_interval=10 |
Mike Frysinger | 7f7a763 | 2011-09-28 11:48:20 -0400 | [diff] [blame] | 150 | local sync_files=( etc/resolv.conf etc/hosts ) |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 151 | |
Mike Frysinger | 4d8c285 | 2012-05-16 15:22:49 -0400 | [diff] [blame] | 152 | # Make sure the files exist before the find -- they might not in a |
| 153 | # fresh chroot which results in warnings in the build output. |
| 154 | local chown_cmd=( |
| 155 | # Make sure the files exists first -- they might not in a fresh chroot. |
| 156 | "touch ${sync_files[*]/#/${FLAGS_chroot}/}" |
| 157 | # Make sure the files are writable by normal user so that we don't have |
| 158 | # to execute sudo in the main loop below. |
| 159 | "chown ${USER} ${sync_files[*]/#/${FLAGS_chroot}/}" |
| 160 | ) |
| 161 | sudo_multi "${chown_cmd[@]}" |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 162 | |
David James | 412b902 | 2011-07-15 19:54:16 -0700 | [diff] [blame] | 163 | # Drop stdin, stderr, stdout, and chroot lock. |
| 164 | # This is needed for properly daemonizing the process. |
| 165 | exec 0>&- 1>&- 2>&- 200>&- |
| 166 | |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 167 | while true; do |
| 168 | # Sync files |
Ryan Cui | fe573cd | 2012-09-27 15:58:42 -0700 | [diff] [blame^] | 169 | for file in "${sync_files[@]}"; do |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 170 | if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then |
| 171 | cp -f /${file} ${FLAGS_chroot}/${file} |
| 172 | fi |
| 173 | done |
| 174 | |
| 175 | sleep ${poll_interval} |
| 176 | done |
| 177 | } |
| 178 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 179 | copy_ssh_config() { |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 180 | # Copy user .ssh/config into the chroot filtering out strings not supported |
| 181 | # by the chroot ssh. The chroot .ssh directory is passed in as the first |
| 182 | # parameter. |
| 183 | |
| 184 | # ssh options to filter out. The entire strings containing these substrings |
| 185 | # will be deleted before copying. |
| 186 | local bad_options=( |
Matt Tennant | 63c3cc5 | 2011-11-22 11:23:06 -0800 | [diff] [blame] | 187 | 'UseProxyIf' |
| 188 | 'GSSAPIAuthentication' |
| 189 | 'GSSAPIKeyExchange' |
| 190 | 'ProxyUseFdpass' |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 191 | ) |
| 192 | local sshc="${HOME}/.ssh/config" |
| 193 | local chroot_ssh_dir="${1}" |
| 194 | local filter |
| 195 | local option |
| 196 | |
| 197 | if [ ! -f "${sshc}" ]; then |
| 198 | return # Nothing to copy. |
| 199 | fi |
| 200 | |
| 201 | for option in "${bad_options[@]}" |
| 202 | do |
| 203 | if [ -z "${filter}" ]; then |
| 204 | filter="${option}" |
| 205 | else |
| 206 | filter+="\\|${option}" |
| 207 | fi |
| 208 | done |
| 209 | |
| 210 | sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config" |
| 211 | } |
| 212 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 213 | copy_into_chroot_if_exists() { |
Matt Tennant | f7c9e77 | 2012-02-08 17:06:26 -0800 | [diff] [blame] | 214 | # $1 is file path outside of chroot to copy to path $2 inside chroot. |
| 215 | [ -e "$1" ] || return |
| 216 | cp "$1" "${FLAGS_chroot}/$2" |
| 217 | } |
| 218 | |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 219 | # Usage: promote_api_keys |
| 220 | # This takes care of getting the developer API keys into the chroot where |
| 221 | # chrome can build with them. It needs to take it from the places a dev |
| 222 | # is likely to put them, and recognize that older chroots may or may not |
| 223 | # have been used since the concept of keys got added, as well as before |
| 224 | # and after the developer decding to grab his own keys. |
| 225 | promote_api_keys() { |
| 226 | local destination="${FLAGS_chroot}/home/${USER}/.googleapikeys" |
| 227 | # Don't disturb existing keys. They could be set differently |
| 228 | if [[ -s "${destination}" ]]; then |
| 229 | return 0 |
| 230 | fi |
| 231 | if [[ -r "${HOME}/.googleapikeys" ]]; then |
| 232 | cp "${HOME}/.googleapikeys" "${destination}" |
| 233 | if [[ -s "${destination}" ]] ; then |
| 234 | info "Copied Google API keys into chroot." |
| 235 | fi |
| 236 | elif [[ -r "${HOME}/.gyp/include.gypi" ]]; then |
| 237 | local NAME="('google_(api_key|default_client_(id|secret))')" |
| 238 | local WS="[[:space:]]*" |
| 239 | local CONTENTS="('[^\\\\']*')" |
| 240 | sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \ |
| 241 | "${HOME}/.gyp/include.gypi" >"${destination}" |
| 242 | if [[ -s "${destination}" ]]; then |
| 243 | info "Put discovered Google API keys into chroot." |
| 244 | fi |
| 245 | fi |
| 246 | } |
| 247 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 248 | setup_env() { |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 249 | # Validate sudo timestamp before entering the critical section so that we |
| 250 | # don't stall for a password while we have the lockfile. |
Doug Anderson | 3d7fa3a | 2011-02-02 16:10:34 -0800 | [diff] [blame] | 251 | # Don't use sudo -v since that has issues on machines w/ no password. |
| 252 | sudo echo "" > /dev/null |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 253 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 254 | ( |
| 255 | flock 200 |
| 256 | echo $$ >> "$LOCKFILE" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 257 | |
Taylor Hutt | 25bf949 | 2011-07-27 13:54:35 -0700 | [diff] [blame] | 258 | # If there isn't a syncer daemon started already, start one. The |
| 259 | # daemon is considered to not be started under the following |
| 260 | # conditions: |
| 261 | # |
| 262 | # o There is no PID file |
| 263 | # |
| 264 | # o The PID file is 0 bytes in size, which might be a partial |
| 265 | # manifestation of chromium-os:17680. This situation will not |
| 266 | # occur anymore, but you might have a chroot which was already |
| 267 | # affected. |
| 268 | # |
| 269 | # o The /proc node for the process named by the PID file does |
| 270 | # not exist. |
| 271 | # |
| 272 | # Note: This does not address PID recycling. While |
| 273 | # increasingly unlikely, it is possible for the PID in |
| 274 | # the PID file to refer to a running process that is not |
| 275 | # the syncer process. Since the PID file is now |
| 276 | # removed, I think it is only possible for this to occur |
| 277 | # if your system crashes and the PID file exists after |
| 278 | # restart. |
| 279 | # |
| 280 | # The daemon is killed by the enter_chroot that exits last. |
Taylor Hutt | 18594a8 | 2011-07-28 11:45:50 -0700 | [diff] [blame] | 281 | if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then |
Taylor Hutt | 25bf949 | 2011-07-27 13:54:35 -0700 | [diff] [blame] | 282 | info "You may have suffered from chromium-os:17680 and"; |
| 283 | info "could have stray 'enter_chroot.sh' processes running."; |
| 284 | info "You must manually kill any such stray processes."; |
| 285 | info "Exit all chroot shells; remaining 'enter_chroot.sh'"; |
| 286 | info "processes are probably stray."; |
Taylor Hutt | 18594a8 | 2011-07-28 11:45:50 -0700 | [diff] [blame] | 287 | sudo rm -f "${SYNCERPIDFILE}"; |
Taylor Hutt | 25bf949 | 2011-07-27 13:54:35 -0700 | [diff] [blame] | 288 | fi; |
David James | 412b902 | 2011-07-15 19:54:16 -0700 | [diff] [blame] | 289 | if ! [ -f "${SYNCERPIDFILE}" ] || \ |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 290 | ! [ -d /proc/$(<"${SYNCERPIDFILE}") ]; then |
David James | 412b902 | 2011-07-15 19:54:16 -0700 | [diff] [blame] | 291 | debug "Starting sync process" |
| 292 | env_sync_proc & |
| 293 | echo $! > "${SYNCERPIDFILE}" |
| 294 | disown $! |
| 295 | fi |
| 296 | |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 297 | debug "Mounting chroot environment." |
Mike Frysinger | 9e5b0a4 | 2012-05-16 17:30:24 -0400 | [diff] [blame] | 298 | MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts)) |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 299 | mount_queue_init |
| 300 | queue_mount none "-t proc" /proc |
| 301 | queue_mount none "-t sysfs" /sys |
| 302 | queue_mount /dev "--bind" /dev |
| 303 | queue_mount none "-t devpts" /dev/pts |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 304 | if [ -d /run ]; then |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 305 | queue_mount /run "--bind" /run |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 306 | if [ -d /run/shm ]; then |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 307 | queue_mount /run/shm "--bind" /run/shm |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 308 | fi |
| 309 | fi |
Brian Harring | f264b82 | 2012-09-01 01:39:03 -0700 | [diff] [blame] | 310 | # Get path overrides for the chroot in place now- it's possible |
| 311 | # that they may be needed for early teardown. |
| 312 | queue_mount "${FLAGS_trunk}/src/scripts/path-overrides" "--bind" \ |
| 313 | "/usr/local/path-overrides" |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 314 | |
Brian Harring | f264b82 | 2012-09-01 01:39:03 -0700 | [diff] [blame] | 315 | queue_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}" |
Chris Sosa | 389634d | 2012-09-05 19:56:53 -0700 | [diff] [blame] | 316 | |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 317 | debug "Setting up referenced repositories if required." |
| 318 | REFERENCE_DIR=$(git config --file \ |
| 319 | "${FLAGS_trunk}/.repo/manifests.git/config" \ |
| 320 | repo.reference) |
| 321 | if [ -n "${REFERENCE_DIR}" ]; then |
| 322 | |
Brian Harring | 334050f | 2012-06-08 17:40:58 -0700 | [diff] [blame] | 323 | ALTERNATES="${FLAGS_trunk}/.repo/alternates" |
| 324 | |
| 325 | # Ensure this directory exists ourselves, and has the correct ownership. |
| 326 | [ -d "${ALTERNATES}" ] || mkdir "${ALTERNATES}" |
| 327 | [ -w "${ALTERNATES}" ] || sudo chown -R "${USER}" "${ALTERNATES}" |
| 328 | |
| 329 | unset ALTERNATES |
| 330 | |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 331 | IFS=$'\n'; |
| 332 | required=( $( "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \ |
| 333 | "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) ) |
| 334 | unset IFS |
| 335 | |
| 336 | queue_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \ |
| 337 | "${CHROOT_TRUNK_DIR}/.repo/alternates" |
| 338 | |
| 339 | # Note that as we're bringing up each referened repo, we also |
| 340 | # mount bind an empty directory over its alternates. This is |
| 341 | # required to suppress git from tracing through it- we already |
| 342 | # specify the required alternates for CHROOT_TRUNK_DIR, no point |
| 343 | # in having git try recursing through each on their own. |
| 344 | # |
| 345 | # Finally note that if you're unfamiliar w/ chroot/vfs semantics, |
| 346 | # the bind is visible only w/in the chroot. |
| 347 | mkdir -p ${FLAGS_trunk}/.repo/chroot/empty |
| 348 | position=1 |
| 349 | for x in "${required[@]}"; do |
| 350 | base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}" |
| 351 | queue_mount "${x}" "--bind" "${base}" |
| 352 | if [ -e "${x}/.repo/alternates" ]; then |
| 353 | queue_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \ |
| 354 | "${base}/.repo/alternates" |
| 355 | fi |
| 356 | position=$(( ${position} + 1 )) |
| 357 | done |
| 358 | unset required position base |
| 359 | fi |
| 360 | unset REFERENCE_DIR |
| 361 | |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 362 | debug "Setting up shared distfiles directory." |
| 363 | mkdir -p "${FLAGS_distfiles}"/{target,host} |
| 364 | sudo mkdir -p "${FLAGS_chroot}/var/cache/distfiles/" |
| 365 | queue_mount "${FLAGS_distfiles}" "--bind" "/var/cache/distfiles" |
| 366 | |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 367 | if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 368 | if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 369 | TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh" |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 370 | mkdir -p "${TARGET_DIR}" |
Mike Frysinger | 40bb0c3 | 2011-10-31 17:14:15 -0400 | [diff] [blame] | 371 | # Ignore errors as some people won't have these files to copy. |
| 372 | cp "${HOME}"/.ssh/{known_hosts,*.pub} "${TARGET_DIR}/" 2>/dev/null || : |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 373 | copy_ssh_config "${TARGET_DIR}" |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 374 | |
| 375 | # Don't try to bind mount the ssh agent dir if it has gone stale. |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 376 | ASOCK=${SSH_AUTH_SOCK%/*} |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 377 | if [ -d "${ASOCK}" ]; then |
| 378 | queue_mount "${ASOCK}" "--bind" "${ASOCK}" |
| 379 | fi |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 380 | fi |
| 381 | fi |
| 382 | |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 383 | if [ -d "$HOME/.subversion" ]; then |
| 384 | TARGET="/home/${USER}/.subversion" |
| 385 | mkdir -p "${FLAGS_chroot}${TARGET}" |
| 386 | queue_mount "${HOME}/.subversion" "--bind" "${TARGET}" |
| 387 | fi |
| 388 | |
| 389 | if DEPOT_TOOLS=$(type -P gclient) ; then |
| 390 | DEPOT_TOOLS=${DEPOT_TOOLS%/*} # dirname |
| 391 | debug "Mounting depot_tools" |
| 392 | queue_mount "$DEPOT_TOOLS" --bind "$INNER_DEPOT_TOOLS_ROOT" |
| 393 | fi |
| 394 | |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 395 | # Mount additional directories as specified in .local_mounts file. |
| 396 | local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts" |
| 397 | if [[ -f ${local_mounts} ]]; then |
| 398 | info "Mounting local folders (read-only for safety concern)" |
| 399 | # format: mount_source |
| 400 | # or mount_source mount_point |
| 401 | # or # comments |
| 402 | local mount_source mount_point |
| 403 | while read mount_source mount_point; do |
| 404 | if [[ -z ${mount_source} ]]; then |
| 405 | continue |
| 406 | fi |
| 407 | # if only source is assigned, use source as mount point. |
| 408 | : ${mount_point:=${mount_source}} |
| 409 | debug " mounting ${mount_source} on ${mount_point}" |
| 410 | queue_mount "${mount_source}" "--bind" "${mount_point}" |
| 411 | # --bind can't initially be read-only so we have to do it via remount. |
| 412 | queue_mount "" "-o remount,ro" "${mount_point}" |
| 413 | done < <(sed -e 's:#.*::' "${local_mounts}") |
| 414 | fi |
| 415 | |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 416 | process_mounts |
| 417 | |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 418 | CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)" |
| 419 | if [ -z "$CHROME_ROOT" ]; then |
| 420 | CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
| 421 | 2>/dev/null || :)" |
| 422 | CHROME_ROOT_AUTO=1 |
| 423 | fi |
| 424 | if [[ -n "$CHROME_ROOT" ]]; then |
| 425 | if [[ ! -d "${CHROME_ROOT}/src" ]]; then |
| 426 | error "Not mounting chrome source" |
| 427 | sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
| 428 | if [[ ! "$CHROME_ROOT_AUTO" ]]; then |
| 429 | exit 1 |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 430 | fi |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 431 | else |
| 432 | debug "Mounting chrome source at: $INNER_CHROME_ROOT" |
| 433 | sudo bash -c "echo '$CHROME_ROOT' > \ |
| 434 | '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'" |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 435 | queue_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 436 | fi |
| 437 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 438 | |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 439 | process_mounts |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 440 | |
Mike Frysinger | acc4c9b | 2011-09-15 18:06:25 -0400 | [diff] [blame] | 441 | # Install fuse module. Skip modprobe when possible for slight |
| 442 | # speed increase when initializing the env. |
| 443 | if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 444 | sudo modprobe fuse 2> /dev/null ||\ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 445 | warn "-- Note: modprobe fuse failed. gmergefs will not work" |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 446 | fi |
| 447 | |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 448 | # Turn off automounting of external media when we enter the |
| 449 | # chroot; thus we don't have to worry about being able to unmount |
| 450 | # from inside. |
| 451 | if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then |
| 452 | if [ "${SAVED_PREF}" != "false" ]; then |
| 453 | if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then |
| 454 | warn "-- Note: USB sticks may be automounted by your host OS." |
| 455 | warn "-- Note: If you plan to burn bootable media, you may need to" |
| 456 | warn "-- Note: unmount these devices manually, or run image_to_usb.sh" |
| 457 | warn "-- Note: outside the chroot." |
| 458 | fi |
| 459 | fi |
| 460 | fi |
| 461 | # Always write the temp file so we can read it when exiting |
| 462 | echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}" |
| 463 | |
Mike Frysinger | 926a4b9 | 2012-06-27 15:22:28 -0400 | [diff] [blame] | 464 | # Fix permissions on ccache tree. If this is a fresh chroot, then they |
| 465 | # might not be set up yet. Or if the user manually `rm -rf`-ed things, |
| 466 | # we need to reset it. Otherwise, gcc itself takes care of fixing things |
| 467 | # on demand, but only when it updates. |
| 468 | ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache" |
| 469 | if [[ ! -d ${ccache_dir} ]]; then |
| 470 | sudo mkdir -p -m 2775 "${ccache_dir}" |
| 471 | fi |
| 472 | sudo find -H "${ccache_dir}" -type d -exec chmod 2775 {} + & |
| 473 | sudo find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} + & |
| 474 | |
Mike Frysinger | 94717e3 | 2011-09-21 00:10:44 -0400 | [diff] [blame] | 475 | # Configure committer username and email in chroot .gitconfig. Change |
| 476 | # to the root directory first so that random $PWD/.git/config settings |
| 477 | # do not get picked up. We want to stick to ~/.gitconfig only. |
| 478 | ident=$(cd /; git var GIT_COMMITTER_IDENT || :) |
| 479 | ident_name=${ident%% <*} |
| 480 | ident_email=${ident%%>*}; ident_email=${ident_email##*<} |
David James | 342f156 | 2011-07-15 15:21:18 -0700 | [diff] [blame] | 481 | git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ |
Mike Frysinger | 94717e3 | 2011-09-21 00:10:44 -0400 | [diff] [blame] | 482 | user.name "${ident_name}" || true |
David James | 342f156 | 2011-07-15 15:21:18 -0700 | [diff] [blame] | 483 | git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ |
Mike Frysinger | 94717e3 | 2011-09-21 00:10:44 -0400 | [diff] [blame] | 484 | user.email "${ident_email}" || true |
David James | 342f156 | 2011-07-15 15:21:18 -0700 | [diff] [blame] | 485 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 486 | # Certain files get copied into the chroot when entering. |
| 487 | for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do |
Matt Tennant | f7c9e77 | 2012-02-08 17:06:26 -0800 | [diff] [blame] | 488 | copy_into_chroot_if_exists "${HOME}/${fn}" "/home/${USER}/${fn}" |
| 489 | done |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 490 | promote_api_keys |
Matt Tennant | d4316fe | 2011-08-30 12:06:42 -0700 | [diff] [blame] | 491 | |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 492 | # Make sure user's requested locales are available |
| 493 | # http://crosbug.com/19139 |
Mike Frysinger | 4fc9c27 | 2011-08-17 15:23:19 -0400 | [diff] [blame] | 494 | # And make sure en_US{,.UTF-8} are always available as |
| 495 | # that what buildbot forces internally |
| 496 | locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \ |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 497 | $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \ |
| 498 | $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \ |
| 499 | sort -u | sed '/^C$/d') |
| 500 | gen_locales=() |
| 501 | for l in ${locales}; do |
| 502 | if [[ ${l} == *.* ]]; then |
| 503 | enc=${l#*.} |
| 504 | else |
| 505 | enc="ISO-8859-1" |
| 506 | fi |
| 507 | case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in |
| 508 | utf8) enc="UTF-8";; |
| 509 | esac |
| 510 | gen_locales=("${gen_locales[@]}" "${l} ${enc}") |
| 511 | done |
Mike Frysinger | 4d258cd | 2011-09-15 16:17:49 -0400 | [diff] [blame] | 512 | if [[ ${#gen_locales[@]} -gt 0 ]] ; then |
| 513 | # Force LC_ALL=C to workaround slow string parsing in bash |
| 514 | # with long multibyte strings. Newer setups have this fixed, |
| 515 | # but locale-gen doesn't need to be run in any locale in the |
| 516 | # first place, so just go with C to keep it fast. |
| 517 | sudo -- chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \ |
| 518 | -G "$(printf '%s\n' "${gen_locales[@]}")" |
| 519 | fi |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 520 | |
Dale Curtis | 9863173 | 2011-05-05 16:16:59 -0700 | [diff] [blame] | 521 | # Fix permissions on shared memory to allow non-root users access to POSIX |
Mike Frysinger | 8264917 | 2011-09-21 00:18:14 -0400 | [diff] [blame] | 522 | # semaphores. Avoid the sudo call if possible (sudo is slow). |
| 523 | if [ -n "$(find "${FLAGS_chroot}/dev/shm" ! -perm 777)" ] ; then |
| 524 | sudo chmod -R 777 "${FLAGS_chroot}/dev/shm" |
| 525 | fi |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 526 | |
| 527 | # If the private overlays are installed, gsutil can use those credentials. |
Gilad Arnold | 264f64d | 2012-09-06 14:01:45 -0700 | [diff] [blame] | 528 | # We're also installing credentials for use by sudoed invocations. |
| 529 | boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto' |
| 530 | if [ -s "${FLAGS_trunk}/${boto}" ]; then |
| 531 | if [ ! -e "${FLAGS_chroot}/home/${USER}/.boto" ]; then |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 532 | ln -s "trunk/${boto}" "${FLAGS_chroot}/home/${USER}/.boto" |
| 533 | fi |
Gilad Arnold | 264f64d | 2012-09-06 14:01:45 -0700 | [diff] [blame] | 534 | if [ ! -e "${FLAGS_chroot}/root/.boto" ]; then |
| 535 | sudo ln -s "../home/${USER}/trunk/${boto}" "${FLAGS_chroot}/root/.boto" |
| 536 | fi |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 537 | fi |
| 538 | |
| 539 | # Have found a few chroots where ~/.gsutil is owned by root:root, probably |
| 540 | # as a result of old gsutil or tools. This causes permission errors when |
| 541 | # gsutil cp tries to create its cache files, so ensure the user can |
| 542 | # actually write to their directory. |
| 543 | gsutil_dir="${FLAGS_chroot}/home/${USER}/.gsutil" |
| 544 | if [ -d "${gsutil_dir}" ] && [ ! -w "${gsutil_dir}" ]; then |
| 545 | sudo chown -R "${USER}:$(id -gn)" "${gsutil_dir}" |
| 546 | fi |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 547 | ) 200>>"$LOCKFILE" || die "setup_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 550 | teardown_env() { |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 551 | # Validate sudo timestamp before entering the critical section so that we |
| 552 | # don't stall for a password while we have the lockfile. |
Doug Anderson | 3d7fa3a | 2011-02-02 16:10:34 -0800 | [diff] [blame] | 553 | # Don't use sudo -v since that has issues on machines w/ no password. |
| 554 | sudo echo "" > /dev/null |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 555 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 556 | # Only teardown if we're the last enter_chroot to die |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 557 | ( |
| 558 | flock 200 |
| 559 | |
| 560 | # check each pid in $LOCKFILE to see if it's died unexpectedly |
| 561 | TMP_LOCKFILE="$LOCKFILE.tmp" |
| 562 | |
| 563 | echo -n > "$TMP_LOCKFILE" # Erase/reset temp file |
| 564 | cat "$LOCKFILE" | while read PID; do |
| 565 | if [ "$PID" = "$$" ]; then |
| 566 | # ourself, leave PROC_NAME empty |
| 567 | PROC_NAME="" |
| 568 | else |
| 569 | PROC_NAME=$(ps --pid $PID -o comm=) |
| 570 | fi |
| 571 | |
| 572 | if [ ! -z "$PROC_NAME" ]; then |
| 573 | # All good, keep going |
| 574 | echo "$PID" >> "$TMP_LOCKFILE" |
| 575 | fi |
| 576 | done |
| 577 | # Remove any dups from lock file while installing new one |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 578 | sort -u -n "$TMP_LOCKFILE" > "$LOCKFILE" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 579 | |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 580 | SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") |
| 581 | if [ "${SAVED_PREF}" != "false" ]; then |
| 582 | gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \ |
| 583 | warn "could not re-set your automount preference." |
| 584 | fi |
| 585 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 586 | if [ -s "$LOCKFILE" ]; then |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 587 | debug "At least one other pid is running in the chroot, so not" |
| 588 | debug "tearing down env." |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 589 | else |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 590 | debug "Stopping syncer process" |
Taylor Hutt | 25bf949 | 2011-07-27 13:54:35 -0700 | [diff] [blame] | 591 | # If another process entering the chroot is blocked on this |
| 592 | # flock in setup_env(), it can be a race condition. |
| 593 | # |
| 594 | # When this locked region is exited, the setup_env() flock can |
| 595 | # be entered before the script can exit and the /proc entry for |
| 596 | # the PID is removed. The newly-created chroot will not end up |
| 597 | # with a syncer process. To avoid that situation, remove the |
| 598 | # syncer PID file. |
| 599 | # |
| 600 | # The syncer PID file should also be removed because the kernel |
| 601 | # will reuse PIDs. It's possible that the PID in the syncer PID |
| 602 | # has been reused by another process; make sure we don't skip |
| 603 | # starting the syncer process when this occurs by deleting the |
| 604 | # PID file. |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 605 | kill $(<"${SYNCERPIDFILE}") && \ |
Mike Frysinger | 470be99 | 2011-09-28 11:53:56 -0400 | [diff] [blame] | 606 | { rm -f "${SYNCERPIDFILE}" 2>/dev/null || \ |
| 607 | sudo rm -f "${SYNCERPIDFILE}" ; } || |
| 608 | debug "Unable to clean up syncer process."; |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 609 | |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 610 | debug "Unmounting chroot environment." |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 611 | safe_umount_tree "${MOUNTED_PATH}/" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 612 | fi |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 613 | ) 200>>"$LOCKFILE" || die "teardown_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 616 | if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 617 | setup_env |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 618 | info "Make sure you run" |
| 619 | info " $0 --unmount" |
| 620 | info "before deleting $FLAGS_chroot" |
| 621 | info "or you'll end up deleting $FLAGS_trunk too!" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 622 | exit 0 |
| 623 | fi |
| 624 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 625 | if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 626 | teardown_env |
| 627 | exit 0 |
| 628 | fi |
| 629 | |
| 630 | # Make sure we unmount before exiting |
| 631 | trap teardown_env EXIT |
| 632 | setup_env |
| 633 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 634 | CHROOT_PASSTHRU=( |
| 635 | "BUILDBOT_BUILD=$FLAGS_build_number" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 636 | "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 637 | "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 638 | ) |
Liam McLoughlin | 3b11b45 | 2011-03-14 16:04:07 -0700 | [diff] [blame] | 639 | |
Brian Harring | 06d3c2e | 2012-08-23 07:35:43 -0700 | [diff] [blame] | 640 | # Add the standard proxied variables, and a few we specifically |
| 641 | # export for script usage; USE/GCC_GITHASH are for ebuilds/portage, |
| 642 | # CHROMEOS_VERSION_* is for cros_set_lsb_release and local AU server |
| 643 | # (builders export this for marking reasons). |
| 644 | KEEP_VARS=( |
| 645 | CHROMEOS_VERSION_{TRACK,AUSERVER,DEVSERVER} |
| 646 | USE GCC_GITHASH |
| 647 | ) |
| 648 | for var in "${ENVIRONMENT_WHITELIST[@]}" "${KEEP_VARS[@]}"; do |
| 649 | [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" ) |
Mario Limonciello | 7052ae1 | 2011-05-05 12:00:49 -0500 | [diff] [blame] | 650 | done |
| 651 | |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 652 | # Run command or interactive shell. Also include the non-chrooted path to |
| 653 | # the source trunk for scripts that may need to print it (e.g. |
| 654 | # build_image.sh). |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 655 | |
| 656 | if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then |
| 657 | cmd=( /bin/bash -l -c 'env "$@"' -- ) |
| 658 | elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then |
| 659 | # Complain that sudo is missing. |
| 660 | error "Failing since the chroot lacks sudo." |
| 661 | error "Requested enter_chroot command was: $@" |
| 662 | exit 127 |
| 663 | else |
| 664 | cmd=( sudo -i -u "$USER" ) |
| 665 | fi |
| 666 | |
| 667 | sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" "$@" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 668 | |
| 669 | # Remove trap and explicitly unmount |
| 670 | trap - EXIT |
| 671 | teardown_env |