blob: e3635e5ed05c77bda6cf1ba2bba2950f6bb6ad0a [file] [log] [blame]
rspangler@google.comd74220d2009-10-09 20:56:14 +00001#!/bin/bash
2
Darin Petkov47974d42011-03-03 15:55:04 -08003# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.comd74220d2009-10-09 20:56:14 +00004# 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 Barnette8e6750d2011-08-22 12:35:52 -07009SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..)
10. "${SCRIPT_ROOT}/common.sh" || exit 1
rspangler@google.comd74220d2009-10-09 20:56:14 +000011
derat@google.com86dcc8e2009-11-21 19:49:49 +000012# Script must be run outside the chroot and as a regular user.
rspangler@google.comd74220d2009-10-09 20:56:14 +000013assert_outside_chroot
derat@google.com86dcc8e2009-11-21 19:49:49 +000014assert_not_root_user
rspangler@google.comd74220d2009-10-09 20:56:14 +000015
16# Define command line flags
17# See http://code.google.com/p/shflags/wiki/Documentation10x
18DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
19 "The destination dir for the chroot environment." "d"
20DEFINE_string trunk "$GCLIENT_ROOT" \
21 "The source trunk to bind mount within the chroot." "s"
David McMahon03aeb202009-12-08 12:47:08 -080022DEFINE_string build_number "" \
23 "The build-bot build number (when called by buildbot only)." "b"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080024DEFINE_string chrome_root "" \
25 "The root of your chrome browser source. Should contain a 'src' subdir."
Sean Parent2898f752010-05-25 15:06:33 -070026DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \
27 "The mount point of the chrome broswer source in the chroot."
rspangler@google.comd74220d2009-10-09 20:56:14 +000028
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070029DEFINE_boolean official_build $FLAGS_FALSE \
30 "Set CHROMEOS_OFFICIAL=1 for release builds."
rspangler@google.comd74220d2009-10-09 20:56:14 +000031DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
32DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
Elly Jones7990a062010-09-02 09:23:23 -040033DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
Don Garrettad3f0592011-02-04 14:59:56 -080034DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000035
36# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080037FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000038
39One or more VAR=value pairs can be specified to export variables into
40the chroot environment. For example:
41
42 $0 FOO=bar BAZ=bel
43
Doug Anderson9362fa82010-12-16 14:44:12 -080044If [-- command] is present, runs the command inside the chroot,
45after changing directory to /$USER/trunk/src/scripts. Note that neither
46the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000047
Doug Anderson9362fa82010-12-16 14:44:12 -080048 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000049
50Otherwise, provides an interactive shell.
51"
52
Don Garrettad3f0592011-02-04 14:59:56 -080053# Version of info from common.sh that only echos if --verbose is set.
Don Garretta0e7ea12011-02-07 18:39:59 -080054function debug {
Don Garrettad3f0592011-02-04 14:59:56 -080055 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
David Rochberg351a76f2011-02-16 11:14:00 -050056 info "$*"
Don Garrettad3f0592011-02-04 14:59:56 -080057 fi
58}
59
rspangler@google.comd74220d2009-10-09 20:56:14 +000060# Parse command line flags
61FLAGS "$@" || exit 1
62eval set -- "${FLAGS_ARGV}"
63
Greg Spencer798d75f2011-02-01 22:04:49 -080064if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
David McMahon857dbb52009-12-09 18:21:05 -080065 CHROMEOS_OFFICIAL=1
66fi
67
rspangler@google.comd74220d2009-10-09 20:56:14 +000068# Only now can we die on error. shflags functions leak non-zero error codes,
69# so will die prematurely if 'set -e' is specified before now.
70# TODO: replace shflags with something less error-prone, or contribute a fix.
71set -e
72
Sean Parent2898f752010-05-25 15:06:33 -070073INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080074CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080075INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070076FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -070077AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
78SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080079
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080080sudo chmod 0777 "$FLAGS_chroot/var/lock"
81
82LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Zdenek Behane28239d2011-04-07 00:37:20 +020083SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080084
David Rochberg351a76f2011-02-16 11:14:00 -050085
86function ensure_mounted {
87 # If necessary, mount $source in the host FS at $target inside the
88 # chroot directory with $mount_args.
89 local source="$1"
90 local mount_args="$2"
91 local target="$3"
92
93 local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")"
94
95 if [ -z "$(mount | grep -F "on ${mounted_path} ")" ]; then
David Rochberg33373ef2011-02-16 14:48:27 -050096 # Attempt to make the mountpoint as the user. This depends on the
97 # fact that all mountpoints that should be owned by root are
98 # already present.
99 mkdir -p "${mounted_path}"
100
David Rochberg351a76f2011-02-16 11:14:00 -0500101 # NB: mount_args deliberately left unquoted
102 debug mount ${mount_args} "${source}" "${mounted_path}"
103 sudo -- mount ${mount_args} "${source}" "${mounted_path}" || \
104 die "Could not mount ${source} on ${mounted_path}"
105 fi
106}
107
Zdenek Behane28239d2011-04-07 00:37:20 +0200108function env_sync_proc {
109 # This function runs and performs periodic updates to the chroot env, if
110 # necessary.
111
112 local poll_interval=10
113 local sync_files="etc/resolv.conf etc/hosts"
114
115 # Make sure the synced files are writable by normal user, so that we
116 # don't have to sudo inside the loop.
117 for file in ${sync_files}; do
118 sudo chown ${USER} ${FLAGS_chroot}/${file} 1>&2
119 done
120
David James412b9022011-07-15 19:54:16 -0700121 # Drop stdin, stderr, stdout, and chroot lock.
122 # This is needed for properly daemonizing the process.
123 exec 0>&- 1>&- 2>&- 200>&-
124
Zdenek Behane28239d2011-04-07 00:37:20 +0200125 while true; do
126 # Sync files
127 for file in ${sync_files}; do
128 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
129 cp -f /${file} ${FLAGS_chroot}/${file}
130 fi
131 done
132
133 sleep ${poll_interval}
134 done
135}
136
Vadim Bendebury0779f522011-06-12 12:09:16 -0700137function copy_ssh_config {
138 # Copy user .ssh/config into the chroot filtering out strings not supported
139 # by the chroot ssh. The chroot .ssh directory is passed in as the first
140 # parameter.
141
142 # ssh options to filter out. The entire strings containing these substrings
143 # will be deleted before copying.
144 local bad_options=(
145 'UseProxyIf='
146 'GSSAPIAuthentication no'
147 )
148 local sshc="${HOME}/.ssh/config"
149 local chroot_ssh_dir="${1}"
150 local filter
151 local option
152
153 if [ ! -f "${sshc}" ]; then
154 return # Nothing to copy.
155 fi
156
157 for option in "${bad_options[@]}"
158 do
159 if [ -z "${filter}" ]; then
160 filter="${option}"
161 else
162 filter+="\\|${option}"
163 fi
164 done
165
166 sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config"
167}
168
rspangler@google.comd74220d2009-10-09 20:56:14 +0000169function setup_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800170 # Validate sudo timestamp before entering the critical section so that we
171 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800172 # Don't use sudo -v since that has issues on machines w/ no password.
173 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800174
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800175 (
176 flock 200
177 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000178
Taylor Hutt25bf9492011-07-27 13:54:35 -0700179 # If there isn't a syncer daemon started already, start one. The
180 # daemon is considered to not be started under the following
181 # conditions:
182 #
183 # o There is no PID file
184 #
185 # o The PID file is 0 bytes in size, which might be a partial
186 # manifestation of chromium-os:17680. This situation will not
187 # occur anymore, but you might have a chroot which was already
188 # affected.
189 #
190 # o The /proc node for the process named by the PID file does
191 # not exist.
192 #
193 # Note: This does not address PID recycling. While
194 # increasingly unlikely, it is possible for the PID in
195 # the PID file to refer to a running process that is not
196 # the syncer process. Since the PID file is now
197 # removed, I think it is only possible for this to occur
198 # if your system crashes and the PID file exists after
199 # restart.
200 #
201 # The daemon is killed by the enter_chroot that exits last.
Taylor Hutt18594a82011-07-28 11:45:50 -0700202 if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then
Taylor Hutt25bf9492011-07-27 13:54:35 -0700203 info "You may have suffered from chromium-os:17680 and";
204 info "could have stray 'enter_chroot.sh' processes running.";
205 info "You must manually kill any such stray processes.";
206 info "Exit all chroot shells; remaining 'enter_chroot.sh'";
207 info "processes are probably stray.";
Taylor Hutt18594a82011-07-28 11:45:50 -0700208 sudo rm -f "${SYNCERPIDFILE}";
Taylor Hutt25bf9492011-07-27 13:54:35 -0700209 fi;
David James412b9022011-07-15 19:54:16 -0700210 if ! [ -f "${SYNCERPIDFILE}" ] || \
211 ! [ -d /proc/$(cat "${SYNCERPIDFILE}") ]; then
212 debug "Starting sync process"
213 env_sync_proc &
214 echo $! > "${SYNCERPIDFILE}"
215 disown $!
216 fi
217
Don Garretta0e7ea12011-02-07 18:39:59 -0800218 debug "Mounting chroot environment."
David Rochberg351a76f2011-02-16 11:14:00 -0500219 ensure_mounted none "-t proc" /proc
220 ensure_mounted none "-t sysfs" /sys
221 ensure_mounted /dev "--bind" /dev
222 ensure_mounted none "-t devpts" /dev/pts
223 ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800224
Elly Jones7990a062010-09-02 09:23:23 -0400225 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
226 TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800227 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
Elly Jones7990a062010-09-02 09:23:23 -0400228 mkdir -p "${TARGET_DIR}"
229 cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}"
Elly Jones39ba1e52011-06-30 11:08:28 -0400230 cp -r ${HOME}/.ssh/*.pub "${TARGET_DIR}"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700231 copy_ssh_config "${TARGET_DIR}"
Elly Jones7990a062010-09-02 09:23:23 -0400232 ASOCK="$(dirname "${SSH_AUTH_SOCK}")"
David Rochberg351a76f2011-02-16 11:14:00 -0500233 ensure_mounted "${ASOCK}" "--bind" "${ASOCK}"
Elly Jones7990a062010-09-02 09:23:23 -0400234 fi
235 fi
236
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800237 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800238 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
Andrew de los Reyesc1e8d272010-02-13 12:39:21 -0800239 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800240 if [ -z "$CHROME_ROOT" ]; then
241 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
242 2>/dev/null)"
Don Garretta0e7ea12011-02-07 18:39:59 -0800243 CHROME_ROOT_AUTO=1
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800244 fi
Don Garretta0e7ea12011-02-07 18:39:59 -0800245 if [[ ( -n "$CHROME_ROOT" ) ]]; then
246 if [[ ( ! -d "${CHROME_ROOT}/src" ) ]]; then
247 error "Not mounting chrome source"
248 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
249 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
250 exit 1
251 fi
252 else
253 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
254 sudo bash -c "echo '$CHROME_ROOT' > \
255 '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'"
256 mkdir -p "$MOUNTED_PATH"
257 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \
258 die "Could not mount $MOUNTED_PATH"
259 fi
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800260 fi
261 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800262
263 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800264 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800265 if [ $(which gclient 2>/dev/null) ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800266 debug "Mounting depot_tools"
Greg Spencer798d75f2011-02-01 22:04:49 -0800267 DEPOT_TOOLS=$(dirname "$(which gclient)")
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800268 mkdir -p "$MOUNTED_PATH"
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800269 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200270 warn "depot_tools failed to mount; perhaps it's on NFS?"
271 warn "This may impact chromium build."
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800272 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800273 fi
Chris Sosa317d8eb2010-04-05 15:45:28 -0700274 fi
275
robotboy152a1ab2010-04-26 14:07:27 -0700276 # Install fuse module.
Greg Spencer798d75f2011-02-01 22:04:49 -0800277 if [ -c "${FUSE_DEVICE}" ]; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700278 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200279 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700280 fi
281
Chris Masone162f6542010-05-12 14:58:37 -0700282 # Turn off automounting of external media when we enter the
283 # chroot; thus we don't have to worry about being able to unmount
284 # from inside.
285 if [ $(which gconftool-2 2>/dev/null) ]; then
286 gconftool-2 -g ${AUTOMOUNT_PREF} > \
287 "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
288 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200289 warn "-- Note: USB sticks may be automounted by your host OS."
290 warn "-- Note: If you plan to burn bootable media, you may need to"
291 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
292 warn "-- Note: outside the chroot."
Chris Masone162f6542010-05-12 14:58:37 -0700293 fi
294 fi
295
David James342f1562011-07-15 15:21:18 -0700296 if [ -d "$HOME/.subversion" ]; then
297 TARGET="/home/${USER}/.subversion"
298 mkdir -p "${FLAGS_chroot}${TARGET}"
299 ensure_mounted "${HOME}/.subversion" "--bind" "${TARGET}"
300 fi
301
302 # Configure committer username and email in chroot .gitconfig
303 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
304 user.name "$(cd /tmp; git var GIT_COMMITTER_IDENT | \
305 sed -e 's/ *<.*//')" || true
306 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
307 user.email "$(cd /tmp; git var GIT_COMMITTER_IDENT | \
308 sed -e 's/.*<\([^>]*\)>.*/\1/')" || true
309
Mike Frysinger6601c522011-08-15 11:05:39 -0400310 # Make sure user's requested locales are available
311 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400312 # And make sure en_US{,.UTF-8} are always available as
313 # that what buildbot forces internally
314 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400315 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
316 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
317 sort -u | sed '/^C$/d')
318 gen_locales=()
319 for l in ${locales}; do
320 if [[ ${l} == *.* ]]; then
321 enc=${l#*.}
322 else
323 enc="ISO-8859-1"
324 fi
325 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
326 utf8) enc="UTF-8";;
327 esac
328 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
329 done
330 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
331 sudo -- chroot "$FLAGS_chroot" locale-gen -q -u \
332 -G "$(printf '%s\n' "${gen_locales[@]}")"
333 fi
334
Dale Curtis98631732011-05-05 16:16:59 -0700335 # Fix permissions on shared memory to allow non-root users access to POSIX
336 # semaphores.
337 sudo chmod -R 777 "${FLAGS_chroot}/dev/shm"
David James546747b2010-03-23 15:19:43 -0700338 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000339}
340
341function teardown_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800342 # Validate sudo timestamp before entering the critical section so that we
343 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800344 # Don't use sudo -v since that has issues on machines w/ no password.
345 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800346
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800347 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800348 (
349 flock 200
350
351 # check each pid in $LOCKFILE to see if it's died unexpectedly
352 TMP_LOCKFILE="$LOCKFILE.tmp"
353
354 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
355 cat "$LOCKFILE" | while read PID; do
356 if [ "$PID" = "$$" ]; then
357 # ourself, leave PROC_NAME empty
358 PROC_NAME=""
359 else
360 PROC_NAME=$(ps --pid $PID -o comm=)
361 fi
362
363 if [ ! -z "$PROC_NAME" ]; then
364 # All good, keep going
365 echo "$PID" >> "$TMP_LOCKFILE"
366 fi
367 done
368 # Remove any dups from lock file while installing new one
369 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE"
370
Chris Masone162f6542010-05-12 14:58:37 -0700371 if [ $(which gconftool-2 2>/dev/null) ]; then
372 SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
373 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
Sean Ocd8b1d12010-09-02 17:34:49 +0200374 warn "could not re-set your automount preference."
Chris Masone162f6542010-05-12 14:58:37 -0700375 fi
376
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800377 if [ -s "$LOCKFILE" ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800378 debug "At least one other pid is running in the chroot, so not"
379 debug "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800380 else
Zdenek Behane28239d2011-04-07 00:37:20 +0200381 debug "Stopping syncer process"
Taylor Hutt25bf9492011-07-27 13:54:35 -0700382 # If another process entering the chroot is blocked on this
383 # flock in setup_env(), it can be a race condition.
384 #
385 # When this locked region is exited, the setup_env() flock can
386 # be entered before the script can exit and the /proc entry for
387 # the PID is removed. The newly-created chroot will not end up
388 # with a syncer process. To avoid that situation, remove the
389 # syncer PID file.
390 #
391 # The syncer PID file should also be removed because the kernel
392 # will reuse PIDs. It's possible that the PID in the syncer PID
393 # has been reused by another process; make sure we don't skip
394 # starting the syncer process when this occurs by deleting the
395 # PID file.
396 kill $(cat "${SYNCERPIDFILE}") && \
Taylor Hutt18594a82011-07-28 11:45:50 -0700397 sudo rm -f "${SYNCERPIDFILE}" || \
Taylor Hutt25bf9492011-07-27 13:54:35 -0700398 debug "Unable to clean up syncer process.";
Zdenek Behane28239d2011-04-07 00:37:20 +0200399
David James546747b2010-03-23 15:19:43 -0700400 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Don Garretta0e7ea12011-02-07 18:39:59 -0800401 debug "Unmounting chroot environment."
Zdenek Behan6f17b5e2010-06-10 16:50:52 -0700402 # sort the list of mounts in reverse order, to ensure umount of
403 # cascading mounts in proper order
404 for i in \
405 $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do
robotboy98912212010-04-12 14:08:14 -0700406 safe_umount "$i"
David James546747b2010-03-23 15:19:43 -0700407 done
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800408 fi
David James546747b2010-03-23 15:19:43 -0700409 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000410}
411
Greg Spencer798d75f2011-02-01 22:04:49 -0800412if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000413 setup_env
Don Garretta0e7ea12011-02-07 18:39:59 -0800414 info "Make sure you run"
415 info " $0 --unmount"
416 info "before deleting $FLAGS_chroot"
417 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000418 exit 0
419fi
420
Greg Spencer798d75f2011-02-01 22:04:49 -0800421if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000422 teardown_env
423 exit 0
424fi
425
426# Make sure we unmount before exiting
427trap teardown_env EXIT
428setup_env
429
Zdenek Behan892e6ac2011-08-12 05:56:40 +0200430CHROOT_PASSTHRU="BUILDBOT_BUILD=$FLAGS_build_number \
431CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
Raja Aluri8f2a9952010-11-17 15:03:00 -0800432CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
433CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}"
Raja Alurie891eea2010-11-16 20:12:56 -0800434
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700435# Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER,
436# CHROMEOS_VERSION_DEVSERVER as environment variables to override the default
437# assumptions (local AU server). These are used in cros_set_lsb_release, and
438# are used by external Chromium OS builders.
439CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
440CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK} \
441CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER} \
442CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}"
443
Mario Limonciello7052ae12011-05-05 12:00:49 -0500444# Pass proxy variables into the environment.
445for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do
446 eval value=\$${type}
447 if [ -n "${value}" ]; then
448 CHROOT_PASSTHRU="${CHROOT_PASSTHRU} ${type}=${value}"
449 fi
450done
451
derat@google.com4e7a92b2009-11-21 23:44:14 +0000452# Run command or interactive shell. Also include the non-chrooted path to
453# the source trunk for scripts that may need to print it (e.g.
454# build_image.sh).
Doug Anderson9362fa82010-12-16 14:44:12 -0800455sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
Mike Frysinger5df0eab2011-08-11 15:39:24 -0400456 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" SSH_AGENT_PID="${SSH_AGENT_PID}" \
Doug Anderson9362fa82010-12-16 14:44:12 -0800457 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000458
459# Remove trap and explicitly unmount
460trap - EXIT
461teardown_env