blob: 619529d2ab67e209e0ab099f366fc2becdcb4937 [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
Peter Mayo4411efe2012-09-21 04:41:27 -040059CROS_LOG_PREFIX=cros_sdk:enter_chroot
60
Don Garrettad3f0592011-02-04 14:59:56 -080061# Version of info from common.sh that only echos if --verbose is set.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040062debug() {
Don Garrettad3f0592011-02-04 14:59:56 -080063 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
David Rochberg351a76f2011-02-16 11:14:00 -050064 info "$*"
Don Garrettad3f0592011-02-04 14:59:56 -080065 fi
66}
67
rspangler@google.comd74220d2009-10-09 20:56:14 +000068# Parse command line flags
69FLAGS "$@" || exit 1
70eval set -- "${FLAGS_ARGV}"
71
Greg Spencer798d75f2011-02-01 22:04:49 -080072if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
David McMahon857dbb52009-12-09 18:21:05 -080073 CHROMEOS_OFFICIAL=1
74fi
75
Brian Harring7ee892d2012-02-02 09:33:10 -080076[ -z "${FLAGS_distfiles}" ] && \
77 FLAGS_distfiles="${FLAGS_trunk}/distfiles"
78
rspangler@google.comd74220d2009-10-09 20:56:14 +000079# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080080# so will die prematurely if 'switch_to_strict_mode' is specified before now.
rspangler@google.comd74220d2009-10-09 20:56:14 +000081# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080082switch_to_strict_mode
rspangler@google.comd74220d2009-10-09 20:56:14 +000083
Matt Tennant298f61a2012-06-25 21:54:33 -070084# These config files are to be copied into chroot if they exist in home dir.
85FILES_TO_COPY_TO_CHROOT=(
86 .gdata_cred.txt # User/password for Google Docs on chromium.org
87 .gdata_token # Auth token for Google Docs on chromium.org
88 .disable_build_stats_upload # Presence of file disables command stats upload
89)
90
Sean Parent2898f752010-05-25 15:06:33 -070091INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080092CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080093INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070094FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -070095AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
96SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080097
Mike Frysinger82649172011-09-21 00:18:14 -040098# Avoid the sudo call if possible since it is a little slow.
99if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then
100 sudo chmod 0777 "$FLAGS_chroot/var/lock"
101fi
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800102
103LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Zdenek Behane28239d2011-04-07 00:37:20 +0200104SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800105
David Rochberg351a76f2011-02-16 11:14:00 -0500106
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400107MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400108mount_queue_init() {
Mike Frysinger286b5922011-09-28 11:59:53 -0400109 MOUNT_QUEUE=()
110}
111
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400112queue_mount() {
David Rochberg351a76f2011-02-16 11:14:00 -0500113 # If necessary, mount $source in the host FS at $target inside the
114 # chroot directory with $mount_args.
115 local source="$1"
116 local mount_args="$2"
117 local target="$3"
118
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400119 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500120
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400121 case " ${MOUNT_CACHE} " in
122 *" ${mounted_path} "*)
Mike Frysinger2a970592011-09-20 22:49:01 -0400123 # Already mounted!
124 ;;
125 *)
Mike Frysinger286b5922011-09-28 11:59:53 -0400126 MOUNT_QUEUE+=(
127 "mkdir -p '${mounted_path}'"
128 # The args are left unquoted on purpose.
129 "mount ${mount_args} '${source}' '${mounted_path}'"
130 )
Mike Frysinger2a970592011-09-20 22:49:01 -0400131 ;;
132 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500133}
134
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400135process_mounts() {
Mike Frysinger286b5922011-09-28 11:59:53 -0400136 if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then
137 return 0
138 fi
139 sudo_multi "${MOUNT_QUEUE[@]}"
140 mount_queue_init
141}
142
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400143env_sync_proc() {
Zdenek Behane28239d2011-04-07 00:37:20 +0200144 # This function runs and performs periodic updates to the chroot env, if
145 # necessary.
146
147 local poll_interval=10
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400148 local sync_files=( etc/resolv.conf etc/hosts )
Zdenek Behane28239d2011-04-07 00:37:20 +0200149
Mike Frysinger4d8c2852012-05-16 15:22:49 -0400150 # Make sure the files exist before the find -- they might not in a
151 # fresh chroot which results in warnings in the build output.
152 local chown_cmd=(
153 # Make sure the files exists first -- they might not in a fresh chroot.
154 "touch ${sync_files[*]/#/${FLAGS_chroot}/}"
155 # Make sure the files are writable by normal user so that we don't have
156 # to execute sudo in the main loop below.
157 "chown ${USER} ${sync_files[*]/#/${FLAGS_chroot}/}"
158 )
159 sudo_multi "${chown_cmd[@]}"
Zdenek Behane28239d2011-04-07 00:37:20 +0200160
David James412b9022011-07-15 19:54:16 -0700161 # Drop stdin, stderr, stdout, and chroot lock.
162 # This is needed for properly daemonizing the process.
163 exec 0>&- 1>&- 2>&- 200>&-
164
Zdenek Behane28239d2011-04-07 00:37:20 +0200165 while true; do
166 # Sync files
167 for file in ${sync_files}; do
168 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
169 cp -f /${file} ${FLAGS_chroot}/${file}
170 fi
171 done
172
173 sleep ${poll_interval}
174 done
175}
176
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400177copy_ssh_config() {
Vadim Bendebury0779f522011-06-12 12:09:16 -0700178 # Copy user .ssh/config into the chroot filtering out strings not supported
179 # by the chroot ssh. The chroot .ssh directory is passed in as the first
180 # parameter.
181
182 # ssh options to filter out. The entire strings containing these substrings
183 # will be deleted before copying.
184 local bad_options=(
Matt Tennant63c3cc52011-11-22 11:23:06 -0800185 'UseProxyIf'
186 'GSSAPIAuthentication'
187 'GSSAPIKeyExchange'
188 'ProxyUseFdpass'
Vadim Bendebury0779f522011-06-12 12:09:16 -0700189 )
190 local sshc="${HOME}/.ssh/config"
191 local chroot_ssh_dir="${1}"
192 local filter
193 local option
194
195 if [ ! -f "${sshc}" ]; then
196 return # Nothing to copy.
197 fi
198
199 for option in "${bad_options[@]}"
200 do
201 if [ -z "${filter}" ]; then
202 filter="${option}"
203 else
204 filter+="\\|${option}"
205 fi
206 done
207
208 sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config"
209}
210
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400211copy_into_chroot_if_exists() {
Matt Tennantf7c9e772012-02-08 17:06:26 -0800212 # $1 is file path outside of chroot to copy to path $2 inside chroot.
213 [ -e "$1" ] || return
214 cp "$1" "${FLAGS_chroot}/$2"
215}
216
Peter Mayo4411efe2012-09-21 04:41:27 -0400217# Usage: promote_api_keys
218# This takes care of getting the developer API keys into the chroot where
219# chrome can build with them. It needs to take it from the places a dev
220# is likely to put them, and recognize that older chroots may or may not
221# have been used since the concept of keys got added, as well as before
222# and after the developer decding to grab his own keys.
223promote_api_keys() {
224 local destination="${FLAGS_chroot}/home/${USER}/.googleapikeys"
225 # Don't disturb existing keys. They could be set differently
226 if [[ -s "${destination}" ]]; then
227 return 0
228 fi
229 if [[ -r "${HOME}/.googleapikeys" ]]; then
230 cp "${HOME}/.googleapikeys" "${destination}"
231 if [[ -s "${destination}" ]] ; then
232 info "Copied Google API keys into chroot."
233 fi
234 elif [[ -r "${HOME}/.gyp/include.gypi" ]]; then
235 local NAME="('google_(api_key|default_client_(id|secret))')"
236 local WS="[[:space:]]*"
237 local CONTENTS="('[^\\\\']*')"
238 sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \
239 "${HOME}/.gyp/include.gypi" >"${destination}"
240 if [[ -s "${destination}" ]]; then
241 info "Put discovered Google API keys into chroot."
242 fi
243 fi
244}
245
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400246setup_env() {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800247 # Validate sudo timestamp before entering the critical section so that we
248 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800249 # Don't use sudo -v since that has issues on machines w/ no password.
250 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800251
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800252 (
253 flock 200
254 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000255
Taylor Hutt25bf9492011-07-27 13:54:35 -0700256 # If there isn't a syncer daemon started already, start one. The
257 # daemon is considered to not be started under the following
258 # conditions:
259 #
260 # o There is no PID file
261 #
262 # o The PID file is 0 bytes in size, which might be a partial
263 # manifestation of chromium-os:17680. This situation will not
264 # occur anymore, but you might have a chroot which was already
265 # affected.
266 #
267 # o The /proc node for the process named by the PID file does
268 # not exist.
269 #
270 # Note: This does not address PID recycling. While
271 # increasingly unlikely, it is possible for the PID in
272 # the PID file to refer to a running process that is not
273 # the syncer process. Since the PID file is now
274 # removed, I think it is only possible for this to occur
275 # if your system crashes and the PID file exists after
276 # restart.
277 #
278 # The daemon is killed by the enter_chroot that exits last.
Taylor Hutt18594a82011-07-28 11:45:50 -0700279 if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then
Taylor Hutt25bf9492011-07-27 13:54:35 -0700280 info "You may have suffered from chromium-os:17680 and";
281 info "could have stray 'enter_chroot.sh' processes running.";
282 info "You must manually kill any such stray processes.";
283 info "Exit all chroot shells; remaining 'enter_chroot.sh'";
284 info "processes are probably stray.";
Taylor Hutt18594a82011-07-28 11:45:50 -0700285 sudo rm -f "${SYNCERPIDFILE}";
Taylor Hutt25bf9492011-07-27 13:54:35 -0700286 fi;
David James412b9022011-07-15 19:54:16 -0700287 if ! [ -f "${SYNCERPIDFILE}" ] || \
Mike Frysinger61e4f282011-09-20 22:37:22 -0400288 ! [ -d /proc/$(<"${SYNCERPIDFILE}") ]; then
David James412b9022011-07-15 19:54:16 -0700289 debug "Starting sync process"
290 env_sync_proc &
291 echo $! > "${SYNCERPIDFILE}"
292 disown $!
293 fi
294
Don Garretta0e7ea12011-02-07 18:39:59 -0800295 debug "Mounting chroot environment."
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400296 MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts))
Mike Frysinger286b5922011-09-28 11:59:53 -0400297 mount_queue_init
298 queue_mount none "-t proc" /proc
299 queue_mount none "-t sysfs" /sys
300 queue_mount /dev "--bind" /dev
301 queue_mount none "-t devpts" /dev/pts
Aaron Plattner130d3632011-10-18 08:54:57 -0700302 if [ -d /run ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400303 queue_mount /run "--bind" /run
Aaron Plattner130d3632011-10-18 08:54:57 -0700304 if [ -d /run/shm ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400305 queue_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700306 fi
307 fi
Brian Harringf264b822012-09-01 01:39:03 -0700308 # Get path overrides for the chroot in place now- it's possible
309 # that they may be needed for early teardown.
310 queue_mount "${FLAGS_trunk}/src/scripts/path-overrides" "--bind" \
311 "/usr/local/path-overrides"
Brian Harringdd7d7932011-12-23 04:21:33 -0800312
Brian Harringf264b822012-09-01 01:39:03 -0700313 queue_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Chris Sosa389634d2012-09-05 19:56:53 -0700314
Brian Harringdd7d7932011-12-23 04:21:33 -0800315 debug "Setting up referenced repositories if required."
316 REFERENCE_DIR=$(git config --file \
317 "${FLAGS_trunk}/.repo/manifests.git/config" \
318 repo.reference)
319 if [ -n "${REFERENCE_DIR}" ]; then
320
Brian Harring334050f2012-06-08 17:40:58 -0700321 ALTERNATES="${FLAGS_trunk}/.repo/alternates"
322
323 # Ensure this directory exists ourselves, and has the correct ownership.
324 [ -d "${ALTERNATES}" ] || mkdir "${ALTERNATES}"
325 [ -w "${ALTERNATES}" ] || sudo chown -R "${USER}" "${ALTERNATES}"
326
327 unset ALTERNATES
328
Brian Harringdd7d7932011-12-23 04:21:33 -0800329 IFS=$'\n';
330 required=( $( "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \
331 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
332 unset IFS
333
334 queue_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
335 "${CHROOT_TRUNK_DIR}/.repo/alternates"
336
337 # Note that as we're bringing up each referened repo, we also
338 # mount bind an empty directory over its alternates. This is
339 # required to suppress git from tracing through it- we already
340 # specify the required alternates for CHROOT_TRUNK_DIR, no point
341 # in having git try recursing through each on their own.
342 #
343 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
344 # the bind is visible only w/in the chroot.
345 mkdir -p ${FLAGS_trunk}/.repo/chroot/empty
346 position=1
347 for x in "${required[@]}"; do
348 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
349 queue_mount "${x}" "--bind" "${base}"
350 if [ -e "${x}/.repo/alternates" ]; then
351 queue_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
352 "${base}/.repo/alternates"
353 fi
354 position=$(( ${position} + 1 ))
355 done
356 unset required position base
357 fi
358 unset REFERENCE_DIR
359
Brian Harring7ee892d2012-02-02 09:33:10 -0800360 debug "Setting up shared distfiles directory."
361 mkdir -p "${FLAGS_distfiles}"/{target,host}
362 sudo mkdir -p "${FLAGS_chroot}/var/cache/distfiles/"
363 queue_mount "${FLAGS_distfiles}" "--bind" "/var/cache/distfiles"
364
Elly Jones7990a062010-09-02 09:23:23 -0400365 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800366 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
Mike Frysinger61e4f282011-09-20 22:37:22 -0400367 TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh"
Elly Jones7990a062010-09-02 09:23:23 -0400368 mkdir -p "${TARGET_DIR}"
Mike Frysinger40bb0c32011-10-31 17:14:15 -0400369 # Ignore errors as some people won't have these files to copy.
370 cp "${HOME}"/.ssh/{known_hosts,*.pub} "${TARGET_DIR}/" 2>/dev/null || :
Vadim Bendebury0779f522011-06-12 12:09:16 -0700371 copy_ssh_config "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500372
373 # Don't try to bind mount the ssh agent dir if it has gone stale.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400374 ASOCK=${SSH_AUTH_SOCK%/*}
Mike Frysinger9de707f2011-12-12 14:21:44 -0500375 if [ -d "${ASOCK}" ]; then
376 queue_mount "${ASOCK}" "--bind" "${ASOCK}"
377 fi
Elly Jones7990a062010-09-02 09:23:23 -0400378 fi
379 fi
380
Mike Frysinger286b5922011-09-28 11:59:53 -0400381 if [ -d "$HOME/.subversion" ]; then
382 TARGET="/home/${USER}/.subversion"
383 mkdir -p "${FLAGS_chroot}${TARGET}"
384 queue_mount "${HOME}/.subversion" "--bind" "${TARGET}"
385 fi
386
387 if DEPOT_TOOLS=$(type -P gclient) ; then
388 DEPOT_TOOLS=${DEPOT_TOOLS%/*} # dirname
389 debug "Mounting depot_tools"
390 queue_mount "$DEPOT_TOOLS" --bind "$INNER_DEPOT_TOOLS_ROOT"
391 fi
392
393 process_mounts
394
Mike Frysinger9a50b642011-09-20 23:37:14 -0400395 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
396 if [ -z "$CHROME_ROOT" ]; then
397 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
398 2>/dev/null || :)"
399 CHROME_ROOT_AUTO=1
400 fi
401 if [[ -n "$CHROME_ROOT" ]]; then
402 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
403 error "Not mounting chrome source"
404 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
405 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
406 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800407 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400408 else
409 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
410 sudo bash -c "echo '$CHROME_ROOT' > \
411 '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'"
Mike Frysinger286b5922011-09-28 11:59:53 -0400412 queue_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800413 fi
414 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800415
Mike Frysinger286b5922011-09-28 11:59:53 -0400416 process_mounts
Chris Sosa317d8eb2010-04-05 15:45:28 -0700417
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400418 # Install fuse module. Skip modprobe when possible for slight
419 # speed increase when initializing the env.
420 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700421 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200422 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700423 fi
424
David Jamesc9ca3db2012-07-09 08:12:26 -0700425 # Turn off automounting of external media when we enter the
426 # chroot; thus we don't have to worry about being able to unmount
427 # from inside.
428 if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then
429 if [ "${SAVED_PREF}" != "false" ]; then
430 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
431 warn "-- Note: USB sticks may be automounted by your host OS."
432 warn "-- Note: If you plan to burn bootable media, you may need to"
433 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
434 warn "-- Note: outside the chroot."
435 fi
436 fi
437 fi
438 # Always write the temp file so we can read it when exiting
439 echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
440
Mike Frysinger926a4b92012-06-27 15:22:28 -0400441 # Fix permissions on ccache tree. If this is a fresh chroot, then they
442 # might not be set up yet. Or if the user manually `rm -rf`-ed things,
443 # we need to reset it. Otherwise, gcc itself takes care of fixing things
444 # on demand, but only when it updates.
445 ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache"
446 if [[ ! -d ${ccache_dir} ]]; then
447 sudo mkdir -p -m 2775 "${ccache_dir}"
448 fi
449 sudo find -H "${ccache_dir}" -type d -exec chmod 2775 {} + &
450 sudo find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} + &
451
Mike Frysinger94717e32011-09-21 00:10:44 -0400452 # Configure committer username and email in chroot .gitconfig. Change
453 # to the root directory first so that random $PWD/.git/config settings
454 # do not get picked up. We want to stick to ~/.gitconfig only.
455 ident=$(cd /; git var GIT_COMMITTER_IDENT || :)
456 ident_name=${ident%% <*}
457 ident_email=${ident%%>*}; ident_email=${ident_email##*<}
David James342f1562011-07-15 15:21:18 -0700458 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400459 user.name "${ident_name}" || true
David James342f1562011-07-15 15:21:18 -0700460 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400461 user.email "${ident_email}" || true
David James342f1562011-07-15 15:21:18 -0700462
Matt Tennant298f61a2012-06-25 21:54:33 -0700463 # Certain files get copied into the chroot when entering.
464 for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do
Matt Tennantf7c9e772012-02-08 17:06:26 -0800465 copy_into_chroot_if_exists "${HOME}/${fn}" "/home/${USER}/${fn}"
466 done
Peter Mayo4411efe2012-09-21 04:41:27 -0400467 promote_api_keys
Matt Tennantd4316fe2011-08-30 12:06:42 -0700468
Mike Frysinger6601c522011-08-15 11:05:39 -0400469 # Make sure user's requested locales are available
470 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400471 # And make sure en_US{,.UTF-8} are always available as
472 # that what buildbot forces internally
473 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400474 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
475 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
476 sort -u | sed '/^C$/d')
477 gen_locales=()
478 for l in ${locales}; do
479 if [[ ${l} == *.* ]]; then
480 enc=${l#*.}
481 else
482 enc="ISO-8859-1"
483 fi
484 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
485 utf8) enc="UTF-8";;
486 esac
487 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
488 done
Mike Frysinger4d258cd2011-09-15 16:17:49 -0400489 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
490 # Force LC_ALL=C to workaround slow string parsing in bash
491 # with long multibyte strings. Newer setups have this fixed,
492 # but locale-gen doesn't need to be run in any locale in the
493 # first place, so just go with C to keep it fast.
494 sudo -- chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \
495 -G "$(printf '%s\n' "${gen_locales[@]}")"
496 fi
Mike Frysinger6601c522011-08-15 11:05:39 -0400497
Dale Curtis98631732011-05-05 16:16:59 -0700498 # Fix permissions on shared memory to allow non-root users access to POSIX
Mike Frysinger82649172011-09-21 00:18:14 -0400499 # semaphores. Avoid the sudo call if possible (sudo is slow).
500 if [ -n "$(find "${FLAGS_chroot}/dev/shm" ! -perm 777)" ] ; then
501 sudo chmod -R 777 "${FLAGS_chroot}/dev/shm"
502 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400503
504 # If the private overlays are installed, gsutil can use those credentials.
Gilad Arnold264f64d2012-09-06 14:01:45 -0700505 # We're also installing credentials for use by sudoed invocations.
506 boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto'
507 if [ -s "${FLAGS_trunk}/${boto}" ]; then
508 if [ ! -e "${FLAGS_chroot}/home/${USER}/.boto" ]; then
Chris Wolfe916b1f12012-04-30 16:03:06 -0400509 ln -s "trunk/${boto}" "${FLAGS_chroot}/home/${USER}/.boto"
510 fi
Gilad Arnold264f64d2012-09-06 14:01:45 -0700511 if [ ! -e "${FLAGS_chroot}/root/.boto" ]; then
512 sudo ln -s "../home/${USER}/trunk/${boto}" "${FLAGS_chroot}/root/.boto"
513 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400514 fi
515
516 # Have found a few chroots where ~/.gsutil is owned by root:root, probably
517 # as a result of old gsutil or tools. This causes permission errors when
518 # gsutil cp tries to create its cache files, so ensure the user can
519 # actually write to their directory.
520 gsutil_dir="${FLAGS_chroot}/home/${USER}/.gsutil"
521 if [ -d "${gsutil_dir}" ] && [ ! -w "${gsutil_dir}" ]; then
522 sudo chown -R "${USER}:$(id -gn)" "${gsutil_dir}"
523 fi
David James546747b2010-03-23 15:19:43 -0700524 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000525}
526
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400527teardown_env() {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800528 # Validate sudo timestamp before entering the critical section so that we
529 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800530 # Don't use sudo -v since that has issues on machines w/ no password.
531 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800532
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800533 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800534 (
535 flock 200
536
537 # check each pid in $LOCKFILE to see if it's died unexpectedly
538 TMP_LOCKFILE="$LOCKFILE.tmp"
539
540 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
541 cat "$LOCKFILE" | while read PID; do
542 if [ "$PID" = "$$" ]; then
543 # ourself, leave PROC_NAME empty
544 PROC_NAME=""
545 else
546 PROC_NAME=$(ps --pid $PID -o comm=)
547 fi
548
549 if [ ! -z "$PROC_NAME" ]; then
550 # All good, keep going
551 echo "$PID" >> "$TMP_LOCKFILE"
552 fi
553 done
554 # Remove any dups from lock file while installing new one
Mike Frysinger61e4f282011-09-20 22:37:22 -0400555 sort -u -n "$TMP_LOCKFILE" > "$LOCKFILE"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800556
David Jamesc9ca3db2012-07-09 08:12:26 -0700557 SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
558 if [ "${SAVED_PREF}" != "false" ]; then
559 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
560 warn "could not re-set your automount preference."
561 fi
562
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800563 if [ -s "$LOCKFILE" ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800564 debug "At least one other pid is running in the chroot, so not"
565 debug "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800566 else
Zdenek Behane28239d2011-04-07 00:37:20 +0200567 debug "Stopping syncer process"
Taylor Hutt25bf9492011-07-27 13:54:35 -0700568 # If another process entering the chroot is blocked on this
569 # flock in setup_env(), it can be a race condition.
570 #
571 # When this locked region is exited, the setup_env() flock can
572 # be entered before the script can exit and the /proc entry for
573 # the PID is removed. The newly-created chroot will not end up
574 # with a syncer process. To avoid that situation, remove the
575 # syncer PID file.
576 #
577 # The syncer PID file should also be removed because the kernel
578 # will reuse PIDs. It's possible that the PID in the syncer PID
579 # has been reused by another process; make sure we don't skip
580 # starting the syncer process when this occurs by deleting the
581 # PID file.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400582 kill $(<"${SYNCERPIDFILE}") && \
Mike Frysinger470be992011-09-28 11:53:56 -0400583 { rm -f "${SYNCERPIDFILE}" 2>/dev/null || \
584 sudo rm -f "${SYNCERPIDFILE}" ; } ||
585 debug "Unable to clean up syncer process.";
Zdenek Behane28239d2011-04-07 00:37:20 +0200586
Don Garretta0e7ea12011-02-07 18:39:59 -0800587 debug "Unmounting chroot environment."
Mike Frysinger1aa61242011-09-15 17:46:44 -0400588 safe_umount_tree "${MOUNTED_PATH}/"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800589 fi
David James546747b2010-03-23 15:19:43 -0700590 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000591}
592
Greg Spencer798d75f2011-02-01 22:04:49 -0800593if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000594 setup_env
Don Garretta0e7ea12011-02-07 18:39:59 -0800595 info "Make sure you run"
596 info " $0 --unmount"
597 info "before deleting $FLAGS_chroot"
598 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000599 exit 0
600fi
601
Greg Spencer798d75f2011-02-01 22:04:49 -0800602if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000603 teardown_env
604 exit 0
605fi
606
607# Make sure we unmount before exiting
608trap teardown_env EXIT
609setup_env
610
Brian Harring35767822012-02-01 23:50:45 -0800611CHROOT_PASSTHRU=(
612 "BUILDBOT_BUILD=$FLAGS_build_number"
Brian Harring35767822012-02-01 23:50:45 -0800613 "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
Brian Harring35767822012-02-01 23:50:45 -0800614 "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
Brian Harring35767822012-02-01 23:50:45 -0800615)
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700616
Brian Harring06d3c2e2012-08-23 07:35:43 -0700617# Add the standard proxied variables, and a few we specifically
618# export for script usage; USE/GCC_GITHASH are for ebuilds/portage,
619# CHROMEOS_VERSION_* is for cros_set_lsb_release and local AU server
620# (builders export this for marking reasons).
621KEEP_VARS=(
622 CHROMEOS_VERSION_{TRACK,AUSERVER,DEVSERVER}
623 USE GCC_GITHASH
624)
625for var in "${ENVIRONMENT_WHITELIST[@]}" "${KEEP_VARS[@]}"; do
626 [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" )
Mario Limonciello7052ae12011-05-05 12:00:49 -0500627done
628
derat@google.com4e7a92b2009-11-21 23:44:14 +0000629# Run command or interactive shell. Also include the non-chrooted path to
630# the source trunk for scripts that may need to print it (e.g.
631# build_image.sh).
Brian Harring35767822012-02-01 23:50:45 -0800632
633if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
634 cmd=( /bin/bash -l -c 'env "$@"' -- )
635elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
636 # Complain that sudo is missing.
637 error "Failing since the chroot lacks sudo."
638 error "Requested enter_chroot command was: $@"
639 exit 127
640else
641 cmd=( sudo -i -u "$USER" )
642fi
643
644sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000645
646# Remove trap and explicitly unmount
647trap - EXIT
648teardown_env