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 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 9 | # --- BEGIN COMMON.SH BOILERPLATE --- |
| 10 | # Load common CrOS utilities. Inside the chroot this file is installed in |
| 11 | # /usr/lib/crosutils. Outside the chroot we find it relative to the script's |
| 12 | # location. |
| 13 | find_common_sh() { |
| 14 | local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")")) |
| 15 | local path |
| 16 | |
| 17 | SCRIPT_ROOT= |
| 18 | for path in "${common_paths[@]}"; do |
| 19 | if [ -r "${path}/common.sh" ]; then |
| 20 | SCRIPT_ROOT=${path} |
| 21 | break |
| 22 | fi |
| 23 | done |
| 24 | } |
| 25 | |
| 26 | find_common_sh |
| 27 | . "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1) |
| 28 | # --- END COMMON.SH BOILERPLATE --- |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 29 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 30 | # 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] | 31 | assert_outside_chroot |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 32 | assert_not_root_user |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 33 | |
| 34 | # Define command line flags |
| 35 | # See http://code.google.com/p/shflags/wiki/Documentation10x |
| 36 | DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \ |
| 37 | "The destination dir for the chroot environment." "d" |
| 38 | DEFINE_string trunk "$GCLIENT_ROOT" \ |
| 39 | "The source trunk to bind mount within the chroot." "s" |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 40 | DEFINE_string build_number "" \ |
| 41 | "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] | 42 | DEFINE_string chrome_root "" \ |
| 43 | "The root of your chrome browser source. Should contain a 'src' subdir." |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 44 | DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \ |
| 45 | "The mount point of the chrome broswer source in the chroot." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 46 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 47 | DEFINE_boolean official_build $FLAGS_FALSE \ |
| 48 | "Set CHROMEOS_OFFICIAL=1 for release builds." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 49 | DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts." |
| 50 | DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts." |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 51 | DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent." |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 52 | DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 53 | |
| 54 | # More useful help |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 55 | FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...] |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 56 | |
| 57 | One or more VAR=value pairs can be specified to export variables into |
| 58 | the chroot environment. For example: |
| 59 | |
| 60 | $0 FOO=bar BAZ=bel |
| 61 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 62 | If [-- command] is present, runs the command inside the chroot, |
| 63 | after changing directory to /$USER/trunk/src/scripts. Note that neither |
| 64 | the command nor args should include single quotes. For example: |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 65 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 66 | $0 -- ./build_platform_packages.sh |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 67 | |
| 68 | Otherwise, provides an interactive shell. |
| 69 | " |
| 70 | |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 71 | # Version of info from common.sh that only echos if --verbose is set. |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 72 | function debug { |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 73 | if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 74 | info "$*" |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 75 | fi |
| 76 | } |
| 77 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 78 | # Parse command line flags |
| 79 | FLAGS "$@" || exit 1 |
| 80 | eval set -- "${FLAGS_ARGV}" |
| 81 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 82 | if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 83 | CHROMEOS_OFFICIAL=1 |
| 84 | fi |
| 85 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 86 | # Only now can we die on error. shflags functions leak non-zero error codes, |
| 87 | # so will die prematurely if 'set -e' is specified before now. |
| 88 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
| 89 | set -e |
| 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 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 98 | sudo chmod 0777 "$FLAGS_chroot/var/lock" |
| 99 | |
| 100 | LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 101 | SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 102 | |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 103 | |
| 104 | function ensure_mounted { |
| 105 | # If necessary, mount $source in the host FS at $target inside the |
| 106 | # chroot directory with $mount_args. |
| 107 | local source="$1" |
| 108 | local mount_args="$2" |
| 109 | local target="$3" |
| 110 | |
| 111 | local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")" |
| 112 | |
| 113 | if [ -z "$(mount | grep -F "on ${mounted_path} ")" ]; then |
David Rochberg | 33373ef | 2011-02-16 14:48:27 -0500 | [diff] [blame] | 114 | # Attempt to make the mountpoint as the user. This depends on the |
| 115 | # fact that all mountpoints that should be owned by root are |
| 116 | # already present. |
| 117 | mkdir -p "${mounted_path}" |
| 118 | |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 119 | # NB: mount_args deliberately left unquoted |
| 120 | debug mount ${mount_args} "${source}" "${mounted_path}" |
| 121 | sudo -- mount ${mount_args} "${source}" "${mounted_path}" || \ |
| 122 | die "Could not mount ${source} on ${mounted_path}" |
| 123 | fi |
| 124 | } |
| 125 | |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 126 | function env_sync_proc { |
| 127 | # This function runs and performs periodic updates to the chroot env, if |
| 128 | # necessary. |
| 129 | |
| 130 | local poll_interval=10 |
| 131 | local sync_files="etc/resolv.conf etc/hosts" |
| 132 | |
| 133 | # Make sure the synced files are writable by normal user, so that we |
| 134 | # don't have to sudo inside the loop. |
| 135 | for file in ${sync_files}; do |
| 136 | sudo chown ${USER} ${FLAGS_chroot}/${file} 1>&2 |
| 137 | done |
| 138 | |
| 139 | while true; do |
| 140 | # Sync files |
| 141 | for file in ${sync_files}; do |
| 142 | if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then |
| 143 | cp -f /${file} ${FLAGS_chroot}/${file} |
| 144 | fi |
| 145 | done |
| 146 | |
| 147 | sleep ${poll_interval} |
| 148 | done |
| 149 | } |
| 150 | |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 151 | function copy_ssh_config { |
| 152 | # Copy user .ssh/config into the chroot filtering out strings not supported |
| 153 | # by the chroot ssh. The chroot .ssh directory is passed in as the first |
| 154 | # parameter. |
| 155 | |
| 156 | # ssh options to filter out. The entire strings containing these substrings |
| 157 | # will be deleted before copying. |
| 158 | local bad_options=( |
| 159 | 'UseProxyIf=' |
| 160 | 'GSSAPIAuthentication no' |
| 161 | ) |
| 162 | local sshc="${HOME}/.ssh/config" |
| 163 | local chroot_ssh_dir="${1}" |
| 164 | local filter |
| 165 | local option |
| 166 | |
| 167 | if [ ! -f "${sshc}" ]; then |
| 168 | return # Nothing to copy. |
| 169 | fi |
| 170 | |
| 171 | for option in "${bad_options[@]}" |
| 172 | do |
| 173 | if [ -z "${filter}" ]; then |
| 174 | filter="${option}" |
| 175 | else |
| 176 | filter+="\\|${option}" |
| 177 | fi |
| 178 | done |
| 179 | |
| 180 | sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config" |
| 181 | } |
| 182 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 183 | function setup_env { |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 184 | # Validate sudo timestamp before entering the critical section so that we |
| 185 | # don't stall for a password while we have the lockfile. |
Doug Anderson | 3d7fa3a | 2011-02-02 16:10:34 -0800 | [diff] [blame] | 186 | # Don't use sudo -v since that has issues on machines w/ no password. |
| 187 | sudo echo "" > /dev/null |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 188 | |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 189 | # Syncer proc, but only the first time |
| 190 | if ! [ -f "${SYNCERPIDFILE}" ] || \ |
| 191 | ! [ -d /proc/$(cat "${SYNCERPIDFILE}") ]; then |
| 192 | debug "Starting sync process" |
| 193 | env_sync_proc & |
| 194 | echo $! > "${SYNCERPIDFILE}" |
| 195 | disown $! |
| 196 | fi |
| 197 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 198 | ( |
| 199 | flock 200 |
| 200 | echo $$ >> "$LOCKFILE" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 201 | |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 202 | debug "Mounting chroot environment." |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 203 | ensure_mounted none "-t proc" /proc |
| 204 | ensure_mounted none "-t sysfs" /sys |
| 205 | ensure_mounted /dev "--bind" /dev |
| 206 | ensure_mounted none "-t devpts" /dev/pts |
| 207 | ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 208 | |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 209 | if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then |
| 210 | TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 211 | if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 212 | mkdir -p "${TARGET_DIR}" |
| 213 | cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}" |
Elly Jones | 39ba1e5 | 2011-06-30 11:08:28 -0400 | [diff] [blame] | 214 | cp -r ${HOME}/.ssh/*.pub "${TARGET_DIR}" |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 215 | copy_ssh_config "${TARGET_DIR}" |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 216 | ASOCK="$(dirname "${SSH_AUTH_SOCK}")" |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 217 | ensure_mounted "${ASOCK}" "--bind" "${ASOCK}" |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 218 | fi |
| 219 | fi |
| 220 | |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 221 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 222 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
Andrew de los Reyes | c1e8d27 | 2010-02-13 12:39:21 -0800 | [diff] [blame] | 223 | ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 224 | if [ -z "$CHROME_ROOT" ]; then |
| 225 | ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
| 226 | 2>/dev/null)" |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 227 | CHROME_ROOT_AUTO=1 |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 228 | fi |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 229 | if [[ ( -n "$CHROME_ROOT" ) ]]; then |
| 230 | if [[ ( ! -d "${CHROME_ROOT}/src" ) ]]; then |
| 231 | error "Not mounting chrome source" |
| 232 | sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
| 233 | if [[ ! "$CHROME_ROOT_AUTO" ]]; then |
| 234 | exit 1 |
| 235 | fi |
| 236 | else |
| 237 | debug "Mounting chrome source at: $INNER_CHROME_ROOT" |
| 238 | sudo bash -c "echo '$CHROME_ROOT' > \ |
| 239 | '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'" |
| 240 | mkdir -p "$MOUNTED_PATH" |
| 241 | sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \ |
| 242 | die "Could not mount $MOUNTED_PATH" |
| 243 | fi |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 244 | fi |
| 245 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 246 | |
| 247 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 248 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 249 | if [ $(which gclient 2>/dev/null) ]; then |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 250 | debug "Mounting depot_tools" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 251 | DEPOT_TOOLS=$(dirname "$(which gclient)") |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 252 | mkdir -p "$MOUNTED_PATH" |
Andrew de los Reyes | e8b6315 | 2010-02-16 14:18:34 -0800 | [diff] [blame] | 253 | if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 254 | warn "depot_tools failed to mount; perhaps it's on NFS?" |
| 255 | warn "This may impact chromium build." |
Andrew de los Reyes | e8b6315 | 2010-02-16 14:18:34 -0800 | [diff] [blame] | 256 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 257 | fi |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 258 | fi |
| 259 | |
robotboy | 152a1ab | 2010-04-26 14:07:27 -0700 | [diff] [blame] | 260 | # Install fuse module. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 261 | if [ -c "${FUSE_DEVICE}" ]; then |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 262 | sudo modprobe fuse 2> /dev/null ||\ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 263 | warn "-- Note: modprobe fuse failed. gmergefs will not work" |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 264 | fi |
| 265 | |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 266 | # Turn off automounting of external media when we enter the |
| 267 | # chroot; thus we don't have to worry about being able to unmount |
| 268 | # from inside. |
| 269 | if [ $(which gconftool-2 2>/dev/null) ]; then |
| 270 | gconftool-2 -g ${AUTOMOUNT_PREF} > \ |
| 271 | "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}" |
| 272 | if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 273 | warn "-- Note: USB sticks may be automounted by your host OS." |
| 274 | warn "-- Note: If you plan to burn bootable media, you may need to" |
| 275 | warn "-- Note: unmount these devices manually, or run image_to_usb.sh" |
| 276 | warn "-- Note: outside the chroot." |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 277 | fi |
| 278 | fi |
| 279 | |
Dale Curtis | 9863173 | 2011-05-05 16:16:59 -0700 | [diff] [blame] | 280 | # Fix permissions on shared memory to allow non-root users access to POSIX |
| 281 | # semaphores. |
| 282 | sudo chmod -R 777 "${FLAGS_chroot}/dev/shm" |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 283 | ) 200>>"$LOCKFILE" || die "setup_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | function teardown_env { |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 287 | # Validate sudo timestamp before entering the critical section so that we |
| 288 | # don't stall for a password while we have the lockfile. |
Doug Anderson | 3d7fa3a | 2011-02-02 16:10:34 -0800 | [diff] [blame] | 289 | # Don't use sudo -v since that has issues on machines w/ no password. |
| 290 | sudo echo "" > /dev/null |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 291 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 292 | # 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] | 293 | ( |
| 294 | flock 200 |
| 295 | |
| 296 | # check each pid in $LOCKFILE to see if it's died unexpectedly |
| 297 | TMP_LOCKFILE="$LOCKFILE.tmp" |
| 298 | |
| 299 | echo -n > "$TMP_LOCKFILE" # Erase/reset temp file |
| 300 | cat "$LOCKFILE" | while read PID; do |
| 301 | if [ "$PID" = "$$" ]; then |
| 302 | # ourself, leave PROC_NAME empty |
| 303 | PROC_NAME="" |
| 304 | else |
| 305 | PROC_NAME=$(ps --pid $PID -o comm=) |
| 306 | fi |
| 307 | |
| 308 | if [ ! -z "$PROC_NAME" ]; then |
| 309 | # All good, keep going |
| 310 | echo "$PID" >> "$TMP_LOCKFILE" |
| 311 | fi |
| 312 | done |
| 313 | # Remove any dups from lock file while installing new one |
| 314 | sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE" |
| 315 | |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 316 | if [ $(which gconftool-2 2>/dev/null) ]; then |
| 317 | SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") |
| 318 | gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 319 | warn "could not re-set your automount preference." |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 320 | fi |
| 321 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 322 | if [ -s "$LOCKFILE" ]; then |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 323 | debug "At least one other pid is running in the chroot, so not" |
| 324 | debug "tearing down env." |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 325 | else |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 326 | debug "Stopping syncer process" |
| 327 | kill $(cat "${SYNCERPIDFILE}") |
| 328 | |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 329 | MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 330 | debug "Unmounting chroot environment." |
Zdenek Behan | 6f17b5e | 2010-06-10 16:50:52 -0700 | [diff] [blame] | 331 | # sort the list of mounts in reverse order, to ensure umount of |
| 332 | # cascading mounts in proper order |
| 333 | for i in \ |
| 334 | $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 335 | safe_umount "$i" |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 336 | done |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 337 | fi |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 338 | ) 200>>"$LOCKFILE" || die "teardown_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 341 | if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 342 | setup_env |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 343 | info "Make sure you run" |
| 344 | info " $0 --unmount" |
| 345 | info "before deleting $FLAGS_chroot" |
| 346 | info "or you'll end up deleting $FLAGS_trunk too!" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 347 | exit 0 |
| 348 | fi |
| 349 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 350 | if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 351 | teardown_env |
| 352 | exit 0 |
| 353 | fi |
| 354 | |
| 355 | # Make sure we unmount before exiting |
| 356 | trap teardown_env EXIT |
| 357 | setup_env |
| 358 | |
Darin Petkov | 87dd2be | 2011-03-01 09:25:09 -0800 | [diff] [blame] | 359 | CHROOT_PASSTHRU="BUILDBOT_BUILD=$FLAGS_build_number CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL" |
Raja Aluri | 8f2a995 | 2010-11-17 15:03:00 -0800 | [diff] [blame] | 360 | CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \ |
| 361 | CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}" |
Raja Aluri | e891eea | 2010-11-16 20:12:56 -0800 | [diff] [blame] | 362 | |
Liam McLoughlin | 3b11b45 | 2011-03-14 16:04:07 -0700 | [diff] [blame] | 363 | # Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER, |
| 364 | # CHROMEOS_VERSION_DEVSERVER as environment variables to override the default |
| 365 | # assumptions (local AU server). These are used in cros_set_lsb_release, and |
| 366 | # are used by external Chromium OS builders. |
| 367 | CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \ |
| 368 | CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK} \ |
| 369 | CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER} \ |
| 370 | CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}" |
| 371 | |
Mario Limonciello | 7052ae1 | 2011-05-05 12:00:49 -0500 | [diff] [blame] | 372 | # Pass proxy variables into the environment. |
| 373 | for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do |
| 374 | eval value=\$${type} |
| 375 | if [ -n "${value}" ]; then |
| 376 | CHROOT_PASSTHRU="${CHROOT_PASSTHRU} ${type}=${value}" |
| 377 | fi |
| 378 | done |
| 379 | |
Raja Aluri | 32759cf | 2010-08-30 18:44:39 -0700 | [diff] [blame] | 380 | if [ -d "$HOME/.subversion" ]; then |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 381 | TARGET="/home/${USER}/.subversion" |
| 382 | mkdir -p "${FLAGS_chroot}${TARGET}" |
| 383 | ensure_mounted "${HOME}/.subversion" "--bind" "${TARGET}" |
Raja Aluri | 32759cf | 2010-08-30 18:44:39 -0700 | [diff] [blame] | 384 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 385 | |
David James | 0fd8af4 | 2010-09-27 10:04:57 -0700 | [diff] [blame] | 386 | # Configure committer username and email in chroot .gitconfig |
Chris Sosa | 9d30ce8 | 2011-03-14 15:00:46 -0700 | [diff] [blame] | 387 | git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ |
| 388 | user.name "$(cd /tmp; git var GIT_COMMITTER_IDENT | sed -e 's/ *<.*//')" || |
| 389 | true |
| 390 | git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ |
| 391 | user.email "$(cd /tmp; git var GIT_COMMITTER_IDENT | \ |
| 392 | sed -e 's/.*<\([^>]*\)>.*/\1/')" || |
| 393 | true |
David James | 0fd8af4 | 2010-09-27 10:04:57 -0700 | [diff] [blame] | 394 | |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 395 | # Run command or interactive shell. Also include the non-chrooted path to |
| 396 | # the source trunk for scripts that may need to print it (e.g. |
| 397 | # build_image.sh). |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 398 | sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 399 | EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \ |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 400 | SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 401 | |
| 402 | # Remove trap and explicitly unmount |
| 403 | trap - EXIT |
| 404 | teardown_env |