blob: e8d0d5dab7fb37e0a51e814ae9e33cd7d263cf34 [file] [log] [blame]
rspangler@google.comd74220d2009-10-09 20:56:14 +00001#!/bin/bash
2
Darin Petkov47974d42011-03-03 15:55:04 -08003# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
rspangler@google.comd74220d2009-10-09 20:56:14 +00004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Script to enter the chroot environment
8
J. Richard Barnette8e6750d2011-08-22 12:35:52 -07009SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..)
10. "${SCRIPT_ROOT}/common.sh" || exit 1
rspangler@google.comd74220d2009-10-09 20:56:14 +000011
derat@google.com86dcc8e2009-11-21 19:49:49 +000012# Script must be run outside the chroot and as a regular user.
rspangler@google.comd74220d2009-10-09 20:56:14 +000013assert_outside_chroot
derat@google.com86dcc8e2009-11-21 19:49:49 +000014assert_not_root_user
rspangler@google.comd74220d2009-10-09 20:56:14 +000015
16# Define command line flags
17# See http://code.google.com/p/shflags/wiki/Documentation10x
18DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
19 "The destination dir for the chroot environment." "d"
20DEFINE_string trunk "$GCLIENT_ROOT" \
21 "The source trunk to bind mount within the chroot." "s"
David McMahon03aeb202009-12-08 12:47:08 -080022DEFINE_string build_number "" \
23 "The build-bot build number (when called by buildbot only)." "b"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080024DEFINE_string chrome_root "" \
25 "The root of your chrome browser source. Should contain a 'src' subdir."
Sean Parent2898f752010-05-25 15:06:33 -070026DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \
27 "The mount point of the chrome broswer source in the chroot."
rspangler@google.comd74220d2009-10-09 20:56:14 +000028
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070029DEFINE_boolean official_build $FLAGS_FALSE \
30 "Set CHROMEOS_OFFICIAL=1 for release builds."
rspangler@google.comd74220d2009-10-09 20:56:14 +000031DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
32DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
Elly Jones7990a062010-09-02 09:23:23 -040033DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
Don Garrettad3f0592011-02-04 14:59:56 -080034DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000035
36# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080037FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000038
39One or more VAR=value pairs can be specified to export variables into
40the chroot environment. For example:
41
42 $0 FOO=bar BAZ=bel
43
Doug Anderson9362fa82010-12-16 14:44:12 -080044If [-- command] is present, runs the command inside the chroot,
45after changing directory to /$USER/trunk/src/scripts. Note that neither
46the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000047
Doug Anderson9362fa82010-12-16 14:44:12 -080048 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000049
50Otherwise, provides an interactive shell.
51"
52
Don Garrettad3f0592011-02-04 14:59:56 -080053# Version of info from common.sh that only echos if --verbose is set.
Don Garretta0e7ea12011-02-07 18:39:59 -080054function debug {
Don Garrettad3f0592011-02-04 14:59:56 -080055 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
David Rochberg351a76f2011-02-16 11:14:00 -050056 info "$*"
Don Garrettad3f0592011-02-04 14:59:56 -080057 fi
58}
59
rspangler@google.comd74220d2009-10-09 20:56:14 +000060# Parse command line flags
61FLAGS "$@" || exit 1
62eval set -- "${FLAGS_ARGV}"
63
Greg Spencer798d75f2011-02-01 22:04:49 -080064if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
David McMahon857dbb52009-12-09 18:21:05 -080065 CHROMEOS_OFFICIAL=1
66fi
67
rspangler@google.comd74220d2009-10-09 20:56:14 +000068# Only now can we die on error. shflags functions leak non-zero error codes,
69# so will die prematurely if 'set -e' is specified before now.
70# TODO: replace shflags with something less error-prone, or contribute a fix.
71set -e
72
Sean Parent2898f752010-05-25 15:06:33 -070073INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080074CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080075INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070076FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -070077AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
78SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080079
Mike Frysinger82649172011-09-21 00:18:14 -040080# Avoid the sudo call if possible since it is a little slow.
81if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then
82 sudo chmod 0777 "$FLAGS_chroot/var/lock"
83fi
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080084
85LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Zdenek Behane28239d2011-04-07 00:37:20 +020086SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080087
David Rochberg351a76f2011-02-16 11:14:00 -050088
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -040089MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger286b5922011-09-28 11:59:53 -040090function mount_queue_init {
91 MOUNT_QUEUE=()
92}
93
94function queue_mount {
David Rochberg351a76f2011-02-16 11:14:00 -050095 # If necessary, mount $source in the host FS at $target inside the
96 # chroot directory with $mount_args.
97 local source="$1"
98 local mount_args="$2"
99 local target="$3"
100
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400101 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500102
Mike Frysinger2a970592011-09-20 22:49:01 -0400103 case ${MOUNT_CACHE} in
104 *" on ${mounted_path} "*)
105 # Already mounted!
106 ;;
107 *)
Mike Frysinger286b5922011-09-28 11:59:53 -0400108 MOUNT_QUEUE+=(
109 "mkdir -p '${mounted_path}'"
110 # The args are left unquoted on purpose.
111 "mount ${mount_args} '${source}' '${mounted_path}'"
112 )
Mike Frysinger2a970592011-09-20 22:49:01 -0400113 ;;
114 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500115}
116
Mike Frysinger286b5922011-09-28 11:59:53 -0400117function process_mounts {
118 if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then
119 return 0
120 fi
121 sudo_multi "${MOUNT_QUEUE[@]}"
122 mount_queue_init
123}
124
Zdenek Behane28239d2011-04-07 00:37:20 +0200125function env_sync_proc {
126 # This function runs and performs periodic updates to the chroot env, if
127 # necessary.
128
129 local poll_interval=10
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400130 local sync_files=( etc/resolv.conf etc/hosts )
Zdenek Behane28239d2011-04-07 00:37:20 +0200131
132 # Make sure the synced files are writable by normal user, so that we
133 # don't have to sudo inside the loop.
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400134 local chmods=$(find ${sync_files[@]/#/${FLAGS_chroot}/} '!' -user ${USER})
135 if [ -n "${chmods}" ]; then
136 sudo -- chown ${USER} ${chmods} 1>&2
137 fi
Zdenek Behane28239d2011-04-07 00:37:20 +0200138
David James412b9022011-07-15 19:54:16 -0700139 # Drop stdin, stderr, stdout, and chroot lock.
140 # This is needed for properly daemonizing the process.
141 exec 0>&- 1>&- 2>&- 200>&-
142
Zdenek Behane28239d2011-04-07 00:37:20 +0200143 while true; do
144 # Sync files
145 for file in ${sync_files}; do
146 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
147 cp -f /${file} ${FLAGS_chroot}/${file}
148 fi
149 done
150
151 sleep ${poll_interval}
152 done
153}
154
Vadim Bendebury0779f522011-06-12 12:09:16 -0700155function copy_ssh_config {
156 # Copy user .ssh/config into the chroot filtering out strings not supported
157 # by the chroot ssh. The chroot .ssh directory is passed in as the first
158 # parameter.
159
160 # ssh options to filter out. The entire strings containing these substrings
161 # will be deleted before copying.
162 local bad_options=(
Matt Tennant63c3cc52011-11-22 11:23:06 -0800163 'UseProxyIf'
164 'GSSAPIAuthentication'
165 'GSSAPIKeyExchange'
166 'ProxyUseFdpass'
Vadim Bendebury0779f522011-06-12 12:09:16 -0700167 )
168 local sshc="${HOME}/.ssh/config"
169 local chroot_ssh_dir="${1}"
170 local filter
171 local option
172
173 if [ ! -f "${sshc}" ]; then
174 return # Nothing to copy.
175 fi
176
177 for option in "${bad_options[@]}"
178 do
179 if [ -z "${filter}" ]; then
180 filter="${option}"
181 else
182 filter+="\\|${option}"
183 fi
184 done
185
186 sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config"
187}
188
rspangler@google.comd74220d2009-10-09 20:56:14 +0000189function setup_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800190 # Validate sudo timestamp before entering the critical section so that we
191 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800192 # Don't use sudo -v since that has issues on machines w/ no password.
193 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800194
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800195 (
196 flock 200
197 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000198
Taylor Hutt25bf9492011-07-27 13:54:35 -0700199 # If there isn't a syncer daemon started already, start one. The
200 # daemon is considered to not be started under the following
201 # conditions:
202 #
203 # o There is no PID file
204 #
205 # o The PID file is 0 bytes in size, which might be a partial
206 # manifestation of chromium-os:17680. This situation will not
207 # occur anymore, but you might have a chroot which was already
208 # affected.
209 #
210 # o The /proc node for the process named by the PID file does
211 # not exist.
212 #
213 # Note: This does not address PID recycling. While
214 # increasingly unlikely, it is possible for the PID in
215 # the PID file to refer to a running process that is not
216 # the syncer process. Since the PID file is now
217 # removed, I think it is only possible for this to occur
218 # if your system crashes and the PID file exists after
219 # restart.
220 #
221 # The daemon is killed by the enter_chroot that exits last.
Taylor Hutt18594a82011-07-28 11:45:50 -0700222 if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then
Taylor Hutt25bf9492011-07-27 13:54:35 -0700223 info "You may have suffered from chromium-os:17680 and";
224 info "could have stray 'enter_chroot.sh' processes running.";
225 info "You must manually kill any such stray processes.";
226 info "Exit all chroot shells; remaining 'enter_chroot.sh'";
227 info "processes are probably stray.";
Taylor Hutt18594a82011-07-28 11:45:50 -0700228 sudo rm -f "${SYNCERPIDFILE}";
Taylor Hutt25bf9492011-07-27 13:54:35 -0700229 fi;
David James412b9022011-07-15 19:54:16 -0700230 if ! [ -f "${SYNCERPIDFILE}" ] || \
Mike Frysinger61e4f282011-09-20 22:37:22 -0400231 ! [ -d /proc/$(<"${SYNCERPIDFILE}") ]; then
David James412b9022011-07-15 19:54:16 -0700232 debug "Starting sync process"
233 env_sync_proc &
234 echo $! > "${SYNCERPIDFILE}"
235 disown $!
236 fi
237
Don Garretta0e7ea12011-02-07 18:39:59 -0800238 debug "Mounting chroot environment."
Mike Frysinger2a970592011-09-20 22:49:01 -0400239 MOUNT_CACHE=$(mount)
Mike Frysinger286b5922011-09-28 11:59:53 -0400240 mount_queue_init
241 queue_mount none "-t proc" /proc
242 queue_mount none "-t sysfs" /sys
243 queue_mount /dev "--bind" /dev
244 queue_mount none "-t devpts" /dev/pts
Aaron Plattner130d3632011-10-18 08:54:57 -0700245 if [ -d /run ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400246 queue_mount /run "--bind" /run
Aaron Plattner130d3632011-10-18 08:54:57 -0700247 if [ -d /run/shm ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400248 queue_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700249 fi
250 fi
Mike Frysinger286b5922011-09-28 11:59:53 -0400251 queue_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800252
Brian Harringdd7d7932011-12-23 04:21:33 -0800253
254 debug "Setting up referenced repositories if required."
255 REFERENCE_DIR=$(git config --file \
256 "${FLAGS_trunk}/.repo/manifests.git/config" \
257 repo.reference)
258 if [ -n "${REFERENCE_DIR}" ]; then
259
260 IFS=$'\n';
261 required=( $( "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \
262 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
263 unset IFS
264
265 queue_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
266 "${CHROOT_TRUNK_DIR}/.repo/alternates"
267
268 # Note that as we're bringing up each referened repo, we also
269 # mount bind an empty directory over its alternates. This is
270 # required to suppress git from tracing through it- we already
271 # specify the required alternates for CHROOT_TRUNK_DIR, no point
272 # in having git try recursing through each on their own.
273 #
274 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
275 # the bind is visible only w/in the chroot.
276 mkdir -p ${FLAGS_trunk}/.repo/chroot/empty
277 position=1
278 for x in "${required[@]}"; do
279 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
280 queue_mount "${x}" "--bind" "${base}"
281 if [ -e "${x}/.repo/alternates" ]; then
282 queue_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
283 "${base}/.repo/alternates"
284 fi
285 position=$(( ${position} + 1 ))
286 done
287 unset required position base
288 fi
289 unset REFERENCE_DIR
290
Elly Jones7990a062010-09-02 09:23:23 -0400291 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800292 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
Mike Frysinger61e4f282011-09-20 22:37:22 -0400293 TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh"
Elly Jones7990a062010-09-02 09:23:23 -0400294 mkdir -p "${TARGET_DIR}"
Mike Frysinger40bb0c32011-10-31 17:14:15 -0400295 # Ignore errors as some people won't have these files to copy.
296 cp "${HOME}"/.ssh/{known_hosts,*.pub} "${TARGET_DIR}/" 2>/dev/null || :
Vadim Bendebury0779f522011-06-12 12:09:16 -0700297 copy_ssh_config "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500298
299 # Don't try to bind mount the ssh agent dir if it has gone stale.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400300 ASOCK=${SSH_AUTH_SOCK%/*}
Mike Frysinger9de707f2011-12-12 14:21:44 -0500301 if [ -d "${ASOCK}" ]; then
302 queue_mount "${ASOCK}" "--bind" "${ASOCK}"
303 fi
Elly Jones7990a062010-09-02 09:23:23 -0400304 fi
305 fi
306
Mike Frysinger286b5922011-09-28 11:59:53 -0400307 if [ -d "$HOME/.subversion" ]; then
308 TARGET="/home/${USER}/.subversion"
309 mkdir -p "${FLAGS_chroot}${TARGET}"
310 queue_mount "${HOME}/.subversion" "--bind" "${TARGET}"
311 fi
312
313 if DEPOT_TOOLS=$(type -P gclient) ; then
314 DEPOT_TOOLS=${DEPOT_TOOLS%/*} # dirname
315 debug "Mounting depot_tools"
316 queue_mount "$DEPOT_TOOLS" --bind "$INNER_DEPOT_TOOLS_ROOT"
317 fi
318
319 process_mounts
320
Mike Frysinger9a50b642011-09-20 23:37:14 -0400321 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
322 if [ -z "$CHROME_ROOT" ]; then
323 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
324 2>/dev/null || :)"
325 CHROME_ROOT_AUTO=1
326 fi
327 if [[ -n "$CHROME_ROOT" ]]; then
328 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
329 error "Not mounting chrome source"
330 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
331 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
332 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800333 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400334 else
335 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
336 sudo bash -c "echo '$CHROME_ROOT' > \
337 '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'"
Mike Frysinger286b5922011-09-28 11:59:53 -0400338 queue_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800339 fi
340 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800341
Mike Frysinger286b5922011-09-28 11:59:53 -0400342 process_mounts
Chris Sosa317d8eb2010-04-05 15:45:28 -0700343
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400344 # Install fuse module. Skip modprobe when possible for slight
345 # speed increase when initializing the env.
346 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700347 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200348 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700349 fi
350
Chris Masone162f6542010-05-12 14:58:37 -0700351 # Turn off automounting of external media when we enter the
352 # chroot; thus we don't have to worry about being able to unmount
353 # from inside.
Mike Frysingerda7be782011-09-15 17:58:19 -0400354 if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then
355 if [ "${SAVED_PREF}" != "false" ]; then
356 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
357 warn "-- Note: USB sticks may be automounted by your host OS."
358 warn "-- Note: If you plan to burn bootable media, you may need to"
359 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
360 warn "-- Note: outside the chroot."
361 fi
Chris Masone162f6542010-05-12 14:58:37 -0700362 fi
363 fi
Mike Frysingerda7be782011-09-15 17:58:19 -0400364 # Always write the temp file so we can read it when exiting
365 echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
Chris Masone162f6542010-05-12 14:58:37 -0700366
Mike Frysinger94717e32011-09-21 00:10:44 -0400367 # Configure committer username and email in chroot .gitconfig. Change
368 # to the root directory first so that random $PWD/.git/config settings
369 # do not get picked up. We want to stick to ~/.gitconfig only.
370 ident=$(cd /; git var GIT_COMMITTER_IDENT || :)
371 ident_name=${ident%% <*}
372 ident_email=${ident%%>*}; ident_email=${ident_email##*<}
David James342f1562011-07-15 15:21:18 -0700373 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400374 user.name "${ident_name}" || true
David James342f1562011-07-15 15:21:18 -0700375 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400376 user.email "${ident_email}" || true
David James342f1562011-07-15 15:21:18 -0700377
Matt Tennantd4316fe2011-08-30 12:06:42 -0700378 # Copy ~/.gdata_cred.txt to chroot if it exists. This file contains
379 # credentials for reading/writing Google Docs on chromium.org.
380 if [ -f "$HOME/.gdata_cred.txt" ]; then
381 cp "$HOME/.gdata_cred.txt" "${FLAGS_chroot}/home/${USER}/.gdata_cred.txt"
382 fi
383
Mike Frysinger6601c522011-08-15 11:05:39 -0400384 # Make sure user's requested locales are available
385 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400386 # And make sure en_US{,.UTF-8} are always available as
387 # that what buildbot forces internally
388 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400389 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
390 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
391 sort -u | sed '/^C$/d')
392 gen_locales=()
393 for l in ${locales}; do
394 if [[ ${l} == *.* ]]; then
395 enc=${l#*.}
396 else
397 enc="ISO-8859-1"
398 fi
399 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
400 utf8) enc="UTF-8";;
401 esac
402 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
403 done
Mike Frysinger4d258cd2011-09-15 16:17:49 -0400404 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
405 # Force LC_ALL=C to workaround slow string parsing in bash
406 # with long multibyte strings. Newer setups have this fixed,
407 # but locale-gen doesn't need to be run in any locale in the
408 # first place, so just go with C to keep it fast.
409 sudo -- chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \
410 -G "$(printf '%s\n' "${gen_locales[@]}")"
411 fi
Mike Frysinger6601c522011-08-15 11:05:39 -0400412
Dale Curtis98631732011-05-05 16:16:59 -0700413 # Fix permissions on shared memory to allow non-root users access to POSIX
Mike Frysinger82649172011-09-21 00:18:14 -0400414 # semaphores. Avoid the sudo call if possible (sudo is slow).
415 if [ -n "$(find "${FLAGS_chroot}/dev/shm" ! -perm 777)" ] ; then
416 sudo chmod -R 777 "${FLAGS_chroot}/dev/shm"
417 fi
David James546747b2010-03-23 15:19:43 -0700418 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000419}
420
421function teardown_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800422 # Validate sudo timestamp before entering the critical section so that we
423 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800424 # Don't use sudo -v since that has issues on machines w/ no password.
425 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800426
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800427 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800428 (
429 flock 200
430
431 # check each pid in $LOCKFILE to see if it's died unexpectedly
432 TMP_LOCKFILE="$LOCKFILE.tmp"
433
434 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
435 cat "$LOCKFILE" | while read PID; do
436 if [ "$PID" = "$$" ]; then
437 # ourself, leave PROC_NAME empty
438 PROC_NAME=""
439 else
440 PROC_NAME=$(ps --pid $PID -o comm=)
441 fi
442
443 if [ ! -z "$PROC_NAME" ]; then
444 # All good, keep going
445 echo "$PID" >> "$TMP_LOCKFILE"
446 fi
447 done
448 # Remove any dups from lock file while installing new one
Mike Frysinger61e4f282011-09-20 22:37:22 -0400449 sort -u -n "$TMP_LOCKFILE" > "$LOCKFILE"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800450
Mike Frysingerda7be782011-09-15 17:58:19 -0400451 SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
452 if [ "${SAVED_PREF}" != "false" ]; then
Chris Masone162f6542010-05-12 14:58:37 -0700453 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
Sean Ocd8b1d12010-09-02 17:34:49 +0200454 warn "could not re-set your automount preference."
Chris Masone162f6542010-05-12 14:58:37 -0700455 fi
456
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800457 if [ -s "$LOCKFILE" ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800458 debug "At least one other pid is running in the chroot, so not"
459 debug "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800460 else
Zdenek Behane28239d2011-04-07 00:37:20 +0200461 debug "Stopping syncer process"
Taylor Hutt25bf9492011-07-27 13:54:35 -0700462 # If another process entering the chroot is blocked on this
463 # flock in setup_env(), it can be a race condition.
464 #
465 # When this locked region is exited, the setup_env() flock can
466 # be entered before the script can exit and the /proc entry for
467 # the PID is removed. The newly-created chroot will not end up
468 # with a syncer process. To avoid that situation, remove the
469 # syncer PID file.
470 #
471 # The syncer PID file should also be removed because the kernel
472 # will reuse PIDs. It's possible that the PID in the syncer PID
473 # has been reused by another process; make sure we don't skip
474 # starting the syncer process when this occurs by deleting the
475 # PID file.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400476 kill $(<"${SYNCERPIDFILE}") && \
Mike Frysinger470be992011-09-28 11:53:56 -0400477 { rm -f "${SYNCERPIDFILE}" 2>/dev/null || \
478 sudo rm -f "${SYNCERPIDFILE}" ; } ||
479 debug "Unable to clean up syncer process.";
Zdenek Behane28239d2011-04-07 00:37:20 +0200480
Don Garretta0e7ea12011-02-07 18:39:59 -0800481 debug "Unmounting chroot environment."
Mike Frysinger1aa61242011-09-15 17:46:44 -0400482 safe_umount_tree "${MOUNTED_PATH}/"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800483 fi
David James546747b2010-03-23 15:19:43 -0700484 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000485}
486
Greg Spencer798d75f2011-02-01 22:04:49 -0800487if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000488 setup_env
Don Garretta0e7ea12011-02-07 18:39:59 -0800489 info "Make sure you run"
490 info " $0 --unmount"
491 info "before deleting $FLAGS_chroot"
492 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000493 exit 0
494fi
495
Greg Spencer798d75f2011-02-01 22:04:49 -0800496if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000497 teardown_env
498 exit 0
499fi
500
501# Make sure we unmount before exiting
502trap teardown_env EXIT
503setup_env
504
Zdenek Behan892e6ac2011-08-12 05:56:40 +0200505CHROOT_PASSTHRU="BUILDBOT_BUILD=$FLAGS_build_number \
506CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
Raja Aluri8f2a9952010-11-17 15:03:00 -0800507CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
508CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}"
Raja Alurie891eea2010-11-16 20:12:56 -0800509
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700510# Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER,
511# CHROMEOS_VERSION_DEVSERVER as environment variables to override the default
512# assumptions (local AU server). These are used in cros_set_lsb_release, and
513# are used by external Chromium OS builders.
514CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
515CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK} \
516CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER} \
517CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}"
518
Mario Limonciello7052ae12011-05-05 12:00:49 -0500519# Pass proxy variables into the environment.
520for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do
Mike Frysinger61e4f282011-09-20 22:37:22 -0400521 if [ -n "${!type}" ]; then
522 CHROOT_PASSTHRU="${CHROOT_PASSTHRU} ${type}=${!type}"
Mario Limonciello7052ae12011-05-05 12:00:49 -0500523 fi
524done
525
derat@google.com4e7a92b2009-11-21 23:44:14 +0000526# Run command or interactive shell. Also include the non-chrooted path to
527# the source trunk for scripts that may need to print it (e.g.
528# build_image.sh).
Doug Anderson9362fa82010-12-16 14:44:12 -0800529sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
Mike Frysinger5df0eab2011-08-11 15:39:24 -0400530 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" SSH_AGENT_PID="${SSH_AGENT_PID}" \
Doug Anderson9362fa82010-12-16 14:44:12 -0800531 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000532
533# Remove trap and explicitly unmount
534trap - EXIT
535teardown_env