blob: f80e371e528a6eed7884d12ed36a8cc30d4962c4 [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
David James76764882012-10-24 19:46:29 -070012# Script must be run outside the chroot and as root.
rspangler@google.comd74220d2009-10-09 20:56:14 +000013assert_outside_chroot
David James76764882012-10-24 19:46:29 -070014assert_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."
David James76764882012-10-24 19:46:29 -070026DEFINE_string chrome_root_mount "/home/${SUDO_USER}/chrome_root" \
Sean Parent2898f752010-05-25 15:06:33 -070027 "The mount point of the chrome broswer source in the chroot."
Brian Harring7b6f3772012-09-23 14:01:13 -070028DEFINE_string cache_dir "" "Directory to use for caching."
Hidehiko Abe63d6c462017-03-02 17:40:54 +090029DEFINE_string goma_dir "" "Goma installed directory."
30DEFINE_string goma_client_json "" "Service account json file for goma."
Yong Hong7dee2cc2018-02-06 16:02:56 +080031DEFINE_string working_dir "" \
32 "The working directory relative to ${CHROOT_TRUNK_DIR} for the command in \
33chroot, must start with '/' if set."
rspangler@google.comd74220d2009-10-09 20:56:14 +000034
Elly Jones7990a062010-09-02 09:23:23 -040035DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
Brian Harring35767822012-02-01 23:50:45 -080036DEFINE_boolean early_make_chroot $FLAGS_FALSE \
37 "Internal flag. If set, the command is run as root without sudo."
Don Garrettad3f0592011-02-04 14:59:56 -080038DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000039
40# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080041FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000042
43One or more VAR=value pairs can be specified to export variables into
44the chroot environment. For example:
45
46 $0 FOO=bar BAZ=bel
47
Doug Anderson9362fa82010-12-16 14:44:12 -080048If [-- command] is present, runs the command inside the chroot,
David James76764882012-10-24 19:46:29 -070049after changing directory to /${SUDO_USER}/trunk/src/scripts. Note that neither
Doug Anderson9362fa82010-12-16 14:44:12 -080050the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000051
Doug Anderson9362fa82010-12-16 14:44:12 -080052 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000053
54Otherwise, provides an interactive shell.
55"
56
Peter Mayo4411efe2012-09-21 04:41:27 -040057CROS_LOG_PREFIX=cros_sdk:enter_chroot
David James76764882012-10-24 19:46:29 -070058SUDO_HOME=$(eval echo ~${SUDO_USER})
Peter Mayo4411efe2012-09-21 04:41:27 -040059
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
Brian Harring7b6f3772012-09-23 14:01:13 -070071[ -z "${FLAGS_cache_dir}" ] && \
72 die "--cache_dir is required"
Brian Harring7ee892d2012-02-02 09:33:10 -080073
rspangler@google.comd74220d2009-10-09 20:56:14 +000074# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080075# so will die prematurely if 'switch_to_strict_mode' is specified before now.
rspangler@google.comd74220d2009-10-09 20:56:14 +000076# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080077switch_to_strict_mode
rspangler@google.comd74220d2009-10-09 20:56:14 +000078
Matt Tennant298f61a2012-06-25 21:54:33 -070079# These config files are to be copied into chroot if they exist in home dir.
Prathmesh Prabhu74ebbbd2015-03-17 13:50:29 -070080# Additionally, git relevant files are copied by setup_git.
Matt Tennant298f61a2012-06-25 21:54:33 -070081FILES_TO_COPY_TO_CHROOT=(
Ben Pasteneaa6c9302020-01-28 11:41:57 -080082 # Creds used to authenticate with LUCI services.
83 .config/chrome_infra/auth/creds.json
Matt Tennant298f61a2012-06-25 21:54:33 -070084 .gdata_cred.txt # User/password for Google Docs on chromium.org
85 .gdata_token # Auth token for Google Docs on chromium.org
Ben Pastene4ef8d112020-05-05 14:16:21 -070086 .goma_client_oauth2_config # Auth token for Goma
Marc Herbert0c422062015-05-29 16:18:54 -070087 .inputrc # Preserve command line customizations
Matt Tennant298f61a2012-06-25 21:54:33 -070088)
Mike Frysinger270f4052019-12-13 04:31:30 -050089if [[ "${SUDO_USER:-${USER}}" == "chrome-bot" ]]; then
Mike Frysinger61205452019-12-11 05:08:02 -050090 # Builders still haven't migrated fully to gitcookies.
91 # https://crbug.com/1032944
92 FILES_TO_COPY_TO_CHROOT+=( .netrc )
93fi
Matt Tennant298f61a2012-06-25 21:54:33 -070094
Sean Parent2898f752010-05-25 15:06:33 -070095INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080096CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070097FUSE_DEVICE="/dev/fuse"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080098
Mike Frysinger01c205d2013-03-22 00:59:58 -040099# We can't use /var/lock because that might be a symlink to /run/lock outside
100# of the chroot. Or /run on the host system might not exist.
101LOCKFILE="${FLAGS_chroot}/.enter_chroot.lock"
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400102MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger286b5922011-09-28 11:59:53 -0400103
Brian Harring2499bfb2012-12-18 16:23:31 -0800104# Reset the depot tools/internal trunk pathways to what they'll
105# be w/in the chroot.
106set_chroot_trunk_dir "${FLAGS_chroot}"
107
Mike Frysingereab3f072019-08-21 22:57:49 -0400108# Writes stdin to the given file name as the sudo user in overwrite mode.
109#
110# $@ - The output file names.
111user_clobber() {
112 install -m644 -o ${SUDO_UID} -g ${SUDO_GID} /dev/stdin "$@"
113}
114
115# Copies the specified file owned by the user to the specified location.
116# If the copy fails as root (e.g. due to root_squash and NFS), retry the copy
117# with the user's account before failing.
118user_cp() {
119 cp -p "$@" 2>/dev/null || sudo -u ${SUDO_USER} -- cp -p "$@"
120}
121
122# Appends stdin to the given file name as the sudo user.
123#
124# $1 - The output file name.
125user_append() {
126 cat >> "$1"
127 chown ${SUDO_UID}:${SUDO_GID} "$1"
128}
129
130# Create the specified directory, along with parents, as the sudo user.
131#
132# $@ - The directories to create.
133user_mkdir() {
134 install -o ${SUDO_UID} -g ${SUDO_GID} -d "$@"
135}
136
137# Create the specified symlink as the sudo user.
138#
139# $1 - Link target
140# $2 - Link name
141user_symlink() {
142 ln -sfT "$1" "$2"
143 chown -h ${SUDO_UID}:${SUDO_GID} "$2"
144}
Brian Harring2499bfb2012-12-18 16:23:31 -0800145
David James76764882012-10-24 19:46:29 -0700146setup_mount() {
David Rochberg351a76f2011-02-16 11:14:00 -0500147 # If necessary, mount $source in the host FS at $target inside the
David James76764882012-10-24 19:46:29 -0700148 # chroot directory with $mount_args. We don't write to /etc/mtab because
149 # these mounts are all contained within an unshare and are therefore
150 # inaccessible to other namespaces (e.g. the host desktop system).
David Rochberg351a76f2011-02-16 11:14:00 -0500151 local source="$1"
David James76764882012-10-24 19:46:29 -0700152 local mount_args="-n $2"
David Rochberg351a76f2011-02-16 11:14:00 -0500153 local target="$3"
154
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400155 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500156
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400157 case " ${MOUNT_CACHE} " in
158 *" ${mounted_path} "*)
Mike Frysinger2a970592011-09-20 22:49:01 -0400159 # Already mounted!
160 ;;
161 *)
Mike Frysingeradd99762014-03-27 13:17:58 -0400162 # If it doesn't exist, assume they want a dir. But don't blindly run
163 # it all the time in case they're trying to bind mount a file.
164 if [[ ! -e ${mounted_path} ]]; then
165 mkdir -p "${mounted_path}"
166 fi
Michael Krebs04c4f732012-09-07 18:31:46 -0700167 # The args are left unquoted on purpose.
168 if [[ -n ${source} ]]; then
David James76764882012-10-24 19:46:29 -0700169 mount ${mount_args} "${source}" "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700170 else
David James76764882012-10-24 19:46:29 -0700171 mount ${mount_args} "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700172 fi
Mike Frysinger2a970592011-09-20 22:49:01 -0400173 ;;
174 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500175}
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 )
David James76764882012-10-24 19:46:29 -0700190 local sshc="${SUDO_HOME}/.ssh/config"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700191 local chroot_ssh_dir="${1}"
192 local filter
193 local option
194
David James22dc2ba2012-11-29 15:46:58 -0800195 if ! user_cp "${sshc}" "${chroot_ssh_dir}/config.orig" 2>/dev/null; then
Vadim Bendebury0779f522011-06-12 12:09:16 -0700196 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
David James22dc2ba2012-11-29 15:46:58 -0800208 sed "/^.*\(${filter}\).*$/d" "${chroot_ssh_dir}/config.orig" | \
David James76764882012-10-24 19:46:29 -0700209 user_clobber "${chroot_ssh_dir}/config"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700210}
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.
Mike Frysinger61205452019-12-11 05:08:02 -0500214 if [[ -e "$1" ]]; then
Ben Pasteneaa6c9302020-01-28 11:41:57 -0800215 local dir
216 dir=$(dirname "${FLAGS_chroot}/$2")
217 if [[ ! -d "${dir}" ]]; then
218 user_mkdir "${dir}"
219 fi
Mike Frysinger0c4821f2019-12-15 19:35:43 -0500220 user_cp "$1" "${FLAGS_chroot}/$2"
Mike Frysinger61205452019-12-11 05:08:02 -0500221 fi
Matt Tennantf7c9e772012-02-08 17:06:26 -0800222}
223
Peter Mayo4411efe2012-09-21 04:41:27 -0400224# Usage: promote_api_keys
225# This takes care of getting the developer API keys into the chroot where
226# chrome can build with them. It needs to take it from the places a dev
227# is likely to put them, and recognize that older chroots may or may not
228# have been used since the concept of keys got added, as well as before
Thiemo Nagel3a4d23b2017-05-26 16:52:05 +0200229# and after the developer deciding to grab their own keys.
Peter Mayo4411efe2012-09-21 04:41:27 -0400230promote_api_keys() {
David James76764882012-10-24 19:46:29 -0700231 local destination="${FLAGS_chroot}/home/${SUDO_USER}/.googleapikeys"
Peter Mayo4411efe2012-09-21 04:41:27 -0400232 # Don't disturb existing keys. They could be set differently
233 if [[ -s "${destination}" ]]; then
234 return 0
235 fi
David James76764882012-10-24 19:46:29 -0700236 if [[ -r "${SUDO_HOME}/.googleapikeys" ]]; then
237 cp -p "${SUDO_HOME}/.googleapikeys" "${destination}"
Peter Mayo4411efe2012-09-21 04:41:27 -0400238 if [[ -s "${destination}" ]] ; then
239 info "Copied Google API keys into chroot."
240 fi
David James76764882012-10-24 19:46:29 -0700241 elif [[ -r "${SUDO_HOME}/.gyp/include.gypi" ]]; then
Peter Mayo4411efe2012-09-21 04:41:27 -0400242 local NAME="('google_(api_key|default_client_(id|secret))')"
243 local WS="[[:space:]]*"
244 local CONTENTS="('[^\\\\']*')"
245 sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \
David James76764882012-10-24 19:46:29 -0700246 "${SUDO_HOME}/.gyp/include.gypi" | user_clobber "${destination}"
Peter Mayo4411efe2012-09-21 04:41:27 -0400247 if [[ -s "${destination}" ]]; then
248 info "Put discovered Google API keys into chroot."
249 fi
250 fi
251}
252
Mike Frysinger5ba57532020-04-07 01:05:25 -0400253# Sanity check the user's environment locale settings.
254check_locale() {
255 if [[ "$(locale -k charmap)" != 'charmap="UTF-8"' ]]; then
256 error "Your locale appears to not support UTF-8 which is required."
257 error "The output from 'locale':"
258 locale
259 error "The output from 'locale -k LC_CTYPE':"
260 locale -k LC_CTYPE
261 error "The language env vars:"
262 env | grep -E '^(LC_|LANG)'
263 if [[ -n "${LC_ALL}" ]]; then
264 error "Never set LC_ALL (=${LC_ALL}) in your environment; only set LANG."
265 fi
266 die_notrace "Please fix your locale settings by setting LANG to UTF-8" \
267 "compatible locale and removing any LC_ variable settings."
268 fi
Mike Frysinger5ba57532020-04-07 01:05:25 -0400269}
270
Mike Frysinger4f2d5cc2013-06-09 23:34:52 -0400271generate_locales() {
272 # Make sure user's requested locales are available
273 # http://crosbug.com/19139
274 # And make sure en_US{,.UTF-8} are always available as
275 # that what buildbot forces internally
276 local l locales gen_locales=()
277
278 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
279 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
280 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
281 sort -u | sed '/^C$/d')
282 for l in ${locales}; do
283 if [[ ${l} == *.* ]]; then
284 enc=${l#*.}
285 else
286 enc="ISO-8859-1"
287 fi
288 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
289 utf8) enc="UTF-8";;
290 esac
291 gen_locales+=("${l} ${enc}")
292 done
293 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
294 # Force LC_ALL=C to workaround slow string parsing in bash
295 # with long multibyte strings. Newer setups have this fixed,
296 # but locale-gen doesn't need to be run in any locale in the
297 # first place, so just go with C to keep it fast.
Brian Norrise83a1472017-08-31 16:24:45 -0700298 # Force jobs=1 to work around locale-gen weirdness where it simultaneously
299 # wants to and doesn't want to run multiple jobs (and complains about it).
300 # crbug.com/761153
Mike Frysinger4f2d5cc2013-06-09 23:34:52 -0400301 chroot "${FLAGS_chroot}" env LC_ALL=C locale-gen -q -u \
Brian Norrise83a1472017-08-31 16:24:45 -0700302 -j 1 -G "$(printf '%s\n' "${gen_locales[@]}")"
Mike Frysinger4f2d5cc2013-06-09 23:34:52 -0400303 fi
304}
305
Mike Frysinger09c27c72019-12-10 18:44:39 -0500306git_config() {
307 USER="${SUDO_USER:-${USER}}" \
308 HOME="${SUDO_HOME:-${HOME}}" \
309 git config "$@"
310}
311
Mike Frysinger2a67d482019-12-13 14:07:19 -0500312# The --type=path option is new to git-2.18 and not everyone upgrades.
313# But not everyone uses the ~ prefix, so try that option if needed.
314git_config_path() {
315 local out
316 out=$(git_config "$@")
317 if [[ "${out:0:1}" == "~" ]]; then
318 git_config --type path "$@"
319 else
320 echo "${out}"
321 fi
322}
323
Prathmesh Prabhu74ebbbd2015-03-17 13:50:29 -0700324setup_git() {
325 # Copy .gitconfig into chroot so repo and git can be used from inside.
326 # This is required for repo to work since it validates the email address.
327 copy_into_chroot_if_exists "${SUDO_HOME}/.gitconfig" \
328 "/home/${SUDO_USER}/.gitconfig"
329 local -r chroot_gitconfig="${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig"
330
331 # If the user didn't set up their username in their gitconfig, look
332 # at the default git settings for the user.
333 if ! git config -f "${chroot_gitconfig}" user.email >& /dev/null; then
334 local ident=$(cd /; sudo -u ${SUDO_USER} -- git var GIT_COMMITTER_IDENT || \
335 :)
336 local ident_name=${ident%% <*}
337 local ident_email=${ident%%>*}; ident_email=${ident_email##*<}
338 git config -f "${chroot_gitconfig}" --replace-all user.name \
339 "${ident_name}" || :
340 git config -f "${chroot_gitconfig}" --replace-all user.email \
341 "${ident_email}" || :
342 fi
343
344 # Copy the gitcookies file, updating the user's gitconfig to point to it.
345 local gitcookies
Mike Frysinger2a67d482019-12-13 14:07:19 -0500346 gitcookies="$(git_config_path --file "${chroot_gitconfig}" \
Mike Frysinger09c27c72019-12-10 18:44:39 -0500347 --get http.cookiefile)"
Prathmesh Prabhu74ebbbd2015-03-17 13:50:29 -0700348 if [[ $? -ne 0 ]]; then
349 # Try the default location anyway.
350 gitcookies="${SUDO_HOME}/.gitcookies"
351 fi
352 copy_into_chroot_if_exists "${gitcookies}" "/home/${SUDO_USER}/.gitcookies"
353 local -r chroot_gitcookies="${FLAGS_chroot}/home/${SUDO_USER}/.gitcookies"
354 if [[ -e "${chroot_gitcookies}" ]]; then
355 git config -f "${chroot_gitconfig}" --replace-all http.cookiefile \
356 "/home/${SUDO_USER}/.gitcookies"
357 fi
358 # This line must be at the end because using `git config` changes ownership of
359 # the .gitconfig.
360 chown ${SUDO_UID}:${SUDO_GID} "${chroot_gitconfig}"
361}
362
Xiyuan Xiaa6e21ae2018-03-28 08:47:23 -0700363setup_gclient_cache_dir_mount() {
364 # Mount "cache_dir" if a glient checkout depends on it.
365 # Otherwise, git command inside chroot fails. See https://crbug.com/747349
366 local checkout_root="$1"
367
368 if [[ ! -e "${checkout_root}/.gclient" ]]; then
369 return 0
370 fi
371
372 local cache_dir=$(sed -n -E "s/^ *cache_dir *= *'(.*)'/\1/p" \
373 "${checkout_root}/.gclient")
374 if [[ -z "${cache_dir}" ]]; then
375 return 0
376 fi
377
Mike Frysingerd5a3e6d2019-08-07 15:23:15 -0400378 # See if the cache dir exists outside of the chroot.
Xiyuan Xiaa6e21ae2018-03-28 08:47:23 -0700379 if [[ ! -d "${cache_dir}" ]]; then
Mike Frysingerd5a3e6d2019-08-07 15:23:15 -0400380 # See if it exists inside the chroot (which can happen if the checkout was
381 # created in there).
382 if [[ ! -d "${FLAGS_chroot}/${cache_dir}" ]]; then
383 warn "Gclient cache dir \"${cache_dir}\" is not a directory."
384 fi
Xiyuan Xiaa6e21ae2018-03-28 08:47:23 -0700385 return 0
386 fi
387
388 setup_mount "${cache_dir}" "--bind" "${cache_dir}"
389}
390
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400391setup_env() {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800392 (
393 flock 200
rspangler@google.comd74220d2009-10-09 20:56:14 +0000394
David James76764882012-10-24 19:46:29 -0700395 # Make the lockfile writable for backwards compatibility.
396 chown ${SUDO_UID}:${SUDO_GID} "${LOCKFILE}"
397
398 # Refresh /etc/resolv.conf and /etc/hosts in the chroot.
399 install -C -m644 /etc/resolv.conf ${FLAGS_chroot}/etc/resolv.conf
400 install -C -m644 /etc/hosts ${FLAGS_chroot}/etc/hosts
David James412b9022011-07-15 19:54:16 -0700401
Don Garretta0e7ea12011-02-07 18:39:59 -0800402 debug "Mounting chroot environment."
Steev Klimaszewskia10974c2013-11-30 20:38:34 -0600403 mount --make-rslave /
Benjamin Gordon1d5afed2017-06-14 16:35:32 -0600404 if grep -q -e "${FLAGS_chroot}[[:space:]]" /proc/mounts; then
405 debug "Changing $FLAGS_chroot to a private subtree."
406 mount --make-private "$FLAGS_chroot"
407 fi
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400408 MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts))
David James76764882012-10-24 19:46:29 -0700409 setup_mount none "-t proc" /proc
410 setup_mount none "-t sysfs" /sys
Gaurav Shah365b7062013-12-03 18:12:58 -0800411 if grep -q binfmt_misc /proc/filesystems; then
412 setup_mount binfmt_misc "-t binfmt_misc" /proc/sys/fs/binfmt_misc
413 fi
Victor Dodonc34d4bc2016-05-09 12:09:21 -0700414 if grep -q configfs /proc/filesystems; then
415 setup_mount none "-t configfs" /sys/kernel/config
416 fi
Mike Frysingerb5080fb2019-03-19 04:13:02 -0400417 setup_mount /dev "--rbind" /dev
David James482fb292013-03-10 21:06:54 -0700418 if [[ -d /run ]]; then
David James76764882012-10-24 19:46:29 -0700419 setup_mount /run "--bind" /run
David James482fb292013-03-10 21:06:54 -0700420 if [[ -d /run/shm && ! -L /run/shm ]]; then
David James76764882012-10-24 19:46:29 -0700421 setup_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700422 fi
423 fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800424
Mike Frysinger4f2d5cc2013-06-09 23:34:52 -0400425 # Do this early as it's slow and only needs basic mounts (above).
426 generate_locales &
427
Michael Spang15144f42013-04-02 17:42:55 -0400428 setup_mount "${FLAGS_trunk}" "--rbind" "${CHROOT_TRUNK_DIR}"
Chris Sosa389634d2012-09-05 19:56:53 -0700429
Brian Harringdd7d7932011-12-23 04:21:33 -0800430 debug "Setting up referenced repositories if required."
Mike Frysinger2a67d482019-12-13 14:07:19 -0500431 REFERENCE_DIR=$(git_config_path --file \
Brian Harringdd7d7932011-12-23 04:21:33 -0800432 "${FLAGS_trunk}/.repo/manifests.git/config" \
433 repo.reference)
434 if [ -n "${REFERENCE_DIR}" ]; then
435
Brian Harring334050f2012-06-08 17:40:58 -0700436 ALTERNATES="${FLAGS_trunk}/.repo/alternates"
437
438 # Ensure this directory exists ourselves, and has the correct ownership.
David James76764882012-10-24 19:46:29 -0700439 user_mkdir "${ALTERNATES}"
Brian Harring334050f2012-06-08 17:40:58 -0700440
441 unset ALTERNATES
442
Brian Harringdd7d7932011-12-23 04:21:33 -0800443 IFS=$'\n';
David James76764882012-10-24 19:46:29 -0700444 required=( $( sudo -u "${SUDO_USER}" -- \
Mike Frysinger1658c3e2019-08-21 20:24:02 -0400445 "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates" \
Brian Harringdd7d7932011-12-23 04:21:33 -0800446 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
447 unset IFS
448
David James76764882012-10-24 19:46:29 -0700449 setup_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
Brian Harringdd7d7932011-12-23 04:21:33 -0800450 "${CHROOT_TRUNK_DIR}/.repo/alternates"
451
452 # Note that as we're bringing up each referened repo, we also
453 # mount bind an empty directory over its alternates. This is
454 # required to suppress git from tracing through it- we already
455 # specify the required alternates for CHROOT_TRUNK_DIR, no point
456 # in having git try recursing through each on their own.
457 #
458 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
459 # the bind is visible only w/in the chroot.
David James76764882012-10-24 19:46:29 -0700460 user_mkdir ${FLAGS_trunk}/.repo/chroot/empty
Brian Harringdd7d7932011-12-23 04:21:33 -0800461 position=1
462 for x in "${required[@]}"; do
463 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
David James76764882012-10-24 19:46:29 -0700464 setup_mount "${x}" "--bind" "${base}"
Brian Harringdd7d7932011-12-23 04:21:33 -0800465 if [ -e "${x}/.repo/alternates" ]; then
David James76764882012-10-24 19:46:29 -0700466 setup_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
Brian Harringdd7d7932011-12-23 04:21:33 -0800467 "${base}/.repo/alternates"
468 fi
469 position=$(( ${position} + 1 ))
470 done
471 unset required position base
472 fi
473 unset REFERENCE_DIR
474
Brian Harring7b6f3772012-09-23 14:01:13 -0700475 chroot_cache='/var/cache/chromeos-cache'
476 debug "Setting up shared cache dir directory."
David James76764882012-10-24 19:46:29 -0700477 user_mkdir "${FLAGS_cache_dir}"/distfiles/{target,host}
478 user_mkdir "${FLAGS_chroot}/${chroot_cache}"
479 setup_mount "${FLAGS_cache_dir}" "--bind" "${chroot_cache}"
Brian Harring7b6f3772012-09-23 14:01:13 -0700480 # TODO(build): remove this as of 12/01/12.
481 # Because of how distfiles -> cache_dir was deployed, if this isn't
482 # a symlink, we *know* the ondisk pathways aren't compatible- thus
483 # fix it now.
484 distfiles_path="${FLAGS_chroot}/var/cache/distfiles"
485 if [ ! -L "${distfiles_path}" ]; then
486 # While we're at it, ensure the var is exported w/in the chroot; it
487 # won't exist if distfiles isn't a symlink.
Paul Drewsb688cbe2012-10-08 15:33:43 -0700488 p="${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
David James76764882012-10-24 19:46:29 -0700489 rm -rf "${distfiles_path}"
490 ln -s chromeos-cache/distfiles "${distfiles_path}"
491 mkdir -p -m 775 "${p%/*}"
492 echo 'export CHROMEOS_CACHEDIR=${chroot_cache}' > "${p}"
493 chmod 0644 "${p}"
Brian Harring7b6f3772012-09-23 14:01:13 -0700494 fi
Brian Harring7ee892d2012-02-02 09:33:10 -0800495
Aviv Keshet97a35f42014-07-24 12:11:10 -0700496 if [ -d "${SUDO_HOME}/.cidb_creds" ]; then
497 setup_mount "${SUDO_HOME}/.cidb_creds" --bind \
498 "/home/${SUDO_USER}/.cidb_creds"
499 fi
500
Elly Jones7990a062010-09-02 09:23:23 -0400501 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
David James76764882012-10-24 19:46:29 -0700502 if [ -n "${SSH_AUTH_SOCK}" -a -d "${SUDO_HOME}/.ssh" ]; then
Mike Frysingeradd99762014-03-27 13:17:58 -0400503 local target_ssh="/home/${SUDO_USER}/.ssh"
504 TARGET_DIR="${FLAGS_chroot}${target_ssh}"
David James76764882012-10-24 19:46:29 -0700505 user_mkdir "${TARGET_DIR}"
Mike Frysingeradd99762014-03-27 13:17:58 -0400506
507 local known_hosts="${SUDO_HOME}/.ssh/known_hosts"
508 if [[ -e ${known_hosts} ]]; then
509 truncate -s 0 "${TARGET_DIR}/known_hosts"
510 setup_mount "${known_hosts}" --bind "${target_ssh}/known_hosts"
511 fi
David James22dc2ba2012-11-29 15:46:58 -0800512 (
Mike Frysingeradd99762014-03-27 13:17:58 -0400513 # Only copy ~/.ssh/*.pub if they exist. Since we set
David James22dc2ba2012-11-29 15:46:58 -0800514 # nullglob, this needs to happen within a subshell.
515 shopt -s nullglob
Mike Frysingeradd99762014-03-27 13:17:58 -0400516 files=("${SUDO_HOME}"/.ssh/*.pub)
David James22dc2ba2012-11-29 15:46:58 -0800517 if [[ ${#files[@]} -gt 0 ]]; then
518 user_cp "${files[@]}" "${TARGET_DIR}/"
519 fi
520 )
Vadim Bendebury0779f522011-06-12 12:09:16 -0700521 copy_ssh_config "${TARGET_DIR}"
David James76764882012-10-24 19:46:29 -0700522 chown -R ${SUDO_UID}:${SUDO_GID} "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500523
Kevin Cernekeea1718392016-10-16 19:54:57 -0700524 if [ -S "${SSH_AUTH_SOCK}" ]; then
525 touch "${FLAGS_chroot}/tmp/ssh-auth-sock"
526 setup_mount "${SSH_AUTH_SOCK}" "--bind" "/tmp/ssh-auth-sock"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500527 fi
Elly Jones7990a062010-09-02 09:23:23 -0400528 fi
529 fi
530
Marc MERLINfcd84ef2013-03-07 14:34:43 -0800531 if [[ -d "$SUDO_HOME/.subversion" ]]; then
David James76764882012-10-24 19:46:29 -0700532 TARGET="/home/${SUDO_USER}/.subversion"
David James76764882012-10-24 19:46:29 -0700533 setup_mount "${SUDO_HOME}/.subversion" "--bind" "${TARGET}"
Paul Drewsb4605b42012-10-11 23:10:43 -0700534 # Symbolic-link the .subversion directory so sandboxed subversion.class
535 # clients can use it.
Paul Drewsb4605b42012-10-11 23:10:43 -0700536 for d in \
David James76764882012-10-24 19:46:29 -0700537 "${FLAGS_cache_dir}"/distfiles/{host,target}/svn-src/"${SUDO_USER}"; do
Paul Drewsb4605b42012-10-11 23:10:43 -0700538 if [[ ! -L "${d}/.subversion" ]]; then
David James76764882012-10-24 19:46:29 -0700539 rm -rf "${d}/.subversion"
540 user_mkdir "${d}"
541 user_symlink /home/${SUDO_USER}/.subversion "${d}/.subversion"
Paul Drewsb4605b42012-10-11 23:10:43 -0700542 fi
543 done
Mike Frysinger286b5922011-09-28 11:59:53 -0400544 fi
545
David James76764882012-10-24 19:46:29 -0700546 # A reference to the DEPOT_TOOLS path may be passed in by cros_sdk.
547 if [ -n "${DEPOT_TOOLS}" ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400548 debug "Mounting depot_tools"
Brian Harring2499bfb2012-12-18 16:23:31 -0800549 setup_mount "${DEPOT_TOOLS}" --bind "${DEPOT_TOOLS_DIR}"
Mike Frysinger286b5922011-09-28 11:59:53 -0400550 fi
551
Hidehiko Abe63d6c462017-03-02 17:40:54 +0900552 if [[ -n "${FLAGS_goma_dir}" ]]; then
553 debug "Mounting goma"
554 # $HOME/goma is the default directory for goma.
555 # It is used by goma if GOMA_DIR is not provide.
556 setup_mount "${FLAGS_goma_dir}" --bind "/home/${SUDO_USER}/goma"
557 fi
558
559 if [[ -n "${FLAGS_goma_client_json}" ]]; then
560 debug "Mounting service-account-goma-client.json"
561 local dest_path="/creds/service_accounts/service-account-goma-client.json"
562 # setup_mount assumes the target is a directory by default.
563 # So here, touch the file in advance.
564 local mounted_path="${MOUNTED_PATH}${dest_path}"
Hidehiko Abe053a1992017-03-07 16:44:18 +0900565 mkdir -p "$(dirname "${mounted_path}")"
Hidehiko Abe63d6c462017-03-02 17:40:54 +0900566 touch "${mounted_path}"
Hidehiko Abe053a1992017-03-07 16:44:18 +0900567 # Note: Original UID:GID and permission should be inherited even after
Hidehiko Abe63d6c462017-03-02 17:40:54 +0900568 # mounting.
569 setup_mount "${FLAGS_goma_client_json}" --bind "${dest_path}"
570 fi
571
Michael Krebs04c4f732012-09-07 18:31:46 -0700572 # Mount additional directories as specified in .local_mounts file.
573 local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts"
574 if [[ -f ${local_mounts} ]]; then
Gaurav Shahe3cffca2013-11-26 14:45:10 -0800575 info "Mounting local folders"
Michael Krebs04c4f732012-09-07 18:31:46 -0700576 # format: mount_source
577 # or mount_source mount_point
578 # or # comments
579 local mount_source mount_point
580 while read mount_source mount_point; do
581 if [[ -z ${mount_source} ]]; then
582 continue
583 fi
584 # if only source is assigned, use source as mount point.
585 : ${mount_point:=${mount_source}}
586 debug " mounting ${mount_source} on ${mount_point}"
David James76764882012-10-24 19:46:29 -0700587 setup_mount "${mount_source}" "--bind" "${mount_point}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700588 done < <(sed -e 's:#.*::' "${local_mounts}")
589 fi
590
Mike Frysinger9a50b642011-09-20 23:37:14 -0400591 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
592 if [ -z "$CHROME_ROOT" ]; then
593 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
594 2>/dev/null || :)"
595 CHROME_ROOT_AUTO=1
596 fi
597 if [[ -n "$CHROME_ROOT" ]]; then
598 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
Mike Frysinger9d73a852013-12-02 19:47:08 -0500599 error "Not mounting chrome source: could not find CHROME_ROOT/src dir."
600 error "Full path we tried: ${CHROME_ROOT}/src"
David James76764882012-10-24 19:46:29 -0700601 rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
Mike Frysinger9a50b642011-09-20 23:37:14 -0400602 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
603 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800604 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400605 else
606 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
David James76764882012-10-24 19:46:29 -0700607 echo $CHROME_ROOT > "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
608 setup_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Xiyuan Xiaa6e21ae2018-03-28 08:47:23 -0700609 setup_gclient_cache_dir_mount "$CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800610 fi
611 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800612
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400613 # Install fuse module. Skip modprobe when possible for slight
614 # speed increase when initializing the env.
615 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
David James76764882012-10-24 19:46:29 -0700616 modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200617 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700618 fi
619
Allen Webb1add6952020-04-02 11:45:00 -0700620 # Bind mount the host kernel modules read-only so modprobe can be used
621 # inside the chroot for things like usbip-host.
622 local modules_dir="/lib/modules"
623 if [ -d "${modules_dir}" ]; then
624 setup_mount "${modules_dir}" "--bind -o ro" "${modules_dir}"
625 fi
626
Mike Frysinger926a4b92012-06-27 15:22:28 -0400627 # Fix permissions on ccache tree. If this is a fresh chroot, then they
628 # might not be set up yet. Or if the user manually `rm -rf`-ed things,
629 # we need to reset it. Otherwise, gcc itself takes care of fixing things
630 # on demand, but only when it updates.
631 ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache"
632 if [[ ! -d ${ccache_dir} ]]; then
David James76764882012-10-24 19:46:29 -0700633 mkdir -p -m 2775 "${ccache_dir}"
Mike Frysinger926a4b92012-06-27 15:22:28 -0400634 fi
Mike Frysingerba609912015-05-20 00:44:25 -0400635 (
636 find -H "${ccache_dir}" '(' -type d -a '!' -perm 2775 ')' \
637 -exec chmod 2775 {} +
638 find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} +
639 # These settings are kept in sync with the gcc ebuild.
640 chroot "${FLAGS_chroot}" env CCACHE_DIR=/var/cache/distfiles/ccache \
641 CCACHE_UMASK=002 ccache -F 0 -M 11G >/dev/null
642 ) &
David James342f1562011-07-15 15:21:18 -0700643
Matt Tennant298f61a2012-06-25 21:54:33 -0700644 # Certain files get copied into the chroot when entering.
645 for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do
David James76764882012-10-24 19:46:29 -0700646 copy_into_chroot_if_exists "${SUDO_HOME}/${fn}" "/home/${SUDO_USER}/${fn}"
Matt Tennantf7c9e772012-02-08 17:06:26 -0800647 done
Prathmesh Prabhu74ebbbd2015-03-17 13:50:29 -0700648 setup_git
Peter Mayo4411efe2012-09-21 04:41:27 -0400649 promote_api_keys
Matt Tennantd4316fe2011-08-30 12:06:42 -0700650
Dale Curtis98631732011-05-05 16:16:59 -0700651 # Fix permissions on shared memory to allow non-root users access to POSIX
Josh McSavaney8477dad2016-04-05 13:43:26 -0700652 # semaphores. Take special care to only change the permissions on the
653 # directory and not all of its contents.
654 chmod 1777 "${FLAGS_chroot}/dev/shm"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400655
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800656 # gsutil uses boto config to store settings and credentials. Copy
657 # user's own boto file into the chroot if it exists. Also copy it
658 # to /root for sudoed invocations.
659 chroot_user_boto="${FLAGS_chroot}/home/${SUDO_USER}/.boto"
660 chroot_root_boto="${FLAGS_chroot}/root/.boto"
661 if [ -f ${SUDO_HOME}/.boto ]; then
662 # Pass --remote-destination to overwrite a symlink.
663 user_cp "--remove-destination" "${SUDO_HOME}/.boto" "$chroot_user_boto"
Frank Henigman696ce082014-03-27 21:21:55 -0400664 cp "--remove-destination" "$chroot_user_boto" "$chroot_root_boto"
David Rileyb6e9d862016-02-25 16:58:02 -0800665 elif [ -f "/etc/boto.cfg" ]; then
666 # For GCE instances, the non-chroot .boto file is not deployed so
667 # use the system /etc/boto.cfg if it exists.
668 user_cp "--remove-destination" "/etc/boto.cfg" "$chroot_user_boto"
669 cp "--remove-destination" "$chroot_user_boto" "$chroot_root_boto"
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800670 fi
671
672 # If user doesn't have a boto file, check if the private overlays
673 # are installed and use those credentials.
Gilad Arnold264f64d2012-09-06 14:01:45 -0700674 boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto'
675 if [ -s "${FLAGS_trunk}/${boto}" ]; then
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800676 if [ ! -e "$chroot_user_boto" ]; then
677 user_symlink "trunk/${boto}" "$chroot_user_boto"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400678 fi
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800679 if [ ! -e "$chroot_root_boto" ]; then
680 ln -sf "${CHROOT_TRUNK_DIR}/${boto}" "$chroot_root_boto"
Gilad Arnold264f64d2012-09-06 14:01:45 -0700681 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400682 fi
683
684 # Have found a few chroots where ~/.gsutil is owned by root:root, probably
685 # as a result of old gsutil or tools. This causes permission errors when
686 # gsutil cp tries to create its cache files, so ensure the user can
687 # actually write to their directory.
David James76764882012-10-24 19:46:29 -0700688 gsutil_dir="${FLAGS_chroot}/home/${SUDO_USER}/.gsutil"
689 if [ -d "${gsutil_dir}" ]; then
690 chown -R ${SUDO_UID}:${SUDO_GID} "${gsutil_dir}"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400691 fi
David James546747b2010-03-23 15:19:43 -0700692 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000693}
694
Josh Triplettd6a13bf2014-06-13 15:36:52 -0700695# Translate C.UTF-8 into something we support. Remove this when our glibc
696# starts supporting C.UTF-8. https://bugzilla.redhat.com/show_bug.cgi?id=902094
697for var in LANG \
698 LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
699 LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME}; do
700 if [[ ${!var} =~ C\.[Uu][Tt][Ff]-?8 ]]; then
701 if [[ ${var} == LC_COLLATE ]]; then
702 export LC_COLLATE=C
703 else
704 export ${var}=en_US.UTF-8
705 fi
706 fi
707done
708
Mike Frysinger1e002932020-04-10 17:21:46 -0400709check_locale
710setup_env
711
712CHROOT_PASSTHRU=(
713 "BUILDBOT_BUILD=$FLAGS_build_number"
714 "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
715 "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
716
717 # The default ~/.bash_profile in chroot will cd to $CHROOT_CWD instead of
718 # ~/trunk/src/script if that environment variable is set.
719 "CHROOT_CWD=${FLAGS_working_dir}"
720)
721
Kevin Cernekeea1718392016-10-16 19:54:57 -0700722# Needs to be set here because setup_env runs in a subshell.
723[ -S "${FLAGS_chroot}/tmp/ssh-auth-sock" ] && SSH_AUTH_SOCK=/tmp/ssh-auth-sock
724
David James76764882012-10-24 19:46:29 -0700725# Add the whitelisted environment variables to CHROOT_PASSTHRU.
726load_environment_whitelist
727for var in "${ENVIRONMENT_WHITELIST[@]}" ; do
Brian Harring06d3c2e2012-08-23 07:35:43 -0700728 [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" )
Mario Limonciello7052ae12011-05-05 12:00:49 -0500729done
730
Paul Drews67d9ef12013-03-20 08:47:47 -0700731# Set up GIT_PROXY_COMMAND so git:// URLs automatically work behind a proxy.
732if [[ -n "${all_proxy}" || -n "${https_proxy}" || -n "${http_proxy}" ]]; then
733 CHROOT_PASSTHRU+=(
734 "GIT_PROXY_COMMAND=${CHROOT_TRUNK_DIR}/src/scripts/bin/proxy-gw"
735 )
736fi
737
derat@google.com4e7a92b2009-11-21 23:44:14 +0000738# Run command or interactive shell. Also include the non-chrooted path to
739# the source trunk for scripts that may need to print it (e.g.
740# build_image.sh).
Brian Harring35767822012-02-01 23:50:45 -0800741
742if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
743 cmd=( /bin/bash -l -c 'env "$@"' -- )
744elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
745 # Complain that sudo is missing.
746 error "Failing since the chroot lacks sudo."
747 error "Requested enter_chroot command was: $@"
748 exit 127
749else
David James76764882012-10-24 19:46:29 -0700750 cmd=( sudo -i -u "${SUDO_USER}" )
Brian Harring35767822012-02-01 23:50:45 -0800751fi
752
David James76764882012-10-24 19:46:29 -0700753cmd+=( "${CHROOT_PASSTHRU[@]}" "$@" )
754exec chroot "${FLAGS_chroot}" "${cmd[@]}"