blob: 4b16a14fc4233a444ae52ac2db19939a0ad3554b [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."
Don Garrett63b47d32015-03-09 16:18:58 -070028DEFINE_string workspace_root "" \
29 "The root of your workspace."
Brian Harring7b6f3772012-09-23 14:01:13 -070030DEFINE_string cache_dir "" "Directory to use for caching."
rspangler@google.comd74220d2009-10-09 20:56:14 +000031
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070032DEFINE_boolean official_build $FLAGS_FALSE \
33 "Set CHROMEOS_OFFICIAL=1 for release builds."
Elly Jones7990a062010-09-02 09:23:23 -040034DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
Brian Harring35767822012-02-01 23:50:45 -080035DEFINE_boolean early_make_chroot $FLAGS_FALSE \
36 "Internal flag. If set, the command is run as root without sudo."
Don Garrettad3f0592011-02-04 14:59:56 -080037DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken"
rspangler@google.comd74220d2009-10-09 20:56:14 +000038
39# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080040FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000041
42One or more VAR=value pairs can be specified to export variables into
43the chroot environment. For example:
44
45 $0 FOO=bar BAZ=bel
46
Doug Anderson9362fa82010-12-16 14:44:12 -080047If [-- command] is present, runs the command inside the chroot,
David James76764882012-10-24 19:46:29 -070048after changing directory to /${SUDO_USER}/trunk/src/scripts. Note that neither
Doug Anderson9362fa82010-12-16 14:44:12 -080049the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000050
Doug Anderson9362fa82010-12-16 14:44:12 -080051 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000052
53Otherwise, provides an interactive shell.
54"
55
Peter Mayo4411efe2012-09-21 04:41:27 -040056CROS_LOG_PREFIX=cros_sdk:enter_chroot
David James76764882012-10-24 19:46:29 -070057SUDO_HOME=$(eval echo ~${SUDO_USER})
Peter Mayo4411efe2012-09-21 04:41:27 -040058
Don Garrettad3f0592011-02-04 14:59:56 -080059# Version of info from common.sh that only echos if --verbose is set.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040060debug() {
Don Garrettad3f0592011-02-04 14:59:56 -080061 if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then
David Rochberg351a76f2011-02-16 11:14:00 -050062 info "$*"
Don Garrettad3f0592011-02-04 14:59:56 -080063 fi
64}
65
rspangler@google.comd74220d2009-10-09 20:56:14 +000066# Parse command line flags
67FLAGS "$@" || exit 1
68eval set -- "${FLAGS_ARGV}"
69
Greg Spencer798d75f2011-02-01 22:04:49 -080070if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then
David McMahon857dbb52009-12-09 18:21:05 -080071 CHROMEOS_OFFICIAL=1
72fi
73
Brian Harring7b6f3772012-09-23 14:01:13 -070074[ -z "${FLAGS_cache_dir}" ] && \
75 die "--cache_dir is required"
Brian Harring7ee892d2012-02-02 09:33:10 -080076
rspangler@google.comd74220d2009-10-09 20:56:14 +000077# Only now can we die on error. shflags functions leak non-zero error codes,
Brian Harring7f175a52012-03-02 05:37:00 -080078# so will die prematurely if 'switch_to_strict_mode' is specified before now.
rspangler@google.comd74220d2009-10-09 20:56:14 +000079# TODO: replace shflags with something less error-prone, or contribute a fix.
Brian Harring7f175a52012-03-02 05:37:00 -080080switch_to_strict_mode
rspangler@google.comd74220d2009-10-09 20:56:14 +000081
Matt Tennant298f61a2012-06-25 21:54:33 -070082# These config files are to be copied into chroot if they exist in home dir.
Prathmesh Prabhu74ebbbd2015-03-17 13:50:29 -070083# Additionally, git relevant files are copied by setup_git.
Matt Tennant298f61a2012-06-25 21:54:33 -070084FILES_TO_COPY_TO_CHROOT=(
85 .gdata_cred.txt # User/password for Google Docs on chromium.org
86 .gdata_token # Auth token for Google Docs on chromium.org
87 .disable_build_stats_upload # Presence of file disables command stats upload
Peter Mayo45ebc4c2013-01-28 15:49:06 -050088 .netrc # May contain required source fetching credentials
Matt Tennant298f61a2012-06-25 21:54:33 -070089)
90
Sean Parent2898f752010-05-25 15:06:33 -070091INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080092CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070093FUSE_DEVICE="/dev/fuse"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080094
Mike Frysinger01c205d2013-03-22 00:59:58 -040095# We can't use /var/lock because that might be a symlink to /run/lock outside
96# of the chroot. Or /run on the host system might not exist.
97LOCKFILE="${FLAGS_chroot}/.enter_chroot.lock"
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -040098MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger286b5922011-09-28 11:59:53 -040099
Brian Harring2499bfb2012-12-18 16:23:31 -0800100# Reset the depot tools/internal trunk pathways to what they'll
101# be w/in the chroot.
102set_chroot_trunk_dir "${FLAGS_chroot}"
103
104
David James76764882012-10-24 19:46:29 -0700105setup_mount() {
David Rochberg351a76f2011-02-16 11:14:00 -0500106 # If necessary, mount $source in the host FS at $target inside the
David James76764882012-10-24 19:46:29 -0700107 # chroot directory with $mount_args. We don't write to /etc/mtab because
108 # these mounts are all contained within an unshare and are therefore
109 # inaccessible to other namespaces (e.g. the host desktop system).
David Rochberg351a76f2011-02-16 11:14:00 -0500110 local source="$1"
David James76764882012-10-24 19:46:29 -0700111 local mount_args="-n $2"
David Rochberg351a76f2011-02-16 11:14:00 -0500112 local target="$3"
113
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400114 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500115
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400116 case " ${MOUNT_CACHE} " in
117 *" ${mounted_path} "*)
Mike Frysinger2a970592011-09-20 22:49:01 -0400118 # Already mounted!
119 ;;
120 *)
Mike Frysingeradd99762014-03-27 13:17:58 -0400121 # If it doesn't exist, assume they want a dir. But don't blindly run
122 # it all the time in case they're trying to bind mount a file.
123 if [[ ! -e ${mounted_path} ]]; then
124 mkdir -p "${mounted_path}"
125 fi
Michael Krebs04c4f732012-09-07 18:31:46 -0700126 # The args are left unquoted on purpose.
127 if [[ -n ${source} ]]; then
David James76764882012-10-24 19:46:29 -0700128 mount ${mount_args} "${source}" "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700129 else
David James76764882012-10-24 19:46:29 -0700130 mount ${mount_args} "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700131 fi
Mike Frysinger2a970592011-09-20 22:49:01 -0400132 ;;
133 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500134}
135
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400136copy_ssh_config() {
Vadim Bendebury0779f522011-06-12 12:09:16 -0700137 # Copy user .ssh/config into the chroot filtering out strings not supported
138 # by the chroot ssh. The chroot .ssh directory is passed in as the first
139 # parameter.
140
141 # ssh options to filter out. The entire strings containing these substrings
142 # will be deleted before copying.
143 local bad_options=(
Matt Tennant63c3cc52011-11-22 11:23:06 -0800144 'UseProxyIf'
145 'GSSAPIAuthentication'
146 'GSSAPIKeyExchange'
147 'ProxyUseFdpass'
Vadim Bendebury0779f522011-06-12 12:09:16 -0700148 )
David James76764882012-10-24 19:46:29 -0700149 local sshc="${SUDO_HOME}/.ssh/config"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700150 local chroot_ssh_dir="${1}"
151 local filter
152 local option
153
David James22dc2ba2012-11-29 15:46:58 -0800154 if ! user_cp "${sshc}" "${chroot_ssh_dir}/config.orig" 2>/dev/null; then
Vadim Bendebury0779f522011-06-12 12:09:16 -0700155 return # Nothing to copy.
156 fi
157
158 for option in "${bad_options[@]}"
159 do
160 if [ -z "${filter}" ]; then
161 filter="${option}"
162 else
163 filter+="\\|${option}"
164 fi
165 done
166
David James22dc2ba2012-11-29 15:46:58 -0800167 sed "/^.*\(${filter}\).*$/d" "${chroot_ssh_dir}/config.orig" | \
David James76764882012-10-24 19:46:29 -0700168 user_clobber "${chroot_ssh_dir}/config"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700169}
170
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400171copy_into_chroot_if_exists() {
Matt Tennantf7c9e772012-02-08 17:06:26 -0800172 # $1 is file path outside of chroot to copy to path $2 inside chroot.
Frank Henigman784e76c2013-08-30 10:30:12 -0400173 [ -e "$1" ] && user_cp -p "$1" "${FLAGS_chroot}/$2"
Matt Tennantf7c9e772012-02-08 17:06:26 -0800174}
175
Peter Mayo4411efe2012-09-21 04:41:27 -0400176# Usage: promote_api_keys
177# This takes care of getting the developer API keys into the chroot where
178# chrome can build with them. It needs to take it from the places a dev
179# is likely to put them, and recognize that older chroots may or may not
180# have been used since the concept of keys got added, as well as before
181# and after the developer decding to grab his own keys.
182promote_api_keys() {
David James76764882012-10-24 19:46:29 -0700183 local destination="${FLAGS_chroot}/home/${SUDO_USER}/.googleapikeys"
Peter Mayo4411efe2012-09-21 04:41:27 -0400184 # Don't disturb existing keys. They could be set differently
185 if [[ -s "${destination}" ]]; then
186 return 0
187 fi
David James76764882012-10-24 19:46:29 -0700188 if [[ -r "${SUDO_HOME}/.googleapikeys" ]]; then
189 cp -p "${SUDO_HOME}/.googleapikeys" "${destination}"
Peter Mayo4411efe2012-09-21 04:41:27 -0400190 if [[ -s "${destination}" ]] ; then
191 info "Copied Google API keys into chroot."
192 fi
David James76764882012-10-24 19:46:29 -0700193 elif [[ -r "${SUDO_HOME}/.gyp/include.gypi" ]]; then
Peter Mayo4411efe2012-09-21 04:41:27 -0400194 local NAME="('google_(api_key|default_client_(id|secret))')"
195 local WS="[[:space:]]*"
196 local CONTENTS="('[^\\\\']*')"
197 sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \
David James76764882012-10-24 19:46:29 -0700198 "${SUDO_HOME}/.gyp/include.gypi" | user_clobber "${destination}"
Peter Mayo4411efe2012-09-21 04:41:27 -0400199 if [[ -s "${destination}" ]]; then
200 info "Put discovered Google API keys into chroot."
201 fi
202 fi
203}
204
Mike Frysinger4f2d5cc2013-06-09 23:34:52 -0400205generate_locales() {
206 # Make sure user's requested locales are available
207 # http://crosbug.com/19139
208 # And make sure en_US{,.UTF-8} are always available as
209 # that what buildbot forces internally
210 local l locales gen_locales=()
211
212 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
213 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
214 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
215 sort -u | sed '/^C$/d')
216 for l in ${locales}; do
217 if [[ ${l} == *.* ]]; then
218 enc=${l#*.}
219 else
220 enc="ISO-8859-1"
221 fi
222 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
223 utf8) enc="UTF-8";;
224 esac
225 gen_locales+=("${l} ${enc}")
226 done
227 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
228 # Force LC_ALL=C to workaround slow string parsing in bash
229 # with long multibyte strings. Newer setups have this fixed,
230 # but locale-gen doesn't need to be run in any locale in the
231 # first place, so just go with C to keep it fast.
232 chroot "${FLAGS_chroot}" env LC_ALL=C locale-gen -q -u \
233 -G "$(printf '%s\n' "${gen_locales[@]}")"
234 fi
235}
236
Prathmesh Prabhu74ebbbd2015-03-17 13:50:29 -0700237setup_git() {
238 # Copy .gitconfig into chroot so repo and git can be used from inside.
239 # This is required for repo to work since it validates the email address.
240 copy_into_chroot_if_exists "${SUDO_HOME}/.gitconfig" \
241 "/home/${SUDO_USER}/.gitconfig"
242 local -r chroot_gitconfig="${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig"
243
244 # If the user didn't set up their username in their gitconfig, look
245 # at the default git settings for the user.
246 if ! git config -f "${chroot_gitconfig}" user.email >& /dev/null; then
247 local ident=$(cd /; sudo -u ${SUDO_USER} -- git var GIT_COMMITTER_IDENT || \
248 :)
249 local ident_name=${ident%% <*}
250 local ident_email=${ident%%>*}; ident_email=${ident_email##*<}
251 git config -f "${chroot_gitconfig}" --replace-all user.name \
252 "${ident_name}" || :
253 git config -f "${chroot_gitconfig}" --replace-all user.email \
254 "${ident_email}" || :
255 fi
256
257 # Copy the gitcookies file, updating the user's gitconfig to point to it.
258 local gitcookies
259 gitcookies="$(git config -f "${chroot_gitconfig}" --get http.cookiefile)"
260 if [[ $? -ne 0 ]]; then
261 # Try the default location anyway.
262 gitcookies="${SUDO_HOME}/.gitcookies"
263 fi
264 copy_into_chroot_if_exists "${gitcookies}" "/home/${SUDO_USER}/.gitcookies"
265 local -r chroot_gitcookies="${FLAGS_chroot}/home/${SUDO_USER}/.gitcookies"
266 if [[ -e "${chroot_gitcookies}" ]]; then
267 git config -f "${chroot_gitconfig}" --replace-all http.cookiefile \
268 "/home/${SUDO_USER}/.gitcookies"
269 fi
270 # This line must be at the end because using `git config` changes ownership of
271 # the .gitconfig.
272 chown ${SUDO_UID}:${SUDO_GID} "${chroot_gitconfig}"
273}
274
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400275setup_env() {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800276 (
277 flock 200
rspangler@google.comd74220d2009-10-09 20:56:14 +0000278
David James76764882012-10-24 19:46:29 -0700279 # Make the lockfile writable for backwards compatibility.
280 chown ${SUDO_UID}:${SUDO_GID} "${LOCKFILE}"
281
282 # Refresh /etc/resolv.conf and /etc/hosts in the chroot.
283 install -C -m644 /etc/resolv.conf ${FLAGS_chroot}/etc/resolv.conf
284 install -C -m644 /etc/hosts ${FLAGS_chroot}/etc/hosts
David James412b9022011-07-15 19:54:16 -0700285
Don Garretta0e7ea12011-02-07 18:39:59 -0800286 debug "Mounting chroot environment."
Steev Klimaszewskia10974c2013-11-30 20:38:34 -0600287 mount --make-rslave /
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400288 MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts))
David James76764882012-10-24 19:46:29 -0700289 setup_mount none "-t proc" /proc
290 setup_mount none "-t sysfs" /sys
Gaurav Shah365b7062013-12-03 18:12:58 -0800291 if grep -q binfmt_misc /proc/filesystems; then
292 setup_mount binfmt_misc "-t binfmt_misc" /proc/sys/fs/binfmt_misc
293 fi
David James76764882012-10-24 19:46:29 -0700294 setup_mount /dev "--bind" /dev
Mike Frysingere22afc72013-04-12 13:47:01 -0400295 setup_mount /dev/pts "--bind" /dev/pts
David James482fb292013-03-10 21:06:54 -0700296 if [[ -d /run ]]; then
David James76764882012-10-24 19:46:29 -0700297 setup_mount /run "--bind" /run
David James482fb292013-03-10 21:06:54 -0700298 if [[ -d /run/shm && ! -L /run/shm ]]; then
David James76764882012-10-24 19:46:29 -0700299 setup_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700300 fi
301 fi
Brian Harring2499bfb2012-12-18 16:23:31 -0800302
Mike Frysinger4f2d5cc2013-06-09 23:34:52 -0400303 # Do this early as it's slow and only needs basic mounts (above).
304 generate_locales &
305
Michael Spang15144f42013-04-02 17:42:55 -0400306 setup_mount "${FLAGS_trunk}" "--rbind" "${CHROOT_TRUNK_DIR}"
Chris Sosa389634d2012-09-05 19:56:53 -0700307
Brian Harringdd7d7932011-12-23 04:21:33 -0800308 debug "Setting up referenced repositories if required."
309 REFERENCE_DIR=$(git config --file \
310 "${FLAGS_trunk}/.repo/manifests.git/config" \
311 repo.reference)
312 if [ -n "${REFERENCE_DIR}" ]; then
313
Brian Harring334050f2012-06-08 17:40:58 -0700314 ALTERNATES="${FLAGS_trunk}/.repo/alternates"
315
316 # Ensure this directory exists ourselves, and has the correct ownership.
David James76764882012-10-24 19:46:29 -0700317 user_mkdir "${ALTERNATES}"
Brian Harring334050f2012-06-08 17:40:58 -0700318
319 unset ALTERNATES
320
Brian Harringdd7d7932011-12-23 04:21:33 -0800321 IFS=$'\n';
David James76764882012-10-24 19:46:29 -0700322 required=( $( sudo -u "${SUDO_USER}" -- \
323 "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \
Brian Harringdd7d7932011-12-23 04:21:33 -0800324 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
325 unset IFS
326
David James76764882012-10-24 19:46:29 -0700327 setup_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
Brian Harringdd7d7932011-12-23 04:21:33 -0800328 "${CHROOT_TRUNK_DIR}/.repo/alternates"
329
330 # Note that as we're bringing up each referened repo, we also
331 # mount bind an empty directory over its alternates. This is
332 # required to suppress git from tracing through it- we already
333 # specify the required alternates for CHROOT_TRUNK_DIR, no point
334 # in having git try recursing through each on their own.
335 #
336 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
337 # the bind is visible only w/in the chroot.
David James76764882012-10-24 19:46:29 -0700338 user_mkdir ${FLAGS_trunk}/.repo/chroot/empty
Brian Harringdd7d7932011-12-23 04:21:33 -0800339 position=1
340 for x in "${required[@]}"; do
341 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
David James76764882012-10-24 19:46:29 -0700342 setup_mount "${x}" "--bind" "${base}"
Brian Harringdd7d7932011-12-23 04:21:33 -0800343 if [ -e "${x}/.repo/alternates" ]; then
David James76764882012-10-24 19:46:29 -0700344 setup_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
Brian Harringdd7d7932011-12-23 04:21:33 -0800345 "${base}/.repo/alternates"
346 fi
347 position=$(( ${position} + 1 ))
348 done
349 unset required position base
350 fi
351 unset REFERENCE_DIR
352
Brian Harring7b6f3772012-09-23 14:01:13 -0700353 chroot_cache='/var/cache/chromeos-cache'
354 debug "Setting up shared cache dir directory."
David James76764882012-10-24 19:46:29 -0700355 user_mkdir "${FLAGS_cache_dir}"/distfiles/{target,host}
356 user_mkdir "${FLAGS_chroot}/${chroot_cache}"
357 setup_mount "${FLAGS_cache_dir}" "--bind" "${chroot_cache}"
Brian Harring7b6f3772012-09-23 14:01:13 -0700358 # TODO(build): remove this as of 12/01/12.
359 # Because of how distfiles -> cache_dir was deployed, if this isn't
360 # a symlink, we *know* the ondisk pathways aren't compatible- thus
361 # fix it now.
362 distfiles_path="${FLAGS_chroot}/var/cache/distfiles"
363 if [ ! -L "${distfiles_path}" ]; then
364 # While we're at it, ensure the var is exported w/in the chroot; it
365 # won't exist if distfiles isn't a symlink.
Paul Drewsb688cbe2012-10-08 15:33:43 -0700366 p="${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
David James76764882012-10-24 19:46:29 -0700367 rm -rf "${distfiles_path}"
368 ln -s chromeos-cache/distfiles "${distfiles_path}"
369 mkdir -p -m 775 "${p%/*}"
370 echo 'export CHROMEOS_CACHEDIR=${chroot_cache}' > "${p}"
371 chmod 0644 "${p}"
Brian Harring7b6f3772012-09-23 14:01:13 -0700372 fi
Brian Harring7ee892d2012-02-02 09:33:10 -0800373
Aviv Keshet97a35f42014-07-24 12:11:10 -0700374 if [ -d "${SUDO_HOME}/.cidb_creds" ]; then
375 setup_mount "${SUDO_HOME}/.cidb_creds" --bind \
376 "/home/${SUDO_USER}/.cidb_creds"
377 fi
378
Elly Jones7990a062010-09-02 09:23:23 -0400379 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
Mike Frysinger93a2eca2012-11-30 14:00:35 -0500380 # Clean up previous ssh agents.
381 rmdir "${FLAGS_chroot}"/tmp/ssh-* 2>/dev/null
382
David James76764882012-10-24 19:46:29 -0700383 if [ -n "${SSH_AUTH_SOCK}" -a -d "${SUDO_HOME}/.ssh" ]; then
Mike Frysingeradd99762014-03-27 13:17:58 -0400384 local target_ssh="/home/${SUDO_USER}/.ssh"
385 TARGET_DIR="${FLAGS_chroot}${target_ssh}"
David James76764882012-10-24 19:46:29 -0700386 user_mkdir "${TARGET_DIR}"
Mike Frysingeradd99762014-03-27 13:17:58 -0400387
388 local known_hosts="${SUDO_HOME}/.ssh/known_hosts"
389 if [[ -e ${known_hosts} ]]; then
390 truncate -s 0 "${TARGET_DIR}/known_hosts"
391 setup_mount "${known_hosts}" --bind "${target_ssh}/known_hosts"
392 fi
David James22dc2ba2012-11-29 15:46:58 -0800393 (
Mike Frysingeradd99762014-03-27 13:17:58 -0400394 # Only copy ~/.ssh/*.pub if they exist. Since we set
David James22dc2ba2012-11-29 15:46:58 -0800395 # nullglob, this needs to happen within a subshell.
396 shopt -s nullglob
Mike Frysingeradd99762014-03-27 13:17:58 -0400397 files=("${SUDO_HOME}"/.ssh/*.pub)
David James22dc2ba2012-11-29 15:46:58 -0800398 if [[ ${#files[@]} -gt 0 ]]; then
399 user_cp "${files[@]}" "${TARGET_DIR}/"
400 fi
401 )
Vadim Bendebury0779f522011-06-12 12:09:16 -0700402 copy_ssh_config "${TARGET_DIR}"
David James76764882012-10-24 19:46:29 -0700403 chown -R ${SUDO_UID}:${SUDO_GID} "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500404
405 # Don't try to bind mount the ssh agent dir if it has gone stale.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400406 ASOCK=${SSH_AUTH_SOCK%/*}
Mike Frysinger9de707f2011-12-12 14:21:44 -0500407 if [ -d "${ASOCK}" ]; then
David James76764882012-10-24 19:46:29 -0700408 setup_mount "${ASOCK}" "--bind" "${ASOCK}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500409 fi
Elly Jones7990a062010-09-02 09:23:23 -0400410 fi
411 fi
412
Marc MERLINfcd84ef2013-03-07 14:34:43 -0800413 if [[ -d "$SUDO_HOME/.subversion" ]]; then
David James76764882012-10-24 19:46:29 -0700414 TARGET="/home/${SUDO_USER}/.subversion"
David James76764882012-10-24 19:46:29 -0700415 setup_mount "${SUDO_HOME}/.subversion" "--bind" "${TARGET}"
Paul Drewsb4605b42012-10-11 23:10:43 -0700416 # Symbolic-link the .subversion directory so sandboxed subversion.class
417 # clients can use it.
Paul Drewsb4605b42012-10-11 23:10:43 -0700418 for d in \
David James76764882012-10-24 19:46:29 -0700419 "${FLAGS_cache_dir}"/distfiles/{host,target}/svn-src/"${SUDO_USER}"; do
Paul Drewsb4605b42012-10-11 23:10:43 -0700420 if [[ ! -L "${d}/.subversion" ]]; then
David James76764882012-10-24 19:46:29 -0700421 rm -rf "${d}/.subversion"
422 user_mkdir "${d}"
423 user_symlink /home/${SUDO_USER}/.subversion "${d}/.subversion"
Paul Drewsb4605b42012-10-11 23:10:43 -0700424 fi
425 done
Mike Frysinger286b5922011-09-28 11:59:53 -0400426 fi
427
David James76764882012-10-24 19:46:29 -0700428 # A reference to the DEPOT_TOOLS path may be passed in by cros_sdk.
429 if [ -n "${DEPOT_TOOLS}" ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400430 debug "Mounting depot_tools"
Brian Harring2499bfb2012-12-18 16:23:31 -0800431 setup_mount "${DEPOT_TOOLS}" --bind "${DEPOT_TOOLS_DIR}"
Mike Frysinger286b5922011-09-28 11:59:53 -0400432 fi
433
Don Garrett63b47d32015-03-09 16:18:58 -0700434 if [ -n "${FLAGS_workspace_root}" ]; then
435 debug "Mounting workspace"
436 setup_mount "${FLAGS_workspace_root}" --bind "${WORKSPACE_DIR}"
437 fi
438
Michael Krebs04c4f732012-09-07 18:31:46 -0700439 # Mount additional directories as specified in .local_mounts file.
440 local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts"
441 if [[ -f ${local_mounts} ]]; then
Gaurav Shahe3cffca2013-11-26 14:45:10 -0800442 info "Mounting local folders"
Michael Krebs04c4f732012-09-07 18:31:46 -0700443 # format: mount_source
444 # or mount_source mount_point
445 # or # comments
446 local mount_source mount_point
447 while read mount_source mount_point; do
448 if [[ -z ${mount_source} ]]; then
449 continue
450 fi
451 # if only source is assigned, use source as mount point.
452 : ${mount_point:=${mount_source}}
453 debug " mounting ${mount_source} on ${mount_point}"
David James76764882012-10-24 19:46:29 -0700454 setup_mount "${mount_source}" "--bind" "${mount_point}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700455 done < <(sed -e 's:#.*::' "${local_mounts}")
456 fi
457
Mike Frysinger9a50b642011-09-20 23:37:14 -0400458 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
459 if [ -z "$CHROME_ROOT" ]; then
460 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
461 2>/dev/null || :)"
462 CHROME_ROOT_AUTO=1
463 fi
464 if [[ -n "$CHROME_ROOT" ]]; then
465 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
Mike Frysinger9d73a852013-12-02 19:47:08 -0500466 error "Not mounting chrome source: could not find CHROME_ROOT/src dir."
467 error "Full path we tried: ${CHROME_ROOT}/src"
David James76764882012-10-24 19:46:29 -0700468 rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
Mike Frysinger9a50b642011-09-20 23:37:14 -0400469 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
470 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800471 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400472 else
473 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
David James76764882012-10-24 19:46:29 -0700474 echo $CHROME_ROOT > "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
475 setup_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800476 fi
477 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800478
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400479 # Install fuse module. Skip modprobe when possible for slight
480 # speed increase when initializing the env.
481 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
David James76764882012-10-24 19:46:29 -0700482 modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200483 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700484 fi
485
Mike Frysinger926a4b92012-06-27 15:22:28 -0400486 # Fix permissions on ccache tree. If this is a fresh chroot, then they
487 # might not be set up yet. Or if the user manually `rm -rf`-ed things,
488 # we need to reset it. Otherwise, gcc itself takes care of fixing things
489 # on demand, but only when it updates.
490 ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache"
491 if [[ ! -d ${ccache_dir} ]]; then
David James76764882012-10-24 19:46:29 -0700492 mkdir -p -m 2775 "${ccache_dir}"
Mike Frysinger926a4b92012-06-27 15:22:28 -0400493 fi
Mike Frysingerba609912015-05-20 00:44:25 -0400494 (
495 find -H "${ccache_dir}" '(' -type d -a '!' -perm 2775 ')' \
496 -exec chmod 2775 {} +
497 find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} +
498 # These settings are kept in sync with the gcc ebuild.
499 chroot "${FLAGS_chroot}" env CCACHE_DIR=/var/cache/distfiles/ccache \
500 CCACHE_UMASK=002 ccache -F 0 -M 11G >/dev/null
501 ) &
David James342f1562011-07-15 15:21:18 -0700502
Matt Tennant298f61a2012-06-25 21:54:33 -0700503 # Certain files get copied into the chroot when entering.
504 for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do
David James76764882012-10-24 19:46:29 -0700505 copy_into_chroot_if_exists "${SUDO_HOME}/${fn}" "/home/${SUDO_USER}/${fn}"
Matt Tennantf7c9e772012-02-08 17:06:26 -0800506 done
Prathmesh Prabhu74ebbbd2015-03-17 13:50:29 -0700507 setup_git
Peter Mayo4411efe2012-09-21 04:41:27 -0400508 promote_api_keys
Matt Tennantd4316fe2011-08-30 12:06:42 -0700509
Dale Curtis98631732011-05-05 16:16:59 -0700510 # Fix permissions on shared memory to allow non-root users access to POSIX
David James76764882012-10-24 19:46:29 -0700511 # semaphores.
512 chmod -R 777 "${FLAGS_chroot}/dev/shm"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400513
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800514 # gsutil uses boto config to store settings and credentials. Copy
515 # user's own boto file into the chroot if it exists. Also copy it
516 # to /root for sudoed invocations.
517 chroot_user_boto="${FLAGS_chroot}/home/${SUDO_USER}/.boto"
518 chroot_root_boto="${FLAGS_chroot}/root/.boto"
519 if [ -f ${SUDO_HOME}/.boto ]; then
520 # Pass --remote-destination to overwrite a symlink.
521 user_cp "--remove-destination" "${SUDO_HOME}/.boto" "$chroot_user_boto"
Frank Henigman696ce082014-03-27 21:21:55 -0400522 cp "--remove-destination" "$chroot_user_boto" "$chroot_root_boto"
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800523 fi
524
525 # If user doesn't have a boto file, check if the private overlays
526 # are installed and use those credentials.
Gilad Arnold264f64d2012-09-06 14:01:45 -0700527 boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto'
528 if [ -s "${FLAGS_trunk}/${boto}" ]; then
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800529 if [ ! -e "$chroot_user_boto" ]; then
530 user_symlink "trunk/${boto}" "$chroot_user_boto"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400531 fi
Yu-Ju Hong22278a22014-01-16 12:46:53 -0800532 if [ ! -e "$chroot_root_boto" ]; then
533 ln -sf "${CHROOT_TRUNK_DIR}/${boto}" "$chroot_root_boto"
Gilad Arnold264f64d2012-09-06 14:01:45 -0700534 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400535 fi
536
537 # Have found a few chroots where ~/.gsutil is owned by root:root, probably
538 # as a result of old gsutil or tools. This causes permission errors when
539 # gsutil cp tries to create its cache files, so ensure the user can
540 # actually write to their directory.
David James76764882012-10-24 19:46:29 -0700541 gsutil_dir="${FLAGS_chroot}/home/${SUDO_USER}/.gsutil"
542 if [ -d "${gsutil_dir}" ]; then
543 chown -R ${SUDO_UID}:${SUDO_GID} "${gsutil_dir}"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400544 fi
David James546747b2010-03-23 15:19:43 -0700545 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000546}
547
rspangler@google.comd74220d2009-10-09 20:56:14 +0000548setup_env
549
Brian Harring35767822012-02-01 23:50:45 -0800550CHROOT_PASSTHRU=(
551 "BUILDBOT_BUILD=$FLAGS_build_number"
Brian Harring35767822012-02-01 23:50:45 -0800552 "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
Brian Harring35767822012-02-01 23:50:45 -0800553 "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
Brian Harring35767822012-02-01 23:50:45 -0800554)
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700555
Josh Triplettd6a13bf2014-06-13 15:36:52 -0700556# Translate C.UTF-8 into something we support. Remove this when our glibc
557# starts supporting C.UTF-8. https://bugzilla.redhat.com/show_bug.cgi?id=902094
558for var in LANG \
559 LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
560 LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME}; do
561 if [[ ${!var} =~ C\.[Uu][Tt][Ff]-?8 ]]; then
562 if [[ ${var} == LC_COLLATE ]]; then
563 export LC_COLLATE=C
564 else
565 export ${var}=en_US.UTF-8
566 fi
567 fi
568done
569
David James76764882012-10-24 19:46:29 -0700570# Add the whitelisted environment variables to CHROOT_PASSTHRU.
571load_environment_whitelist
572for var in "${ENVIRONMENT_WHITELIST[@]}" ; do
Brian Harring06d3c2e2012-08-23 07:35:43 -0700573 [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" )
Mario Limonciello7052ae12011-05-05 12:00:49 -0500574done
575
Paul Drews67d9ef12013-03-20 08:47:47 -0700576# Set up GIT_PROXY_COMMAND so git:// URLs automatically work behind a proxy.
577if [[ -n "${all_proxy}" || -n "${https_proxy}" || -n "${http_proxy}" ]]; then
578 CHROOT_PASSTHRU+=(
579 "GIT_PROXY_COMMAND=${CHROOT_TRUNK_DIR}/src/scripts/bin/proxy-gw"
580 )
581fi
582
derat@google.com4e7a92b2009-11-21 23:44:14 +0000583# Run command or interactive shell. Also include the non-chrooted path to
584# the source trunk for scripts that may need to print it (e.g.
585# build_image.sh).
Brian Harring35767822012-02-01 23:50:45 -0800586
587if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
588 cmd=( /bin/bash -l -c 'env "$@"' -- )
589elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
590 # Complain that sudo is missing.
591 error "Failing since the chroot lacks sudo."
592 error "Requested enter_chroot command was: $@"
593 exit 127
594else
David James76764882012-10-24 19:46:29 -0700595 cmd=( sudo -i -u "${SUDO_USER}" )
Brian Harring35767822012-02-01 23:50:45 -0800596fi
597
David James76764882012-10-24 19:46:29 -0700598cmd+=( "${CHROOT_PASSTHRU[@]}" "$@" )
599exec chroot "${FLAGS_chroot}" "${cmd[@]}"