blob: e6acc97b96db6f09d6db51ddba63d83798dbff21 [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."
Brian Harring35767822012-02-01 23:50:45 -080034DEFINE_boolean early_make_chroot $FLAGS_FALSE \
35 "Internal flag. If set, the command is run as root without sudo."
Don Garrettad3f0592011-02-04 14:59:56 -080036DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000037
38# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080039FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000040
41One or more VAR=value pairs can be specified to export variables into
42the chroot environment. For example:
43
44 $0 FOO=bar BAZ=bel
45
Doug Anderson9362fa82010-12-16 14:44:12 -080046If [-- command] is present, runs the command inside the chroot,
47after changing directory to /$USER/trunk/src/scripts. Note that neither
48the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000049
Doug Anderson9362fa82010-12-16 14:44:12 -080050 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000051
52Otherwise, provides an interactive shell.
53"
54
Don Garrettad3f0592011-02-04 14:59:56 -080055# Version of info from common.sh that only echos if --verbose is set.
Don Garretta0e7ea12011-02-07 18:39:59 -080056function debug {
Don Garrettad3f0592011-02-04 14:59:56 -080057 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
David Rochberg351a76f2011-02-16 11:14:00 -050058 info "$*"
Don Garrettad3f0592011-02-04 14:59:56 -080059 fi
60}
61
rspangler@google.comd74220d2009-10-09 20:56:14 +000062# Parse command line flags
63FLAGS "$@" || exit 1
64eval set -- "${FLAGS_ARGV}"
65
Greg Spencer798d75f2011-02-01 22:04:49 -080066if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
David McMahon857dbb52009-12-09 18:21:05 -080067 CHROMEOS_OFFICIAL=1
68fi
69
rspangler@google.comd74220d2009-10-09 20:56:14 +000070# Only now can we die on error. shflags functions leak non-zero error codes,
71# so will die prematurely if 'set -e' is specified before now.
72# TODO: replace shflags with something less error-prone, or contribute a fix.
73set -e
74
Sean Parent2898f752010-05-25 15:06:33 -070075INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080076CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080077INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070078FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -070079AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
80SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080081
Mike Frysinger82649172011-09-21 00:18:14 -040082# Avoid the sudo call if possible since it is a little slow.
83if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then
84 sudo chmod 0777 "$FLAGS_chroot/var/lock"
85fi
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080086
87LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Zdenek Behane28239d2011-04-07 00:37:20 +020088SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080089
David Rochberg351a76f2011-02-16 11:14:00 -050090
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -040091MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger286b5922011-09-28 11:59:53 -040092function mount_queue_init {
93 MOUNT_QUEUE=()
94}
95
96function queue_mount {
David Rochberg351a76f2011-02-16 11:14:00 -050097 # If necessary, mount $source in the host FS at $target inside the
98 # chroot directory with $mount_args.
99 local source="$1"
100 local mount_args="$2"
101 local target="$3"
102
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400103 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500104
Mike Frysinger2a970592011-09-20 22:49:01 -0400105 case ${MOUNT_CACHE} in
106 *" on ${mounted_path} "*)
107 # Already mounted!
108 ;;
109 *)
Mike Frysinger286b5922011-09-28 11:59:53 -0400110 MOUNT_QUEUE+=(
111 "mkdir -p '${mounted_path}'"
112 # The args are left unquoted on purpose.
113 "mount ${mount_args} '${source}' '${mounted_path}'"
114 )
Mike Frysinger2a970592011-09-20 22:49:01 -0400115 ;;
116 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500117}
118
Mike Frysinger286b5922011-09-28 11:59:53 -0400119function process_mounts {
120 if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then
121 return 0
122 fi
123 sudo_multi "${MOUNT_QUEUE[@]}"
124 mount_queue_init
125}
126
Zdenek Behane28239d2011-04-07 00:37:20 +0200127function env_sync_proc {
128 # This function runs and performs periodic updates to the chroot env, if
129 # necessary.
130
131 local poll_interval=10
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400132 local sync_files=( etc/resolv.conf etc/hosts )
Zdenek Behane28239d2011-04-07 00:37:20 +0200133
134 # Make sure the synced files are writable by normal user, so that we
135 # don't have to sudo inside the loop.
Mike Frysinger7f7a7632011-09-28 11:48:20 -0400136 local chmods=$(find ${sync_files[@]/#/${FLAGS_chroot}/} '!' -user ${USER})
137 if [ -n "${chmods}" ]; then
138 sudo -- chown ${USER} ${chmods} 1>&2
139 fi
Zdenek Behane28239d2011-04-07 00:37:20 +0200140
David James412b9022011-07-15 19:54:16 -0700141 # Drop stdin, stderr, stdout, and chroot lock.
142 # This is needed for properly daemonizing the process.
143 exec 0>&- 1>&- 2>&- 200>&-
144
Zdenek Behane28239d2011-04-07 00:37:20 +0200145 while true; do
146 # Sync files
147 for file in ${sync_files}; do
148 if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then
149 cp -f /${file} ${FLAGS_chroot}/${file}
150 fi
151 done
152
153 sleep ${poll_interval}
154 done
155}
156
Vadim Bendebury0779f522011-06-12 12:09:16 -0700157function copy_ssh_config {
158 # Copy user .ssh/config into the chroot filtering out strings not supported
159 # by the chroot ssh. The chroot .ssh directory is passed in as the first
160 # parameter.
161
162 # ssh options to filter out. The entire strings containing these substrings
163 # will be deleted before copying.
164 local bad_options=(
Matt Tennant63c3cc52011-11-22 11:23:06 -0800165 'UseProxyIf'
166 'GSSAPIAuthentication'
167 'GSSAPIKeyExchange'
168 'ProxyUseFdpass'
Vadim Bendebury0779f522011-06-12 12:09:16 -0700169 )
170 local sshc="${HOME}/.ssh/config"
171 local chroot_ssh_dir="${1}"
172 local filter
173 local option
174
175 if [ ! -f "${sshc}" ]; then
176 return # Nothing to copy.
177 fi
178
179 for option in "${bad_options[@]}"
180 do
181 if [ -z "${filter}" ]; then
182 filter="${option}"
183 else
184 filter+="\\|${option}"
185 fi
186 done
187
188 sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config"
189}
190
rspangler@google.comd74220d2009-10-09 20:56:14 +0000191function setup_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800192 # Validate sudo timestamp before entering the critical section so that we
193 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800194 # Don't use sudo -v since that has issues on machines w/ no password.
195 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800196
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800197 (
198 flock 200
199 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000200
Taylor Hutt25bf9492011-07-27 13:54:35 -0700201 # If there isn't a syncer daemon started already, start one. The
202 # daemon is considered to not be started under the following
203 # conditions:
204 #
205 # o There is no PID file
206 #
207 # o The PID file is 0 bytes in size, which might be a partial
208 # manifestation of chromium-os:17680. This situation will not
209 # occur anymore, but you might have a chroot which was already
210 # affected.
211 #
212 # o The /proc node for the process named by the PID file does
213 # not exist.
214 #
215 # Note: This does not address PID recycling. While
216 # increasingly unlikely, it is possible for the PID in
217 # the PID file to refer to a running process that is not
218 # the syncer process. Since the PID file is now
219 # removed, I think it is only possible for this to occur
220 # if your system crashes and the PID file exists after
221 # restart.
222 #
223 # The daemon is killed by the enter_chroot that exits last.
Taylor Hutt18594a82011-07-28 11:45:50 -0700224 if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then
Taylor Hutt25bf9492011-07-27 13:54:35 -0700225 info "You may have suffered from chromium-os:17680 and";
226 info "could have stray 'enter_chroot.sh' processes running.";
227 info "You must manually kill any such stray processes.";
228 info "Exit all chroot shells; remaining 'enter_chroot.sh'";
229 info "processes are probably stray.";
Taylor Hutt18594a82011-07-28 11:45:50 -0700230 sudo rm -f "${SYNCERPIDFILE}";
Taylor Hutt25bf9492011-07-27 13:54:35 -0700231 fi;
David James412b9022011-07-15 19:54:16 -0700232 if ! [ -f "${SYNCERPIDFILE}" ] || \
Mike Frysinger61e4f282011-09-20 22:37:22 -0400233 ! [ -d /proc/$(<"${SYNCERPIDFILE}") ]; then
David James412b9022011-07-15 19:54:16 -0700234 debug "Starting sync process"
235 env_sync_proc &
236 echo $! > "${SYNCERPIDFILE}"
237 disown $!
238 fi
239
Don Garretta0e7ea12011-02-07 18:39:59 -0800240 debug "Mounting chroot environment."
Mike Frysinger2a970592011-09-20 22:49:01 -0400241 MOUNT_CACHE=$(mount)
Mike Frysinger286b5922011-09-28 11:59:53 -0400242 mount_queue_init
243 queue_mount none "-t proc" /proc
244 queue_mount none "-t sysfs" /sys
245 queue_mount /dev "--bind" /dev
246 queue_mount none "-t devpts" /dev/pts
Aaron Plattner130d3632011-10-18 08:54:57 -0700247 if [ -d /run ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400248 queue_mount /run "--bind" /run
Aaron Plattner130d3632011-10-18 08:54:57 -0700249 if [ -d /run/shm ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400250 queue_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700251 fi
252 fi
Mike Frysinger286b5922011-09-28 11:59:53 -0400253 queue_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800254
Brian Harringdd7d7932011-12-23 04:21:33 -0800255
256 debug "Setting up referenced repositories if required."
257 REFERENCE_DIR=$(git config --file \
258 "${FLAGS_trunk}/.repo/manifests.git/config" \
259 repo.reference)
260 if [ -n "${REFERENCE_DIR}" ]; then
261
262 IFS=$'\n';
263 required=( $( "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \
264 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
265 unset IFS
266
267 queue_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
268 "${CHROOT_TRUNK_DIR}/.repo/alternates"
269
270 # Note that as we're bringing up each referened repo, we also
271 # mount bind an empty directory over its alternates. This is
272 # required to suppress git from tracing through it- we already
273 # specify the required alternates for CHROOT_TRUNK_DIR, no point
274 # in having git try recursing through each on their own.
275 #
276 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
277 # the bind is visible only w/in the chroot.
278 mkdir -p ${FLAGS_trunk}/.repo/chroot/empty
279 position=1
280 for x in "${required[@]}"; do
281 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
282 queue_mount "${x}" "--bind" "${base}"
283 if [ -e "${x}/.repo/alternates" ]; then
284 queue_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
285 "${base}/.repo/alternates"
286 fi
287 position=$(( ${position} + 1 ))
288 done
289 unset required position base
290 fi
291 unset REFERENCE_DIR
292
Elly Jones7990a062010-09-02 09:23:23 -0400293 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800294 if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
Mike Frysinger61e4f282011-09-20 22:37:22 -0400295 TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh"
Elly Jones7990a062010-09-02 09:23:23 -0400296 mkdir -p "${TARGET_DIR}"
Mike Frysinger40bb0c32011-10-31 17:14:15 -0400297 # Ignore errors as some people won't have these files to copy.
298 cp "${HOME}"/.ssh/{known_hosts,*.pub} "${TARGET_DIR}/" 2>/dev/null || :
Vadim Bendebury0779f522011-06-12 12:09:16 -0700299 copy_ssh_config "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500300
301 # Don't try to bind mount the ssh agent dir if it has gone stale.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400302 ASOCK=${SSH_AUTH_SOCK%/*}
Mike Frysinger9de707f2011-12-12 14:21:44 -0500303 if [ -d "${ASOCK}" ]; then
304 queue_mount "${ASOCK}" "--bind" "${ASOCK}"
305 fi
Elly Jones7990a062010-09-02 09:23:23 -0400306 fi
307 fi
308
Mike Frysinger286b5922011-09-28 11:59:53 -0400309 if [ -d "$HOME/.subversion" ]; then
310 TARGET="/home/${USER}/.subversion"
311 mkdir -p "${FLAGS_chroot}${TARGET}"
312 queue_mount "${HOME}/.subversion" "--bind" "${TARGET}"
313 fi
314
315 if DEPOT_TOOLS=$(type -P gclient) ; then
316 DEPOT_TOOLS=${DEPOT_TOOLS%/*} # dirname
317 debug "Mounting depot_tools"
318 queue_mount "$DEPOT_TOOLS" --bind "$INNER_DEPOT_TOOLS_ROOT"
319 fi
320
321 process_mounts
322
Mike Frysinger9a50b642011-09-20 23:37:14 -0400323 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
324 if [ -z "$CHROME_ROOT" ]; then
325 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
326 2>/dev/null || :)"
327 CHROME_ROOT_AUTO=1
328 fi
329 if [[ -n "$CHROME_ROOT" ]]; then
330 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
331 error "Not mounting chrome source"
332 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
333 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
334 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800335 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400336 else
337 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
338 sudo bash -c "echo '$CHROME_ROOT' > \
339 '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'"
Mike Frysinger286b5922011-09-28 11:59:53 -0400340 queue_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800341 fi
342 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800343
Mike Frysinger286b5922011-09-28 11:59:53 -0400344 process_mounts
Chris Sosa317d8eb2010-04-05 15:45:28 -0700345
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400346 # Install fuse module. Skip modprobe when possible for slight
347 # speed increase when initializing the env.
348 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700349 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200350 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700351 fi
352
Chris Masone162f6542010-05-12 14:58:37 -0700353 # Turn off automounting of external media when we enter the
354 # chroot; thus we don't have to worry about being able to unmount
355 # from inside.
Mike Frysingerda7be782011-09-15 17:58:19 -0400356 if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then
357 if [ "${SAVED_PREF}" != "false" ]; then
358 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
359 warn "-- Note: USB sticks may be automounted by your host OS."
360 warn "-- Note: If you plan to burn bootable media, you may need to"
361 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
362 warn "-- Note: outside the chroot."
363 fi
Chris Masone162f6542010-05-12 14:58:37 -0700364 fi
365 fi
Mike Frysingerda7be782011-09-15 17:58:19 -0400366 # Always write the temp file so we can read it when exiting
367 echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
Chris Masone162f6542010-05-12 14:58:37 -0700368
Mike Frysinger94717e32011-09-21 00:10:44 -0400369 # Configure committer username and email in chroot .gitconfig. Change
370 # to the root directory first so that random $PWD/.git/config settings
371 # do not get picked up. We want to stick to ~/.gitconfig only.
372 ident=$(cd /; git var GIT_COMMITTER_IDENT || :)
373 ident_name=${ident%% <*}
374 ident_email=${ident%%>*}; ident_email=${ident_email##*<}
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.name "${ident_name}" || true
David James342f1562011-07-15 15:21:18 -0700377 git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \
Mike Frysinger94717e32011-09-21 00:10:44 -0400378 user.email "${ident_email}" || true
David James342f1562011-07-15 15:21:18 -0700379
Matt Tennantd4316fe2011-08-30 12:06:42 -0700380 # Copy ~/.gdata_cred.txt to chroot if it exists. This file contains
381 # credentials for reading/writing Google Docs on chromium.org.
382 if [ -f "$HOME/.gdata_cred.txt" ]; then
383 cp "$HOME/.gdata_cred.txt" "${FLAGS_chroot}/home/${USER}/.gdata_cred.txt"
384 fi
385
Mike Frysinger6601c522011-08-15 11:05:39 -0400386 # Make sure user's requested locales are available
387 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400388 # And make sure en_US{,.UTF-8} are always available as
389 # that what buildbot forces internally
390 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400391 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
392 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
393 sort -u | sed '/^C$/d')
394 gen_locales=()
395 for l in ${locales}; do
396 if [[ ${l} == *.* ]]; then
397 enc=${l#*.}
398 else
399 enc="ISO-8859-1"
400 fi
401 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
402 utf8) enc="UTF-8";;
403 esac
404 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
405 done
Mike Frysinger4d258cd2011-09-15 16:17:49 -0400406 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
407 # Force LC_ALL=C to workaround slow string parsing in bash
408 # with long multibyte strings. Newer setups have this fixed,
409 # but locale-gen doesn't need to be run in any locale in the
410 # first place, so just go with C to keep it fast.
411 sudo -- chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \
412 -G "$(printf '%s\n' "${gen_locales[@]}")"
413 fi
Mike Frysinger6601c522011-08-15 11:05:39 -0400414
Dale Curtis98631732011-05-05 16:16:59 -0700415 # Fix permissions on shared memory to allow non-root users access to POSIX
Mike Frysinger82649172011-09-21 00:18:14 -0400416 # semaphores. Avoid the sudo call if possible (sudo is slow).
417 if [ -n "$(find "${FLAGS_chroot}/dev/shm" ! -perm 777)" ] ; then
418 sudo chmod -R 777 "${FLAGS_chroot}/dev/shm"
419 fi
David James546747b2010-03-23 15:19:43 -0700420 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000421}
422
423function teardown_env {
Doug Andersona8d9cc12011-02-02 15:47:00 -0800424 # Validate sudo timestamp before entering the critical section so that we
425 # don't stall for a password while we have the lockfile.
Doug Anderson3d7fa3a2011-02-02 16:10:34 -0800426 # Don't use sudo -v since that has issues on machines w/ no password.
427 sudo echo "" > /dev/null
Doug Andersona8d9cc12011-02-02 15:47:00 -0800428
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800429 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800430 (
431 flock 200
432
433 # check each pid in $LOCKFILE to see if it's died unexpectedly
434 TMP_LOCKFILE="$LOCKFILE.tmp"
435
436 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
437 cat "$LOCKFILE" | while read PID; do
438 if [ "$PID" = "$$" ]; then
439 # ourself, leave PROC_NAME empty
440 PROC_NAME=""
441 else
442 PROC_NAME=$(ps --pid $PID -o comm=)
443 fi
444
445 if [ ! -z "$PROC_NAME" ]; then
446 # All good, keep going
447 echo "$PID" >> "$TMP_LOCKFILE"
448 fi
449 done
450 # Remove any dups from lock file while installing new one
Mike Frysinger61e4f282011-09-20 22:37:22 -0400451 sort -u -n "$TMP_LOCKFILE" > "$LOCKFILE"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800452
Mike Frysingerda7be782011-09-15 17:58:19 -0400453 SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
454 if [ "${SAVED_PREF}" != "false" ]; then
Chris Masone162f6542010-05-12 14:58:37 -0700455 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
Sean Ocd8b1d12010-09-02 17:34:49 +0200456 warn "could not re-set your automount preference."
Chris Masone162f6542010-05-12 14:58:37 -0700457 fi
458
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800459 if [ -s "$LOCKFILE" ]; then
Don Garretta0e7ea12011-02-07 18:39:59 -0800460 debug "At least one other pid is running in the chroot, so not"
461 debug "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800462 else
Zdenek Behane28239d2011-04-07 00:37:20 +0200463 debug "Stopping syncer process"
Taylor Hutt25bf9492011-07-27 13:54:35 -0700464 # If another process entering the chroot is blocked on this
465 # flock in setup_env(), it can be a race condition.
466 #
467 # When this locked region is exited, the setup_env() flock can
468 # be entered before the script can exit and the /proc entry for
469 # the PID is removed. The newly-created chroot will not end up
470 # with a syncer process. To avoid that situation, remove the
471 # syncer PID file.
472 #
473 # The syncer PID file should also be removed because the kernel
474 # will reuse PIDs. It's possible that the PID in the syncer PID
475 # has been reused by another process; make sure we don't skip
476 # starting the syncer process when this occurs by deleting the
477 # PID file.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400478 kill $(<"${SYNCERPIDFILE}") && \
Mike Frysinger470be992011-09-28 11:53:56 -0400479 { rm -f "${SYNCERPIDFILE}" 2>/dev/null || \
480 sudo rm -f "${SYNCERPIDFILE}" ; } ||
481 debug "Unable to clean up syncer process.";
Zdenek Behane28239d2011-04-07 00:37:20 +0200482
Don Garretta0e7ea12011-02-07 18:39:59 -0800483 debug "Unmounting chroot environment."
Mike Frysinger1aa61242011-09-15 17:46:44 -0400484 safe_umount_tree "${MOUNTED_PATH}/"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800485 fi
David James546747b2010-03-23 15:19:43 -0700486 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000487}
488
Greg Spencer798d75f2011-02-01 22:04:49 -0800489if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000490 setup_env
Don Garretta0e7ea12011-02-07 18:39:59 -0800491 info "Make sure you run"
492 info " $0 --unmount"
493 info "before deleting $FLAGS_chroot"
494 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000495 exit 0
496fi
497
Greg Spencer798d75f2011-02-01 22:04:49 -0800498if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000499 teardown_env
500 exit 0
501fi
502
503# Make sure we unmount before exiting
504trap teardown_env EXIT
505setup_env
506
Brian Harring35767822012-02-01 23:50:45 -0800507CHROOT_PASSTHRU=(
508 "BUILDBOT_BUILD=$FLAGS_build_number"
509 "CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
510 "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
Raja Alurie891eea2010-11-16 20:12:56 -0800511
Brian Harring35767822012-02-01 23:50:45 -0800512 # Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER,
513 # CHROMEOS_VERSION_DEVSERVER as environment variables to override the default
514 # assumptions (local AU server). These are used in cros_set_lsb_release, and
515 # are used by external Chromium OS builders.
516
517 "CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK}"
518 "CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER}"
519 "CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}"
520 "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
521 "SSH_AGENT_PID=${SSH_AGENT_PID}"
522 "SSH_AUTH_SOCK=${SSH_AUTH_SOCK}"
523)
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700524
Mario Limonciello7052ae12011-05-05 12:00:49 -0500525# Pass proxy variables into the environment.
526for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do
Brian Harring35767822012-02-01 23:50:45 -0800527 if [ -n "${!type}" ]; then
528 CHROOT_PASSTHRU+=( "${type}=${!type}" )
529 fi
Mario Limonciello7052ae12011-05-05 12:00:49 -0500530done
531
derat@google.com4e7a92b2009-11-21 23:44:14 +0000532# Run command or interactive shell. Also include the non-chrooted path to
533# the source trunk for scripts that may need to print it (e.g.
534# build_image.sh).
Brian Harring35767822012-02-01 23:50:45 -0800535
536if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
537 cmd=( /bin/bash -l -c 'env "$@"' -- )
538elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
539 # Complain that sudo is missing.
540 error "Failing since the chroot lacks sudo."
541 error "Requested enter_chroot command was: $@"
542 exit 127
543else
544 cmd=( sudo -i -u "$USER" )
545fi
546
547sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000548
549# Remove trap and explicitly unmount
550trap - EXIT
551teardown_env