blob: 8da0ab5fa97bc176a2f0838b2bef0da906717b08 [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
Mike Frysinger82649172011-09-21 00:18:14 -040080# Avoid the sudo call if possible since it is a little slow.
81if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then
82 sudo chmod 0777 "$FLAGS_chroot/var/lock"
83fi
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080084
85LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Zdenek Behane28239d2011-04-07 00:37:20 +020086SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080087
David Rochberg351a76f2011-02-16 11:14:00 -050088
89function ensure_mounted {
90 # If necessary, mount $source in the host FS at $target inside the
91 # chroot directory with $mount_args.
92 local source="$1"
93 local mount_args="$2"
94 local target="$3"
95
96 local mounted_path="$(readlink -f "${FLAGS_chroot}/$target")"
97
Mike Frysinger2a970592011-09-20 22:49:01 -040098 case ${MOUNT_CACHE} in
99 *" on ${mounted_path} "*)
100 # Already mounted!
101 ;;
102 *)
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}"
Mike Frysinger2a970592011-09-20 22:49:01 -0400112 ;;
113 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500114}
115
Zdenek Behane28239d2011-04-07 00:37:20 +0200116function env_sync_proc {
117 # This function runs and performs periodic updates to the chroot env, if
118 # necessary.
119
120 local poll_interval=10
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400121 local sync_files=( etc/resolv.conf etc/hosts )
Zdenek Behane28239d2011-04-07 00:37:20 +0200122
123 # Make sure the synced files are writable by normal user, so that we
124 # don't have to sudo inside the loop.
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400125 local chmods=$(find ${sync_files[@]/#/${FLAGS_chroot}/} '!' -user ${USER})
126 if [ -n "${chmods}" ]; then
127 sudo -- chown ${USER} ${chmods} 1>&2
128 fi
Zdenek Behane28239d2011-04-07 00:37:20 +0200129
David James412b9022011-07-15 19:54:16 -0700130 # Drop stdin, stderr, stdout, and chroot lock.
131 # This is needed for properly daemonizing the process.
132 exec 0>&- 1>&- 2>&- 200>&-
133
Zdenek Behane28239d2011-04-07 00:37:20 +0200134 while true; do
135 # Sync files
136 for file in ${sync_files}; do
137 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
138 cp -f /${file} ${FLAGS_chroot}/${file}
139 fi
140 done
141
142 sleep ${poll_interval}
143 done
144}
145
Vadim Bendebury0779f522011-06-12 12:09:16 -0700146function copy_ssh_config {
147 # Copy user .ssh/config into the chroot filtering out strings not supported
148 # by the chroot ssh. The chroot .ssh directory is passed in as the first
149 # parameter.
150
151 # ssh options to filter out. The entire strings containing these substrings
152 # will be deleted before copying.
153 local bad_options=(
154 'UseProxyIf='
155 'GSSAPIAuthentication no'
156 )
157 local sshc="${HOME}/.ssh/config"
158 local chroot_ssh_dir="${1}"
159 local filter
160 local option
161
162 if [ ! -f "${sshc}" ]; then
163 return # Nothing to copy.
164 fi
165
166 for option in "${bad_options[@]}"
167 do
168 if [ -z "${filter}" ]; then
169 filter="${option}"
170 else
171 filter+="\\|${option}"
172 fi
173 done
174
175 sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config"
176}
177
rspangler@google.comd74220d2009-10-09 20:56:14 +0000178function setup_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800179 # Validate sudo timestamp before entering the critical section so that we
180 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800181 # Don't use sudo -v since that has issues on machines w/ no password.
182 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800183
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800184 (
185 flock 200
186 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000187
Taylor Hutt25bf9492011-07-27 13:54:35 -0700188 # If there isn't a syncer daemon started already, start one. The
189 # daemon is considered to not be started under the following
190 # conditions:
191 #
192 # o There is no PID file
193 #
194 # o The PID file is 0 bytes in size, which might be a partial
195 # manifestation of chromium-os:17680. This situation will not
196 # occur anymore, but you might have a chroot which was already
197 # affected.
198 #
199 # o The /proc node for the process named by the PID file does
200 # not exist.
201 #
202 # Note: This does not address PID recycling. While
203 # increasingly unlikely, it is possible for the PID in
204 # the PID file to refer to a running process that is not
205 # the syncer process. Since the PID file is now
206 # removed, I think it is only possible for this to occur
207 # if your system crashes and the PID file exists after
208 # restart.
209 #
210 # The daemon is killed by the enter_chroot that exits last.
Taylor Hutt18594a82011-07-28 11:45:50 -0700211 if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then
Taylor Hutt25bf9492011-07-27 13:54:35 -0700212 info "You may have suffered from chromium-os:17680 and";
213 info "could have stray 'enter_chroot.sh' processes running.";
214 info "You must manually kill any such stray processes.";
215 info "Exit all chroot shells; remaining 'enter_chroot.sh'";
216 info "processes are probably stray.";
Taylor Hutt18594a82011-07-28 11:45:50 -0700217 sudo rm -f "${SYNCERPIDFILE}";
Taylor Hutt25bf9492011-07-27 13:54:35 -0700218 fi;
David James412b9022011-07-15 19:54:16 -0700219 if ! [ -f "${SYNCERPIDFILE}" ] || \
220 ! [ -d /proc/$(cat "${SYNCERPIDFILE}") ]; then
221 debug "Starting sync process"
222 env_sync_proc &
223 echo $! > "${SYNCERPIDFILE}"
224 disown $!
225 fi
226
Don Garretta0e7ea12011-02-07 18:39:59 -0800227 debug "Mounting chroot environment."
Mike Frysinger2a970592011-09-20 22:49:01 -0400228 MOUNT_CACHE=$(mount)
David Rochberg351a76f2011-02-16 11:14:00 -0500229 ensure_mounted none "-t proc" /proc
230 ensure_mounted none "-t sysfs" /sys
231 ensure_mounted /dev "--bind" /dev
232 ensure_mounted none "-t devpts" /dev/pts
233 ensure_mounted "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800234
Elly Jones7990a062010-09-02 09:23:23 -0400235 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
236 TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800237 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
Elly Jones7990a062010-09-02 09:23:23 -0400238 mkdir -p "${TARGET_DIR}"
239 cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}"
Elly Jones39ba1e52011-06-30 11:08:28 -0400240 cp -r ${HOME}/.ssh/*.pub "${TARGET_DIR}"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700241 copy_ssh_config "${TARGET_DIR}"
Elly Jones7990a062010-09-02 09:23:23 -0400242 ASOCK="$(dirname "${SSH_AUTH_SOCK}")"
David Rochberg351a76f2011-02-16 11:14:00 -0500243 ensure_mounted "${ASOCK}" "--bind" "${ASOCK}"
Elly Jones7990a062010-09-02 09:23:23 -0400244 fi
245 fi
246
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800247 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800248 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
Andrew de los Reyesc1e8d272010-02-13 12:39:21 -0800249 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800250 if [ -z "$CHROME_ROOT" ]; then
251 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
252 2>/dev/null)"
Don Garretta0e7ea12011-02-07 18:39:59 -0800253 CHROME_ROOT_AUTO=1
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800254 fi
Don Garretta0e7ea12011-02-07 18:39:59 -0800255 if [[ ( -n "$CHROME_ROOT" ) ]]; then
256 if [[ ( ! -d "${CHROME_ROOT}/src" ) ]]; then
257 error "Not mounting chrome source"
258 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
259 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
260 exit 1
261 fi
262 else
263 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
264 sudo bash -c "echo '$CHROME_ROOT' > \
265 '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'"
266 mkdir -p "$MOUNTED_PATH"
267 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \
268 die "Could not mount $MOUNTED_PATH"
269 fi
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800270 fi
271 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800272
273 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")"
Greg Spencer798d75f2011-02-01 22:04:49 -0800274 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800275 if [ $(which gclient 2>/dev/null) ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800276 debug "Mounting depot_tools"
Greg Spencer798d75f2011-02-01 22:04:49 -0800277 DEPOT_TOOLS=$(dirname "$(which gclient)")
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800278 mkdir -p "$MOUNTED_PATH"
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800279 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200280 warn "depot_tools failed to mount; perhaps it's on NFS?"
281 warn "This may impact chromium build."
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800282 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800283 fi
Chris Sosa317d8eb2010-04-05 15:45:28 -0700284 fi
285
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400286 # Install fuse module. Skip modprobe when possible for slight
287 # speed increase when initializing the env.
288 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700289 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200290 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700291 fi
292
Chris Masone162f6542010-05-12 14:58:37 -0700293 # Turn off automounting of external media when we enter the
294 # chroot; thus we don't have to worry about being able to unmount
295 # from inside.
Mike Frysingerda7be782011-09-15 17:58:19 -0400296 if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then
297 if [ "${SAVED_PREF}" != "false" ]; then
298 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
299 warn "-- Note: USB sticks may be automounted by your host OS."
300 warn "-- Note: If you plan to burn bootable media, you may need to"
301 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
302 warn "-- Note: outside the chroot."
303 fi
Chris Masone162f6542010-05-12 14:58:37 -0700304 fi
305 fi
Mike Frysingerda7be782011-09-15 17:58:19 -0400306 # Always write the temp file so we can read it when exiting
307 echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
Chris Masone162f6542010-05-12 14:58:37 -0700308
David James342f1562011-07-15 15:21:18 -0700309 if [ -d "$HOME/.subversion" ]; then
310 TARGET="/home/${USER}/.subversion"
311 mkdir -p "${FLAGS_chroot}${TARGET}"
312 ensure_mounted "${HOME}/.subversion" "--bind" "${TARGET}"
313 fi
314
Mike Frysinger94717e32011-09-21 00:10:44 -0400315 # Configure committer username and email in chroot .gitconfig. Change
316 # to the root directory first so that random $PWD/.git/config settings
317 # do not get picked up. We want to stick to ~/.gitconfig only.
318 ident=$(cd /; git var GIT_COMMITTER_IDENT || :)
319 ident_name=${ident%% <*}
320 ident_email=${ident%%>*}; ident_email=${ident_email##*<}
David James342f1562011-07-15 15:21:18 -0700321 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400322 user.name "${ident_name}" || true
David James342f1562011-07-15 15:21:18 -0700323 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400324 user.email "${ident_email}" || true
David James342f1562011-07-15 15:21:18 -0700325
Matt Tennantd4316fe2011-08-30 12:06:42 -0700326 # Copy ~/.gdata_cred.txt to chroot if it exists. This file contains
327 # credentials for reading/writing Google Docs on chromium.org.
328 if [ -f "$HOME/.gdata_cred.txt" ]; then
329 cp "$HOME/.gdata_cred.txt" "${FLAGS_chroot}/home/${USER}/.gdata_cred.txt"
330 fi
331
Mike Frysinger6601c522011-08-15 11:05:39 -0400332 # Make sure user's requested locales are available
333 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400334 # And make sure en_US{,.UTF-8} are always available as
335 # that what buildbot forces internally
336 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400337 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
338 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
339 sort -u | sed '/^C$/d')
340 gen_locales=()
341 for l in ${locales}; do
342 if [[ ${l} == *.* ]]; then
343 enc=${l#*.}
344 else
345 enc="ISO-8859-1"
346 fi
347 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
348 utf8) enc="UTF-8";;
349 esac
350 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
351 done
Mike Frysinger4d258cd2011-09-15 16:17:49 -0400352 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
353 # Force LC_ALL=C to workaround slow string parsing in bash
354 # with long multibyte strings. Newer setups have this fixed,
355 # but locale-gen doesn't need to be run in any locale in the
356 # first place, so just go with C to keep it fast.
357 sudo -- chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \
358 -G "$(printf '%s\n' "${gen_locales[@]}")"
359 fi
Mike Frysinger6601c522011-08-15 11:05:39 -0400360
Dale Curtis98631732011-05-05 16:16:59 -0700361 # Fix permissions on shared memory to allow non-root users access to POSIX
Mike Frysinger82649172011-09-21 00:18:14 -0400362 # semaphores. Avoid the sudo call if possible (sudo is slow).
363 if [ -n "$(find "${FLAGS_chroot}/dev/shm" ! -perm 777)" ] ; then
364 sudo chmod -R 777 "${FLAGS_chroot}/dev/shm"
365 fi
David James546747b2010-03-23 15:19:43 -0700366 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000367}
368
369function teardown_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800370 # Validate sudo timestamp before entering the critical section so that we
371 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800372 # Don't use sudo -v since that has issues on machines w/ no password.
373 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800374
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800375 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800376 (
377 flock 200
378
379 # check each pid in $LOCKFILE to see if it's died unexpectedly
380 TMP_LOCKFILE="$LOCKFILE.tmp"
381
382 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
383 cat "$LOCKFILE" | while read PID; do
384 if [ "$PID" = "$$" ]; then
385 # ourself, leave PROC_NAME empty
386 PROC_NAME=""
387 else
388 PROC_NAME=$(ps --pid $PID -o comm=)
389 fi
390
391 if [ ! -z "$PROC_NAME" ]; then
392 # All good, keep going
393 echo "$PID" >> "$TMP_LOCKFILE"
394 fi
395 done
396 # Remove any dups from lock file while installing new one
397 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE"
398
Mike Frysingerda7be782011-09-15 17:58:19 -0400399 SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
400 if [ "${SAVED_PREF}" != "false" ]; then
Chris Masone162f6542010-05-12 14:58:37 -0700401 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
Sean Ocd8b1d12010-09-02 17:34:49 +0200402 warn "could not re-set your automount preference."
Chris Masone162f6542010-05-12 14:58:37 -0700403 fi
404
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800405 if [ -s "$LOCKFILE" ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800406 debug "At least one other pid is running in the chroot, so not"
407 debug "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800408 else
Zdenek Behane28239d2011-04-07 00:37:20 +0200409 debug "Stopping syncer process"
Taylor Hutt25bf9492011-07-27 13:54:35 -0700410 # If another process entering the chroot is blocked on this
411 # flock in setup_env(), it can be a race condition.
412 #
413 # When this locked region is exited, the setup_env() flock can
414 # be entered before the script can exit and the /proc entry for
415 # the PID is removed. The newly-created chroot will not end up
416 # with a syncer process. To avoid that situation, remove the
417 # syncer PID file.
418 #
419 # The syncer PID file should also be removed because the kernel
420 # will reuse PIDs. It's possible that the PID in the syncer PID
421 # has been reused by another process; make sure we don't skip
422 # starting the syncer process when this occurs by deleting the
423 # PID file.
424 kill $(cat "${SYNCERPIDFILE}") && \
Taylor Hutt18594a82011-07-28 11:45:50 -0700425 sudo rm -f "${SYNCERPIDFILE}" || \
Taylor Hutt25bf9492011-07-27 13:54:35 -0700426 debug "Unable to clean up syncer process.";
Zdenek Behane28239d2011-04-07 00:37:20 +0200427
David James546747b2010-03-23 15:19:43 -0700428 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Don Garretta0e7ea12011-02-07 18:39:59 -0800429 debug "Unmounting chroot environment."
Mike Frysinger1aa61242011-09-15 17:46:44 -0400430 safe_umount_tree "${MOUNTED_PATH}/"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800431 fi
David James546747b2010-03-23 15:19:43 -0700432 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000433}
434
Greg Spencer798d75f2011-02-01 22:04:49 -0800435if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000436 setup_env
Don Garretta0e7ea12011-02-07 18:39:59 -0800437 info "Make sure you run"
438 info " $0 --unmount"
439 info "before deleting $FLAGS_chroot"
440 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000441 exit 0
442fi
443
Greg Spencer798d75f2011-02-01 22:04:49 -0800444if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000445 teardown_env
446 exit 0
447fi
448
449# Make sure we unmount before exiting
450trap teardown_env EXIT
451setup_env
452
Zdenek Behan892e6ac2011-08-12 05:56:40 +0200453CHROOT_PASSTHRU="BUILDBOT_BUILD=$FLAGS_build_number \
454CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
Raja Aluri8f2a9952010-11-17 15:03:00 -0800455CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
456CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}"
Raja Alurie891eea2010-11-16 20:12:56 -0800457
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700458# Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER,
459# CHROMEOS_VERSION_DEVSERVER as environment variables to override the default
460# assumptions (local AU server). These are used in cros_set_lsb_release, and
461# are used by external Chromium OS builders.
462CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
463CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK} \
464CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER} \
465CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}"
466
Mario Limonciello7052ae12011-05-05 12:00:49 -0500467# Pass proxy variables into the environment.
468for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do
469 eval value=\$${type}
470 if [ -n "${value}" ]; then
471 CHROOT_PASSTHRU="${CHROOT_PASSTHRU} ${type}=${value}"
472 fi
473done
474
derat@google.com4e7a92b2009-11-21 23:44:14 +0000475# Run command or interactive shell. Also include the non-chrooted path to
476# the source trunk for scripts that may need to print it (e.g.
477# build_image.sh).
Doug Anderson9362fa82010-12-16 14:44:12 -0800478sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
Mike Frysinger5df0eab2011-08-11 15:39:24 -0400479 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" SSH_AGENT_PID="${SSH_AGENT_PID}" \
Doug Anderson9362fa82010-12-16 14:44:12 -0800480 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000481
482# Remove trap and explicitly unmount
483trap - EXIT
484teardown_env