rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Darin Petkov | 47974d4 | 2011-03-03 15:55:04 -0800 | [diff] [blame] | 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 4 | # 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 Barnette | 8e6750d | 2011-08-22 12:35:52 -0700 | [diff] [blame] | 9 | SCRIPT_ROOT=$(readlink -f $(dirname "$0")/..) |
| 10 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 11 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 12 | # Script must be run outside the chroot and as root. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 13 | assert_outside_chroot |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 14 | assert_root_user |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 15 | |
| 16 | # Define command line flags |
| 17 | # See http://code.google.com/p/shflags/wiki/Documentation10x |
| 18 | DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \ |
| 19 | "The destination dir for the chroot environment." "d" |
| 20 | DEFINE_string trunk "$GCLIENT_ROOT" \ |
| 21 | "The source trunk to bind mount within the chroot." "s" |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 22 | DEFINE_string build_number "" \ |
| 23 | "The build-bot build number (when called by buildbot only)." "b" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 24 | DEFINE_string chrome_root "" \ |
| 25 | "The root of your chrome browser source. Should contain a 'src' subdir." |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 26 | DEFINE_string chrome_root_mount "/home/${SUDO_USER}/chrome_root" \ |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 27 | "The mount point of the chrome broswer source in the chroot." |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 28 | DEFINE_string cache_dir "" "Directory to use for caching." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 29 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 30 | DEFINE_boolean official_build $FLAGS_FALSE \ |
| 31 | "Set CHROMEOS_OFFICIAL=1 for release builds." |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 32 | DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent." |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 33 | DEFINE_boolean early_make_chroot $FLAGS_FALSE \ |
| 34 | "Internal flag. If set, the command is run as root without sudo." |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 35 | DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 36 | |
| 37 | # More useful help |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 38 | FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...] |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 39 | |
| 40 | One or more VAR=value pairs can be specified to export variables into |
| 41 | the chroot environment. For example: |
| 42 | |
| 43 | $0 FOO=bar BAZ=bel |
| 44 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 45 | If [-- command] is present, runs the command inside the chroot, |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 46 | after changing directory to /${SUDO_USER}/trunk/src/scripts. Note that neither |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 47 | the command nor args should include single quotes. For example: |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 48 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 49 | $0 -- ./build_platform_packages.sh |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 50 | |
| 51 | Otherwise, provides an interactive shell. |
| 52 | " |
| 53 | |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 54 | CROS_LOG_PREFIX=cros_sdk:enter_chroot |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 55 | SUDO_HOME=$(eval echo ~${SUDO_USER}) |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 56 | |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 57 | # Version of info from common.sh that only echos if --verbose is set. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 58 | debug() { |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 59 | if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 60 | info "$*" |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 61 | fi |
| 62 | } |
| 63 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 64 | # Parse command line flags |
| 65 | FLAGS "$@" || exit 1 |
| 66 | eval set -- "${FLAGS_ARGV}" |
| 67 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 68 | if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 69 | CHROMEOS_OFFICIAL=1 |
| 70 | fi |
| 71 | |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 72 | [ -z "${FLAGS_cache_dir}" ] && \ |
| 73 | die "--cache_dir is required" |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 74 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 75 | # Only now can we die on error. shflags functions leak non-zero error codes, |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 76 | # so will die prematurely if 'switch_to_strict_mode' is specified before now. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 77 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 78 | switch_to_strict_mode |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 79 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 80 | # These config files are to be copied into chroot if they exist in home dir. |
| 81 | FILES_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 Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 87 | INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 88 | CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 89 | INNER_DEPOT_TOOLS_ROOT="/home/${SUDO_USER}/depot_tools" # inside chroot |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 90 | FUSE_DEVICE="/dev/fuse" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 91 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 92 | chmod 0777 "$FLAGS_chroot/var/lock" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 93 | |
| 94 | LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
Mike Frysinger | 0a0e6ec | 2011-09-28 11:57:21 -0400 | [diff] [blame] | 95 | MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 96 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 97 | setup_mount() { |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 98 | # If necessary, mount $source in the host FS at $target inside the |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 99 | # 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 Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 102 | local source="$1" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 103 | local mount_args="-n $2" |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 104 | local target="$3" |
| 105 | |
Mike Frysinger | 0a0e6ec | 2011-09-28 11:57:21 -0400 | [diff] [blame] | 106 | local mounted_path="${MOUNTED_PATH}$target" |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 107 | |
Mike Frysinger | 9e5b0a4 | 2012-05-16 17:30:24 -0400 | [diff] [blame] | 108 | case " ${MOUNT_CACHE} " in |
| 109 | *" ${mounted_path} "*) |
Mike Frysinger | 2a97059 | 2011-09-20 22:49:01 -0400 | [diff] [blame] | 110 | # Already mounted! |
| 111 | ;; |
| 112 | *) |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 113 | mkdir -p "${mounted_path}" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 114 | # The args are left unquoted on purpose. |
| 115 | if [[ -n ${source} ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 116 | mount ${mount_args} "${source}" "${mounted_path}" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 117 | else |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 118 | mount ${mount_args} "${mounted_path}" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 119 | fi |
Mike Frysinger | 2a97059 | 2011-09-20 22:49:01 -0400 | [diff] [blame] | 120 | ;; |
| 121 | esac |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 124 | copy_ssh_config() { |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 125 | # 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 Tennant | 63c3cc5 | 2011-11-22 11:23:06 -0800 | [diff] [blame] | 132 | 'UseProxyIf' |
| 133 | 'GSSAPIAuthentication' |
| 134 | 'GSSAPIKeyExchange' |
| 135 | 'ProxyUseFdpass' |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 136 | ) |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 137 | local sshc="${SUDO_HOME}/.ssh/config" |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 138 | local chroot_ssh_dir="${1}" |
| 139 | local filter |
| 140 | local option |
| 141 | |
David James | 22dc2ba | 2012-11-29 15:46:58 -0800 | [diff] [blame] | 142 | if ! user_cp "${sshc}" "${chroot_ssh_dir}/config.orig" 2>/dev/null; then |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 143 | 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 James | 22dc2ba | 2012-11-29 15:46:58 -0800 | [diff] [blame] | 155 | sed "/^.*\(${filter}\).*$/d" "${chroot_ssh_dir}/config.orig" | \ |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 156 | user_clobber "${chroot_ssh_dir}/config" |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 159 | copy_into_chroot_if_exists() { |
Matt Tennant | f7c9e77 | 2012-02-08 17:06:26 -0800 | [diff] [blame] | 160 | # $1 is file path outside of chroot to copy to path $2 inside chroot. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 161 | [ -e "$1" ] && cp -p "$1" "${FLAGS_chroot}/$2" |
Matt Tennant | f7c9e77 | 2012-02-08 17:06:26 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 164 | # 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. |
| 170 | promote_api_keys() { |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 171 | local destination="${FLAGS_chroot}/home/${SUDO_USER}/.googleapikeys" |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 172 | # Don't disturb existing keys. They could be set differently |
| 173 | if [[ -s "${destination}" ]]; then |
| 174 | return 0 |
| 175 | fi |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 176 | if [[ -r "${SUDO_HOME}/.googleapikeys" ]]; then |
| 177 | cp -p "${SUDO_HOME}/.googleapikeys" "${destination}" |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 178 | if [[ -s "${destination}" ]] ; then |
| 179 | info "Copied Google API keys into chroot." |
| 180 | fi |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 181 | elif [[ -r "${SUDO_HOME}/.gyp/include.gypi" ]]; then |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 182 | 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 James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 186 | "${SUDO_HOME}/.gyp/include.gypi" | user_clobber "${destination}" |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 187 | if [[ -s "${destination}" ]]; then |
| 188 | info "Put discovered Google API keys into chroot." |
| 189 | fi |
| 190 | fi |
| 191 | } |
| 192 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 193 | setup_env() { |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 194 | ( |
| 195 | flock 200 |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 196 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 197 | # 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 James | 412b902 | 2011-07-15 19:54:16 -0700 | [diff] [blame] | 203 | |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 204 | debug "Mounting chroot environment." |
Mike Frysinger | 9e5b0a4 | 2012-05-16 17:30:24 -0400 | [diff] [blame] | 205 | MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts)) |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 206 | 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 Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 210 | if [ -d /run ]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 211 | setup_mount /run "--bind" /run |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 212 | if [ -d /run/shm ]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 213 | setup_mount /run/shm "--bind" /run/shm |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 214 | fi |
| 215 | fi |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 216 | setup_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}" |
Chris Sosa | 389634d | 2012-09-05 19:56:53 -0700 | [diff] [blame] | 217 | |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 218 | 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 Harring | 334050f | 2012-06-08 17:40:58 -0700 | [diff] [blame] | 224 | ALTERNATES="${FLAGS_trunk}/.repo/alternates" |
| 225 | |
| 226 | # Ensure this directory exists ourselves, and has the correct ownership. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 227 | user_mkdir "${ALTERNATES}" |
Brian Harring | 334050f | 2012-06-08 17:40:58 -0700 | [diff] [blame] | 228 | |
| 229 | unset ALTERNATES |
| 230 | |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 231 | IFS=$'\n'; |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 232 | required=( $( sudo -u "${SUDO_USER}" -- \ |
| 233 | "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \ |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 234 | "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) ) |
| 235 | unset IFS |
| 236 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 237 | setup_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \ |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 238 | "${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 James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 248 | user_mkdir ${FLAGS_trunk}/.repo/chroot/empty |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 249 | position=1 |
| 250 | for x in "${required[@]}"; do |
| 251 | base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 252 | setup_mount "${x}" "--bind" "${base}" |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 253 | if [ -e "${x}/.repo/alternates" ]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 254 | setup_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \ |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 255 | "${base}/.repo/alternates" |
| 256 | fi |
| 257 | position=$(( ${position} + 1 )) |
| 258 | done |
| 259 | unset required position base |
| 260 | fi |
| 261 | unset REFERENCE_DIR |
| 262 | |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 263 | chroot_cache='/var/cache/chromeos-cache' |
| 264 | debug "Setting up shared cache dir directory." |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 265 | 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 Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 268 | # 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 Drews | b688cbe | 2012-10-08 15:33:43 -0700 | [diff] [blame] | 276 | p="${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 277 | 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 Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 282 | fi |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 283 | |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 284 | if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then |
Mike Frysinger | 93a2eca | 2012-11-30 14:00:35 -0500 | [diff] [blame^] | 285 | # Clean up previous ssh agents. |
| 286 | rmdir "${FLAGS_chroot}"/tmp/ssh-* 2>/dev/null |
| 287 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 288 | if [ -n "${SSH_AUTH_SOCK}" -a -d "${SUDO_HOME}/.ssh" ]; then |
| 289 | TARGET_DIR="${FLAGS_chroot}/home/${SUDO_USER}/.ssh" |
| 290 | user_mkdir "${TARGET_DIR}" |
David James | 22dc2ba | 2012-11-29 15:46:58 -0800 | [diff] [blame] | 291 | ( |
| 292 | # Only copy ~/.ssh/{known_hosts,*.pub} if they exist. Since we set |
| 293 | # nullglob, this needs to happen within a subshell. |
| 294 | shopt -s nullglob |
| 295 | files=("${SUDO_HOME}"/.ssh/{known_hosts,*.pub}) |
| 296 | if [[ ${#files[@]} -gt 0 ]]; then |
| 297 | user_cp "${files[@]}" "${TARGET_DIR}/" |
| 298 | fi |
| 299 | ) |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 300 | copy_ssh_config "${TARGET_DIR}" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 301 | chown -R ${SUDO_UID}:${SUDO_GID} "${TARGET_DIR}" |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 302 | |
| 303 | # Don't try to bind mount the ssh agent dir if it has gone stale. |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 304 | ASOCK=${SSH_AUTH_SOCK%/*} |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 305 | if [ -d "${ASOCK}" ]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 306 | setup_mount "${ASOCK}" "--bind" "${ASOCK}" |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 307 | fi |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 308 | fi |
| 309 | fi |
| 310 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 311 | if [ -d "$SUDO_HOME/.subversion" ]; then |
| 312 | TARGET="/home/${SUDO_USER}/.subversion" |
| 313 | user_mkdir "${FLAGS_chroot}${TARGET}" |
| 314 | setup_mount "${SUDO_HOME}/.subversion" "--bind" "${TARGET}" |
Paul Drews | b4605b4 | 2012-10-11 23:10:43 -0700 | [diff] [blame] | 315 | # Symbolic-link the .subversion directory so sandboxed subversion.class |
| 316 | # clients can use it. |
Paul Drews | b4605b4 | 2012-10-11 23:10:43 -0700 | [diff] [blame] | 317 | for d in \ |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 318 | "${FLAGS_cache_dir}"/distfiles/{host,target}/svn-src/"${SUDO_USER}"; do |
Paul Drews | b4605b4 | 2012-10-11 23:10:43 -0700 | [diff] [blame] | 319 | if [[ ! -L "${d}/.subversion" ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 320 | rm -rf "${d}/.subversion" |
| 321 | user_mkdir "${d}" |
| 322 | user_symlink /home/${SUDO_USER}/.subversion "${d}/.subversion" |
Paul Drews | b4605b4 | 2012-10-11 23:10:43 -0700 | [diff] [blame] | 323 | fi |
| 324 | done |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 325 | fi |
| 326 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 327 | # A reference to the DEPOT_TOOLS path may be passed in by cros_sdk. |
| 328 | if [ -n "${DEPOT_TOOLS}" ]; then |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 329 | debug "Mounting depot_tools" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 330 | setup_mount "${DEPOT_TOOLS}" --bind "${INNER_DEPOT_TOOLS_ROOT}" |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 331 | fi |
| 332 | |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 333 | # Mount additional directories as specified in .local_mounts file. |
| 334 | local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts" |
| 335 | if [[ -f ${local_mounts} ]]; then |
| 336 | info "Mounting local folders (read-only for safety concern)" |
| 337 | # format: mount_source |
| 338 | # or mount_source mount_point |
| 339 | # or # comments |
| 340 | local mount_source mount_point |
| 341 | while read mount_source mount_point; do |
| 342 | if [[ -z ${mount_source} ]]; then |
| 343 | continue |
| 344 | fi |
| 345 | # if only source is assigned, use source as mount point. |
| 346 | : ${mount_point:=${mount_source}} |
| 347 | debug " mounting ${mount_source} on ${mount_point}" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 348 | setup_mount "${mount_source}" "--bind" "${mount_point}" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 349 | # --bind can't initially be read-only so we have to do it via remount. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 350 | setup_mount "" "-o remount,ro" "${mount_point}" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 351 | done < <(sed -e 's:#.*::' "${local_mounts}") |
| 352 | fi |
| 353 | |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 354 | CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)" |
| 355 | if [ -z "$CHROME_ROOT" ]; then |
| 356 | CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
| 357 | 2>/dev/null || :)" |
| 358 | CHROME_ROOT_AUTO=1 |
| 359 | fi |
| 360 | if [[ -n "$CHROME_ROOT" ]]; then |
| 361 | if [[ ! -d "${CHROME_ROOT}/src" ]]; then |
| 362 | error "Not mounting chrome source" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 363 | rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 364 | if [[ ! "$CHROME_ROOT_AUTO" ]]; then |
| 365 | exit 1 |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 366 | fi |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 367 | else |
| 368 | debug "Mounting chrome source at: $INNER_CHROME_ROOT" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 369 | echo $CHROME_ROOT > "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
| 370 | setup_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 371 | fi |
| 372 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 373 | |
Mike Frysinger | acc4c9b | 2011-09-15 18:06:25 -0400 | [diff] [blame] | 374 | # Install fuse module. Skip modprobe when possible for slight |
| 375 | # speed increase when initializing the env. |
| 376 | if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 377 | modprobe fuse 2> /dev/null ||\ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 378 | warn "-- Note: modprobe fuse failed. gmergefs will not work" |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 379 | fi |
| 380 | |
Mike Frysinger | 926a4b9 | 2012-06-27 15:22:28 -0400 | [diff] [blame] | 381 | # Fix permissions on ccache tree. If this is a fresh chroot, then they |
| 382 | # might not be set up yet. Or if the user manually `rm -rf`-ed things, |
| 383 | # we need to reset it. Otherwise, gcc itself takes care of fixing things |
| 384 | # on demand, but only when it updates. |
| 385 | ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache" |
| 386 | if [[ ! -d ${ccache_dir} ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 387 | mkdir -p -m 2775 "${ccache_dir}" |
Mike Frysinger | 926a4b9 | 2012-06-27 15:22:28 -0400 | [diff] [blame] | 388 | fi |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 389 | find -H "${ccache_dir}" -type d -exec chmod 2775 {} + & |
| 390 | find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} + & |
David James | 342f156 | 2011-07-15 15:21:18 -0700 | [diff] [blame] | 391 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 392 | # Certain files get copied into the chroot when entering. |
| 393 | for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 394 | copy_into_chroot_if_exists "${SUDO_HOME}/${fn}" "/home/${SUDO_USER}/${fn}" |
Matt Tennant | f7c9e77 | 2012-02-08 17:06:26 -0800 | [diff] [blame] | 395 | done |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 396 | promote_api_keys |
Matt Tennant | d4316fe | 2011-08-30 12:06:42 -0700 | [diff] [blame] | 397 | |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 398 | # Make sure user's requested locales are available |
| 399 | # http://crosbug.com/19139 |
Mike Frysinger | 4fc9c27 | 2011-08-17 15:23:19 -0400 | [diff] [blame] | 400 | # And make sure en_US{,.UTF-8} are always available as |
| 401 | # that what buildbot forces internally |
| 402 | locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \ |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 403 | $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \ |
| 404 | $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \ |
| 405 | sort -u | sed '/^C$/d') |
| 406 | gen_locales=() |
| 407 | for l in ${locales}; do |
| 408 | if [[ ${l} == *.* ]]; then |
| 409 | enc=${l#*.} |
| 410 | else |
| 411 | enc="ISO-8859-1" |
| 412 | fi |
| 413 | case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in |
| 414 | utf8) enc="UTF-8";; |
| 415 | esac |
| 416 | gen_locales=("${gen_locales[@]}" "${l} ${enc}") |
| 417 | done |
Mike Frysinger | 4d258cd | 2011-09-15 16:17:49 -0400 | [diff] [blame] | 418 | if [[ ${#gen_locales[@]} -gt 0 ]] ; then |
| 419 | # Force LC_ALL=C to workaround slow string parsing in bash |
| 420 | # with long multibyte strings. Newer setups have this fixed, |
| 421 | # but locale-gen doesn't need to be run in any locale in the |
| 422 | # first place, so just go with C to keep it fast. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 423 | chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \ |
Mike Frysinger | 4d258cd | 2011-09-15 16:17:49 -0400 | [diff] [blame] | 424 | -G "$(printf '%s\n' "${gen_locales[@]}")" |
| 425 | fi |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 426 | |
Dale Curtis | 9863173 | 2011-05-05 16:16:59 -0700 | [diff] [blame] | 427 | # Fix permissions on shared memory to allow non-root users access to POSIX |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 428 | # semaphores. |
| 429 | chmod -R 777 "${FLAGS_chroot}/dev/shm" |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 430 | |
| 431 | # If the private overlays are installed, gsutil can use those credentials. |
Gilad Arnold | 264f64d | 2012-09-06 14:01:45 -0700 | [diff] [blame] | 432 | # We're also installing credentials for use by sudoed invocations. |
| 433 | boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto' |
| 434 | if [ -s "${FLAGS_trunk}/${boto}" ]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 435 | if [ ! -e "${FLAGS_chroot}/home/${SUDO_USER}/.boto" ]; then |
| 436 | user_symlink "trunk/${boto}" "${FLAGS_chroot}/home/${SUDO_USER}/.boto" |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 437 | fi |
Gilad Arnold | 264f64d | 2012-09-06 14:01:45 -0700 | [diff] [blame] | 438 | if [ ! -e "${FLAGS_chroot}/root/.boto" ]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 439 | ln -s "../home/${SUDO_USER}/trunk/${boto}" "${FLAGS_chroot}/root/.boto" |
Gilad Arnold | 264f64d | 2012-09-06 14:01:45 -0700 | [diff] [blame] | 440 | fi |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 441 | fi |
| 442 | |
| 443 | # Have found a few chroots where ~/.gsutil is owned by root:root, probably |
| 444 | # as a result of old gsutil or tools. This causes permission errors when |
| 445 | # gsutil cp tries to create its cache files, so ensure the user can |
| 446 | # actually write to their directory. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 447 | gsutil_dir="${FLAGS_chroot}/home/${SUDO_USER}/.gsutil" |
| 448 | if [ -d "${gsutil_dir}" ]; then |
| 449 | chown -R ${SUDO_UID}:${SUDO_GID} "${gsutil_dir}" |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 450 | fi |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 451 | ) 200>>"$LOCKFILE" || die "setup_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 452 | } |
| 453 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 454 | setup_env |
| 455 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 456 | CHROOT_PASSTHRU=( |
| 457 | "BUILDBOT_BUILD=$FLAGS_build_number" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 458 | "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 459 | "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}" |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 460 | ) |
Liam McLoughlin | 3b11b45 | 2011-03-14 16:04:07 -0700 | [diff] [blame] | 461 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 462 | # Add the whitelisted environment variables to CHROOT_PASSTHRU. |
| 463 | load_environment_whitelist |
| 464 | for var in "${ENVIRONMENT_WHITELIST[@]}" ; do |
Brian Harring | 06d3c2e | 2012-08-23 07:35:43 -0700 | [diff] [blame] | 465 | [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" ) |
Mario Limonciello | 7052ae1 | 2011-05-05 12:00:49 -0500 | [diff] [blame] | 466 | done |
| 467 | |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 468 | # Run command or interactive shell. Also include the non-chrooted path to |
| 469 | # the source trunk for scripts that may need to print it (e.g. |
| 470 | # build_image.sh). |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 471 | |
| 472 | if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then |
| 473 | cmd=( /bin/bash -l -c 'env "$@"' -- ) |
| 474 | elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then |
| 475 | # Complain that sudo is missing. |
| 476 | error "Failing since the chroot lacks sudo." |
| 477 | error "Requested enter_chroot command was: $@" |
| 478 | exit 127 |
| 479 | else |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 480 | cmd=( sudo -i -u "${SUDO_USER}" ) |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 481 | fi |
| 482 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 483 | cmd+=( "${CHROOT_PASSTHRU[@]}" "$@" ) |
| 484 | exec chroot "${FLAGS_chroot}" "${cmd[@]}" |