blob: c81f89fc154599e632ef90d45925f21d834e69b8 [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 Harring7b6f3772012-09-23 14:01:13 -070030DEFINE_string cache_dir "" "Directory to use for caching."
rspangler@google.comd74220d2009-10-09 20:56:14 +000031
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070032DEFINE_boolean official_build $FLAGS_FALSE \
33 "Set CHROMEOS_OFFICIAL=1 for release builds."
rspangler@google.comd74220d2009-10-09 20:56:14 +000034DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
35DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
Elly Jones7990a062010-09-02 09:23:23 -040036DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
Brian Harring35767822012-02-01 23:50:45 -080037DEFINE_boolean early_make_chroot $FLAGS_FALSE \
38 "Internal flag. If set, the command is run as root without sudo."
Don Garrettad3f0592011-02-04 14:59:56 -080039DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000040
41# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080042FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000043
44One or more VAR=value pairs can be specified to export variables into
45the chroot environment. For example:
46
47 $0 FOO=bar BAZ=bel
48
Doug Anderson9362fa82010-12-16 14:44:12 -080049If [-- command] is present, runs the command inside the chroot,
50after changing directory to /$USER/trunk/src/scripts. Note that neither
51the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000052
Doug Anderson9362fa82010-12-16 14:44:12 -080053 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000054
55Otherwise, provides an interactive shell.
56"
57
Peter Mayo4411efe2012-09-21 04:41:27 -040058CROS_LOG_PREFIX=cros_sdk:enter_chroot
59
Don Garrettad3f0592011-02-04 14:59:56 -080060# Version of info from common.sh that only echos if --verbose is set.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040061debug() {
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
Brian Harring7b6f3772012-09-23 14:01:13 -070075[ -z "${FLAGS_cache_dir}" ] && \
76 die "--cache_dir is required"
Brian Harring7ee892d2012-02-02 09:33:10 -080077
rspangler@google.comd74220d2009-10-09 20:56:14 +000078# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080079# so will die prematurely if 'switch_to_strict_mode' is specified before now.
rspangler@google.comd74220d2009-10-09 20:56:14 +000080# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080081switch_to_strict_mode
rspangler@google.comd74220d2009-10-09 20:56:14 +000082
Matt Tennant298f61a2012-06-25 21:54:33 -070083# These config files are to be copied into chroot if they exist in home dir.
84FILES_TO_COPY_TO_CHROOT=(
85 .gdata_cred.txt # User/password for Google Docs on chromium.org
86 .gdata_token # Auth token for Google Docs on chromium.org
87 .disable_build_stats_upload # Presence of file disables command stats upload
88)
89
Sean Parent2898f752010-05-25 15:06:33 -070090INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080091CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080092INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070093FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -070094AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
95SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080096
Mike Frysinger82649172011-09-21 00:18:14 -040097# Avoid the sudo call if possible since it is a little slow.
98if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then
99 sudo chmod 0777 "$FLAGS_chroot/var/lock"
100fi
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800101
102LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Zdenek Behane28239d2011-04-07 00:37:20 +0200103SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800104
David Rochberg351a76f2011-02-16 11:14:00 -0500105
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400106MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400107mount_queue_init() {
Mike Frysinger286b5922011-09-28 11:59:53 -0400108 MOUNT_QUEUE=()
109}
110
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400111queue_mount() {
David Rochberg351a76f2011-02-16 11:14:00 -0500112 # If necessary, mount $source in the host FS at $target inside the
113 # chroot directory with $mount_args.
114 local source="$1"
115 local mount_args="$2"
116 local target="$3"
117
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400118 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500119
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400120 case " ${MOUNT_CACHE} " in
121 *" ${mounted_path} "*)
Mike Frysinger2a970592011-09-20 22:49:01 -0400122 # Already mounted!
123 ;;
124 *)
Michael Krebs04c4f732012-09-07 18:31:46 -0700125 MOUNT_QUEUE+=( "mkdir -p '${mounted_path}'" )
126 # The args are left unquoted on purpose.
127 if [[ -n ${source} ]]; then
128 MOUNT_QUEUE+=( "mount ${mount_args} '${source}' '${mounted_path}'" )
129 else
130 MOUNT_QUEUE+=( "mount ${mount_args} '${mounted_path}'" )
131 fi
Mike Frysinger2a970592011-09-20 22:49:01 -0400132 ;;
133 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500134}
135
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400136process_mounts() {
Mike Frysinger286b5922011-09-28 11:59:53 -0400137 if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then
138 return 0
139 fi
140 sudo_multi "${MOUNT_QUEUE[@]}"
141 mount_queue_init
142}
143
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400144env_sync_proc() {
Zdenek Behane28239d2011-04-07 00:37:20 +0200145 # This function runs and performs periodic updates to the chroot env, if
146 # necessary.
147
148 local poll_interval=10
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400149 local sync_files=( etc/resolv.conf etc/hosts )
Zdenek Behane28239d2011-04-07 00:37:20 +0200150
Mike Frysinger4d8c2852012-05-16 15:22:49 -0400151 # Make sure the files exist before the find -- they might not in a
152 # fresh chroot which results in warnings in the build output.
153 local chown_cmd=(
154 # Make sure the files exists first -- they might not in a fresh chroot.
155 "touch ${sync_files[*]/#/${FLAGS_chroot}/}"
156 # Make sure the files are writable by normal user so that we don't have
157 # to execute sudo in the main loop below.
158 "chown ${USER} ${sync_files[*]/#/${FLAGS_chroot}/}"
159 )
160 sudo_multi "${chown_cmd[@]}"
Zdenek Behane28239d2011-04-07 00:37:20 +0200161
David James412b9022011-07-15 19:54:16 -0700162 # Drop stdin, stderr, stdout, and chroot lock.
163 # This is needed for properly daemonizing the process.
164 exec 0>&- 1>&- 2>&- 200>&-
165
Zdenek Behane28239d2011-04-07 00:37:20 +0200166 while true; do
167 # Sync files
Ryan Cuife573cd2012-09-27 15:58:42 -0700168 for file in "${sync_files[@]}"; do
Zdenek Behane28239d2011-04-07 00:37:20 +0200169 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
170 cp -f /${file} ${FLAGS_chroot}/${file}
171 fi
172 done
173
174 sleep ${poll_interval}
175 done
176}
177
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400178copy_ssh_config() {
Vadim Bendebury0779f522011-06-12 12:09:16 -0700179 # Copy user .ssh/config into the chroot filtering out strings not supported
180 # by the chroot ssh. The chroot .ssh directory is passed in as the first
181 # parameter.
182
183 # ssh options to filter out. The entire strings containing these substrings
184 # will be deleted before copying.
185 local bad_options=(
Matt Tennant63c3cc52011-11-22 11:23:06 -0800186 'UseProxyIf'
187 'GSSAPIAuthentication'
188 'GSSAPIKeyExchange'
189 'ProxyUseFdpass'
Vadim Bendebury0779f522011-06-12 12:09:16 -0700190 )
191 local sshc="${HOME}/.ssh/config"
192 local chroot_ssh_dir="${1}"
193 local filter
194 local option
195
196 if [ ! -f "${sshc}" ]; then
197 return # Nothing to copy.
198 fi
199
200 for option in "${bad_options[@]}"
201 do
202 if [ -z "${filter}" ]; then
203 filter="${option}"
204 else
205 filter+="\\|${option}"
206 fi
207 done
208
209 sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config"
210}
211
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400212copy_into_chroot_if_exists() {
Matt Tennantf7c9e772012-02-08 17:06:26 -0800213 # $1 is file path outside of chroot to copy to path $2 inside chroot.
214 [ -e "$1" ] || return
215 cp "$1" "${FLAGS_chroot}/$2"
216}
217
Peter Mayo4411efe2012-09-21 04:41:27 -0400218# Usage: promote_api_keys
219# This takes care of getting the developer API keys into the chroot where
220# chrome can build with them. It needs to take it from the places a dev
221# is likely to put them, and recognize that older chroots may or may not
222# have been used since the concept of keys got added, as well as before
223# and after the developer decding to grab his own keys.
224promote_api_keys() {
225 local destination="${FLAGS_chroot}/home/${USER}/.googleapikeys"
226 # Don't disturb existing keys. They could be set differently
227 if [[ -s "${destination}" ]]; then
228 return 0
229 fi
230 if [[ -r "${HOME}/.googleapikeys" ]]; then
231 cp "${HOME}/.googleapikeys" "${destination}"
232 if [[ -s "${destination}" ]] ; then
233 info "Copied Google API keys into chroot."
234 fi
235 elif [[ -r "${HOME}/.gyp/include.gypi" ]]; then
236 local NAME="('google_(api_key|default_client_(id|secret))')"
237 local WS="[[:space:]]*"
238 local CONTENTS="('[^\\\\']*')"
239 sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \
240 "${HOME}/.gyp/include.gypi" >"${destination}"
241 if [[ -s "${destination}" ]]; then
242 info "Put discovered Google API keys into chroot."
243 fi
244 fi
245}
246
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400247setup_env() {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800248 # Validate sudo timestamp before entering the critical section so that we
249 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800250 # Don't use sudo -v since that has issues on machines w/ no password.
251 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800252
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800253 (
254 flock 200
255 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000256
Taylor Hutt25bf9492011-07-27 13:54:35 -0700257 # If there isn't a syncer daemon started already, start one. The
258 # daemon is considered to not be started under the following
259 # conditions:
260 #
261 # o There is no PID file
262 #
263 # o The PID file is 0 bytes in size, which might be a partial
264 # manifestation of chromium-os:17680. This situation will not
265 # occur anymore, but you might have a chroot which was already
266 # affected.
267 #
268 # o The /proc node for the process named by the PID file does
269 # not exist.
270 #
271 # Note: This does not address PID recycling. While
272 # increasingly unlikely, it is possible for the PID in
273 # the PID file to refer to a running process that is not
274 # the syncer process. Since the PID file is now
275 # removed, I think it is only possible for this to occur
276 # if your system crashes and the PID file exists after
277 # restart.
278 #
279 # The daemon is killed by the enter_chroot that exits last.
Taylor Hutt18594a82011-07-28 11:45:50 -0700280 if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then
Taylor Hutt25bf9492011-07-27 13:54:35 -0700281 info "You may have suffered from chromium-os:17680 and";
282 info "could have stray 'enter_chroot.sh' processes running.";
283 info "You must manually kill any such stray processes.";
284 info "Exit all chroot shells; remaining 'enter_chroot.sh'";
285 info "processes are probably stray.";
Taylor Hutt18594a82011-07-28 11:45:50 -0700286 sudo rm -f "${SYNCERPIDFILE}";
Taylor Hutt25bf9492011-07-27 13:54:35 -0700287 fi;
David James412b9022011-07-15 19:54:16 -0700288 if ! [ -f "${SYNCERPIDFILE}" ] || \
Mike Frysinger61e4f282011-09-20 22:37:22 -0400289 ! [ -d /proc/$(<"${SYNCERPIDFILE}") ]; then
David James412b9022011-07-15 19:54:16 -0700290 debug "Starting sync process"
291 env_sync_proc &
292 echo $! > "${SYNCERPIDFILE}"
293 disown $!
294 fi
295
Don Garretta0e7ea12011-02-07 18:39:59 -0800296 debug "Mounting chroot environment."
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400297 MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts))
Mike Frysinger286b5922011-09-28 11:59:53 -0400298 mount_queue_init
299 queue_mount none "-t proc" /proc
300 queue_mount none "-t sysfs" /sys
301 queue_mount /dev "--bind" /dev
302 queue_mount none "-t devpts" /dev/pts
Aaron Plattner130d3632011-10-18 08:54:57 -0700303 if [ -d /run ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400304 queue_mount /run "--bind" /run
Aaron Plattner130d3632011-10-18 08:54:57 -0700305 if [ -d /run/shm ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400306 queue_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700307 fi
308 fi
Brian Harringf264b822012-09-01 01:39:03 -0700309 # Get path overrides for the chroot in place now- it's possible
310 # that they may be needed for early teardown.
311 queue_mount "${FLAGS_trunk}/src/scripts/path-overrides" "--bind" \
312 "/usr/local/path-overrides"
Brian Harringdd7d7932011-12-23 04:21:33 -0800313
Brian Harringf264b822012-09-01 01:39:03 -0700314 queue_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Chris Sosa389634d2012-09-05 19:56:53 -0700315
Brian Harringdd7d7932011-12-23 04:21:33 -0800316 debug "Setting up referenced repositories if required."
317 REFERENCE_DIR=$(git config --file \
318 "${FLAGS_trunk}/.repo/manifests.git/config" \
319 repo.reference)
320 if [ -n "${REFERENCE_DIR}" ]; then
321
Brian Harring334050f2012-06-08 17:40:58 -0700322 ALTERNATES="${FLAGS_trunk}/.repo/alternates"
323
324 # Ensure this directory exists ourselves, and has the correct ownership.
325 [ -d "${ALTERNATES}" ] || mkdir "${ALTERNATES}"
326 [ -w "${ALTERNATES}" ] || sudo chown -R "${USER}" "${ALTERNATES}"
327
328 unset ALTERNATES
329
Brian Harringdd7d7932011-12-23 04:21:33 -0800330 IFS=$'\n';
331 required=( $( "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \
332 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
333 unset IFS
334
335 queue_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
336 "${CHROOT_TRUNK_DIR}/.repo/alternates"
337
338 # Note that as we're bringing up each referened repo, we also
339 # mount bind an empty directory over its alternates. This is
340 # required to suppress git from tracing through it- we already
341 # specify the required alternates for CHROOT_TRUNK_DIR, no point
342 # in having git try recursing through each on their own.
343 #
344 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
345 # the bind is visible only w/in the chroot.
346 mkdir -p ${FLAGS_trunk}/.repo/chroot/empty
347 position=1
348 for x in "${required[@]}"; do
349 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
350 queue_mount "${x}" "--bind" "${base}"
351 if [ -e "${x}/.repo/alternates" ]; then
352 queue_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
353 "${base}/.repo/alternates"
354 fi
355 position=$(( ${position} + 1 ))
356 done
357 unset required position base
358 fi
359 unset REFERENCE_DIR
360
Brian Harring7b6f3772012-09-23 14:01:13 -0700361 chroot_cache='/var/cache/chromeos-cache'
362 debug "Setting up shared cache dir directory."
363 mkdir -p "${FLAGS_cache_dir}"/distfiles/{target,host}
364 sudo mkdir -p "${FLAGS_chroot}/${chroot_cache}"
365 queue_mount "${FLAGS_cache_dir}" "--bind" "${chroot_cache}"
366 # TODO(build): remove this as of 12/01/12.
367 # Because of how distfiles -> cache_dir was deployed, if this isn't
368 # a symlink, we *know* the ondisk pathways aren't compatible- thus
369 # fix it now.
370 distfiles_path="${FLAGS_chroot}/var/cache/distfiles"
371 if [ ! -L "${distfiles_path}" ]; then
372 # While we're at it, ensure the var is exported w/in the chroot; it
373 # won't exist if distfiles isn't a symlink.
374 p="${FLAGS_chroot}/etc/profile.d/chromeos-cache.sh"
375 sudo_multi "rm -rf '${distfiles_path}'" \
376 "ln -s chromeos-cache/distfiles '${distfiles_path}'" \
377 "echo 'export CHROMEOS_CACHEDIR=${chroot_cache}' > '${p}'" \
378 "chmod 0644 '${p}'"
379 fi
Brian Harring7ee892d2012-02-02 09:33:10 -0800380
Elly Jones7990a062010-09-02 09:23:23 -0400381 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800382 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
Mike Frysinger61e4f282011-09-20 22:37:22 -0400383 TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh"
Elly Jones7990a062010-09-02 09:23:23 -0400384 mkdir -p "${TARGET_DIR}"
Mike Frysinger40bb0c32011-10-31 17:14:15 -0400385 # Ignore errors as some people won't have these files to copy.
386 cp "${HOME}"/.ssh/{known_hosts,*.pub} "${TARGET_DIR}/" 2>/dev/null || :
Vadim Bendebury0779f522011-06-12 12:09:16 -0700387 copy_ssh_config "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500388
389 # Don't try to bind mount the ssh agent dir if it has gone stale.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400390 ASOCK=${SSH_AUTH_SOCK%/*}
Mike Frysinger9de707f2011-12-12 14:21:44 -0500391 if [ -d "${ASOCK}" ]; then
392 queue_mount "${ASOCK}" "--bind" "${ASOCK}"
393 fi
Elly Jones7990a062010-09-02 09:23:23 -0400394 fi
395 fi
396
Mike Frysinger286b5922011-09-28 11:59:53 -0400397 if [ -d "$HOME/.subversion" ]; then
398 TARGET="/home/${USER}/.subversion"
399 mkdir -p "${FLAGS_chroot}${TARGET}"
400 queue_mount "${HOME}/.subversion" "--bind" "${TARGET}"
401 fi
402
403 if DEPOT_TOOLS=$(type -P gclient) ; then
404 DEPOT_TOOLS=${DEPOT_TOOLS%/*} # dirname
405 debug "Mounting depot_tools"
406 queue_mount "$DEPOT_TOOLS" --bind "$INNER_DEPOT_TOOLS_ROOT"
407 fi
408
Michael Krebs04c4f732012-09-07 18:31:46 -0700409 # Mount additional directories as specified in .local_mounts file.
410 local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts"
411 if [[ -f ${local_mounts} ]]; then
412 info "Mounting local folders (read-only for safety concern)"
413 # format: mount_source
414 # or mount_source mount_point
415 # or # comments
416 local mount_source mount_point
417 while read mount_source mount_point; do
418 if [[ -z ${mount_source} ]]; then
419 continue
420 fi
421 # if only source is assigned, use source as mount point.
422 : ${mount_point:=${mount_source}}
423 debug " mounting ${mount_source} on ${mount_point}"
424 queue_mount "${mount_source}" "--bind" "${mount_point}"
425 # --bind can't initially be read-only so we have to do it via remount.
426 queue_mount "" "-o remount,ro" "${mount_point}"
427 done < <(sed -e 's:#.*::' "${local_mounts}")
428 fi
429
Mike Frysinger286b5922011-09-28 11:59:53 -0400430 process_mounts
431
Mike Frysinger9a50b642011-09-20 23:37:14 -0400432 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
433 if [ -z "$CHROME_ROOT" ]; then
434 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
435 2>/dev/null || :)"
436 CHROME_ROOT_AUTO=1
437 fi
438 if [[ -n "$CHROME_ROOT" ]]; then
439 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
440 error "Not mounting chrome source"
441 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
442 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
443 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800444 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400445 else
446 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
447 sudo bash -c "echo '$CHROME_ROOT' > \
448 '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'"
Mike Frysinger286b5922011-09-28 11:59:53 -0400449 queue_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800450 fi
451 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800452
Mike Frysinger286b5922011-09-28 11:59:53 -0400453 process_mounts
Chris Sosa317d8eb2010-04-05 15:45:28 -0700454
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400455 # Install fuse module. Skip modprobe when possible for slight
456 # speed increase when initializing the env.
457 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700458 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200459 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700460 fi
461
David Jamesc9ca3db2012-07-09 08:12:26 -0700462 # Turn off automounting of external media when we enter the
463 # chroot; thus we don't have to worry about being able to unmount
464 # from inside.
465 if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then
466 if [ "${SAVED_PREF}" != "false" ]; then
467 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
468 warn "-- Note: USB sticks may be automounted by your host OS."
469 warn "-- Note: If you plan to burn bootable media, you may need to"
470 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
471 warn "-- Note: outside the chroot."
472 fi
473 fi
474 fi
475 # Always write the temp file so we can read it when exiting
476 echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
477
Mike Frysinger926a4b92012-06-27 15:22:28 -0400478 # Fix permissions on ccache tree. If this is a fresh chroot, then they
479 # might not be set up yet. Or if the user manually `rm -rf`-ed things,
480 # we need to reset it. Otherwise, gcc itself takes care of fixing things
481 # on demand, but only when it updates.
482 ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache"
483 if [[ ! -d ${ccache_dir} ]]; then
484 sudo mkdir -p -m 2775 "${ccache_dir}"
485 fi
486 sudo find -H "${ccache_dir}" -type d -exec chmod 2775 {} + &
487 sudo find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} + &
488
Mike Frysinger94717e32011-09-21 00:10:44 -0400489 # Configure committer username and email in chroot .gitconfig. Change
490 # to the root directory first so that random $PWD/.git/config settings
491 # do not get picked up. We want to stick to ~/.gitconfig only.
492 ident=$(cd /; git var GIT_COMMITTER_IDENT || :)
493 ident_name=${ident%% <*}
494 ident_email=${ident%%>*}; ident_email=${ident_email##*<}
David James342f1562011-07-15 15:21:18 -0700495 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400496 user.name "${ident_name}" || true
David James342f1562011-07-15 15:21:18 -0700497 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400498 user.email "${ident_email}" || true
David James342f1562011-07-15 15:21:18 -0700499
Matt Tennant298f61a2012-06-25 21:54:33 -0700500 # Certain files get copied into the chroot when entering.
501 for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do
Matt Tennantf7c9e772012-02-08 17:06:26 -0800502 copy_into_chroot_if_exists "${HOME}/${fn}" "/home/${USER}/${fn}"
503 done
Peter Mayo4411efe2012-09-21 04:41:27 -0400504 promote_api_keys
Matt Tennantd4316fe2011-08-30 12:06:42 -0700505
Mike Frysinger6601c522011-08-15 11:05:39 -0400506 # Make sure user's requested locales are available
507 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400508 # And make sure en_US{,.UTF-8} are always available as
509 # that what buildbot forces internally
510 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400511 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
512 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
513 sort -u | sed '/^C$/d')
514 gen_locales=()
515 for l in ${locales}; do
516 if [[ ${l} == *.* ]]; then
517 enc=${l#*.}
518 else
519 enc="ISO-8859-1"
520 fi
521 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
522 utf8) enc="UTF-8";;
523 esac
524 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
525 done
Mike Frysinger4d258cd2011-09-15 16:17:49 -0400526 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
527 # Force LC_ALL=C to workaround slow string parsing in bash
528 # with long multibyte strings. Newer setups have this fixed,
529 # but locale-gen doesn't need to be run in any locale in the
530 # first place, so just go with C to keep it fast.
531 sudo -- chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \
532 -G "$(printf '%s\n' "${gen_locales[@]}")"
533 fi
Mike Frysinger6601c522011-08-15 11:05:39 -0400534
Dale Curtis98631732011-05-05 16:16:59 -0700535 # Fix permissions on shared memory to allow non-root users access to POSIX
Mike Frysinger82649172011-09-21 00:18:14 -0400536 # semaphores. Avoid the sudo call if possible (sudo is slow).
537 if [ -n "$(find "${FLAGS_chroot}/dev/shm" ! -perm 777)" ] ; then
538 sudo chmod -R 777 "${FLAGS_chroot}/dev/shm"
539 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400540
541 # If the private overlays are installed, gsutil can use those credentials.
Gilad Arnold264f64d2012-09-06 14:01:45 -0700542 # We're also installing credentials for use by sudoed invocations.
543 boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto'
544 if [ -s "${FLAGS_trunk}/${boto}" ]; then
545 if [ ! -e "${FLAGS_chroot}/home/${USER}/.boto" ]; then
Chris Wolfe916b1f12012-04-30 16:03:06 -0400546 ln -s "trunk/${boto}" "${FLAGS_chroot}/home/${USER}/.boto"
547 fi
Gilad Arnold264f64d2012-09-06 14:01:45 -0700548 if [ ! -e "${FLAGS_chroot}/root/.boto" ]; then
549 sudo ln -s "../home/${USER}/trunk/${boto}" "${FLAGS_chroot}/root/.boto"
550 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400551 fi
552
553 # Have found a few chroots where ~/.gsutil is owned by root:root, probably
554 # as a result of old gsutil or tools. This causes permission errors when
555 # gsutil cp tries to create its cache files, so ensure the user can
556 # actually write to their directory.
557 gsutil_dir="${FLAGS_chroot}/home/${USER}/.gsutil"
558 if [ -d "${gsutil_dir}" ] && [ ! -w "${gsutil_dir}" ]; then
559 sudo chown -R "${USER}:$(id -gn)" "${gsutil_dir}"
560 fi
David James546747b2010-03-23 15:19:43 -0700561 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000562}
563
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400564teardown_env() {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800565 # Validate sudo timestamp before entering the critical section so that we
566 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800567 # Don't use sudo -v since that has issues on machines w/ no password.
568 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800569
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800570 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800571 (
572 flock 200
573
574 # check each pid in $LOCKFILE to see if it's died unexpectedly
575 TMP_LOCKFILE="$LOCKFILE.tmp"
576
577 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
578 cat "$LOCKFILE" | while read PID; do
579 if [ "$PID" = "$$" ]; then
580 # ourself, leave PROC_NAME empty
581 PROC_NAME=""
582 else
583 PROC_NAME=$(ps --pid $PID -o comm=)
584 fi
585
586 if [ ! -z "$PROC_NAME" ]; then
587 # All good, keep going
588 echo "$PID" >> "$TMP_LOCKFILE"
589 fi
590 done
591 # Remove any dups from lock file while installing new one
Mike Frysinger61e4f282011-09-20 22:37:22 -0400592 sort -u -n "$TMP_LOCKFILE" > "$LOCKFILE"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800593
David Jamesc9ca3db2012-07-09 08:12:26 -0700594 SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
595 if [ "${SAVED_PREF}" != "false" ]; then
596 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
597 warn "could not re-set your automount preference."
598 fi
599
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800600 if [ -s "$LOCKFILE" ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800601 debug "At least one other pid is running in the chroot, so not"
602 debug "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800603 else
Zdenek Behane28239d2011-04-07 00:37:20 +0200604 debug "Stopping syncer process"
Taylor Hutt25bf9492011-07-27 13:54:35 -0700605 # If another process entering the chroot is blocked on this
606 # flock in setup_env(), it can be a race condition.
607 #
608 # When this locked region is exited, the setup_env() flock can
609 # be entered before the script can exit and the /proc entry for
610 # the PID is removed. The newly-created chroot will not end up
611 # with a syncer process. To avoid that situation, remove the
612 # syncer PID file.
613 #
614 # The syncer PID file should also be removed because the kernel
615 # will reuse PIDs. It's possible that the PID in the syncer PID
616 # has been reused by another process; make sure we don't skip
617 # starting the syncer process when this occurs by deleting the
618 # PID file.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400619 kill $(<"${SYNCERPIDFILE}") && \
Mike Frysinger470be992011-09-28 11:53:56 -0400620 { rm -f "${SYNCERPIDFILE}" 2>/dev/null || \
621 sudo rm -f "${SYNCERPIDFILE}" ; } ||
622 debug "Unable to clean up syncer process.";
Zdenek Behane28239d2011-04-07 00:37:20 +0200623
Don Garretta0e7ea12011-02-07 18:39:59 -0800624 debug "Unmounting chroot environment."
Mike Frysinger1aa61242011-09-15 17:46:44 -0400625 safe_umount_tree "${MOUNTED_PATH}/"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800626 fi
David James546747b2010-03-23 15:19:43 -0700627 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000628}
629
Greg Spencer798d75f2011-02-01 22:04:49 -0800630if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000631 setup_env
Don Garretta0e7ea12011-02-07 18:39:59 -0800632 info "Make sure you run"
633 info " $0 --unmount"
634 info "before deleting $FLAGS_chroot"
635 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000636 exit 0
637fi
638
Greg Spencer798d75f2011-02-01 22:04:49 -0800639if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000640 teardown_env
641 exit 0
642fi
643
644# Make sure we unmount before exiting
645trap teardown_env EXIT
646setup_env
647
Brian Harring35767822012-02-01 23:50:45 -0800648CHROOT_PASSTHRU=(
649 "BUILDBOT_BUILD=$FLAGS_build_number"
Brian Harring35767822012-02-01 23:50:45 -0800650 "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
Brian Harring35767822012-02-01 23:50:45 -0800651 "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
Brian Harring35767822012-02-01 23:50:45 -0800652)
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700653
Brian Harring06d3c2e2012-08-23 07:35:43 -0700654# Add the standard proxied variables, and a few we specifically
655# export for script usage; USE/GCC_GITHASH are for ebuilds/portage,
656# CHROMEOS_VERSION_* is for cros_set_lsb_release and local AU server
657# (builders export this for marking reasons).
658KEEP_VARS=(
659 CHROMEOS_VERSION_{TRACK,AUSERVER,DEVSERVER}
660 USE GCC_GITHASH
661)
662for var in "${ENVIRONMENT_WHITELIST[@]}" "${KEEP_VARS[@]}"; do
663 [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" )
Mario Limonciello7052ae12011-05-05 12:00:49 -0500664done
665
derat@google.com4e7a92b2009-11-21 23:44:14 +0000666# Run command or interactive shell. Also include the non-chrooted path to
667# the source trunk for scripts that may need to print it (e.g.
668# build_image.sh).
Brian Harring35767822012-02-01 23:50:45 -0800669
670if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
671 cmd=( /bin/bash -l -c 'env "$@"' -- )
672elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
673 # Complain that sudo is missing.
674 error "Failing since the chroot lacks sudo."
675 error "Requested enter_chroot command was: $@"
676 exit 127
677else
678 cmd=( sudo -i -u "$USER" )
679fi
680
681sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000682
683# Remove trap and explicitly unmount
684trap - EXIT
685teardown_env