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." |
Hidehiko Abe | 63d6c46 | 2017-03-02 17:40:54 +0900 | [diff] [blame] | 29 | DEFINE_string goma_dir "" "Goma installed directory." |
| 30 | DEFINE_string goma_client_json "" "Service account json file for goma." |
Yong Hong | 7dee2cc | 2018-02-06 16:02:56 +0800 | [diff] [blame] | 31 | DEFINE_string working_dir "" \ |
| 32 | "The working directory relative to ${CHROOT_TRUNK_DIR} for the command in \ |
| 33 | chroot, must start with '/' if set." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 34 | |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 35 | DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent." |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 36 | DEFINE_boolean early_make_chroot $FLAGS_FALSE \ |
| 37 | "Internal flag. If set, the command is run as root without sudo." |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 38 | DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 39 | |
| 40 | # More useful help |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 41 | FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...] |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 42 | |
| 43 | One or more VAR=value pairs can be specified to export variables into |
| 44 | the chroot environment. For example: |
| 45 | |
| 46 | $0 FOO=bar BAZ=bel |
| 47 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 48 | If [-- command] is present, runs the command inside the chroot, |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 49 | after changing directory to /${SUDO_USER}/trunk/src/scripts. Note that neither |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 50 | the command nor args should include single quotes. For example: |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 51 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 52 | $0 -- ./build_platform_packages.sh |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 53 | |
| 54 | Otherwise, provides an interactive shell. |
| 55 | " |
| 56 | |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 57 | CROS_LOG_PREFIX=cros_sdk:enter_chroot |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 58 | SUDO_HOME=$(eval echo ~${SUDO_USER}) |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 59 | |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 60 | # 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] | 61 | debug() { |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 62 | if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 63 | info "$*" |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 64 | fi |
| 65 | } |
| 66 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 67 | # Parse command line flags |
| 68 | FLAGS "$@" || exit 1 |
| 69 | eval set -- "${FLAGS_ARGV}" |
| 70 | |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 71 | [ -z "${FLAGS_cache_dir}" ] && \ |
| 72 | die "--cache_dir is required" |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 73 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 74 | # 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] | 75 | # 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] | 76 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 77 | switch_to_strict_mode |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 78 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 79 | # These config files are to be copied into chroot if they exist in home dir. |
Prathmesh Prabhu | 74ebbbd | 2015-03-17 13:50:29 -0700 | [diff] [blame] | 80 | # Additionally, git relevant files are copied by setup_git. |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 81 | FILES_TO_COPY_TO_CHROOT=( |
Ben Pastene | aa6c930 | 2020-01-28 11:41:57 -0800 | [diff] [blame] | 82 | # Creds used to authenticate with LUCI services. |
| 83 | .config/chrome_infra/auth/creds.json |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 84 | .gdata_cred.txt # User/password for Google Docs on chromium.org |
| 85 | .gdata_token # Auth token for Google Docs on chromium.org |
Ben Pastene | 4ef8d11 | 2020-05-05 14:16:21 -0700 | [diff] [blame] | 86 | .goma_client_oauth2_config # Auth token for Goma |
Marc Herbert | 0c42206 | 2015-05-29 16:18:54 -0700 | [diff] [blame] | 87 | .inputrc # Preserve command line customizations |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 88 | ) |
Mike Frysinger | 270f405 | 2019-12-13 04:31:30 -0500 | [diff] [blame] | 89 | if [[ "${SUDO_USER:-${USER}}" == "chrome-bot" ]]; then |
Mike Frysinger | 6120545 | 2019-12-11 05:08:02 -0500 | [diff] [blame] | 90 | # Builders still haven't migrated fully to gitcookies. |
| 91 | # https://crbug.com/1032944 |
| 92 | FILES_TO_COPY_TO_CHROOT+=( .netrc ) |
| 93 | fi |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 94 | |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 95 | INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 96 | CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 97 | FUSE_DEVICE="/dev/fuse" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 98 | |
Mike Frysinger | 01c205d | 2013-03-22 00:59:58 -0400 | [diff] [blame] | 99 | # We can't use /var/lock because that might be a symlink to /run/lock outside |
| 100 | # of the chroot. Or /run on the host system might not exist. |
| 101 | LOCKFILE="${FLAGS_chroot}/.enter_chroot.lock" |
Mike Frysinger | 0a0e6ec | 2011-09-28 11:57:21 -0400 | [diff] [blame] | 102 | MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 103 | |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 104 | # Reset the depot tools/internal trunk pathways to what they'll |
| 105 | # be w/in the chroot. |
| 106 | set_chroot_trunk_dir "${FLAGS_chroot}" |
| 107 | |
Mike Frysinger | eab3f07 | 2019-08-21 22:57:49 -0400 | [diff] [blame] | 108 | # Writes stdin to the given file name as the sudo user in overwrite mode. |
| 109 | # |
| 110 | # $@ - The output file names. |
| 111 | user_clobber() { |
| 112 | install -m644 -o ${SUDO_UID} -g ${SUDO_GID} /dev/stdin "$@" |
| 113 | } |
| 114 | |
| 115 | # Copies the specified file owned by the user to the specified location. |
| 116 | # If the copy fails as root (e.g. due to root_squash and NFS), retry the copy |
| 117 | # with the user's account before failing. |
| 118 | user_cp() { |
| 119 | cp -p "$@" 2>/dev/null || sudo -u ${SUDO_USER} -- cp -p "$@" |
| 120 | } |
| 121 | |
| 122 | # Appends stdin to the given file name as the sudo user. |
| 123 | # |
| 124 | # $1 - The output file name. |
| 125 | user_append() { |
| 126 | cat >> "$1" |
| 127 | chown ${SUDO_UID}:${SUDO_GID} "$1" |
| 128 | } |
| 129 | |
| 130 | # Create the specified directory, along with parents, as the sudo user. |
| 131 | # |
| 132 | # $@ - The directories to create. |
| 133 | user_mkdir() { |
| 134 | install -o ${SUDO_UID} -g ${SUDO_GID} -d "$@" |
| 135 | } |
| 136 | |
| 137 | # Create the specified symlink as the sudo user. |
| 138 | # |
| 139 | # $1 - Link target |
| 140 | # $2 - Link name |
| 141 | user_symlink() { |
| 142 | ln -sfT "$1" "$2" |
| 143 | chown -h ${SUDO_UID}:${SUDO_GID} "$2" |
| 144 | } |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 145 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 146 | setup_mount() { |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 147 | # If necessary, mount $source in the host FS at $target inside the |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 148 | # chroot directory with $mount_args. We don't write to /etc/mtab because |
| 149 | # these mounts are all contained within an unshare and are therefore |
| 150 | # inaccessible to other namespaces (e.g. the host desktop system). |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 151 | local source="$1" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 152 | local mount_args="-n $2" |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 153 | local target="$3" |
| 154 | |
Mike Frysinger | 0a0e6ec | 2011-09-28 11:57:21 -0400 | [diff] [blame] | 155 | local mounted_path="${MOUNTED_PATH}$target" |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 156 | |
Mike Frysinger | 9e5b0a4 | 2012-05-16 17:30:24 -0400 | [diff] [blame] | 157 | case " ${MOUNT_CACHE} " in |
| 158 | *" ${mounted_path} "*) |
Mike Frysinger | 2a97059 | 2011-09-20 22:49:01 -0400 | [diff] [blame] | 159 | # Already mounted! |
| 160 | ;; |
| 161 | *) |
Mike Frysinger | add9976 | 2014-03-27 13:17:58 -0400 | [diff] [blame] | 162 | # If it doesn't exist, assume they want a dir. But don't blindly run |
| 163 | # it all the time in case they're trying to bind mount a file. |
| 164 | if [[ ! -e ${mounted_path} ]]; then |
| 165 | mkdir -p "${mounted_path}" |
| 166 | fi |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 167 | # The args are left unquoted on purpose. |
| 168 | if [[ -n ${source} ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 169 | mount ${mount_args} "${source}" "${mounted_path}" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 170 | else |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 171 | mount ${mount_args} "${mounted_path}" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 172 | fi |
Mike Frysinger | 2a97059 | 2011-09-20 22:49:01 -0400 | [diff] [blame] | 173 | ;; |
| 174 | esac |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 177 | copy_ssh_config() { |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 178 | # Copy user .ssh/config into the chroot filtering out strings not supported |
| 179 | # by the chroot ssh. The chroot .ssh directory is passed in as the first |
| 180 | # parameter. |
| 181 | |
| 182 | # ssh options to filter out. The entire strings containing these substrings |
| 183 | # will be deleted before copying. |
| 184 | local bad_options=( |
Matt Tennant | 63c3cc5 | 2011-11-22 11:23:06 -0800 | [diff] [blame] | 185 | 'UseProxyIf' |
| 186 | 'GSSAPIAuthentication' |
| 187 | 'GSSAPIKeyExchange' |
| 188 | 'ProxyUseFdpass' |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 189 | ) |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 190 | local sshc="${SUDO_HOME}/.ssh/config" |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 191 | local chroot_ssh_dir="${1}" |
| 192 | local filter |
| 193 | local option |
| 194 | |
David James | 22dc2ba | 2012-11-29 15:46:58 -0800 | [diff] [blame] | 195 | 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] | 196 | return # Nothing to copy. |
| 197 | fi |
| 198 | |
| 199 | for option in "${bad_options[@]}" |
| 200 | do |
| 201 | if [ -z "${filter}" ]; then |
| 202 | filter="${option}" |
| 203 | else |
| 204 | filter+="\\|${option}" |
| 205 | fi |
| 206 | done |
| 207 | |
David James | 22dc2ba | 2012-11-29 15:46:58 -0800 | [diff] [blame] | 208 | sed "/^.*\(${filter}\).*$/d" "${chroot_ssh_dir}/config.orig" | \ |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 209 | user_clobber "${chroot_ssh_dir}/config" |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 212 | copy_into_chroot_if_exists() { |
Matt Tennant | f7c9e77 | 2012-02-08 17:06:26 -0800 | [diff] [blame] | 213 | # $1 is file path outside of chroot to copy to path $2 inside chroot. |
Mike Frysinger | 6120545 | 2019-12-11 05:08:02 -0500 | [diff] [blame] | 214 | if [[ -e "$1" ]]; then |
Ben Pastene | aa6c930 | 2020-01-28 11:41:57 -0800 | [diff] [blame] | 215 | local dir |
| 216 | dir=$(dirname "${FLAGS_chroot}/$2") |
| 217 | if [[ ! -d "${dir}" ]]; then |
| 218 | user_mkdir "${dir}" |
| 219 | fi |
Mike Frysinger | 0c4821f | 2019-12-15 19:35:43 -0500 | [diff] [blame] | 220 | user_cp "$1" "${FLAGS_chroot}/$2" |
Mike Frysinger | 6120545 | 2019-12-11 05:08:02 -0500 | [diff] [blame] | 221 | fi |
Matt Tennant | f7c9e77 | 2012-02-08 17:06:26 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 224 | # Usage: promote_api_keys |
| 225 | # This takes care of getting the developer API keys into the chroot where |
| 226 | # chrome can build with them. It needs to take it from the places a dev |
| 227 | # is likely to put them, and recognize that older chroots may or may not |
| 228 | # have been used since the concept of keys got added, as well as before |
Thiemo Nagel | 3a4d23b | 2017-05-26 16:52:05 +0200 | [diff] [blame] | 229 | # and after the developer deciding to grab their own keys. |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 230 | promote_api_keys() { |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 231 | local destination="${FLAGS_chroot}/home/${SUDO_USER}/.googleapikeys" |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 232 | # Don't disturb existing keys. They could be set differently |
| 233 | if [[ -s "${destination}" ]]; then |
| 234 | return 0 |
| 235 | fi |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 236 | if [[ -r "${SUDO_HOME}/.googleapikeys" ]]; then |
| 237 | cp -p "${SUDO_HOME}/.googleapikeys" "${destination}" |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 238 | if [[ -s "${destination}" ]] ; then |
| 239 | info "Copied Google API keys into chroot." |
| 240 | fi |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 241 | elif [[ -r "${SUDO_HOME}/.gyp/include.gypi" ]]; then |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 242 | local NAME="('google_(api_key|default_client_(id|secret))')" |
| 243 | local WS="[[:space:]]*" |
| 244 | local CONTENTS="('[^\\\\']*')" |
| 245 | sed -nr -e "/^${WS}${NAME}${WS}[:=]${WS}${CONTENTS}.*/{s//\1: \4,/;p;}" \ |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 246 | "${SUDO_HOME}/.gyp/include.gypi" | user_clobber "${destination}" |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 247 | if [[ -s "${destination}" ]]; then |
| 248 | info "Put discovered Google API keys into chroot." |
| 249 | fi |
| 250 | fi |
| 251 | } |
| 252 | |
Mike Frysinger | 5ba5753 | 2020-04-07 01:05:25 -0400 | [diff] [blame] | 253 | # Sanity check the user's environment locale settings. |
| 254 | check_locale() { |
| 255 | if [[ "$(locale -k charmap)" != 'charmap="UTF-8"' ]]; then |
| 256 | error "Your locale appears to not support UTF-8 which is required." |
| 257 | error "The output from 'locale':" |
| 258 | locale |
| 259 | error "The output from 'locale -k LC_CTYPE':" |
| 260 | locale -k LC_CTYPE |
| 261 | error "The language env vars:" |
| 262 | env | grep -E '^(LC_|LANG)' |
| 263 | if [[ -n "${LC_ALL}" ]]; then |
| 264 | error "Never set LC_ALL (=${LC_ALL}) in your environment; only set LANG." |
| 265 | fi |
| 266 | die_notrace "Please fix your locale settings by setting LANG to UTF-8" \ |
| 267 | "compatible locale and removing any LC_ variable settings." |
| 268 | fi |
Mike Frysinger | 5ba5753 | 2020-04-07 01:05:25 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Mike Frysinger | 4f2d5cc | 2013-06-09 23:34:52 -0400 | [diff] [blame] | 271 | generate_locales() { |
| 272 | # Make sure user's requested locales are available |
| 273 | # http://crosbug.com/19139 |
| 274 | # And make sure en_US{,.UTF-8} are always available as |
| 275 | # that what buildbot forces internally |
| 276 | local l locales gen_locales=() |
| 277 | |
| 278 | locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \ |
| 279 | $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \ |
| 280 | $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \ |
| 281 | sort -u | sed '/^C$/d') |
| 282 | for l in ${locales}; do |
| 283 | if [[ ${l} == *.* ]]; then |
| 284 | enc=${l#*.} |
| 285 | else |
| 286 | enc="ISO-8859-1" |
| 287 | fi |
| 288 | case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in |
| 289 | utf8) enc="UTF-8";; |
| 290 | esac |
| 291 | gen_locales+=("${l} ${enc}") |
| 292 | done |
| 293 | if [[ ${#gen_locales[@]} -gt 0 ]] ; then |
| 294 | # Force LC_ALL=C to workaround slow string parsing in bash |
| 295 | # with long multibyte strings. Newer setups have this fixed, |
| 296 | # but locale-gen doesn't need to be run in any locale in the |
| 297 | # first place, so just go with C to keep it fast. |
Brian Norris | e83a147 | 2017-08-31 16:24:45 -0700 | [diff] [blame] | 298 | # Force jobs=1 to work around locale-gen weirdness where it simultaneously |
| 299 | # wants to and doesn't want to run multiple jobs (and complains about it). |
| 300 | # crbug.com/761153 |
Mike Frysinger | bdc7437 | 2020-05-20 03:30:25 -0400 | [diff] [blame] | 301 | # Force PATH to the right value inside the SDK in case the host distro has |
| 302 | # its own incompatible setup. |
| 303 | chroot "${FLAGS_chroot}" env LC_ALL=C PATH='/bin:/sbin:/usr/bin:/usr/sbin' \ |
| 304 | locale-gen -q -u -j 1 -G "$(printf '%s\n' "${gen_locales[@]}")" |
Mike Frysinger | 4f2d5cc | 2013-06-09 23:34:52 -0400 | [diff] [blame] | 305 | fi |
| 306 | } |
| 307 | |
Mike Frysinger | 09c27c7 | 2019-12-10 18:44:39 -0500 | [diff] [blame] | 308 | git_config() { |
| 309 | USER="${SUDO_USER:-${USER}}" \ |
| 310 | HOME="${SUDO_HOME:-${HOME}}" \ |
| 311 | git config "$@" |
| 312 | } |
| 313 | |
Mike Frysinger | 2a67d48 | 2019-12-13 14:07:19 -0500 | [diff] [blame] | 314 | # The --type=path option is new to git-2.18 and not everyone upgrades. |
| 315 | # But not everyone uses the ~ prefix, so try that option if needed. |
| 316 | git_config_path() { |
| 317 | local out |
| 318 | out=$(git_config "$@") |
| 319 | if [[ "${out:0:1}" == "~" ]]; then |
| 320 | git_config --type path "$@" |
| 321 | else |
| 322 | echo "${out}" |
| 323 | fi |
| 324 | } |
| 325 | |
Prathmesh Prabhu | 74ebbbd | 2015-03-17 13:50:29 -0700 | [diff] [blame] | 326 | setup_git() { |
| 327 | # Copy .gitconfig into chroot so repo and git can be used from inside. |
| 328 | # This is required for repo to work since it validates the email address. |
| 329 | copy_into_chroot_if_exists "${SUDO_HOME}/.gitconfig" \ |
| 330 | "/home/${SUDO_USER}/.gitconfig" |
| 331 | local -r chroot_gitconfig="${FLAGS_chroot}/home/${SUDO_USER}/.gitconfig" |
| 332 | |
| 333 | # If the user didn't set up their username in their gitconfig, look |
| 334 | # at the default git settings for the user. |
| 335 | if ! git config -f "${chroot_gitconfig}" user.email >& /dev/null; then |
| 336 | local ident=$(cd /; sudo -u ${SUDO_USER} -- git var GIT_COMMITTER_IDENT || \ |
| 337 | :) |
| 338 | local ident_name=${ident%% <*} |
| 339 | local ident_email=${ident%%>*}; ident_email=${ident_email##*<} |
| 340 | git config -f "${chroot_gitconfig}" --replace-all user.name \ |
| 341 | "${ident_name}" || : |
| 342 | git config -f "${chroot_gitconfig}" --replace-all user.email \ |
| 343 | "${ident_email}" || : |
| 344 | fi |
| 345 | |
| 346 | # Copy the gitcookies file, updating the user's gitconfig to point to it. |
| 347 | local gitcookies |
Mike Frysinger | 2a67d48 | 2019-12-13 14:07:19 -0500 | [diff] [blame] | 348 | gitcookies="$(git_config_path --file "${chroot_gitconfig}" \ |
Mike Frysinger | 09c27c7 | 2019-12-10 18:44:39 -0500 | [diff] [blame] | 349 | --get http.cookiefile)" |
Prathmesh Prabhu | 74ebbbd | 2015-03-17 13:50:29 -0700 | [diff] [blame] | 350 | if [[ $? -ne 0 ]]; then |
| 351 | # Try the default location anyway. |
| 352 | gitcookies="${SUDO_HOME}/.gitcookies" |
| 353 | fi |
| 354 | copy_into_chroot_if_exists "${gitcookies}" "/home/${SUDO_USER}/.gitcookies" |
| 355 | local -r chroot_gitcookies="${FLAGS_chroot}/home/${SUDO_USER}/.gitcookies" |
| 356 | if [[ -e "${chroot_gitcookies}" ]]; then |
| 357 | git config -f "${chroot_gitconfig}" --replace-all http.cookiefile \ |
| 358 | "/home/${SUDO_USER}/.gitcookies" |
| 359 | fi |
| 360 | # This line must be at the end because using `git config` changes ownership of |
| 361 | # the .gitconfig. |
| 362 | chown ${SUDO_UID}:${SUDO_GID} "${chroot_gitconfig}" |
| 363 | } |
| 364 | |
Xiyuan Xia | a6e21ae | 2018-03-28 08:47:23 -0700 | [diff] [blame] | 365 | setup_gclient_cache_dir_mount() { |
| 366 | # Mount "cache_dir" if a glient checkout depends on it. |
| 367 | # Otherwise, git command inside chroot fails. See https://crbug.com/747349 |
| 368 | local checkout_root="$1" |
| 369 | |
| 370 | if [[ ! -e "${checkout_root}/.gclient" ]]; then |
| 371 | return 0 |
| 372 | fi |
| 373 | |
| 374 | local cache_dir=$(sed -n -E "s/^ *cache_dir *= *'(.*)'/\1/p" \ |
| 375 | "${checkout_root}/.gclient") |
| 376 | if [[ -z "${cache_dir}" ]]; then |
| 377 | return 0 |
| 378 | fi |
| 379 | |
Mike Frysinger | d5a3e6d | 2019-08-07 15:23:15 -0400 | [diff] [blame] | 380 | # See if the cache dir exists outside of the chroot. |
Xiyuan Xia | a6e21ae | 2018-03-28 08:47:23 -0700 | [diff] [blame] | 381 | if [[ ! -d "${cache_dir}" ]]; then |
Mike Frysinger | d5a3e6d | 2019-08-07 15:23:15 -0400 | [diff] [blame] | 382 | # See if it exists inside the chroot (which can happen if the checkout was |
| 383 | # created in there). |
| 384 | if [[ ! -d "${FLAGS_chroot}/${cache_dir}" ]]; then |
| 385 | warn "Gclient cache dir \"${cache_dir}\" is not a directory." |
| 386 | fi |
Xiyuan Xia | a6e21ae | 2018-03-28 08:47:23 -0700 | [diff] [blame] | 387 | return 0 |
| 388 | fi |
| 389 | |
| 390 | setup_mount "${cache_dir}" "--bind" "${cache_dir}" |
| 391 | } |
| 392 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 393 | setup_env() { |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 394 | ( |
| 395 | flock 200 |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 396 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 397 | # Make the lockfile writable for backwards compatibility. |
| 398 | chown ${SUDO_UID}:${SUDO_GID} "${LOCKFILE}" |
| 399 | |
| 400 | # Refresh /etc/resolv.conf and /etc/hosts in the chroot. |
| 401 | install -C -m644 /etc/resolv.conf ${FLAGS_chroot}/etc/resolv.conf |
| 402 | install -C -m644 /etc/hosts ${FLAGS_chroot}/etc/hosts |
David James | 412b902 | 2011-07-15 19:54:16 -0700 | [diff] [blame] | 403 | |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 404 | debug "Mounting chroot environment." |
Steev Klimaszewski | a10974c | 2013-11-30 20:38:34 -0600 | [diff] [blame] | 405 | mount --make-rslave / |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 406 | if grep -q -e "${FLAGS_chroot}[[:space:]]" /proc/mounts; then |
| 407 | debug "Changing $FLAGS_chroot to a private subtree." |
| 408 | mount --make-private "$FLAGS_chroot" |
| 409 | fi |
Mike Frysinger | 9e5b0a4 | 2012-05-16 17:30:24 -0400 | [diff] [blame] | 410 | MOUNT_CACHE=$(echo $(awk '{print $2}' /proc/mounts)) |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 411 | setup_mount none "-t proc" /proc |
| 412 | setup_mount none "-t sysfs" /sys |
Gaurav Shah | 365b706 | 2013-12-03 18:12:58 -0800 | [diff] [blame] | 413 | if grep -q binfmt_misc /proc/filesystems; then |
| 414 | setup_mount binfmt_misc "-t binfmt_misc" /proc/sys/fs/binfmt_misc |
| 415 | fi |
Victor Dodon | c34d4bc | 2016-05-09 12:09:21 -0700 | [diff] [blame] | 416 | if grep -q configfs /proc/filesystems; then |
| 417 | setup_mount none "-t configfs" /sys/kernel/config |
| 418 | fi |
Mike Frysinger | b5080fb | 2019-03-19 04:13:02 -0400 | [diff] [blame] | 419 | setup_mount /dev "--rbind" /dev |
David James | 482fb29 | 2013-03-10 21:06:54 -0700 | [diff] [blame] | 420 | if [[ -d /run ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 421 | setup_mount /run "--bind" /run |
David James | 482fb29 | 2013-03-10 21:06:54 -0700 | [diff] [blame] | 422 | if [[ -d /run/shm && ! -L /run/shm ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 423 | setup_mount /run/shm "--bind" /run/shm |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 424 | fi |
| 425 | fi |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 426 | |
Mike Frysinger | 4f2d5cc | 2013-06-09 23:34:52 -0400 | [diff] [blame] | 427 | # Do this early as it's slow and only needs basic mounts (above). |
| 428 | generate_locales & |
| 429 | |
Michael Spang | 15144f4 | 2013-04-02 17:42:55 -0400 | [diff] [blame] | 430 | setup_mount "${FLAGS_trunk}" "--rbind" "${CHROOT_TRUNK_DIR}" |
Chris Sosa | 389634d | 2012-09-05 19:56:53 -0700 | [diff] [blame] | 431 | |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 432 | debug "Setting up referenced repositories if required." |
Mike Frysinger | 2a67d48 | 2019-12-13 14:07:19 -0500 | [diff] [blame] | 433 | REFERENCE_DIR=$(git_config_path --file \ |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 434 | "${FLAGS_trunk}/.repo/manifests.git/config" \ |
| 435 | repo.reference) |
| 436 | if [ -n "${REFERENCE_DIR}" ]; then |
| 437 | |
Brian Harring | 334050f | 2012-06-08 17:40:58 -0700 | [diff] [blame] | 438 | ALTERNATES="${FLAGS_trunk}/.repo/alternates" |
| 439 | |
| 440 | # Ensure this directory exists ourselves, and has the correct ownership. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 441 | user_mkdir "${ALTERNATES}" |
Brian Harring | 334050f | 2012-06-08 17:40:58 -0700 | [diff] [blame] | 442 | |
| 443 | unset ALTERNATES |
| 444 | |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 445 | IFS=$'\n'; |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 446 | required=( $( sudo -u "${SUDO_USER}" -- \ |
Mike Frysinger | 1658c3e | 2019-08-21 20:24:02 -0400 | [diff] [blame] | 447 | "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates" \ |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 448 | "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) ) |
| 449 | unset IFS |
| 450 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 451 | setup_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \ |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 452 | "${CHROOT_TRUNK_DIR}/.repo/alternates" |
| 453 | |
| 454 | # Note that as we're bringing up each referened repo, we also |
| 455 | # mount bind an empty directory over its alternates. This is |
| 456 | # required to suppress git from tracing through it- we already |
| 457 | # specify the required alternates for CHROOT_TRUNK_DIR, no point |
| 458 | # in having git try recursing through each on their own. |
| 459 | # |
| 460 | # Finally note that if you're unfamiliar w/ chroot/vfs semantics, |
| 461 | # the bind is visible only w/in the chroot. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 462 | user_mkdir ${FLAGS_trunk}/.repo/chroot/empty |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 463 | position=1 |
| 464 | for x in "${required[@]}"; do |
| 465 | base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 466 | setup_mount "${x}" "--bind" "${base}" |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 467 | if [ -e "${x}/.repo/alternates" ]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 468 | setup_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \ |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 469 | "${base}/.repo/alternates" |
| 470 | fi |
| 471 | position=$(( ${position} + 1 )) |
| 472 | done |
| 473 | unset required position base |
| 474 | fi |
| 475 | unset REFERENCE_DIR |
| 476 | |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 477 | chroot_cache='/var/cache/chromeos-cache' |
| 478 | debug "Setting up shared cache dir directory." |
Mike Frysinger | 292f21b | 2020-06-30 19:14:00 -0400 | [diff] [blame] | 479 | user_mkdir "${FLAGS_cache_dir}"/distfiles |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 480 | user_mkdir "${FLAGS_chroot}/${chroot_cache}" |
| 481 | setup_mount "${FLAGS_cache_dir}" "--bind" "${chroot_cache}" |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 482 | # TODO(build): remove this as of 12/01/12. |
| 483 | # Because of how distfiles -> cache_dir was deployed, if this isn't |
| 484 | # a symlink, we *know* the ondisk pathways aren't compatible- thus |
| 485 | # fix it now. |
| 486 | distfiles_path="${FLAGS_chroot}/var/cache/distfiles" |
| 487 | if [ ! -L "${distfiles_path}" ]; then |
| 488 | # While we're at it, ensure the var is exported w/in the chroot; it |
| 489 | # won't exist if distfiles isn't a symlink. |
Paul Drews | b688cbe | 2012-10-08 15:33:43 -0700 | [diff] [blame] | 490 | p="${FLAGS_chroot}/etc/profile.d/chromeos-cachedir.sh" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 491 | rm -rf "${distfiles_path}" |
| 492 | ln -s chromeos-cache/distfiles "${distfiles_path}" |
| 493 | mkdir -p -m 775 "${p%/*}" |
| 494 | echo 'export CHROMEOS_CACHEDIR=${chroot_cache}' > "${p}" |
| 495 | chmod 0644 "${p}" |
Brian Harring | 7b6f377 | 2012-09-23 14:01:13 -0700 | [diff] [blame] | 496 | fi |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame] | 497 | |
Aviv Keshet | 97a35f4 | 2014-07-24 12:11:10 -0700 | [diff] [blame] | 498 | if [ -d "${SUDO_HOME}/.cidb_creds" ]; then |
| 499 | setup_mount "${SUDO_HOME}/.cidb_creds" --bind \ |
| 500 | "/home/${SUDO_USER}/.cidb_creds" |
| 501 | fi |
| 502 | |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 503 | if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 504 | if [ -n "${SSH_AUTH_SOCK}" -a -d "${SUDO_HOME}/.ssh" ]; then |
Mike Frysinger | add9976 | 2014-03-27 13:17:58 -0400 | [diff] [blame] | 505 | local target_ssh="/home/${SUDO_USER}/.ssh" |
| 506 | TARGET_DIR="${FLAGS_chroot}${target_ssh}" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 507 | user_mkdir "${TARGET_DIR}" |
Mike Frysinger | add9976 | 2014-03-27 13:17:58 -0400 | [diff] [blame] | 508 | |
| 509 | local known_hosts="${SUDO_HOME}/.ssh/known_hosts" |
| 510 | if [[ -e ${known_hosts} ]]; then |
| 511 | truncate -s 0 "${TARGET_DIR}/known_hosts" |
| 512 | setup_mount "${known_hosts}" --bind "${target_ssh}/known_hosts" |
| 513 | fi |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 514 | copy_ssh_config "${TARGET_DIR}" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 515 | chown -R ${SUDO_UID}:${SUDO_GID} "${TARGET_DIR}" |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 516 | |
Kevin Cernekee | a171839 | 2016-10-16 19:54:57 -0700 | [diff] [blame] | 517 | if [ -S "${SSH_AUTH_SOCK}" ]; then |
| 518 | touch "${FLAGS_chroot}/tmp/ssh-auth-sock" |
| 519 | setup_mount "${SSH_AUTH_SOCK}" "--bind" "/tmp/ssh-auth-sock" |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 520 | fi |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 521 | fi |
| 522 | fi |
| 523 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 524 | # A reference to the DEPOT_TOOLS path may be passed in by cros_sdk. |
| 525 | if [ -n "${DEPOT_TOOLS}" ]; then |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 526 | debug "Mounting depot_tools" |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 527 | setup_mount "${DEPOT_TOOLS}" --bind "${DEPOT_TOOLS_DIR}" |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 528 | fi |
| 529 | |
Hidehiko Abe | 63d6c46 | 2017-03-02 17:40:54 +0900 | [diff] [blame] | 530 | if [[ -n "${FLAGS_goma_dir}" ]]; then |
| 531 | debug "Mounting goma" |
| 532 | # $HOME/goma is the default directory for goma. |
| 533 | # It is used by goma if GOMA_DIR is not provide. |
| 534 | setup_mount "${FLAGS_goma_dir}" --bind "/home/${SUDO_USER}/goma" |
| 535 | fi |
| 536 | |
| 537 | if [[ -n "${FLAGS_goma_client_json}" ]]; then |
| 538 | debug "Mounting service-account-goma-client.json" |
| 539 | local dest_path="/creds/service_accounts/service-account-goma-client.json" |
| 540 | # setup_mount assumes the target is a directory by default. |
| 541 | # So here, touch the file in advance. |
| 542 | local mounted_path="${MOUNTED_PATH}${dest_path}" |
Hidehiko Abe | 053a199 | 2017-03-07 16:44:18 +0900 | [diff] [blame] | 543 | mkdir -p "$(dirname "${mounted_path}")" |
Hidehiko Abe | 63d6c46 | 2017-03-02 17:40:54 +0900 | [diff] [blame] | 544 | touch "${mounted_path}" |
Hidehiko Abe | 053a199 | 2017-03-07 16:44:18 +0900 | [diff] [blame] | 545 | # Note: Original UID:GID and permission should be inherited even after |
Hidehiko Abe | 63d6c46 | 2017-03-02 17:40:54 +0900 | [diff] [blame] | 546 | # mounting. |
| 547 | setup_mount "${FLAGS_goma_client_json}" --bind "${dest_path}" |
| 548 | fi |
| 549 | |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 550 | # Mount additional directories as specified in .local_mounts file. |
| 551 | local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts" |
| 552 | if [[ -f ${local_mounts} ]]; then |
Gaurav Shah | e3cffca | 2013-11-26 14:45:10 -0800 | [diff] [blame] | 553 | info "Mounting local folders" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 554 | # format: mount_source |
| 555 | # or mount_source mount_point |
| 556 | # or # comments |
| 557 | local mount_source mount_point |
| 558 | while read mount_source mount_point; do |
| 559 | if [[ -z ${mount_source} ]]; then |
| 560 | continue |
| 561 | fi |
| 562 | # if only source is assigned, use source as mount point. |
| 563 | : ${mount_point:=${mount_source}} |
| 564 | debug " mounting ${mount_source} on ${mount_point}" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 565 | setup_mount "${mount_source}" "--bind" "${mount_point}" |
Michael Krebs | 04c4f73 | 2012-09-07 18:31:46 -0700 | [diff] [blame] | 566 | done < <(sed -e 's:#.*::' "${local_mounts}") |
| 567 | fi |
| 568 | |
Matt Stark | 8515b7e | 2021-01-28 03:21:40 +0000 | [diff] [blame^] | 569 | if [[ -n "${FLAGS_chrome_root}" ]]; then |
| 570 | if ! CHROME_ROOT="$(readlink -f "${FLAGS_chrome_root}")"; then |
| 571 | die_notrace "${FLAGS_chrome_root} does not exist." |
| 572 | fi |
| 573 | fi |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 574 | if [ -z "$CHROME_ROOT" ]; then |
| 575 | CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
| 576 | 2>/dev/null || :)" |
| 577 | CHROME_ROOT_AUTO=1 |
| 578 | fi |
| 579 | if [[ -n "$CHROME_ROOT" ]]; then |
| 580 | if [[ ! -d "${CHROME_ROOT}/src" ]]; then |
Mike Frysinger | 9d73a85 | 2013-12-02 19:47:08 -0500 | [diff] [blame] | 581 | error "Not mounting chrome source: could not find CHROME_ROOT/src dir." |
| 582 | error "Full path we tried: ${CHROME_ROOT}/src" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 583 | rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 584 | if [[ ! "$CHROME_ROOT_AUTO" ]]; then |
| 585 | exit 1 |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 586 | fi |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 587 | else |
| 588 | debug "Mounting chrome source at: $INNER_CHROME_ROOT" |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 589 | echo $CHROME_ROOT > "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
| 590 | setup_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT" |
Xiyuan Xia | a6e21ae | 2018-03-28 08:47:23 -0700 | [diff] [blame] | 591 | setup_gclient_cache_dir_mount "$CHROME_ROOT" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 592 | fi |
| 593 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 594 | |
Mike Frysinger | acc4c9b | 2011-09-15 18:06:25 -0400 | [diff] [blame] | 595 | # Install fuse module. Skip modprobe when possible for slight |
| 596 | # speed increase when initializing the env. |
| 597 | if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 598 | modprobe fuse 2> /dev/null ||\ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 599 | warn "-- Note: modprobe fuse failed. gmergefs will not work" |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 600 | fi |
| 601 | |
Allen Webb | 1add695 | 2020-04-02 11:45:00 -0700 | [diff] [blame] | 602 | # Bind mount the host kernel modules read-only so modprobe can be used |
| 603 | # inside the chroot for things like usbip-host. |
| 604 | local modules_dir="/lib/modules" |
| 605 | if [ -d "${modules_dir}" ]; then |
| 606 | setup_mount "${modules_dir}" "--bind -o ro" "${modules_dir}" |
| 607 | fi |
| 608 | |
Mike Frysinger | 926a4b9 | 2012-06-27 15:22:28 -0400 | [diff] [blame] | 609 | # Fix permissions on ccache tree. If this is a fresh chroot, then they |
| 610 | # might not be set up yet. Or if the user manually `rm -rf`-ed things, |
| 611 | # we need to reset it. Otherwise, gcc itself takes care of fixing things |
| 612 | # on demand, but only when it updates. |
| 613 | ccache_dir="${FLAGS_chroot}/var/cache/distfiles/ccache" |
| 614 | if [[ ! -d ${ccache_dir} ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 615 | mkdir -p -m 2775 "${ccache_dir}" |
Mike Frysinger | 926a4b9 | 2012-06-27 15:22:28 -0400 | [diff] [blame] | 616 | fi |
Mike Frysinger | ba60991 | 2015-05-20 00:44:25 -0400 | [diff] [blame] | 617 | ( |
| 618 | find -H "${ccache_dir}" '(' -type d -a '!' -perm 2775 ')' \ |
| 619 | -exec chmod 2775 {} + |
| 620 | find -H "${ccache_dir}" -gid 0 -exec chgrp 250 {} + |
| 621 | # These settings are kept in sync with the gcc ebuild. |
| 622 | chroot "${FLAGS_chroot}" env CCACHE_DIR=/var/cache/distfiles/ccache \ |
| 623 | CCACHE_UMASK=002 ccache -F 0 -M 11G >/dev/null |
| 624 | ) & |
David James | 342f156 | 2011-07-15 15:21:18 -0700 | [diff] [blame] | 625 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 626 | # Certain files get copied into the chroot when entering. |
| 627 | for fn in "${FILES_TO_COPY_TO_CHROOT[@]}"; do |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 628 | copy_into_chroot_if_exists "${SUDO_HOME}/${fn}" "/home/${SUDO_USER}/${fn}" |
Matt Tennant | f7c9e77 | 2012-02-08 17:06:26 -0800 | [diff] [blame] | 629 | done |
Michael Mortensen | 91ae060 | 2021-01-15 16:17:57 -0700 | [diff] [blame] | 630 | |
| 631 | # Map in credentials account for log-writing access. |
| 632 | log_cert_dir="/creds/service_accounts/" |
| 633 | log_cert_file="service-account-chromeos-datastore-writer-prod.json" |
| 634 | copy_into_chroot_if_exists "${log_cert_dir}${log_cert_file}" \ |
| 635 | "/home/${SUDO_USER}/${log_cert_file}" |
| 636 | |
| 637 | |
Prathmesh Prabhu | 74ebbbd | 2015-03-17 13:50:29 -0700 | [diff] [blame] | 638 | setup_git |
Peter Mayo | 4411efe | 2012-09-21 04:41:27 -0400 | [diff] [blame] | 639 | promote_api_keys |
Matt Tennant | d4316fe | 2011-08-30 12:06:42 -0700 | [diff] [blame] | 640 | |
Dale Curtis | 9863173 | 2011-05-05 16:16:59 -0700 | [diff] [blame] | 641 | # Fix permissions on shared memory to allow non-root users access to POSIX |
Josh McSavaney | 8477dad | 2016-04-05 13:43:26 -0700 | [diff] [blame] | 642 | # semaphores. Take special care to only change the permissions on the |
| 643 | # directory and not all of its contents. |
| 644 | chmod 1777 "${FLAGS_chroot}/dev/shm" |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 645 | |
Yu-Ju Hong | 22278a2 | 2014-01-16 12:46:53 -0800 | [diff] [blame] | 646 | # gsutil uses boto config to store settings and credentials. Copy |
| 647 | # user's own boto file into the chroot if it exists. Also copy it |
| 648 | # to /root for sudoed invocations. |
| 649 | chroot_user_boto="${FLAGS_chroot}/home/${SUDO_USER}/.boto" |
| 650 | chroot_root_boto="${FLAGS_chroot}/root/.boto" |
| 651 | if [ -f ${SUDO_HOME}/.boto ]; then |
| 652 | # Pass --remote-destination to overwrite a symlink. |
| 653 | user_cp "--remove-destination" "${SUDO_HOME}/.boto" "$chroot_user_boto" |
Frank Henigman | 696ce08 | 2014-03-27 21:21:55 -0400 | [diff] [blame] | 654 | cp "--remove-destination" "$chroot_user_boto" "$chroot_root_boto" |
David Riley | b6e9d86 | 2016-02-25 16:58:02 -0800 | [diff] [blame] | 655 | elif [ -f "/etc/boto.cfg" ]; then |
| 656 | # For GCE instances, the non-chroot .boto file is not deployed so |
| 657 | # use the system /etc/boto.cfg if it exists. |
| 658 | user_cp "--remove-destination" "/etc/boto.cfg" "$chroot_user_boto" |
| 659 | cp "--remove-destination" "$chroot_user_boto" "$chroot_root_boto" |
Yu-Ju Hong | 22278a2 | 2014-01-16 12:46:53 -0800 | [diff] [blame] | 660 | fi |
| 661 | |
| 662 | # If user doesn't have a boto file, check if the private overlays |
| 663 | # are installed and use those credentials. |
Gilad Arnold | 264f64d | 2012-09-06 14:01:45 -0700 | [diff] [blame] | 664 | boto='src/private-overlays/chromeos-overlay/googlestorage_account.boto' |
| 665 | if [ -s "${FLAGS_trunk}/${boto}" ]; then |
Yu-Ju Hong | 22278a2 | 2014-01-16 12:46:53 -0800 | [diff] [blame] | 666 | if [ ! -e "$chroot_user_boto" ]; then |
| 667 | user_symlink "trunk/${boto}" "$chroot_user_boto" |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 668 | fi |
Yu-Ju Hong | 22278a2 | 2014-01-16 12:46:53 -0800 | [diff] [blame] | 669 | if [ ! -e "$chroot_root_boto" ]; then |
| 670 | ln -sf "${CHROOT_TRUNK_DIR}/${boto}" "$chroot_root_boto" |
Gilad Arnold | 264f64d | 2012-09-06 14:01:45 -0700 | [diff] [blame] | 671 | fi |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 672 | fi |
| 673 | |
| 674 | # Have found a few chroots where ~/.gsutil is owned by root:root, probably |
| 675 | # as a result of old gsutil or tools. This causes permission errors when |
| 676 | # gsutil cp tries to create its cache files, so ensure the user can |
| 677 | # actually write to their directory. |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 678 | gsutil_dir="${FLAGS_chroot}/home/${SUDO_USER}/.gsutil" |
| 679 | if [ -d "${gsutil_dir}" ]; then |
| 680 | chown -R ${SUDO_UID}:${SUDO_GID} "${gsutil_dir}" |
Chris Wolfe | 916b1f1 | 2012-04-30 16:03:06 -0400 | [diff] [blame] | 681 | fi |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 682 | ) 200>>"$LOCKFILE" || die "setup_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Josh Triplett | d6a13bf | 2014-06-13 15:36:52 -0700 | [diff] [blame] | 685 | # Translate C.UTF-8 into something we support. Remove this when our glibc |
| 686 | # starts supporting C.UTF-8. https://bugzilla.redhat.com/show_bug.cgi?id=902094 |
| 687 | for var in LANG \ |
| 688 | LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \ |
| 689 | LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME}; do |
| 690 | if [[ ${!var} =~ C\.[Uu][Tt][Ff]-?8 ]]; then |
| 691 | if [[ ${var} == LC_COLLATE ]]; then |
| 692 | export LC_COLLATE=C |
| 693 | else |
| 694 | export ${var}=en_US.UTF-8 |
| 695 | fi |
| 696 | fi |
| 697 | done |
| 698 | |
Mike Frysinger | 1e00293 | 2020-04-10 17:21:46 -0400 | [diff] [blame] | 699 | check_locale |
| 700 | setup_env |
| 701 | |
| 702 | CHROOT_PASSTHRU=( |
| 703 | "BUILDBOT_BUILD=$FLAGS_build_number" |
| 704 | "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}" |
| 705 | "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}" |
| 706 | |
| 707 | # The default ~/.bash_profile in chroot will cd to $CHROOT_CWD instead of |
| 708 | # ~/trunk/src/script if that environment variable is set. |
| 709 | "CHROOT_CWD=${FLAGS_working_dir}" |
Mike Frysinger | d3b4cfd | 2021-01-14 18:14:27 -0500 | [diff] [blame] | 710 | |
| 711 | # We don't want to auto-update depot_tools inside of the SDK as we manage it. |
| 712 | "DEPOT_TOOLS_UPDATE=0" |
Mike Frysinger | 1e00293 | 2020-04-10 17:21:46 -0400 | [diff] [blame] | 713 | ) |
| 714 | |
Kevin Cernekee | a171839 | 2016-10-16 19:54:57 -0700 | [diff] [blame] | 715 | # Needs to be set here because setup_env runs in a subshell. |
| 716 | [ -S "${FLAGS_chroot}/tmp/ssh-auth-sock" ] && SSH_AUTH_SOCK=/tmp/ssh-auth-sock |
| 717 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 718 | # Add the whitelisted environment variables to CHROOT_PASSTHRU. |
| 719 | load_environment_whitelist |
| 720 | for var in "${ENVIRONMENT_WHITELIST[@]}" ; do |
Brian Harring | 06d3c2e | 2012-08-23 07:35:43 -0700 | [diff] [blame] | 721 | [ "${!var+set}" = "set" ] && CHROOT_PASSTHRU+=( "${var}=${!var}" ) |
Mario Limonciello | 7052ae1 | 2011-05-05 12:00:49 -0500 | [diff] [blame] | 722 | done |
| 723 | |
Paul Drews | 67d9ef1 | 2013-03-20 08:47:47 -0700 | [diff] [blame] | 724 | # Set up GIT_PROXY_COMMAND so git:// URLs automatically work behind a proxy. |
| 725 | if [[ -n "${all_proxy}" || -n "${https_proxy}" || -n "${http_proxy}" ]]; then |
| 726 | CHROOT_PASSTHRU+=( |
| 727 | "GIT_PROXY_COMMAND=${CHROOT_TRUNK_DIR}/src/scripts/bin/proxy-gw" |
| 728 | ) |
| 729 | fi |
| 730 | |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 731 | # Run command or interactive shell. Also include the non-chrooted path to |
| 732 | # the source trunk for scripts that may need to print it (e.g. |
| 733 | # build_image.sh). |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 734 | |
| 735 | if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then |
| 736 | cmd=( /bin/bash -l -c 'env "$@"' -- ) |
| 737 | elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then |
| 738 | # Complain that sudo is missing. |
| 739 | error "Failing since the chroot lacks sudo." |
| 740 | error "Requested enter_chroot command was: $@" |
| 741 | exit 127 |
| 742 | else |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 743 | cmd=( sudo -i -u "${SUDO_USER}" ) |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 744 | fi |
| 745 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 746 | cmd+=( "${CHROOT_PASSTHRU[@]}" "$@" ) |
| 747 | exec chroot "${FLAGS_chroot}" "${cmd[@]}" |