blob: b7f8621a5f618d990dd7fbade0f03e61c88b9908 [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
Brian Harringfeb04f72012-02-03 21:22:50 -080012enable_strict_sudo
13
derat@google.com86dcc8e2009-11-21 19:49:49 +000014# Script must be run outside the chroot and as a regular user.
rspangler@google.comd74220d2009-10-09 20:56:14 +000015assert_outside_chroot
derat@google.com86dcc8e2009-11-21 19:49:49 +000016assert_not_root_user
rspangler@google.comd74220d2009-10-09 20:56:14 +000017
18# Define command line flags
19# See http://code.google.com/p/shflags/wiki/Documentation10x
20DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
21 "The destination dir for the chroot environment." "d"
22DEFINE_string trunk "$GCLIENT_ROOT" \
23 "The source trunk to bind mount within the chroot." "s"
David McMahon03aeb202009-12-08 12:47:08 -080024DEFINE_string build_number "" \
25 "The build-bot build number (when called by buildbot only)." "b"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080026DEFINE_string chrome_root "" \
27 "The root of your chrome browser source. Should contain a 'src' subdir."
Sean Parent2898f752010-05-25 15:06:33 -070028DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \
29 "The mount point of the chrome broswer source in the chroot."
Brian Harring7ee892d2012-02-02 09:33:10 -080030DEFINE_string distfiles "" \
31 "Override the destination dir used for distfiles."
rspangler@google.comd74220d2009-10-09 20:56:14 +000032
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070033DEFINE_boolean official_build $FLAGS_FALSE \
34 "Set CHROMEOS_OFFICIAL=1 for release builds."
rspangler@google.comd74220d2009-10-09 20:56:14 +000035DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
36DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
Elly Jones7990a062010-09-02 09:23:23 -040037DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
Brian Harring35767822012-02-01 23:50:45 -080038DEFINE_boolean early_make_chroot $FLAGS_FALSE \
39 "Internal flag. If set, the command is run as root without sudo."
Don Garrettad3f0592011-02-04 14:59:56 -080040DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000041
42# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080043FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000044
45One or more VAR=value pairs can be specified to export variables into
46the chroot environment. For example:
47
48 $0 FOO=bar BAZ=bel
49
Doug Anderson9362fa82010-12-16 14:44:12 -080050If [-- command] is present, runs the command inside the chroot,
51after changing directory to /$USER/trunk/src/scripts. Note that neither
52the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000053
Doug Anderson9362fa82010-12-16 14:44:12 -080054 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000055
56Otherwise, provides an interactive shell.
57"
58
Don Garrettad3f0592011-02-04 14:59:56 -080059# Version of info from common.sh that only echos if --verbose is set.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040060debug() {
Don Garrettad3f0592011-02-04 14:59:56 -080061 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
David Rochberg351a76f2011-02-16 11:14:00 -050062 info "$*"
Don Garrettad3f0592011-02-04 14:59:56 -080063 fi
64}
65
rspangler@google.comd74220d2009-10-09 20:56:14 +000066# Parse command line flags
67FLAGS "$@" || exit 1
68eval set -- "${FLAGS_ARGV}"
69
Greg Spencer798d75f2011-02-01 22:04:49 -080070if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
David McMahon857dbb52009-12-09 18:21:05 -080071 CHROMEOS_OFFICIAL=1
72fi
73
Brian Harring7ee892d2012-02-02 09:33:10 -080074[ -z "${FLAGS_distfiles}" ] && \
75 FLAGS_distfiles="${FLAGS_trunk}/distfiles"
76
rspangler@google.comd74220d2009-10-09 20:56:14 +000077# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080078# so will die prematurely if 'switch_to_strict_mode' is specified before now.
rspangler@google.comd74220d2009-10-09 20:56:14 +000079# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080080switch_to_strict_mode
rspangler@google.comd74220d2009-10-09 20:56:14 +000081
Sean Parent2898f752010-05-25 15:06:33 -070082INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080083CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080084INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070085FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -070086AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
87SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080088
Mike Frysinger82649172011-09-21 00:18:14 -040089# Avoid the sudo call if possible since it is a little slow.
90if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then
91 sudo chmod 0777 "$FLAGS_chroot/var/lock"
92fi
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080093
94LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Zdenek Behane28239d2011-04-07 00:37:20 +020095SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080096
David Rochberg351a76f2011-02-16 11:14:00 -050097
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -040098MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger6b1abb22012-05-11 13:44:06 -040099mount_queue_init() {
Mike Frysinger286b5922011-09-28 11:59:53 -0400100 MOUNT_QUEUE=()
101}
102
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400103queue_mount() {
David Rochberg351a76f2011-02-16 11:14:00 -0500104 # If necessary, mount $source in the host FS at $target inside the
105 # chroot directory with $mount_args.
106 local source="$1"
107 local mount_args="$2"
108 local target="$3"
109
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400110 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500111
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400112 case " ${MOUNT_CACHE} " in
113 *" ${mounted_path} "*)
Mike Frysinger2a970592011-09-20 22:49:01 -0400114 # Already mounted!
115 ;;
116 *)
Mike Frysinger286b5922011-09-28 11:59:53 -0400117 MOUNT_QUEUE+=(
118 "mkdir -p '${mounted_path}'"
119 # The args are left unquoted on purpose.
120 "mount ${mount_args} '${source}' '${mounted_path}'"
121 )
Mike Frysinger2a970592011-09-20 22:49:01 -0400122 ;;
123 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500124}
125
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400126process_mounts() {
Mike Frysinger286b5922011-09-28 11:59:53 -0400127 if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then
128 return 0
129 fi
130 sudo_multi "${MOUNT_QUEUE[@]}"
131 mount_queue_init
132}
133
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400134env_sync_proc() {
Zdenek Behane28239d2011-04-07 00:37:20 +0200135 # This function runs and performs periodic updates to the chroot env, if
136 # necessary.
137
138 local poll_interval=10
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400139 local sync_files=( etc/resolv.conf etc/hosts )
Zdenek Behane28239d2011-04-07 00:37:20 +0200140
141 # Make sure the synced files are writable by normal user, so that we
142 # don't have to sudo inside the loop.
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400143 local chmods=$(find ${sync_files[@]/#/${FLAGS_chroot}/} '!' -user ${USER})
144 if [ -n "${chmods}" ]; then
145 sudo -- chown ${USER} ${chmods} 1>&2
146 fi
Zdenek Behane28239d2011-04-07 00:37:20 +0200147
David James412b9022011-07-15 19:54:16 -0700148 # Drop stdin, stderr, stdout, and chroot lock.
149 # This is needed for properly daemonizing the process.
150 exec 0>&- 1>&- 2>&- 200>&-
151
Zdenek Behane28239d2011-04-07 00:37:20 +0200152 while true; do
153 # Sync files
154 for file in ${sync_files}; do
155 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
156 cp -f /${file} ${FLAGS_chroot}/${file}
157 fi
158 done
159
160 sleep ${poll_interval}
161 done
162}
163
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400164copy_ssh_config() {
Vadim Bendebury0779f522011-06-12 12:09:16 -0700165 # Copy user .ssh/config into the chroot filtering out strings not supported
166 # by the chroot ssh. The chroot .ssh directory is passed in as the first
167 # parameter.
168
169 # ssh options to filter out. The entire strings containing these substrings
170 # will be deleted before copying.
171 local bad_options=(
Matt Tennant63c3cc52011-11-22 11:23:06 -0800172 'UseProxyIf'
173 'GSSAPIAuthentication'
174 'GSSAPIKeyExchange'
175 'ProxyUseFdpass'
Vadim Bendebury0779f522011-06-12 12:09:16 -0700176 )
177 local sshc="${HOME}/.ssh/config"
178 local chroot_ssh_dir="${1}"
179 local filter
180 local option
181
182 if [ ! -f "${sshc}" ]; then
183 return # Nothing to copy.
184 fi
185
186 for option in "${bad_options[@]}"
187 do
188 if [ -z "${filter}" ]; then
189 filter="${option}"
190 else
191 filter+="\\|${option}"
192 fi
193 done
194
195 sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config"
196}
197
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400198copy_into_chroot_if_exists() {
Matt Tennantf7c9e772012-02-08 17:06:26 -0800199 # $1 is file path outside of chroot to copy to path $2 inside chroot.
200 [ -e "$1" ] || return
201 cp "$1" "${FLAGS_chroot}/$2"
202}
203
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400204setup_env() {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800205 # Validate sudo timestamp before entering the critical section so that we
206 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800207 # Don't use sudo -v since that has issues on machines w/ no password.
208 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800209
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800210 (
211 flock 200
212 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000213
Taylor Hutt25bf9492011-07-27 13:54:35 -0700214 # If there isn't a syncer daemon started already, start one. The
215 # daemon is considered to not be started under the following
216 # conditions:
217 #
218 # o There is no PID file
219 #
220 # o The PID file is 0 bytes in size, which might be a partial
221 # manifestation of chromium-os:17680. This situation will not
222 # occur anymore, but you might have a chroot which was already
223 # affected.
224 #
225 # o The /proc node for the process named by the PID file does
226 # not exist.
227 #
228 # Note: This does not address PID recycling. While
229 # increasingly unlikely, it is possible for the PID in
230 # the PID file to refer to a running process that is not
231 # the syncer process. Since the PID file is now
232 # removed, I think it is only possible for this to occur
233 # if your system crashes and the PID file exists after
234 # restart.
235 #
236 # The daemon is killed by the enter_chroot that exits last.
Taylor Hutt18594a82011-07-28 11:45:50 -0700237 if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then
Taylor Hutt25bf9492011-07-27 13:54:35 -0700238 info "You may have suffered from chromium-os:17680 and";
239 info "could have stray 'enter_chroot.sh' processes running.";
240 info "You must manually kill any such stray processes.";
241 info "Exit all chroot shells; remaining 'enter_chroot.sh'";
242 info "processes are probably stray.";
Taylor Hutt18594a82011-07-28 11:45:50 -0700243 sudo rm -f "${SYNCERPIDFILE}";
Taylor Hutt25bf9492011-07-27 13:54:35 -0700244 fi;
David James412b9022011-07-15 19:54:16 -0700245 if ! [ -f "${SYNCERPIDFILE}" ] || \
Mike Frysinger61e4f282011-09-20 22:37:22 -0400246 ! [ -d /proc/$(<"${SYNCERPIDFILE}") ]; then
David James412b9022011-07-15 19:54:16 -0700247 debug "Starting sync process"
248 env_sync_proc &
249 echo $! > "${SYNCERPIDFILE}"
250 disown $!
251 fi
252
Don Garretta0e7ea12011-02-07 18:39:59 -0800253 debug "Mounting chroot environment."
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400254 MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts))
Mike Frysinger286b5922011-09-28 11:59:53 -0400255 mount_queue_init
256 queue_mount none "-t proc" /proc
257 queue_mount none "-t sysfs" /sys
258 queue_mount /dev "--bind" /dev
259 queue_mount none "-t devpts" /dev/pts
Aaron Plattner130d3632011-10-18 08:54:57 -0700260 if [ -d /run ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400261 queue_mount /run "--bind" /run
Aaron Plattner130d3632011-10-18 08:54:57 -0700262 if [ -d /run/shm ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400263 queue_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700264 fi
265 fi
Mike Frysinger286b5922011-09-28 11:59:53 -0400266 queue_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800267
Brian Harringdd7d7932011-12-23 04:21:33 -0800268
269 debug "Setting up referenced repositories if required."
270 REFERENCE_DIR=$(git config --file \
271 "${FLAGS_trunk}/.repo/manifests.git/config" \
272 repo.reference)
273 if [ -n "${REFERENCE_DIR}" ]; then
274
275 IFS=$'\n';
276 required=( $( "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \
277 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
278 unset IFS
279
280 queue_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
281 "${CHROOT_TRUNK_DIR}/.repo/alternates"
282
283 # Note that as we're bringing up each referened repo, we also
284 # mount bind an empty directory over its alternates. This is
285 # required to suppress git from tracing through it- we already
286 # specify the required alternates for CHROOT_TRUNK_DIR, no point
287 # in having git try recursing through each on their own.
288 #
289 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
290 # the bind is visible only w/in the chroot.
291 mkdir -p ${FLAGS_trunk}/.repo/chroot/empty
292 position=1
293 for x in "${required[@]}"; do
294 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
295 queue_mount "${x}" "--bind" "${base}"
296 if [ -e "${x}/.repo/alternates" ]; then
297 queue_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
298 "${base}/.repo/alternates"
299 fi
300 position=$(( ${position} + 1 ))
301 done
302 unset required position base
303 fi
304 unset REFERENCE_DIR
305
Brian Harring7ee892d2012-02-02 09:33:10 -0800306 debug "Setting up shared distfiles directory."
307 mkdir -p "${FLAGS_distfiles}"/{target,host}
308 sudo mkdir -p "${FLAGS_chroot}/var/cache/distfiles/"
309 queue_mount "${FLAGS_distfiles}" "--bind" "/var/cache/distfiles"
310
Elly Jones7990a062010-09-02 09:23:23 -0400311 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800312 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
Mike Frysinger61e4f282011-09-20 22:37:22 -0400313 TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh"
Elly Jones7990a062010-09-02 09:23:23 -0400314 mkdir -p "${TARGET_DIR}"
Mike Frysinger40bb0c32011-10-31 17:14:15 -0400315 # Ignore errors as some people won't have these files to copy.
316 cp "${HOME}"/.ssh/{known_hosts,*.pub} "${TARGET_DIR}/" 2>/dev/null || :
Vadim Bendebury0779f522011-06-12 12:09:16 -0700317 copy_ssh_config "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500318
319 # Don't try to bind mount the ssh agent dir if it has gone stale.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400320 ASOCK=${SSH_AUTH_SOCK%/*}
Mike Frysinger9de707f2011-12-12 14:21:44 -0500321 if [ -d "${ASOCK}" ]; then
322 queue_mount "${ASOCK}" "--bind" "${ASOCK}"
323 fi
Elly Jones7990a062010-09-02 09:23:23 -0400324 fi
325 fi
326
Mike Frysinger286b5922011-09-28 11:59:53 -0400327 if [ -d "$HOME/.subversion" ]; then
328 TARGET="/home/${USER}/.subversion"
329 mkdir -p "${FLAGS_chroot}${TARGET}"
330 queue_mount "${HOME}/.subversion" "--bind" "${TARGET}"
331 fi
332
333 if DEPOT_TOOLS=$(type -P gclient) ; then
334 DEPOT_TOOLS=${DEPOT_TOOLS%/*} # dirname
335 debug "Mounting depot_tools"
336 queue_mount "$DEPOT_TOOLS" --bind "$INNER_DEPOT_TOOLS_ROOT"
337 fi
338
339 process_mounts
340
Mike Frysinger9a50b642011-09-20 23:37:14 -0400341 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
342 if [ -z "$CHROME_ROOT" ]; then
343 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
344 2>/dev/null || :)"
345 CHROME_ROOT_AUTO=1
346 fi
347 if [[ -n "$CHROME_ROOT" ]]; then
348 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
349 error "Not mounting chrome source"
350 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
351 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
352 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800353 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400354 else
355 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
356 sudo bash -c "echo '$CHROME_ROOT' > \
357 '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'"
Mike Frysinger286b5922011-09-28 11:59:53 -0400358 queue_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800359 fi
360 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800361
Mike Frysinger286b5922011-09-28 11:59:53 -0400362 process_mounts
Chris Sosa317d8eb2010-04-05 15:45:28 -0700363
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400364 # Install fuse module. Skip modprobe when possible for slight
365 # speed increase when initializing the env.
366 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700367 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200368 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700369 fi
370
Chris Masone162f6542010-05-12 14:58:37 -0700371 # Turn off automounting of external media when we enter the
372 # chroot; thus we don't have to worry about being able to unmount
373 # from inside.
Mike Frysingerda7be782011-09-15 17:58:19 -0400374 if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then
375 if [ "${SAVED_PREF}" != "false" ]; then
376 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
377 warn "-- Note: USB sticks may be automounted by your host OS."
378 warn "-- Note: If you plan to burn bootable media, you may need to"
379 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
380 warn "-- Note: outside the chroot."
381 fi
Chris Masone162f6542010-05-12 14:58:37 -0700382 fi
383 fi
Mike Frysingerda7be782011-09-15 17:58:19 -0400384 # Always write the temp file so we can read it when exiting
385 echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
Chris Masone162f6542010-05-12 14:58:37 -0700386
Mike Frysinger94717e32011-09-21 00:10:44 -0400387 # Configure committer username and email in chroot .gitconfig. Change
388 # to the root directory first so that random $PWD/.git/config settings
389 # do not get picked up. We want to stick to ~/.gitconfig only.
390 ident=$(cd /; git var GIT_COMMITTER_IDENT || :)
391 ident_name=${ident%% <*}
392 ident_email=${ident%%>*}; ident_email=${ident_email##*<}
David James342f1562011-07-15 15:21:18 -0700393 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400394 user.name "${ident_name}" || true
David James342f1562011-07-15 15:21:18 -0700395 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400396 user.email "${ident_email}" || true
David James342f1562011-07-15 15:21:18 -0700397
Matt Tennantf7c9e772012-02-08 17:06:26 -0800398 # Copy ~/.gdata_cred.txt and ~/.gdata_token to chroot if they exist. These
399 # files contain credentials for reading/writing Google Docs on chromium.org.
400 for fn in ".gdata_cred.txt" ".gdata_token"; do
401 copy_into_chroot_if_exists "${HOME}/${fn}" "/home/${USER}/${fn}"
402 done
Matt Tennantd4316fe2011-08-30 12:06:42 -0700403
Mike Frysinger6601c522011-08-15 11:05:39 -0400404 # Make sure user's requested locales are available
405 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400406 # And make sure en_US{,.UTF-8} are always available as
407 # that what buildbot forces internally
408 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400409 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
410 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
411 sort -u | sed '/^C$/d')
412 gen_locales=()
413 for l in ${locales}; do
414 if [[ ${l} == *.* ]]; then
415 enc=${l#*.}
416 else
417 enc="ISO-8859-1"
418 fi
419 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
420 utf8) enc="UTF-8";;
421 esac
422 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
423 done
Mike Frysinger4d258cd2011-09-15 16:17:49 -0400424 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
425 # Force LC_ALL=C to workaround slow string parsing in bash
426 # with long multibyte strings. Newer setups have this fixed,
427 # but locale-gen doesn't need to be run in any locale in the
428 # first place, so just go with C to keep it fast.
429 sudo -- chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \
430 -G "$(printf '%s\n' "${gen_locales[@]}")"
431 fi
Mike Frysinger6601c522011-08-15 11:05:39 -0400432
Dale Curtis98631732011-05-05 16:16:59 -0700433 # Fix permissions on shared memory to allow non-root users access to POSIX
Mike Frysinger82649172011-09-21 00:18:14 -0400434 # semaphores. Avoid the sudo call if possible (sudo is slow).
435 if [ -n "$(find "${FLAGS_chroot}/dev/shm" ! -perm 777)" ] ; then
436 sudo chmod -R 777 "${FLAGS_chroot}/dev/shm"
437 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400438
439 # If the private overlays are installed, gsutil can use those credentials.
440 if [ ! -e "${FLAGS_chroot}/home/${USER}/.boto" ]; then
441 boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto'
442 if [ -s "${FLAGS_trunk}/${boto}" ]; then
443 ln -s "trunk/${boto}" "${FLAGS_chroot}/home/${USER}/.boto"
444 fi
445 fi
446
447 # Have found a few chroots where ~/.gsutil is owned by root:root, probably
448 # as a result of old gsutil or tools. This causes permission errors when
449 # gsutil cp tries to create its cache files, so ensure the user can
450 # actually write to their directory.
451 gsutil_dir="${FLAGS_chroot}/home/${USER}/.gsutil"
452 if [ -d "${gsutil_dir}" ] && [ ! -w "${gsutil_dir}" ]; then
453 sudo chown -R "${USER}:$(id -gn)" "${gsutil_dir}"
454 fi
David James546747b2010-03-23 15:19:43 -0700455 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000456}
457
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400458teardown_env() {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800459 # Validate sudo timestamp before entering the critical section so that we
460 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800461 # Don't use sudo -v since that has issues on machines w/ no password.
462 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800463
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800464 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800465 (
466 flock 200
467
468 # check each pid in $LOCKFILE to see if it's died unexpectedly
469 TMP_LOCKFILE="$LOCKFILE.tmp"
470
471 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
472 cat "$LOCKFILE" | while read PID; do
473 if [ "$PID" = "$$" ]; then
474 # ourself, leave PROC_NAME empty
475 PROC_NAME=""
476 else
477 PROC_NAME=$(ps --pid $PID -o comm=)
478 fi
479
480 if [ ! -z "$PROC_NAME" ]; then
481 # All good, keep going
482 echo "$PID" >> "$TMP_LOCKFILE"
483 fi
484 done
485 # Remove any dups from lock file while installing new one
Mike Frysinger61e4f282011-09-20 22:37:22 -0400486 sort -u -n "$TMP_LOCKFILE" > "$LOCKFILE"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800487
Mike Frysingerda7be782011-09-15 17:58:19 -0400488 SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
489 if [ "${SAVED_PREF}" != "false" ]; then
Chris Masone162f6542010-05-12 14:58:37 -0700490 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
Sean Ocd8b1d12010-09-02 17:34:49 +0200491 warn "could not re-set your automount preference."
Chris Masone162f6542010-05-12 14:58:37 -0700492 fi
493
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800494 if [ -s "$LOCKFILE" ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800495 debug "At least one other pid is running in the chroot, so not"
496 debug "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800497 else
Zdenek Behane28239d2011-04-07 00:37:20 +0200498 debug "Stopping syncer process"
Taylor Hutt25bf9492011-07-27 13:54:35 -0700499 # If another process entering the chroot is blocked on this
500 # flock in setup_env(), it can be a race condition.
501 #
502 # When this locked region is exited, the setup_env() flock can
503 # be entered before the script can exit and the /proc entry for
504 # the PID is removed. The newly-created chroot will not end up
505 # with a syncer process. To avoid that situation, remove the
506 # syncer PID file.
507 #
508 # The syncer PID file should also be removed because the kernel
509 # will reuse PIDs. It's possible that the PID in the syncer PID
510 # has been reused by another process; make sure we don't skip
511 # starting the syncer process when this occurs by deleting the
512 # PID file.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400513 kill $(<"${SYNCERPIDFILE}") && \
Mike Frysinger470be992011-09-28 11:53:56 -0400514 { rm -f "${SYNCERPIDFILE}" 2>/dev/null || \
515 sudo rm -f "${SYNCERPIDFILE}" ; } ||
516 debug "Unable to clean up syncer process.";
Zdenek Behane28239d2011-04-07 00:37:20 +0200517
Don Garretta0e7ea12011-02-07 18:39:59 -0800518 debug "Unmounting chroot environment."
Mike Frysinger1aa61242011-09-15 17:46:44 -0400519 safe_umount_tree "${MOUNTED_PATH}/"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800520 fi
David James546747b2010-03-23 15:19:43 -0700521 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000522}
523
Greg Spencer798d75f2011-02-01 22:04:49 -0800524if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000525 setup_env
Don Garretta0e7ea12011-02-07 18:39:59 -0800526 info "Make sure you run"
527 info " $0 --unmount"
528 info "before deleting $FLAGS_chroot"
529 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000530 exit 0
531fi
532
Greg Spencer798d75f2011-02-01 22:04:49 -0800533if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000534 teardown_env
535 exit 0
536fi
537
538# Make sure we unmount before exiting
539trap teardown_env EXIT
540setup_env
541
Brian Harring35767822012-02-01 23:50:45 -0800542CHROOT_PASSTHRU=(
543 "BUILDBOT_BUILD=$FLAGS_build_number"
544 "CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
545 "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
Raja Alurie891eea2010-11-16 20:12:56 -0800546
Brian Harring35767822012-02-01 23:50:45 -0800547 # Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER,
548 # CHROMEOS_VERSION_DEVSERVER as environment variables to override the default
549 # assumptions (local AU server). These are used in cros_set_lsb_release, and
550 # are used by external Chromium OS builders.
551
552 "CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK}"
553 "CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER}"
554 "CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}"
555 "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
556 "SSH_AGENT_PID=${SSH_AGENT_PID}"
557 "SSH_AUTH_SOCK=${SSH_AUTH_SOCK}"
558)
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700559
Mario Limonciello7052ae12011-05-05 12:00:49 -0500560# Pass proxy variables into the environment.
561for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do
Brian Harring35767822012-02-01 23:50:45 -0800562 if [ -n "${!type}" ]; then
563 CHROOT_PASSTHRU+=( "${type}=${!type}" )
564 fi
Mario Limonciello7052ae12011-05-05 12:00:49 -0500565done
566
derat@google.com4e7a92b2009-11-21 23:44:14 +0000567# Run command or interactive shell. Also include the non-chrooted path to
568# the source trunk for scripts that may need to print it (e.g.
569# build_image.sh).
Brian Harring35767822012-02-01 23:50:45 -0800570
571if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
572 cmd=( /bin/bash -l -c 'env "$@"' -- )
573elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
574 # Complain that sudo is missing.
575 error "Failing since the chroot lacks sudo."
576 error "Requested enter_chroot command was: $@"
577 exit 127
578else
579 cmd=( sudo -i -u "$USER" )
580fi
581
582sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000583
584# Remove trap and explicitly unmount
585trap - EXIT
586teardown_env