blob: 33fb303017844ce3d424709a10092aba39fe1306 [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
Zdenek Behan892e6ac2011-08-12 05:56:40 +02009# The script lives in scripts/ or scripts/sdk_lib/
10for path in "$(dirname $0)" "$(dirname $0)/../"; do
11 if [ -r "${path}/common.sh" ]; then
12 SCRIPT_ROOT=${path}
13 break
14 fi
15done
Greg Spencer798d75f2011-02-01 22:04:49 -080016
Brian Harringd5d5dbf2011-07-11 16:36:21 -070017. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; }
rspangler@google.comd74220d2009-10-09 20:56:14 +000018
derat@google.com86dcc8e2009-11-21 19:49:49 +000019# Script must be run outside the chroot and as a regular user.
rspangler@google.comd74220d2009-10-09 20:56:14 +000020assert_outside_chroot
derat@google.com86dcc8e2009-11-21 19:49:49 +000021assert_not_root_user
rspangler@google.comd74220d2009-10-09 20:56:14 +000022
23# Define command line flags
24# See http://code.google.com/p/shflags/wiki/Documentation10x
25DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
26 "The destination dir for the chroot environment." "d"
27DEFINE_string trunk "$GCLIENT_ROOT" \
28 "The source trunk to bind mount within the chroot." "s"
David McMahon03aeb202009-12-08 12:47:08 -080029DEFINE_string build_number "" \
30 "The build-bot build number (when called by buildbot only)." "b"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080031DEFINE_string chrome_root "" \
32 "The root of your chrome browser source. Should contain a 'src' subdir."
Sean Parent2898f752010-05-25 15:06:33 -070033DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \
34 "The mount point of the chrome broswer source in the chroot."
rspangler@google.comd74220d2009-10-09 20:56:14 +000035
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070036DEFINE_boolean official_build $FLAGS_FALSE \
37 "Set CHROMEOS_OFFICIAL=1 for release builds."
rspangler@google.comd74220d2009-10-09 20:56:14 +000038DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
39DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
Elly Jones7990a062010-09-02 09:23:23 -040040DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
Don Garrettad3f0592011-02-04 14:59:56 -080041DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000042
43# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080044FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000045
46One or more VAR=value pairs can be specified to export variables into
47the chroot environment. For example:
48
49 $0 FOO=bar BAZ=bel
50
Doug Anderson9362fa82010-12-16 14:44:12 -080051If [-- command] is present, runs the command inside the chroot,
52after changing directory to /$USER/trunk/src/scripts. Note that neither
53the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000054
Doug Anderson9362fa82010-12-16 14:44:12 -080055 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000056
57Otherwise, provides an interactive shell.
58"
59
Don Garrettad3f0592011-02-04 14:59:56 -080060# Version of info from common.sh that only echos if --verbose is set.
Don Garretta0e7ea12011-02-07 18:39:59 -080061function debug {
Don Garrettad3f0592011-02-04 14:59:56 -080062 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
David Rochberg351a76f2011-02-16 11:14:00 -050063 info "$*"
Don Garrettad3f0592011-02-04 14:59:56 -080064 fi
65}
66
rspangler@google.comd74220d2009-10-09 20:56:14 +000067# Parse command line flags
68FLAGS "$@" || exit 1
69eval set -- "${FLAGS_ARGV}"
70
Greg Spencer798d75f2011-02-01 22:04:49 -080071if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
David McMahon857dbb52009-12-09 18:21:05 -080072 CHROMEOS_OFFICIAL=1
73fi
74
rspangler@google.comd74220d2009-10-09 20:56:14 +000075# Only now can we die on error. shflags functions leak non-zero error codes,
76# so will die prematurely if 'set -e' is specified before now.
77# TODO: replace shflags with something less error-prone, or contribute a fix.
78set -e
79
Sean Parent2898f752010-05-25 15:06:33 -070080INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080081CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080082INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070083FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -070084AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
85SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080086
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080087sudo chmod 0777 "$FLAGS_chroot/var/lock"
88
89LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Zdenek Behane28239d2011-04-07 00:37:20 +020090SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080091
David Rochberg351a76f2011-02-16 11:14:00 -050092
93function ensure_mounted {
94 # If necessary, mount $source in the host FS at $target inside the
95 # chroot directory with $mount_args.
96 local source="$1"
97 local mount_args="$2"
98 local target="$3"
99
100 local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")"
101
102 if [ -z "$(mount | grep -F "on ${mounted_path} ")" ]; then
David Rochberg33373ef2011-02-16 14:48:27 -0500103 # Attempt to make the mountpoint as the user. This depends on the
104 # fact that all mountpoints that should be owned by root are
105 # already present.
106 mkdir -p "${mounted_path}"
107
David Rochberg351a76f2011-02-16 11:14:00 -0500108 # NB: mount_args deliberately left unquoted
109 debug mount ${mount_args} "${source}" "${mounted_path}"
110 sudo -- mount ${mount_args} "${source}" "${mounted_path}" || \
111 die "Could not mount ${source} on ${mounted_path}"
112 fi
113}
114
Zdenek Behane28239d2011-04-07 00:37:20 +0200115function env_sync_proc {
116 # This function runs and performs periodic updates to the chroot env, if
117 # necessary.
118
119 local poll_interval=10
120 local sync_files="etc/resolv.conf etc/hosts"
121
122 # Make sure the synced files are writable by normal user, so that we
123 # don't have to sudo inside the loop.
124 for file in ${sync_files}; do
125 sudo chown ${USER} ${FLAGS_chroot}/${file} 1>&2
126 done
127
David James412b9022011-07-15 19:54:16 -0700128 # Drop stdin, stderr, stdout, and chroot lock.
129 # This is needed for properly daemonizing the process.
130 exec 0>&- 1>&- 2>&- 200>&-
131
Zdenek Behane28239d2011-04-07 00:37:20 +0200132 while true; do
133 # Sync files
134 for file in ${sync_files}; do
135 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
136 cp -f /${file} ${FLAGS_chroot}/${file}
137 fi
138 done
139
140 sleep ${poll_interval}
141 done
142}
143
Vadim Bendebury0779f522011-06-12 12:09:16 -0700144function copy_ssh_config {
145 # Copy user .ssh/config into the chroot filtering out strings not supported
146 # by the chroot ssh. The chroot .ssh directory is passed in as the first
147 # parameter.
148
149 # ssh options to filter out. The entire strings containing these substrings
150 # will be deleted before copying.
151 local bad_options=(
152 'UseProxyIf='
153 'GSSAPIAuthentication no'
154 )
155 local sshc="${HOME}/.ssh/config"
156 local chroot_ssh_dir="${1}"
157 local filter
158 local option
159
160 if [ ! -f "${sshc}" ]; then
161 return # Nothing to copy.
162 fi
163
164 for option in "${bad_options[@]}"
165 do
166 if [ -z "${filter}" ]; then
167 filter="${option}"
168 else
169 filter+="\\|${option}"
170 fi
171 done
172
173 sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config"
174}
175
rspangler@google.comd74220d2009-10-09 20:56:14 +0000176function setup_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800177 # Validate sudo timestamp before entering the critical section so that we
178 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800179 # Don't use sudo -v since that has issues on machines w/ no password.
180 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800181
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800182 (
183 flock 200
184 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000185
Taylor Hutt25bf9492011-07-27 13:54:35 -0700186 # If there isn't a syncer daemon started already, start one. The
187 # daemon is considered to not be started under the following
188 # conditions:
189 #
190 # o There is no PID file
191 #
192 # o The PID file is 0 bytes in size, which might be a partial
193 # manifestation of chromium-os:17680. This situation will not
194 # occur anymore, but you might have a chroot which was already
195 # affected.
196 #
197 # o The /proc node for the process named by the PID file does
198 # not exist.
199 #
200 # Note: This does not address PID recycling. While
201 # increasingly unlikely, it is possible for the PID in
202 # the PID file to refer to a running process that is not
203 # the syncer process. Since the PID file is now
204 # removed, I think it is only possible for this to occur
205 # if your system crashes and the PID file exists after
206 # restart.
207 #
208 # The daemon is killed by the enter_chroot that exits last.
Taylor Hutt18594a82011-07-28 11:45:50 -0700209 if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then
Taylor Hutt25bf9492011-07-27 13:54:35 -0700210 info "You may have suffered from chromium-os:17680 and";
211 info "could have stray 'enter_chroot.sh' processes running.";
212 info "You must manually kill any such stray processes.";
213 info "Exit all chroot shells; remaining 'enter_chroot.sh'";
214 info "processes are probably stray.";
Taylor Hutt18594a82011-07-28 11:45:50 -0700215 sudo rm -f "${SYNCERPIDFILE}";
Taylor Hutt25bf9492011-07-27 13:54:35 -0700216 fi;
David James412b9022011-07-15 19:54:16 -0700217 if ! [ -f "${SYNCERPIDFILE}" ] || \
218 ! [ -d /proc/$(cat "${SYNCERPIDFILE}") ]; then
219 debug "Starting sync process"
220 env_sync_proc &
221 echo $! > "${SYNCERPIDFILE}"
222 disown $!
223 fi
224
Don Garretta0e7ea12011-02-07 18:39:59 -0800225 debug "Mounting chroot environment."
David Rochberg351a76f2011-02-16 11:14:00 -0500226 ensure_mounted none "-t proc" /proc
227 ensure_mounted none "-t sysfs" /sys
228 ensure_mounted /dev "--bind" /dev
229 ensure_mounted none "-t devpts" /dev/pts
230 ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800231
Elly Jones7990a062010-09-02 09:23:23 -0400232 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
233 TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800234 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
Elly Jones7990a062010-09-02 09:23:23 -0400235 mkdir -p "${TARGET_DIR}"
236 cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}"
Elly Jones39ba1e52011-06-30 11:08:28 -0400237 cp -r ${HOME}/.ssh/*.pub "${TARGET_DIR}"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700238 copy_ssh_config "${TARGET_DIR}"
Elly Jones7990a062010-09-02 09:23:23 -0400239 ASOCK="$(dirname "${SSH_AUTH_SOCK}")"
David Rochberg351a76f2011-02-16 11:14:00 -0500240 ensure_mounted "${ASOCK}" "--bind" "${ASOCK}"
Elly Jones7990a062010-09-02 09:23:23 -0400241 fi
242 fi
243
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800244 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800245 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
Andrew de los Reyesc1e8d272010-02-13 12:39:21 -0800246 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800247 if [ -z "$CHROME_ROOT" ]; then
248 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
249 2>/dev/null)"
Don Garretta0e7ea12011-02-07 18:39:59 -0800250 CHROME_ROOT_AUTO=1
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800251 fi
Don Garretta0e7ea12011-02-07 18:39:59 -0800252 if [[ ( -n "$CHROME_ROOT" ) ]]; then
253 if [[ ( ! -d "${CHROME_ROOT}/src" ) ]]; then
254 error "Not mounting chrome source"
255 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
256 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
257 exit 1
258 fi
259 else
260 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
261 sudo bash -c "echo '$CHROME_ROOT' > \
262 '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'"
263 mkdir -p "$MOUNTED_PATH"
264 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \
265 die "Could not mount $MOUNTED_PATH"
266 fi
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800267 fi
268 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800269
270 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800271 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800272 if [ $(which gclient 2>/dev/null) ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800273 debug "Mounting depot_tools"
Greg Spencer798d75f2011-02-01 22:04:49 -0800274 DEPOT_TOOLS=$(dirname "$(which gclient)")
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800275 mkdir -p "$MOUNTED_PATH"
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800276 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200277 warn "depot_tools failed to mount; perhaps it's on NFS?"
278 warn "This may impact chromium build."
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800279 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800280 fi
Chris Sosa317d8eb2010-04-05 15:45:28 -0700281 fi
282
robotboy152a1ab2010-04-26 14:07:27 -0700283 # Install fuse module.
Greg Spencer798d75f2011-02-01 22:04:49 -0800284 if [ -c "${FUSE_DEVICE}" ]; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700285 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200286 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700287 fi
288
Chris Masone162f6542010-05-12 14:58:37 -0700289 # Turn off automounting of external media when we enter the
290 # chroot; thus we don't have to worry about being able to unmount
291 # from inside.
292 if [ $(which gconftool-2 2>/dev/null) ]; then
293 gconftool-2 -g ${AUTOMOUNT_PREF} > \
294 "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
295 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200296 warn "-- Note: USB sticks may be automounted by your host OS."
297 warn "-- Note: If you plan to burn bootable media, you may need to"
298 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
299 warn "-- Note: outside the chroot."
Chris Masone162f6542010-05-12 14:58:37 -0700300 fi
301 fi
302
David James342f1562011-07-15 15:21:18 -0700303 if [ -d "$HOME/.subversion" ]; then
304 TARGET="/home/${USER}/.subversion"
305 mkdir -p "${FLAGS_chroot}${TARGET}"
306 ensure_mounted "${HOME}/.subversion" "--bind" "${TARGET}"
307 fi
308
309 # Configure committer username and email in chroot .gitconfig
310 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
311 user.name "$(cd /tmp; git var GIT_COMMITTER_IDENT | \
312 sed -e 's/ *<.*//')" || true
313 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
314 user.email "$(cd /tmp; git var GIT_COMMITTER_IDENT | \
315 sed -e 's/.*<\([^>]*\)>.*/\1/')" || true
316
Mike Frysinger6601c522011-08-15 11:05:39 -0400317 # Make sure user's requested locales are available
318 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400319 # And make sure en_US{,.UTF-8} are always available as
320 # that what buildbot forces internally
321 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400322 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
323 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
324 sort -u | sed '/^C$/d')
325 gen_locales=()
326 for l in ${locales}; do
327 if [[ ${l} == *.* ]]; then
328 enc=${l#*.}
329 else
330 enc="ISO-8859-1"
331 fi
332 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
333 utf8) enc="UTF-8";;
334 esac
335 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
336 done
337 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
338 sudo -- chroot "$FLAGS_chroot" locale-gen -q -u \
339 -G "$(printf '%s\n' "${gen_locales[@]}")"
340 fi
341
Dale Curtis98631732011-05-05 16:16:59 -0700342 # Fix permissions on shared memory to allow non-root users access to POSIX
343 # semaphores.
344 sudo chmod -R 777 "${FLAGS_chroot}/dev/shm"
David James546747b2010-03-23 15:19:43 -0700345 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000346}
347
348function teardown_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800349 # Validate sudo timestamp before entering the critical section so that we
350 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800351 # Don't use sudo -v since that has issues on machines w/ no password.
352 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800353
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800354 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800355 (
356 flock 200
357
358 # check each pid in $LOCKFILE to see if it's died unexpectedly
359 TMP_LOCKFILE="$LOCKFILE.tmp"
360
361 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
362 cat "$LOCKFILE" | while read PID; do
363 if [ "$PID" = "$$" ]; then
364 # ourself, leave PROC_NAME empty
365 PROC_NAME=""
366 else
367 PROC_NAME=$(ps --pid $PID -o comm=)
368 fi
369
370 if [ ! -z "$PROC_NAME" ]; then
371 # All good, keep going
372 echo "$PID" >> "$TMP_LOCKFILE"
373 fi
374 done
375 # Remove any dups from lock file while installing new one
376 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE"
377
Chris Masone162f6542010-05-12 14:58:37 -0700378 if [ $(which gconftool-2 2>/dev/null) ]; then
379 SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
380 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
Sean Ocd8b1d12010-09-02 17:34:49 +0200381 warn "could not re-set your automount preference."
Chris Masone162f6542010-05-12 14:58:37 -0700382 fi
383
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800384 if [ -s "$LOCKFILE" ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800385 debug "At least one other pid is running in the chroot, so not"
386 debug "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800387 else
Zdenek Behane28239d2011-04-07 00:37:20 +0200388 debug "Stopping syncer process"
Taylor Hutt25bf9492011-07-27 13:54:35 -0700389 # If another process entering the chroot is blocked on this
390 # flock in setup_env(), it can be a race condition.
391 #
392 # When this locked region is exited, the setup_env() flock can
393 # be entered before the script can exit and the /proc entry for
394 # the PID is removed. The newly-created chroot will not end up
395 # with a syncer process. To avoid that situation, remove the
396 # syncer PID file.
397 #
398 # The syncer PID file should also be removed because the kernel
399 # will reuse PIDs. It's possible that the PID in the syncer PID
400 # has been reused by another process; make sure we don't skip
401 # starting the syncer process when this occurs by deleting the
402 # PID file.
403 kill $(cat "${SYNCERPIDFILE}") && \
Taylor Hutt18594a82011-07-28 11:45:50 -0700404 sudo rm -f "${SYNCERPIDFILE}" || \
Taylor Hutt25bf9492011-07-27 13:54:35 -0700405 debug "Unable to clean up syncer process.";
Zdenek Behane28239d2011-04-07 00:37:20 +0200406
David James546747b2010-03-23 15:19:43 -0700407 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Don Garretta0e7ea12011-02-07 18:39:59 -0800408 debug "Unmounting chroot environment."
Zdenek Behan6f17b5e2010-06-10 16:50:52 -0700409 # sort the list of mounts in reverse order, to ensure umount of
410 # cascading mounts in proper order
411 for i in \
412 $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do
robotboy98912212010-04-12 14:08:14 -0700413 safe_umount "$i"
David James546747b2010-03-23 15:19:43 -0700414 done
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800415 fi
David James546747b2010-03-23 15:19:43 -0700416 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000417}
418
Greg Spencer798d75f2011-02-01 22:04:49 -0800419if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000420 setup_env
Don Garretta0e7ea12011-02-07 18:39:59 -0800421 info "Make sure you run"
422 info " $0 --unmount"
423 info "before deleting $FLAGS_chroot"
424 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000425 exit 0
426fi
427
Greg Spencer798d75f2011-02-01 22:04:49 -0800428if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000429 teardown_env
430 exit 0
431fi
432
433# Make sure we unmount before exiting
434trap teardown_env EXIT
435setup_env
436
Zdenek Behan892e6ac2011-08-12 05:56:40 +0200437CHROOT_PASSTHRU="BUILDBOT_BUILD=$FLAGS_build_number \
438CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
Raja Aluri8f2a9952010-11-17 15:03:00 -0800439CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
440CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}"
Raja Alurie891eea2010-11-16 20:12:56 -0800441
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700442# Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER,
443# CHROMEOS_VERSION_DEVSERVER as environment variables to override the default
444# assumptions (local AU server). These are used in cros_set_lsb_release, and
445# are used by external Chromium OS builders.
446CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
447CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK} \
448CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER} \
449CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}"
450
Mario Limonciello7052ae12011-05-05 12:00:49 -0500451# Pass proxy variables into the environment.
452for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do
453 eval value=\$${type}
454 if [ -n "${value}" ]; then
455 CHROOT_PASSTHRU="${CHROOT_PASSTHRU} ${type}=${value}"
456 fi
457done
458
derat@google.com4e7a92b2009-11-21 23:44:14 +0000459# Run command or interactive shell. Also include the non-chrooted path to
460# the source trunk for scripts that may need to print it (e.g.
461# build_image.sh).
Doug Anderson9362fa82010-12-16 14:44:12 -0800462sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
Mike Frysinger5df0eab2011-08-11 15:39:24 -0400463 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" SSH_AGENT_PID="${SSH_AGENT_PID}" \
Doug Anderson9362fa82010-12-16 14:44:12 -0800464 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000465
466# Remove trap and explicitly unmount
467trap - EXIT
468teardown_env