blob: f20033556569681e84c813af9ff26833cc216d4c [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
85)
86
Sean Parent2898f752010-05-25 15:06:33 -070087INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080088CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
David James76764882012-10-24 19:46:29 -070089INNER_DEPOT_TOOLS_ROOT="/home/${SUDO_USER}/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070090FUSE_DEVICE="/dev/fuse"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080091
David James76764882012-10-24 19:46:29 -070092chmod 0777 "$FLAGS_chroot/var/lock"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080093
94LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -040095MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Mike Frysinger286b5922011-09-28 11:59:53 -040096
David James76764882012-10-24 19:46:29 -070097setup_mount() {
David Rochberg351a76f2011-02-16 11:14:00 -050098 # If necessary, mount $source in the host FS at $target inside the
David James76764882012-10-24 19:46:29 -070099 # chroot directory with $mount_args. We don't write to /etc/mtab because
100 # these mounts are all contained within an unshare and are therefore
101 # inaccessible to other namespaces (e.g. the host desktop system).
David Rochberg351a76f2011-02-16 11:14:00 -0500102 local source="$1"
David James76764882012-10-24 19:46:29 -0700103 local mount_args="-n $2"
David Rochberg351a76f2011-02-16 11:14:00 -0500104 local target="$3"
105
Mike Frysinger0a0e6ec2011-09-28 11:57:21 -0400106 local mounted_path="${MOUNTED_PATH}$target"
David Rochberg351a76f2011-02-16 11:14:00 -0500107
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400108 case " ${MOUNT_CACHE} " in
109 *" ${mounted_path} "*)
Mike Frysinger2a970592011-09-20 22:49:01 -0400110 # Already mounted!
111 ;;
112 *)
David James76764882012-10-24 19:46:29 -0700113 mkdir -p "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700114 # The args are left unquoted on purpose.
115 if [[ -n ${source} ]]; then
David James76764882012-10-24 19:46:29 -0700116 mount ${mount_args} "${source}" "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700117 else
David James76764882012-10-24 19:46:29 -0700118 mount ${mount_args} "${mounted_path}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700119 fi
Mike Frysinger2a970592011-09-20 22:49:01 -0400120 ;;
121 esac
David Rochberg351a76f2011-02-16 11:14:00 -0500122}
123
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400124copy_ssh_config() {
Vadim Bendebury0779f522011-06-12 12:09:16 -0700125 # Copy user .ssh/config into the chroot filtering out strings not supported
126 # by the chroot ssh. The chroot .ssh directory is passed in as the first
127 # parameter.
128
129 # ssh options to filter out. The entire strings containing these substrings
130 # will be deleted before copying.
131 local bad_options=(
Matt Tennant63c3cc52011-11-22 11:23:06 -0800132 'UseProxyIf'
133 'GSSAPIAuthentication'
134 'GSSAPIKeyExchange'
135 'ProxyUseFdpass'
Vadim Bendebury0779f522011-06-12 12:09:16 -0700136 )
David James76764882012-10-24 19:46:29 -0700137 local sshc="${SUDO_HOME}/.ssh/config"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700138 local chroot_ssh_dir="${1}"
139 local filter
140 local option
141
David James22dc2ba2012-11-29 15:46:58 -0800142 if ! user_cp "${sshc}" "${chroot_ssh_dir}/config.orig" 2>/dev/null; then
Vadim Bendebury0779f522011-06-12 12:09:16 -0700143 return # Nothing to copy.
144 fi
145
146 for option in "${bad_options[@]}"
147 do
148 if [ -z "${filter}" ]; then
149 filter="${option}"
150 else
151 filter+="\\|${option}"
152 fi
153 done
154
David James22dc2ba2012-11-29 15:46:58 -0800155 sed "/^.*\(${filter}\).*$/d" "${chroot_ssh_dir}/config.orig" | \
David James76764882012-10-24 19:46:29 -0700156 user_clobber "${chroot_ssh_dir}/config"
Vadim Bendebury0779f522011-06-12 12:09:16 -0700157}
158
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400159copy_into_chroot_if_exists() {
Matt Tennantf7c9e772012-02-08 17:06:26 -0800160 # $1 is file path outside of chroot to copy to path $2 inside chroot.
David James76764882012-10-24 19:46:29 -0700161 [ -e "$1" ] && cp -p "$1" "${FLAGS_chroot}/$2"
Matt Tennantf7c9e772012-02-08 17:06:26 -0800162}
163
Peter Mayo4411efe2012-09-21 04:41:27 -0400164# Usage: promote_api_keys
165# This takes care of getting the developer API keys into the chroot where
166# chrome can build with them. It needs to take it from the places a dev
167# is likely to put them, and recognize that older chroots may or may not
168# have been used since the concept of keys got added, as well as before
169# and after the developer decding to grab his own keys.
170promote_api_keys() {
David James76764882012-10-24 19:46:29 -0700171 local destination="${FLAGS_chroot}/home/${SUDO_USER}/.googleapikeys"
Peter Mayo4411efe2012-09-21 04:41:27 -0400172 # Don't disturb existing keys. They could be set differently
173 if [[ -s "${destination}" ]]; then
174 return 0
175 fi
David James76764882012-10-24 19:46:29 -0700176 if [[ -r "${SUDO_HOME}/.googleapikeys" ]]; then
177 cp -p "${SUDO_HOME}/.googleapikeys" "${destination}"
Peter Mayo4411efe2012-09-21 04:41:27 -0400178 if [[ -s "${destination}" ]] ; then
179 info "Copied Google API keys into chroot."
180 fi
David James76764882012-10-24 19:46:29 -0700181 elif [[ -r "${SUDO_HOME}/.gyp/include.gypi" ]]; then
Peter Mayo4411efe2012-09-21 04:41:27 -0400182 local NAME="('google_(api_key|default_client_(id|secret))')"
183 local WS="[[:space:]]*"
184 local CONTENTS="('[^\\\\']*')"
185 sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \
David James76764882012-10-24 19:46:29 -0700186 "${SUDO_HOME}/.gyp/include.gypi" | user_clobber "${destination}"
Peter Mayo4411efe2012-09-21 04:41:27 -0400187 if [[ -s "${destination}" ]]; then
188 info "Put discovered Google API keys into chroot."
189 fi
190 fi
191}
192
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400193setup_env() {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800194 (
195 flock 200
rspangler@google.comd74220d2009-10-09 20:56:14 +0000196
David James76764882012-10-24 19:46:29 -0700197 # Make the lockfile writable for backwards compatibility.
198 chown ${SUDO_UID}:${SUDO_GID} "${LOCKFILE}"
199
200 # Refresh /etc/resolv.conf and /etc/hosts in the chroot.
201 install -C -m644 /etc/resolv.conf ${FLAGS_chroot}/etc/resolv.conf
202 install -C -m644 /etc/hosts ${FLAGS_chroot}/etc/hosts
David James412b9022011-07-15 19:54:16 -0700203
Don Garretta0e7ea12011-02-07 18:39:59 -0800204 debug "Mounting chroot environment."
Mike Frysinger9e5b0a42012-05-16 17:30:24 -0400205 MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts))
David James76764882012-10-24 19:46:29 -0700206 setup_mount none "-t proc" /proc
207 setup_mount none "-t sysfs" /sys
208 setup_mount /dev "--bind" /dev
209 setup_mount none "-t devpts" /dev/pts
Aaron Plattner130d3632011-10-18 08:54:57 -0700210 if [ -d /run ]; then
David James76764882012-10-24 19:46:29 -0700211 setup_mount /run "--bind" /run
Aaron Plattner130d3632011-10-18 08:54:57 -0700212 if [ -d /run/shm ]; then
David James76764882012-10-24 19:46:29 -0700213 setup_mount /run/shm "--bind" /run/shm
Aaron Plattner130d3632011-10-18 08:54:57 -0700214 fi
215 fi
David James76764882012-10-24 19:46:29 -0700216 setup_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}"
Chris Sosa389634d2012-09-05 19:56:53 -0700217
Brian Harringdd7d7932011-12-23 04:21:33 -0800218 debug "Setting up referenced repositories if required."
219 REFERENCE_DIR=$(git config --file \
220 "${FLAGS_trunk}/.repo/manifests.git/config" \
221 repo.reference)
222 if [ -n "${REFERENCE_DIR}" ]; then
223
Brian Harring334050f2012-06-08 17:40:58 -0700224 ALTERNATES="${FLAGS_trunk}/.repo/alternates"
225
226 # Ensure this directory exists ourselves, and has the correct ownership.
David James76764882012-10-24 19:46:29 -0700227 user_mkdir "${ALTERNATES}"
Brian Harring334050f2012-06-08 17:40:58 -0700228
229 unset ALTERNATES
230
Brian Harringdd7d7932011-12-23 04:21:33 -0800231 IFS=$'\n';
David James76764882012-10-24 19:46:29 -0700232 required=( $( sudo -u "${SUDO_USER}" -- \
233 "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \
Brian Harringdd7d7932011-12-23 04:21:33 -0800234 "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) )
235 unset IFS
236
David James76764882012-10-24 19:46:29 -0700237 setup_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \
Brian Harringdd7d7932011-12-23 04:21:33 -0800238 "${CHROOT_TRUNK_DIR}/.repo/alternates"
239
240 # Note that as we're bringing up each referened repo, we also
241 # mount bind an empty directory over its alternates. This is
242 # required to suppress git from tracing through it- we already
243 # specify the required alternates for CHROOT_TRUNK_DIR, no point
244 # in having git try recursing through each on their own.
245 #
246 # Finally note that if you're unfamiliar w/ chroot/vfs semantics,
247 # the bind is visible only w/in the chroot.
David James76764882012-10-24 19:46:29 -0700248 user_mkdir ${FLAGS_trunk}/.repo/chroot/empty
Brian Harringdd7d7932011-12-23 04:21:33 -0800249 position=1
250 for x in "${required[@]}"; do
251 base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}"
David James76764882012-10-24 19:46:29 -0700252 setup_mount "${x}" "--bind" "${base}"
Brian Harringdd7d7932011-12-23 04:21:33 -0800253 if [ -e "${x}/.repo/alternates" ]; then
David James76764882012-10-24 19:46:29 -0700254 setup_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \
Brian Harringdd7d7932011-12-23 04:21:33 -0800255 "${base}/.repo/alternates"
256 fi
257 position=$(( ${position} + 1 ))
258 done
259 unset required position base
260 fi
261 unset REFERENCE_DIR
262
Brian Harring7b6f3772012-09-23 14:01:13 -0700263 chroot_cache='/var/cache/chromeos-cache'
264 debug "Setting up shared cache dir directory."
David James76764882012-10-24 19:46:29 -0700265 user_mkdir "${FLAGS_cache_dir}"/distfiles/{target,host}
266 user_mkdir "${FLAGS_chroot}/${chroot_cache}"
267 setup_mount "${FLAGS_cache_dir}" "--bind" "${chroot_cache}"
Brian Harring7b6f3772012-09-23 14:01:13 -0700268 # TODO(build): remove this as of 12/01/12.
269 # Because of how distfiles -> cache_dir was deployed, if this isn't
270 # a symlink, we *know* the ondisk pathways aren't compatible- thus
271 # fix it now.
272 distfiles_path="${FLAGS_chroot}/var/cache/distfiles"
273 if [ ! -L "${distfiles_path}" ]; then
274 # While we're at it, ensure the var is exported w/in the chroot; it
275 # won't exist if distfiles isn't a symlink.
Paul Drewsb688cbe2012-10-08 15:33:43 -0700276 p="${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh"
David James76764882012-10-24 19:46:29 -0700277 rm -rf "${distfiles_path}"
278 ln -s chromeos-cache/distfiles "${distfiles_path}"
279 mkdir -p -m 775 "${p%/*}"
280 echo 'export CHROMEOS_CACHEDIR=${chroot_cache}' > "${p}"
281 chmod 0644 "${p}"
Brian Harring7b6f3772012-09-23 14:01:13 -0700282 fi
Brian Harring7ee892d2012-02-02 09:33:10 -0800283
Elly Jones7990a062010-09-02 09:23:23 -0400284 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
David James76764882012-10-24 19:46:29 -0700285 if [ -n "${SSH_AUTH_SOCK}" -a -d "${SUDO_HOME}/.ssh" ]; then
286 TARGET_DIR="${FLAGS_chroot}/home/${SUDO_USER}/.ssh"
287 user_mkdir "${TARGET_DIR}"
David James22dc2ba2012-11-29 15:46:58 -0800288 (
289 # Only copy ~/.ssh/{known_hosts,*.pub} if they exist. Since we set
290 # nullglob, this needs to happen within a subshell.
291 shopt -s nullglob
292 files=("${SUDO_HOME}"/.ssh/{known_hosts,*.pub})
293 if [[ ${#files[@]} -gt 0 ]]; then
294 user_cp "${files[@]}" "${TARGET_DIR}/"
295 fi
296 )
Vadim Bendebury0779f522011-06-12 12:09:16 -0700297 copy_ssh_config "${TARGET_DIR}"
David James76764882012-10-24 19:46:29 -0700298 chown -R ${SUDO_UID}:${SUDO_GID} "${TARGET_DIR}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500299
300 # Don't try to bind mount the ssh agent dir if it has gone stale.
Mike Frysinger61e4f282011-09-20 22:37:22 -0400301 ASOCK=${SSH_AUTH_SOCK%/*}
Mike Frysinger9de707f2011-12-12 14:21:44 -0500302 if [ -d "${ASOCK}" ]; then
David James76764882012-10-24 19:46:29 -0700303 setup_mount "${ASOCK}" "--bind" "${ASOCK}"
Mike Frysinger9de707f2011-12-12 14:21:44 -0500304 fi
Elly Jones7990a062010-09-02 09:23:23 -0400305 fi
306 fi
307
David James76764882012-10-24 19:46:29 -0700308 if [ -d "$SUDO_HOME/.subversion" ]; then
309 TARGET="/home/${SUDO_USER}/.subversion"
310 user_mkdir "${FLAGS_chroot}${TARGET}"
311 setup_mount "${SUDO_HOME}/.subversion" "--bind" "${TARGET}"
Paul Drewsb4605b42012-10-11 23:10:43 -0700312 # Symbolic-link the .subversion directory so sandboxed subversion.class
313 # clients can use it.
Paul Drewsb4605b42012-10-11 23:10:43 -0700314 for d in \
David James76764882012-10-24 19:46:29 -0700315 "${FLAGS_cache_dir}"/distfiles/{host,target}/svn-src/"${SUDO_USER}"; do
Paul Drewsb4605b42012-10-11 23:10:43 -0700316 if [[ ! -L "${d}/.subversion" ]]; then
David James76764882012-10-24 19:46:29 -0700317 rm -rf "${d}/.subversion"
318 user_mkdir "${d}"
319 user_symlink /home/${SUDO_USER}/.subversion "${d}/.subversion"
Paul Drewsb4605b42012-10-11 23:10:43 -0700320 fi
321 done
Mike Frysinger286b5922011-09-28 11:59:53 -0400322 fi
323
David James76764882012-10-24 19:46:29 -0700324 # A reference to the DEPOT_TOOLS path may be passed in by cros_sdk.
325 if [ -n "${DEPOT_TOOLS}" ]; then
Mike Frysinger286b5922011-09-28 11:59:53 -0400326 debug "Mounting depot_tools"
David James76764882012-10-24 19:46:29 -0700327 setup_mount "${DEPOT_TOOLS}" --bind "${INNER_DEPOT_TOOLS_ROOT}"
Mike Frysinger286b5922011-09-28 11:59:53 -0400328 fi
329
Michael Krebs04c4f732012-09-07 18:31:46 -0700330 # Mount additional directories as specified in .local_mounts file.
331 local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts"
332 if [[ -f ${local_mounts} ]]; then
333 info "Mounting local folders (read-only for safety concern)"
334 # format: mount_source
335 # or mount_source mount_point
336 # or # comments
337 local mount_source mount_point
338 while read mount_source mount_point; do
339 if [[ -z ${mount_source} ]]; then
340 continue
341 fi
342 # if only source is assigned, use source as mount point.
343 : ${mount_point:=${mount_source}}
344 debug " mounting ${mount_source} on ${mount_point}"
David James76764882012-10-24 19:46:29 -0700345 setup_mount "${mount_source}" "--bind" "${mount_point}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700346 # --bind can't initially be read-only so we have to do it via remount.
David James76764882012-10-24 19:46:29 -0700347 setup_mount "" "-o remount,ro" "${mount_point}"
Michael Krebs04c4f732012-09-07 18:31:46 -0700348 done < <(sed -e 's:#.*::' "${local_mounts}")
349 fi
350
Mike Frysinger9a50b642011-09-20 23:37:14 -0400351 CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)"
352 if [ -z "$CHROME_ROOT" ]; then
353 CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
354 2>/dev/null || :)"
355 CHROME_ROOT_AUTO=1
356 fi
357 if [[ -n "$CHROME_ROOT" ]]; then
358 if [[ ! -d "${CHROME_ROOT}/src" ]]; then
359 error "Not mounting chrome source"
David James76764882012-10-24 19:46:29 -0700360 rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
Mike Frysinger9a50b642011-09-20 23:37:14 -0400361 if [[ ! "$CHROME_ROOT_AUTO" ]]; then
362 exit 1
Don Garretta0e7ea12011-02-07 18:39:59 -0800363 fi
Mike Frysinger9a50b642011-09-20 23:37:14 -0400364 else
365 debug "Mounting chrome source at: $INNER_CHROME_ROOT"
David James76764882012-10-24 19:46:29 -0700366 echo $CHROME_ROOT > "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
367 setup_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800368 fi
369 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800370
Mike Frysingeracc4c9b2011-09-15 18:06:25 -0400371 # Install fuse module. Skip modprobe when possible for slight
372 # speed increase when initializing the env.
373 if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then
David James76764882012-10-24 19:46:29 -0700374 modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200375 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700376 fi
377
Mike Frysinger926a4b92012-06-27 15:22:28 -0400378 # Fix permissions on ccache tree. If this is a fresh chroot, then they
379 # might not be set up yet. Or if the user manually `rm -rf`-ed things,
380 # we need to reset it. Otherwise, gcc itself takes care of fixing things
381 # on demand, but only when it updates.
382 ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache"
383 if [[ ! -d ${ccache_dir} ]]; then
David James76764882012-10-24 19:46:29 -0700384 mkdir -p -m 2775 "${ccache_dir}"
Mike Frysinger926a4b92012-06-27 15:22:28 -0400385 fi
David James76764882012-10-24 19:46:29 -0700386 find -H "${ccache_dir}" -type d -exec chmod 2775 {} + &
387 find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} + &
David James342f1562011-07-15 15:21:18 -0700388
Matt Tennant298f61a2012-06-25 21:54:33 -0700389 # Certain files get copied into the chroot when entering.
390 for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do
David James76764882012-10-24 19:46:29 -0700391 copy_into_chroot_if_exists "${SUDO_HOME}/${fn}" "/home/${SUDO_USER}/${fn}"
Matt Tennantf7c9e772012-02-08 17:06:26 -0800392 done
Peter Mayo4411efe2012-09-21 04:41:27 -0400393 promote_api_keys
Matt Tennantd4316fe2011-08-30 12:06:42 -0700394
Mike Frysinger6601c522011-08-15 11:05:39 -0400395 # Make sure user's requested locales are available
396 # http://crosbug.com/19139
Mike Frysinger4fc9c272011-08-17 15:23:19 -0400397 # And make sure en_US{,.UTF-8} are always available as
398 # that what buildbot forces internally
399 locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \
Mike Frysinger6601c522011-08-15 11:05:39 -0400400 $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \
401 $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \
402 sort -u | sed '/^C$/d')
403 gen_locales=()
404 for l in ${locales}; do
405 if [[ ${l} == *.* ]]; then
406 enc=${l#*.}
407 else
408 enc="ISO-8859-1"
409 fi
410 case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in
411 utf8) enc="UTF-8";;
412 esac
413 gen_locales=("${gen_locales[@]}" "${l} ${enc}")
414 done
Mike Frysinger4d258cd2011-09-15 16:17:49 -0400415 if [[ ${#gen_locales[@]} -gt 0 ]] ; then
416 # Force LC_ALL=C to workaround slow string parsing in bash
417 # with long multibyte strings. Newer setups have this fixed,
418 # but locale-gen doesn't need to be run in any locale in the
419 # first place, so just go with C to keep it fast.
David James76764882012-10-24 19:46:29 -0700420 chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \
Mike Frysinger4d258cd2011-09-15 16:17:49 -0400421 -G "$(printf '%s\n' "${gen_locales[@]}")"
422 fi
Mike Frysinger6601c522011-08-15 11:05:39 -0400423
Dale Curtis98631732011-05-05 16:16:59 -0700424 # Fix permissions on shared memory to allow non-root users access to POSIX
David James76764882012-10-24 19:46:29 -0700425 # semaphores.
426 chmod -R 777 "${FLAGS_chroot}/dev/shm"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400427
428 # If the private overlays are installed, gsutil can use those credentials.
Gilad Arnold264f64d2012-09-06 14:01:45 -0700429 # We're also installing credentials for use by sudoed invocations.
430 boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto'
431 if [ -s "${FLAGS_trunk}/${boto}" ]; then
David James76764882012-10-24 19:46:29 -0700432 if [ ! -e "${FLAGS_chroot}/home/${SUDO_USER}/.boto" ]; then
433 user_symlink "trunk/${boto}" "${FLAGS_chroot}/home/${SUDO_USER}/.boto"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400434 fi
Gilad Arnold264f64d2012-09-06 14:01:45 -0700435 if [ ! -e "${FLAGS_chroot}/root/.boto" ]; then
David James76764882012-10-24 19:46:29 -0700436 ln -s "../home/${SUDO_USER}/trunk/${boto}" "${FLAGS_chroot}/root/.boto"
Gilad Arnold264f64d2012-09-06 14:01:45 -0700437 fi
Chris Wolfe916b1f12012-04-30 16:03:06 -0400438 fi
439
440 # Have found a few chroots where ~/.gsutil is owned by root:root, probably
441 # as a result of old gsutil or tools. This causes permission errors when
442 # gsutil cp tries to create its cache files, so ensure the user can
443 # actually write to their directory.
David James76764882012-10-24 19:46:29 -0700444 gsutil_dir="${FLAGS_chroot}/home/${SUDO_USER}/.gsutil"
445 if [ -d "${gsutil_dir}" ]; then
446 chown -R ${SUDO_UID}:${SUDO_GID} "${gsutil_dir}"
Chris Wolfe916b1f12012-04-30 16:03:06 -0400447 fi
David James546747b2010-03-23 15:19:43 -0700448 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000449}
450
rspangler@google.comd74220d2009-10-09 20:56:14 +0000451setup_env
452
Brian Harring35767822012-02-01 23:50:45 -0800453CHROOT_PASSTHRU=(
454 "BUILDBOT_BUILD=$FLAGS_build_number"
Brian Harring35767822012-02-01 23:50:45 -0800455 "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}"
Brian Harring35767822012-02-01 23:50:45 -0800456 "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}"
Brian Harring35767822012-02-01 23:50:45 -0800457)
Liam McLoughlin3b11b452011-03-14 16:04:07 -0700458
David James76764882012-10-24 19:46:29 -0700459# Add the whitelisted environment variables to CHROOT_PASSTHRU.
460load_environment_whitelist
461for var in "${ENVIRONMENT_WHITELIST[@]}" ; do
Brian Harring06d3c2e2012-08-23 07:35:43 -0700462 [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" )
Mario Limonciello7052ae12011-05-05 12:00:49 -0500463done
464
derat@google.com4e7a92b2009-11-21 23:44:14 +0000465# Run command or interactive shell. Also include the non-chrooted path to
466# the source trunk for scripts that may need to print it (e.g.
467# build_image.sh).
Brian Harring35767822012-02-01 23:50:45 -0800468
469if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then
470 cmd=( /bin/bash -l -c 'env "$@"' -- )
471elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then
472 # Complain that sudo is missing.
473 error "Failing since the chroot lacks sudo."
474 error "Requested enter_chroot command was: $@"
475 exit 127
476else
David James76764882012-10-24 19:46:29 -0700477 cmd=( sudo -i -u "${SUDO_USER}" )
Brian Harring35767822012-02-01 23:50:45 -0800478fi
479
David James76764882012-10-24 19:46:29 -0700480cmd+=( "${CHROOT_PASSTHRU[@]}" "$@" )
481exec chroot "${FLAGS_chroot}" "${cmd[@]}"