blob: a099b34ea76b6023efcecd77524cf7b6174f620e [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."
rspangler@google.comd74220d2009-10-09 20:56:14 +000029
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070030DEFINE_boolean official_build $FLAGS_FALSE \
31 "Set CHROMEOS_OFFICIAL=1 for release builds."
Elly Jones7990a062010-09-02 09:23:23 -040032DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
Brian Harring35767822012-02-01 23:50:45 -080033DEFINE_boolean early_make_chroot $FLAGS_FALSE \
34 "Internal flag. If set, the command is run as root without sudo."
Don Garrettad3f0592011-02-04 14:59:56 -080035DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000036
37# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080038FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000039
40One or more VAR=value pairs can be specified to export variables into
41the chroot environment. For example:
42
43 $0 FOO=bar BAZ=bel
44
Doug Anderson9362fa82010-12-16 14:44:12 -080045If [-- command] is present, runs the command inside the chroot,
David James76764882012-10-24 19:46:29 -070046after changing directory to /${SUDO_USER}/trunk/src/scripts. Note that neither
Doug Anderson9362fa82010-12-16 14:44:12 -080047the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000048
Doug Anderson9362fa82010-12-16 14:44:12 -080049 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000050
51Otherwise, provides an interactive shell.
52"
53
Peter Mayo4411efe2012-09-21 04:41:27 -040054CROS_LOG_PREFIX=cros_sdk:enter_chroot
David James76764882012-10-24 19:46:29 -070055SUDO_HOME=$(eval echo ~${SUDO_USER})
Peter Mayo4411efe2012-09-21 04:41:27 -040056
Don Garrettad3f0592011-02-04 14:59:56 -080057# Version of info from common.sh that only echos if --verbose is set.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040058debug() {
Don Garrettad3f0592011-02-04 14:59:56 -080059 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
David Rochberg351a76f2011-02-16 11:14:00 -050060 info "$*"
Don Garrettad3f0592011-02-04 14:59:56 -080061 fi
62}
63
rspangler@google.comd74220d2009-10-09 20:56:14 +000064# Parse command line flags
65FLAGS "$@" || exit 1
66eval set -- "${FLAGS_ARGV}"
67
Greg Spencer798d75f2011-02-01 22:04:49 -080068if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
David McMahon857dbb52009-12-09 18:21:05 -080069 CHROMEOS_OFFICIAL=1
70fi
71
Brian Harring7b6f3772012-09-23 14:01:13 -070072[ -z "${FLAGS_cache_dir}" ] && \
73 die "--cache_dir is required"
Brian Harring7ee892d2012-02-02 09:33:10 -080074
rspangler@google.comd74220d2009-10-09 20:56:14 +000075# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080076# so will die prematurely if 'switch_to_strict_mode' is specified before now.
rspangler@google.comd74220d2009-10-09 20:56:14 +000077# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080078switch_to_strict_mode
rspangler@google.comd74220d2009-10-09 20:56:14 +000079
Matt Tennant298f61a2012-06-25 21:54:33 -070080# These config files are to be copied into chroot if they exist in home dir.
81FILES_TO_COPY_TO_CHROOT=(
82 .gdata_cred.txt # User/password for Google Docs on chromium.org
83 .gdata_token # Auth token for Google Docs on chromium.org
84 .disable_build_stats_upload # Presence of file disables command stats upload
Peter Mayo45ebc4c2013-01-28 15:49:06 -050085 .netrc # May contain required source fetching credentials
Matt Tennant298f61a2012-06-25 21:54:33 -070086)
87
Sean Parent2898f752010-05-25 15:06:33 -070088INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080089CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070090FUSE_DEVICE="/dev/fuse"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080091
Mike Frysinger01c205d2013-03-22 00:59:58 -040092# We can't use /var/lock because that might be a symlink to /run/lock outside
93# of the chroot. Or /run on the host system might not exist.
94LOCKFILE="${FLAGS_chroot}/.enter_chroot.lock"
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -040095MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger286b5922011-09-28 11:59:53 -040096
Brian Harring2499bfb2012-12-18 16:23:31 -080097# Reset the depot tools/internal trunk pathways to what they'll
98# be w/in the chroot.
99set_chroot_trunk_dir "${FLAGS_chroot}"
100
101
David James76764882012-10-24 19:46:29 -0700102setup_mount() {
David Rochberg351a76f2011-02-16 11:14:00 -0500103 # If necessary, mount $source in the host FS at $target inside the
David James76764882012-10-24 19:46:29 -0700104 # chroot directory with $mount_args. We don't write to /etc/mtab because
105 # these mounts are all contained within an unshare and are therefore
106 # inaccessible to other namespaces (e.g. the host desktop system).
David Rochberg351a76f2011-02-16 11:14:00 -0500107 local source="$1"
David James76764882012-10-24 19:46:29 -0700108 local mount_args="-n $2"
David Rochberg351a76f2011-02-16 11:14:00 -0500109 local target="$3"
110
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400111 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500112
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400113 case " ${MOUNT_CACHE} " in
114 *" ${mounted_path} "*)
Mike Frysinger2a970592011-09-20 22:49:01 -0400115 # Already mounted!
116 ;;
117 *)
Mike Frysingeradd99762014-03-27 13:17:58 -0400118 # If it doesn't exist, assume they want a dir. But don't blindly run
119 # it all the time in case they're trying to bind mount a file.
120 if [[ ! -e ${mounted_path} ]]; then
121 mkdir -p "${mounted_path}"
122 fi
Michael Krebs04c4f732012-09-07 18:31:46 -0700123 # The args are left unquoted on purpose.
124 if [[ -n ${source} ]]; then
David James76764882012-10-24 19:46:29 -0700125 mount ${mount_args} "${source}" "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700126 else
David James76764882012-10-24 19:46:29 -0700127 mount ${mount_args} "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700128 fi
Mike Frysinger2a970592011-09-20 22:49:01 -0400129 ;;
130 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500131}
132
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400133copy_ssh_config() {
Vadim Bendebury0779f522011-06-12 12:09:16 -0700134 # Copy user .ssh/config into the chroot filtering out strings not supported
135 # by the chroot ssh. The chroot .ssh directory is passed in as the first
136 # parameter.
137
138 # ssh options to filter out. The entire strings containing these substrings
139 # will be deleted before copying.
140 local bad_options=(
Matt Tennant63c3cc52011-11-22 11:23:06 -0800141 'UseProxyIf'
142 'GSSAPIAuthentication'
143 'GSSAPIKeyExchange'
144 'ProxyUseFdpass'
Vadim Bendebury0779f522011-06-12 12:09:16 -0700145 )
David James76764882012-10-24 19:46:29 -0700146 local sshc="${SUDO_HOME}/.ssh/config"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700147 local chroot_ssh_dir="${1}"
148 local filter
149 local option
150
David James22dc2ba2012-11-29 15:46:58 -0800151 if ! user_cp "${sshc}" "${chroot_ssh_dir}/config.orig" 2>/dev/null; then
Vadim Bendebury0779f522011-06-12 12:09:16 -0700152 return # Nothing to copy.
153 fi
154
155 for option in "${bad_options[@]}"
156 do
157 if [ -z "${filter}" ]; then
158 filter="${option}"
159 else
160 filter+="\\|${option}"
161 fi
162 done
163
David James22dc2ba2012-11-29 15:46:58 -0800164 sed "/^.*\(${filter}\).*$/d" "${chroot_ssh_dir}/config.orig" | \
David James76764882012-10-24 19:46:29 -0700165 user_clobber "${chroot_ssh_dir}/config"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700166}
167
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400168copy_into_chroot_if_exists() {
Matt Tennantf7c9e772012-02-08 17:06:26 -0800169 # $1 is file path outside of chroot to copy to path $2 inside chroot.
Frank Henigman784e76c2013-08-30 10:30:12 -0400170 [ -e "$1" ] && user_cp -p "$1" "${FLAGS_chroot}/$2"
Matt Tennantf7c9e772012-02-08 17:06:26 -0800171}
172
Peter Mayo4411efe2012-09-21 04:41:27 -0400173# Usage: promote_api_keys
174# This takes care of getting the developer API keys into the chroot where
175# chrome can build with them. It needs to take it from the places a dev
176# is likely to put them, and recognize that older chroots may or may not
177# have been used since the concept of keys got added, as well as before
178# and after the developer decding to grab his own keys.
179promote_api_keys() {
David James76764882012-10-24 19:46:29 -0700180 local destination="${FLAGS_chroot}/home/${SUDO_USER}/.googleapikeys"
Peter Mayo4411efe2012-09-21 04:41:27 -0400181 # Don't disturb existing keys. They could be set differently
182 if [[ -s "${destination}" ]]; then
183 return 0
184 fi
David James76764882012-10-24 19:46:29 -0700185 if [[ -r "${SUDO_HOME}/.googleapikeys" ]]; then
186 cp -p "${SUDO_HOME}/.googleapikeys" "${destination}"
Peter Mayo4411efe2012-09-21 04:41:27 -0400187 if [[ -s "${destination}" ]] ; then
188 info "Copied Google API keys into chroot."
189 fi
David James76764882012-10-24 19:46:29 -0700190 elif [[ -r "${SUDO_HOME}/.gyp/include.gypi" ]]; then
Peter Mayo4411efe2012-09-21 04:41:27 -0400191 local NAME="('google_(api_key|default_client_(id|secret))')"
192 local WS="[[:space:]]*"
193 local CONTENTS="('[^\\\\']*')"
194 sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \
David James76764882012-10-24 19:46:29 -0700195 "${SUDO_HOME}/.gyp/include.gypi" | user_clobber "${destination}"
Peter Mayo4411efe2012-09-21 04:41:27 -0400196 if [[ -s "${destination}" ]]; then
197 info "Put discovered Google API keys into chroot."
198 fi
199 fi
200}
201
Mike Frysinger4f2d5cc2013-06-09 23:34:52 -0400202generate_locales() {
203 # Make sure user's requested locales are available
204 # http://crosbug.com/19139
205 # And make sure en_US{,.UTF-8} are always available as
206 # that what buildbot forces internally
207 local l locales gen_locales=()
208
209 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
210 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
211 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
212 sort -u | sed '/^C$/d')
213 for l in ${locales}; do
214 if [[ ${l} == *.* ]]; then
215 enc=${l#*.}
216 else
217 enc="ISO-8859-1"
218 fi
219 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
220 utf8) enc="UTF-8";;
221 esac
222 gen_locales+=("${l} ${enc}")
223 done
224 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
225 # Force LC_ALL=C to workaround slow string parsing in bash
226 # with long multibyte strings. Newer setups have this fixed,
227 # but locale-gen doesn't need to be run in any locale in the
228 # first place, so just go with C to keep it fast.
229 chroot "${FLAGS_chroot}" env LC_ALL=C locale-gen -q -u \
230 -G "$(printf '%s\n' "${gen_locales[@]}")"
231 fi
232}
233
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400234setup_env() {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800235 (
236 flock 200
rspangler@google.comd74220d2009-10-09 20:56:14 +0000237
David James76764882012-10-24 19:46:29 -0700238 # Make the lockfile writable for backwards compatibility.
239 chown ${SUDO_UID}:${SUDO_GID} "${LOCKFILE}"
240
241 # Refresh /etc/resolv.conf and /etc/hosts in the chroot.
242 install -C -m644 /etc/resolv.conf ${FLAGS_chroot}/etc/resolv.conf
243 install -C -m644 /etc/hosts ${FLAGS_chroot}/etc/hosts
David James412b9022011-07-15 19:54:16 -0700244
Don Garretta0e7ea12011-02-07 18:39:59 -0800245 debug "Mounting chroot environment."
Steev Klimaszewskia10974c2013-11-30 20:38:34 -0600246 mount --make-rslave /
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400247 MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts))
David James76764882012-10-24 19:46:29 -0700248 setup_mount none "-t proc" /proc
249 setup_mount none "-t sysfs" /sys
Gaurav Shah365b7062013-12-03 18:12:58 -0800250 if grep -q binfmt_misc /proc/filesystems; then
251 setup_mount binfmt_misc "-t binfmt_misc" /proc/sys/fs/binfmt_misc
252 fi
David James76764882012-10-24 19:46:29 -0700253 setup_mount /dev "--bind" /dev
Mike Frysingere22afc72013-04-12 13:47:01 -0400254 setup_mount /dev/pts "--bind" /dev/pts
David James482fb292013-03-10 21:06:54 -0700255 if [[ -d /run ]]; then
David James76764882012-10-24 19:46:29 -0700256 setup_mount /run "--bind" /run
David James482fb292013-03-10 21:06:54 -0700257 if [[ -d /run/shm && ! -L /run/shm ]]; then
David James76764882012-10-24 19:46:29 -0700258 setup_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700259 fi
260 fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800261
Mike Frysinger4f2d5cc2013-06-09 23:34:52 -0400262 # Do this early as it's slow and only needs basic mounts (above).
263 generate_locales &
264
Michael Spang15144f42013-04-02 17:42:55 -0400265 setup_mount "${FLAGS_trunk}" "--rbind" "${CHROOT_TRUNK_DIR}"
Chris Sosa389634d2012-09-05 19:56:53 -0700266
Brian Harringdd7d7932011-12-23 04:21:33 -0800267 debug "Setting up referenced repositories if required."
268 REFERENCE_DIR=$(git config --file \
269 "${FLAGS_trunk}/.repo/manifests.git/config" \
270 repo.reference)
271 if [ -n "${REFERENCE_DIR}" ]; then
272
Brian Harring334050f2012-06-08 17:40:58 -0700273 ALTERNATES="${FLAGS_trunk}/.repo/alternates"
274
275 # Ensure this directory exists ourselves, and has the correct ownership.
David James76764882012-10-24 19:46:29 -0700276 user_mkdir "${ALTERNATES}"
Brian Harring334050f2012-06-08 17:40:58 -0700277
278 unset ALTERNATES
279
Brian Harringdd7d7932011-12-23 04:21:33 -0800280 IFS=$'\n';
David James76764882012-10-24 19:46:29 -0700281 required=( $( sudo -u "${SUDO_USER}" -- \
282 "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \
Brian Harringdd7d7932011-12-23 04:21:33 -0800283 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
284 unset IFS
285
David James76764882012-10-24 19:46:29 -0700286 setup_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
Brian Harringdd7d7932011-12-23 04:21:33 -0800287 "${CHROOT_TRUNK_DIR}/.repo/alternates"
288
289 # Note that as we're bringing up each referened repo, we also
290 # mount bind an empty directory over its alternates. This is
291 # required to suppress git from tracing through it- we already
292 # specify the required alternates for CHROOT_TRUNK_DIR, no point
293 # in having git try recursing through each on their own.
294 #
295 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
296 # the bind is visible only w/in the chroot.
David James76764882012-10-24 19:46:29 -0700297 user_mkdir ${FLAGS_trunk}/.repo/chroot/empty
Brian Harringdd7d7932011-12-23 04:21:33 -0800298 position=1
299 for x in "${required[@]}"; do
300 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
David James76764882012-10-24 19:46:29 -0700301 setup_mount "${x}" "--bind" "${base}"
Brian Harringdd7d7932011-12-23 04:21:33 -0800302 if [ -e "${x}/.repo/alternates" ]; then
David James76764882012-10-24 19:46:29 -0700303 setup_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
Brian Harringdd7d7932011-12-23 04:21:33 -0800304 "${base}/.repo/alternates"
305 fi
306 position=$(( ${position} + 1 ))
307 done
308 unset required position base
309 fi
310 unset REFERENCE_DIR
311
Brian Harring7b6f3772012-09-23 14:01:13 -0700312 chroot_cache='/var/cache/chromeos-cache'
313 debug "Setting up shared cache dir directory."
David James76764882012-10-24 19:46:29 -0700314 user_mkdir "${FLAGS_cache_dir}"/distfiles/{target,host}
315 user_mkdir "${FLAGS_chroot}/${chroot_cache}"
316 setup_mount "${FLAGS_cache_dir}" "--bind" "${chroot_cache}"
Brian Harring7b6f3772012-09-23 14:01:13 -0700317 # TODO(build): remove this as of 12/01/12.
318 # Because of how distfiles -> cache_dir was deployed, if this isn't
319 # a symlink, we *know* the ondisk pathways aren't compatible- thus
320 # fix it now.
321 distfiles_path="${FLAGS_chroot}/var/cache/distfiles"
322 if [ ! -L "${distfiles_path}" ]; then
323 # While we're at it, ensure the var is exported w/in the chroot; it
324 # won't exist if distfiles isn't a symlink.
Paul Drewsb688cbe2012-10-08 15:33:43 -0700325 p="${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
David James76764882012-10-24 19:46:29 -0700326 rm -rf "${distfiles_path}"
327 ln -s chromeos-cache/distfiles "${distfiles_path}"
328 mkdir -p -m 775 "${p%/*}"
329 echo 'export CHROMEOS_CACHEDIR=${chroot_cache}' > "${p}"
330 chmod 0644 "${p}"
Brian Harring7b6f3772012-09-23 14:01:13 -0700331 fi
Brian Harring7ee892d2012-02-02 09:33:10 -0800332
Elly Jones7990a062010-09-02 09:23:23 -0400333 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
Mike Frysinger93a2eca2012-11-30 14:00:35 -0500334 # Clean up previous ssh agents.
335 rmdir "${FLAGS_chroot}"/tmp/ssh-* 2>/dev/null
336
David James76764882012-10-24 19:46:29 -0700337 if [ -n "${SSH_AUTH_SOCK}" -a -d "${SUDO_HOME}/.ssh" ]; then
Mike Frysingeradd99762014-03-27 13:17:58 -0400338 local target_ssh="/home/${SUDO_USER}/.ssh"
339 TARGET_DIR="${FLAGS_chroot}${target_ssh}"
David James76764882012-10-24 19:46:29 -0700340 user_mkdir "${TARGET_DIR}"
Mike Frysingeradd99762014-03-27 13:17:58 -0400341
342 local known_hosts="${SUDO_HOME}/.ssh/known_hosts"
343 if [[ -e ${known_hosts} ]]; then
344 truncate -s 0 "${TARGET_DIR}/known_hosts"
345 setup_mount "${known_hosts}" --bind "${target_ssh}/known_hosts"
346 fi
David James22dc2ba2012-11-29 15:46:58 -0800347 (
Mike Frysingeradd99762014-03-27 13:17:58 -0400348 # Only copy ~/.ssh/*.pub if they exist. Since we set
David James22dc2ba2012-11-29 15:46:58 -0800349 # nullglob, this needs to happen within a subshell.
350 shopt -s nullglob
Mike Frysingeradd99762014-03-27 13:17:58 -0400351 files=("${SUDO_HOME}"/.ssh/*.pub)
David James22dc2ba2012-11-29 15:46:58 -0800352 if [[ ${#files[@]} -gt 0 ]]; then
353 user_cp "${files[@]}" "${TARGET_DIR}/"
354 fi
355 )
Vadim Bendebury0779f522011-06-12 12:09:16 -0700356 copy_ssh_config "${TARGET_DIR}"
David James76764882012-10-24 19:46:29 -0700357 chown -R ${SUDO_UID}:${SUDO_GID} "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500358
359 # Don't try to bind mount the ssh agent dir if it has gone stale.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400360 ASOCK=${SSH_AUTH_SOCK%/*}
Mike Frysinger9de707f2011-12-12 14:21:44 -0500361 if [ -d "${ASOCK}" ]; then
David James76764882012-10-24 19:46:29 -0700362 setup_mount "${ASOCK}" "--bind" "${ASOCK}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500363 fi
Elly Jones7990a062010-09-02 09:23:23 -0400364 fi
365 fi
366
Marc MERLINfcd84ef2013-03-07 14:34:43 -0800367 if [[ -d "$SUDO_HOME/.subversion" ]]; then
David James76764882012-10-24 19:46:29 -0700368 TARGET="/home/${SUDO_USER}/.subversion"
David James76764882012-10-24 19:46:29 -0700369 setup_mount "${SUDO_HOME}/.subversion" "--bind" "${TARGET}"
Paul Drewsb4605b42012-10-11 23:10:43 -0700370 # Symbolic-link the .subversion directory so sandboxed subversion.class
371 # clients can use it.
Paul Drewsb4605b42012-10-11 23:10:43 -0700372 for d in \
David James76764882012-10-24 19:46:29 -0700373 "${FLAGS_cache_dir}"/distfiles/{host,target}/svn-src/"${SUDO_USER}"; do
Paul Drewsb4605b42012-10-11 23:10:43 -0700374 if [[ ! -L "${d}/.subversion" ]]; then
David James76764882012-10-24 19:46:29 -0700375 rm -rf "${d}/.subversion"
376 user_mkdir "${d}"
377 user_symlink /home/${SUDO_USER}/.subversion "${d}/.subversion"
Paul Drewsb4605b42012-10-11 23:10:43 -0700378 fi
379 done
Mike Frysinger286b5922011-09-28 11:59:53 -0400380 fi
381
David James76764882012-10-24 19:46:29 -0700382 # A reference to the DEPOT_TOOLS path may be passed in by cros_sdk.
383 if [ -n "${DEPOT_TOOLS}" ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400384 debug "Mounting depot_tools"
Brian Harring2499bfb2012-12-18 16:23:31 -0800385 setup_mount "${DEPOT_TOOLS}" --bind "${DEPOT_TOOLS_DIR}"
Mike Frysinger286b5922011-09-28 11:59:53 -0400386 fi
387
Michael Krebs04c4f732012-09-07 18:31:46 -0700388 # Mount additional directories as specified in .local_mounts file.
389 local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts"
390 if [[ -f ${local_mounts} ]]; then
Gaurav Shahe3cffca2013-11-26 14:45:10 -0800391 info "Mounting local folders"
Michael Krebs04c4f732012-09-07 18:31:46 -0700392 # format: mount_source
393 # or mount_source mount_point
394 # or # comments
395 local mount_source mount_point
396 while read mount_source mount_point; do
397 if [[ -z ${mount_source} ]]; then
398 continue
399 fi
400 # if only source is assigned, use source as mount point.
401 : ${mount_point:=${mount_source}}
402 debug " mounting ${mount_source} on ${mount_point}"
David James76764882012-10-24 19:46:29 -0700403 setup_mount "${mount_source}" "--bind" "${mount_point}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700404 done < <(sed -e 's:#.*::' "${local_mounts}")
405 fi
406
Mike Frysinger9a50b642011-09-20 23:37:14 -0400407 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
408 if [ -z "$CHROME_ROOT" ]; then
409 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
410 2>/dev/null || :)"
411 CHROME_ROOT_AUTO=1
412 fi
413 if [[ -n "$CHROME_ROOT" ]]; then
414 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
Mike Frysinger9d73a852013-12-02 19:47:08 -0500415 error "Not mounting chrome source: could not find CHROME_ROOT/src dir."
416 error "Full path we tried: ${CHROME_ROOT}/src"
David James76764882012-10-24 19:46:29 -0700417 rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
Mike Frysinger9a50b642011-09-20 23:37:14 -0400418 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
419 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800420 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400421 else
422 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
David James76764882012-10-24 19:46:29 -0700423 echo $CHROME_ROOT > "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
424 setup_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800425 fi
426 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800427
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400428 # Install fuse module. Skip modprobe when possible for slight
429 # speed increase when initializing the env.
430 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
David James76764882012-10-24 19:46:29 -0700431 modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200432 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700433 fi
434
Mike Frysinger926a4b92012-06-27 15:22:28 -0400435 # Fix permissions on ccache tree. If this is a fresh chroot, then they
436 # might not be set up yet. Or if the user manually `rm -rf`-ed things,
437 # we need to reset it. Otherwise, gcc itself takes care of fixing things
438 # on demand, but only when it updates.
439 ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache"
440 if [[ ! -d ${ccache_dir} ]]; then
David James76764882012-10-24 19:46:29 -0700441 mkdir -p -m 2775 "${ccache_dir}"
Mike Frysinger926a4b92012-06-27 15:22:28 -0400442 fi
David James76764882012-10-24 19:46:29 -0700443 find -H "${ccache_dir}" -type d -exec chmod 2775 {} + &
444 find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} + &
David James342f1562011-07-15 15:21:18 -0700445
Matt Tennant298f61a2012-06-25 21:54:33 -0700446 # Certain files get copied into the chroot when entering.
447 for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do
David James76764882012-10-24 19:46:29 -0700448 copy_into_chroot_if_exists "${SUDO_HOME}/${fn}" "/home/${SUDO_USER}/${fn}"
Matt Tennantf7c9e772012-02-08 17:06:26 -0800449 done
Peter Mayo4411efe2012-09-21 04:41:27 -0400450 promote_api_keys
Matt Tennantd4316fe2011-08-30 12:06:42 -0700451
Dale Curtis98631732011-05-05 16:16:59 -0700452 # Fix permissions on shared memory to allow non-root users access to POSIX
David James76764882012-10-24 19:46:29 -0700453 # semaphores.
454 chmod -R 777 "${FLAGS_chroot}/dev/shm"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400455
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800456 # gsutil uses boto config to store settings and credentials. Copy
457 # user's own boto file into the chroot if it exists. Also copy it
458 # to /root for sudoed invocations.
459 chroot_user_boto="${FLAGS_chroot}/home/${SUDO_USER}/.boto"
460 chroot_root_boto="${FLAGS_chroot}/root/.boto"
461 if [ -f ${SUDO_HOME}/.boto ]; then
462 # Pass --remote-destination to overwrite a symlink.
463 user_cp "--remove-destination" "${SUDO_HOME}/.boto" "$chroot_user_boto"
464 user_cp "--remove-destination" "${SUDO_HOME}/.boto" "$chroot_root_boto"
465 fi
466
467 # If user doesn't have a boto file, check if the private overlays
468 # are installed and use those credentials.
Gilad Arnold264f64d2012-09-06 14:01:45 -0700469 boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto'
470 if [ -s "${FLAGS_trunk}/${boto}" ]; then
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800471 if [ ! -e "$chroot_user_boto" ]; then
472 user_symlink "trunk/${boto}" "$chroot_user_boto"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400473 fi
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800474 if [ ! -e "$chroot_root_boto" ]; then
475 ln -sf "${CHROOT_TRUNK_DIR}/${boto}" "$chroot_root_boto"
Gilad Arnold264f64d2012-09-06 14:01:45 -0700476 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400477 fi
478
479 # Have found a few chroots where ~/.gsutil is owned by root:root, probably
480 # as a result of old gsutil or tools. This causes permission errors when
481 # gsutil cp tries to create its cache files, so ensure the user can
482 # actually write to their directory.
David James76764882012-10-24 19:46:29 -0700483 gsutil_dir="${FLAGS_chroot}/home/${SUDO_USER}/.gsutil"
484 if [ -d "${gsutil_dir}" ]; then
485 chown -R ${SUDO_UID}:${SUDO_GID} "${gsutil_dir}"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400486 fi
David James546747b2010-03-23 15:19:43 -0700487 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000488}
489
rspangler@google.comd74220d2009-10-09 20:56:14 +0000490setup_env
491
Brian Harring35767822012-02-01 23:50:45 -0800492CHROOT_PASSTHRU=(
493 "BUILDBOT_BUILD=$FLAGS_build_number"
Brian Harring35767822012-02-01 23:50:45 -0800494 "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
Brian Harring35767822012-02-01 23:50:45 -0800495 "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
Brian Harring35767822012-02-01 23:50:45 -0800496)
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700497
David James76764882012-10-24 19:46:29 -0700498# Add the whitelisted environment variables to CHROOT_PASSTHRU.
499load_environment_whitelist
500for var in "${ENVIRONMENT_WHITELIST[@]}" ; do
Brian Harring06d3c2e2012-08-23 07:35:43 -0700501 [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" )
Mario Limonciello7052ae12011-05-05 12:00:49 -0500502done
503
Paul Drews67d9ef12013-03-20 08:47:47 -0700504# Set up GIT_PROXY_COMMAND so git:// URLs automatically work behind a proxy.
505if [[ -n "${all_proxy}" || -n "${https_proxy}" || -n "${http_proxy}" ]]; then
506 CHROOT_PASSTHRU+=(
507 "GIT_PROXY_COMMAND=${CHROOT_TRUNK_DIR}/src/scripts/bin/proxy-gw"
508 )
509fi
510
derat@google.com4e7a92b2009-11-21 23:44:14 +0000511# Run command or interactive shell. Also include the non-chrooted path to
512# the source trunk for scripts that may need to print it (e.g.
513# build_image.sh).
Brian Harring35767822012-02-01 23:50:45 -0800514
515if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
516 cmd=( /bin/bash -l -c 'env "$@"' -- )
517elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
518 # Complain that sudo is missing.
519 error "Failing since the chroot lacks sudo."
520 error "Requested enter_chroot command was: $@"
521 exit 127
522else
David James76764882012-10-24 19:46:29 -0700523 cmd=( sudo -i -u "${SUDO_USER}" )
Brian Harring35767822012-02-01 23:50:45 -0800524fi
525
David James76764882012-10-24 19:46:29 -0700526cmd+=( "${CHROOT_PASSTHRU[@]}" "$@" )
527exec chroot "${FLAGS_chroot}" "${cmd[@]}"