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 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 12 | # Script must be run outside the chroot and as a regular user. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 13 | assert_outside_chroot |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 14 | assert_not_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." |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 26 | DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \ |
| 27 | "The mount point of the chrome broswer source in the chroot." |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame^] | 28 | DEFINE_string distfiles "" \ |
| 29 | "Override the destination dir used for distfiles." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 30 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 31 | DEFINE_boolean official_build $FLAGS_FALSE \ |
| 32 | "Set CHROMEOS_OFFICIAL=1 for release builds." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 33 | DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts." |
| 34 | DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts." |
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, |
| 49 | after changing directory to /$USER/trunk/src/scripts. Note that neither |
| 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 | |
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. |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 58 | function 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 | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame^] | 72 | [ -z "${FLAGS_distfiles}" ] && \ |
| 73 | FLAGS_distfiles="${FLAGS_trunk}/distfiles" |
| 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, |
| 76 | # so will die prematurely if 'set -e' is specified before now. |
| 77 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
| 78 | set -e |
| 79 | |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 80 | INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 81 | CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 82 | INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 83 | FUSE_DEVICE="/dev/fuse" |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 84 | AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount" |
| 85 | SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 86 | |
Mike Frysinger | 8264917 | 2011-09-21 00:18:14 -0400 | [diff] [blame] | 87 | # Avoid the sudo call if possible since it is a little slow. |
| 88 | if [ $(stat -c %a "$FLAGS_chroot/var/lock") != 777 ]; then |
| 89 | sudo chmod 0777 "$FLAGS_chroot/var/lock" |
| 90 | fi |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 91 | |
| 92 | LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 93 | SYNCERPIDFILE="${FLAGS_chroot}/var/tmp/enter_chroot_sync.pid" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 94 | |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 95 | |
Mike Frysinger | 0a0e6ec | 2011-09-28 11:57:21 -0400 | [diff] [blame] | 96 | MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 97 | function mount_queue_init { |
| 98 | MOUNT_QUEUE=() |
| 99 | } |
| 100 | |
| 101 | function queue_mount { |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 102 | # If necessary, mount $source in the host FS at $target inside the |
| 103 | # chroot directory with $mount_args. |
| 104 | local source="$1" |
| 105 | local mount_args="$2" |
| 106 | local target="$3" |
| 107 | |
Mike Frysinger | 0a0e6ec | 2011-09-28 11:57:21 -0400 | [diff] [blame] | 108 | local mounted_path="${MOUNTED_PATH}$target" |
David Rochberg | 351a76f | 2011-02-16 11:14:00 -0500 | [diff] [blame] | 109 | |
Mike Frysinger | 2a97059 | 2011-09-20 22:49:01 -0400 | [diff] [blame] | 110 | case ${MOUNT_CACHE} in |
| 111 | *" on ${mounted_path} "*) |
| 112 | # Already mounted! |
| 113 | ;; |
| 114 | *) |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 115 | MOUNT_QUEUE+=( |
| 116 | "mkdir -p '${mounted_path}'" |
| 117 | # The args are left unquoted on purpose. |
| 118 | "mount ${mount_args} '${source}' '${mounted_path}'" |
| 119 | ) |
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 | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 124 | function process_mounts { |
| 125 | if [[ ${#MOUNT_QUEUE[@]} -eq 0 ]]; then |
| 126 | return 0 |
| 127 | fi |
| 128 | sudo_multi "${MOUNT_QUEUE[@]}" |
| 129 | mount_queue_init |
| 130 | } |
| 131 | |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 132 | function env_sync_proc { |
| 133 | # This function runs and performs periodic updates to the chroot env, if |
| 134 | # necessary. |
| 135 | |
| 136 | local poll_interval=10 |
Mike Frysinger | 7f7a763 | 2011-09-28 11:48:20 -0400 | [diff] [blame] | 137 | local sync_files=( etc/resolv.conf etc/hosts ) |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 138 | |
| 139 | # Make sure the synced files are writable by normal user, so that we |
| 140 | # don't have to sudo inside the loop. |
Mike Frysinger | 7f7a763 | 2011-09-28 11:48:20 -0400 | [diff] [blame] | 141 | local chmods=$(find ${sync_files[@]/#/${FLAGS_chroot}/} '!' -user ${USER}) |
| 142 | if [ -n "${chmods}" ]; then |
| 143 | sudo -- chown ${USER} ${chmods} 1>&2 |
| 144 | fi |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 145 | |
David James | 412b902 | 2011-07-15 19:54:16 -0700 | [diff] [blame] | 146 | # Drop stdin, stderr, stdout, and chroot lock. |
| 147 | # This is needed for properly daemonizing the process. |
| 148 | exec 0>&- 1>&- 2>&- 200>&- |
| 149 | |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 150 | while true; do |
| 151 | # Sync files |
| 152 | for file in ${sync_files}; do |
| 153 | if ! cmp /${file} ${FLAGS_chroot}/${file} &> /dev/null; then |
| 154 | cp -f /${file} ${FLAGS_chroot}/${file} |
| 155 | fi |
| 156 | done |
| 157 | |
| 158 | sleep ${poll_interval} |
| 159 | done |
| 160 | } |
| 161 | |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 162 | function copy_ssh_config { |
| 163 | # Copy user .ssh/config into the chroot filtering out strings not supported |
| 164 | # by the chroot ssh. The chroot .ssh directory is passed in as the first |
| 165 | # parameter. |
| 166 | |
| 167 | # ssh options to filter out. The entire strings containing these substrings |
| 168 | # will be deleted before copying. |
| 169 | local bad_options=( |
Matt Tennant | 63c3cc5 | 2011-11-22 11:23:06 -0800 | [diff] [blame] | 170 | 'UseProxyIf' |
| 171 | 'GSSAPIAuthentication' |
| 172 | 'GSSAPIKeyExchange' |
| 173 | 'ProxyUseFdpass' |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 174 | ) |
| 175 | local sshc="${HOME}/.ssh/config" |
| 176 | local chroot_ssh_dir="${1}" |
| 177 | local filter |
| 178 | local option |
| 179 | |
| 180 | if [ ! -f "${sshc}" ]; then |
| 181 | return # Nothing to copy. |
| 182 | fi |
| 183 | |
| 184 | for option in "${bad_options[@]}" |
| 185 | do |
| 186 | if [ -z "${filter}" ]; then |
| 187 | filter="${option}" |
| 188 | else |
| 189 | filter+="\\|${option}" |
| 190 | fi |
| 191 | done |
| 192 | |
| 193 | sed "/^.*\(${filter}\).*$/d" "${sshc}" > "${chroot_ssh_dir}/config" |
| 194 | } |
| 195 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 196 | function setup_env { |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 197 | # Validate sudo timestamp before entering the critical section so that we |
| 198 | # don't stall for a password while we have the lockfile. |
Doug Anderson | 3d7fa3a | 2011-02-02 16:10:34 -0800 | [diff] [blame] | 199 | # Don't use sudo -v since that has issues on machines w/ no password. |
| 200 | sudo echo "" > /dev/null |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 201 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 202 | ( |
| 203 | flock 200 |
| 204 | echo $$ >> "$LOCKFILE" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 205 | |
Taylor Hutt | 25bf949 | 2011-07-27 13:54:35 -0700 | [diff] [blame] | 206 | # If there isn't a syncer daemon started already, start one. The |
| 207 | # daemon is considered to not be started under the following |
| 208 | # conditions: |
| 209 | # |
| 210 | # o There is no PID file |
| 211 | # |
| 212 | # o The PID file is 0 bytes in size, which might be a partial |
| 213 | # manifestation of chromium-os:17680. This situation will not |
| 214 | # occur anymore, but you might have a chroot which was already |
| 215 | # affected. |
| 216 | # |
| 217 | # o The /proc node for the process named by the PID file does |
| 218 | # not exist. |
| 219 | # |
| 220 | # Note: This does not address PID recycling. While |
| 221 | # increasingly unlikely, it is possible for the PID in |
| 222 | # the PID file to refer to a running process that is not |
| 223 | # the syncer process. Since the PID file is now |
| 224 | # removed, I think it is only possible for this to occur |
| 225 | # if your system crashes and the PID file exists after |
| 226 | # restart. |
| 227 | # |
| 228 | # The daemon is killed by the enter_chroot that exits last. |
Taylor Hutt | 18594a8 | 2011-07-28 11:45:50 -0700 | [diff] [blame] | 229 | if [ -f "${SYNCERPIDFILE}" ] && [ ! -s "${SYNCERPIDFILE}" ] ; then |
Taylor Hutt | 25bf949 | 2011-07-27 13:54:35 -0700 | [diff] [blame] | 230 | info "You may have suffered from chromium-os:17680 and"; |
| 231 | info "could have stray 'enter_chroot.sh' processes running."; |
| 232 | info "You must manually kill any such stray processes."; |
| 233 | info "Exit all chroot shells; remaining 'enter_chroot.sh'"; |
| 234 | info "processes are probably stray."; |
Taylor Hutt | 18594a8 | 2011-07-28 11:45:50 -0700 | [diff] [blame] | 235 | sudo rm -f "${SYNCERPIDFILE}"; |
Taylor Hutt | 25bf949 | 2011-07-27 13:54:35 -0700 | [diff] [blame] | 236 | fi; |
David James | 412b902 | 2011-07-15 19:54:16 -0700 | [diff] [blame] | 237 | if ! [ -f "${SYNCERPIDFILE}" ] || \ |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 238 | ! [ -d /proc/$(<"${SYNCERPIDFILE}") ]; then |
David James | 412b902 | 2011-07-15 19:54:16 -0700 | [diff] [blame] | 239 | debug "Starting sync process" |
| 240 | env_sync_proc & |
| 241 | echo $! > "${SYNCERPIDFILE}" |
| 242 | disown $! |
| 243 | fi |
| 244 | |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 245 | debug "Mounting chroot environment." |
Mike Frysinger | 2a97059 | 2011-09-20 22:49:01 -0400 | [diff] [blame] | 246 | MOUNT_CACHE=$(mount) |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 247 | mount_queue_init |
| 248 | queue_mount none "-t proc" /proc |
| 249 | queue_mount none "-t sysfs" /sys |
| 250 | queue_mount /dev "--bind" /dev |
| 251 | queue_mount none "-t devpts" /dev/pts |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 252 | if [ -d /run ]; then |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 253 | queue_mount /run "--bind" /run |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 254 | if [ -d /run/shm ]; then |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 255 | queue_mount /run/shm "--bind" /run/shm |
Aaron Plattner | 130d363 | 2011-10-18 08:54:57 -0700 | [diff] [blame] | 256 | fi |
| 257 | fi |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 258 | queue_mount "${FLAGS_trunk}" "--bind" "${CHROOT_TRUNK_DIR}" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 259 | |
Brian Harring | dd7d793 | 2011-12-23 04:21:33 -0800 | [diff] [blame] | 260 | |
| 261 | debug "Setting up referenced repositories if required." |
| 262 | REFERENCE_DIR=$(git config --file \ |
| 263 | "${FLAGS_trunk}/.repo/manifests.git/config" \ |
| 264 | repo.reference) |
| 265 | if [ -n "${REFERENCE_DIR}" ]; then |
| 266 | |
| 267 | IFS=$'\n'; |
| 268 | required=( $( "${FLAGS_trunk}/chromite/lib/rewrite_git_alternates.py" \ |
| 269 | "${FLAGS_trunk}" "${REFERENCE_DIR}" "${CHROOT_TRUNK_DIR}" ) ) |
| 270 | unset IFS |
| 271 | |
| 272 | queue_mount "${FLAGS_trunk}/.repo/chroot/alternates" --bind \ |
| 273 | "${CHROOT_TRUNK_DIR}/.repo/alternates" |
| 274 | |
| 275 | # Note that as we're bringing up each referened repo, we also |
| 276 | # mount bind an empty directory over its alternates. This is |
| 277 | # required to suppress git from tracing through it- we already |
| 278 | # specify the required alternates for CHROOT_TRUNK_DIR, no point |
| 279 | # in having git try recursing through each on their own. |
| 280 | # |
| 281 | # Finally note that if you're unfamiliar w/ chroot/vfs semantics, |
| 282 | # the bind is visible only w/in the chroot. |
| 283 | mkdir -p ${FLAGS_trunk}/.repo/chroot/empty |
| 284 | position=1 |
| 285 | for x in "${required[@]}"; do |
| 286 | base="${CHROOT_TRUNK_DIR}/.repo/chroot/external${position}" |
| 287 | queue_mount "${x}" "--bind" "${base}" |
| 288 | if [ -e "${x}/.repo/alternates" ]; then |
| 289 | queue_mount "${FLAGS_trunk}/.repo/chroot/empty" "--bind" \ |
| 290 | "${base}/.repo/alternates" |
| 291 | fi |
| 292 | position=$(( ${position} + 1 )) |
| 293 | done |
| 294 | unset required position base |
| 295 | fi |
| 296 | unset REFERENCE_DIR |
| 297 | |
Brian Harring | 7ee892d | 2012-02-02 09:33:10 -0800 | [diff] [blame^] | 298 | debug "Setting up shared distfiles directory." |
| 299 | mkdir -p "${FLAGS_distfiles}"/{target,host} |
| 300 | sudo mkdir -p "${FLAGS_chroot}/var/cache/distfiles/" |
| 301 | queue_mount "${FLAGS_distfiles}" "--bind" "/var/cache/distfiles" |
| 302 | |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 303 | if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 304 | if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 305 | TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh" |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 306 | mkdir -p "${TARGET_DIR}" |
Mike Frysinger | 40bb0c3 | 2011-10-31 17:14:15 -0400 | [diff] [blame] | 307 | # Ignore errors as some people won't have these files to copy. |
| 308 | cp "${HOME}"/.ssh/{known_hosts,*.pub} "${TARGET_DIR}/" 2>/dev/null || : |
Vadim Bendebury | 0779f52 | 2011-06-12 12:09:16 -0700 | [diff] [blame] | 309 | copy_ssh_config "${TARGET_DIR}" |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 310 | |
| 311 | # 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] | 312 | ASOCK=${SSH_AUTH_SOCK%/*} |
Mike Frysinger | 9de707f | 2011-12-12 14:21:44 -0500 | [diff] [blame] | 313 | if [ -d "${ASOCK}" ]; then |
| 314 | queue_mount "${ASOCK}" "--bind" "${ASOCK}" |
| 315 | fi |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 316 | fi |
| 317 | fi |
| 318 | |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 319 | if [ -d "$HOME/.subversion" ]; then |
| 320 | TARGET="/home/${USER}/.subversion" |
| 321 | mkdir -p "${FLAGS_chroot}${TARGET}" |
| 322 | queue_mount "${HOME}/.subversion" "--bind" "${TARGET}" |
| 323 | fi |
| 324 | |
| 325 | if DEPOT_TOOLS=$(type -P gclient) ; then |
| 326 | DEPOT_TOOLS=${DEPOT_TOOLS%/*} # dirname |
| 327 | debug "Mounting depot_tools" |
| 328 | queue_mount "$DEPOT_TOOLS" --bind "$INNER_DEPOT_TOOLS_ROOT" |
| 329 | fi |
| 330 | |
| 331 | process_mounts |
| 332 | |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 333 | CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root" || :)" |
| 334 | if [ -z "$CHROME_ROOT" ]; then |
| 335 | CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
| 336 | 2>/dev/null || :)" |
| 337 | CHROME_ROOT_AUTO=1 |
| 338 | fi |
| 339 | if [[ -n "$CHROME_ROOT" ]]; then |
| 340 | if [[ ! -d "${CHROME_ROOT}/src" ]]; then |
| 341 | error "Not mounting chrome source" |
| 342 | sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
| 343 | if [[ ! "$CHROME_ROOT_AUTO" ]]; then |
| 344 | exit 1 |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 345 | fi |
Mike Frysinger | 9a50b64 | 2011-09-20 23:37:14 -0400 | [diff] [blame] | 346 | else |
| 347 | debug "Mounting chrome source at: $INNER_CHROME_ROOT" |
| 348 | sudo bash -c "echo '$CHROME_ROOT' > \ |
| 349 | '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'" |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 350 | queue_mount "$CHROME_ROOT" --bind "$INNER_CHROME_ROOT" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 351 | fi |
| 352 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 353 | |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 354 | process_mounts |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 355 | |
Mike Frysinger | acc4c9b | 2011-09-15 18:06:25 -0400 | [diff] [blame] | 356 | # Install fuse module. Skip modprobe when possible for slight |
| 357 | # speed increase when initializing the env. |
| 358 | if [ -c "${FUSE_DEVICE}" ] && ! grep -q fuse /proc/filesystems; then |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 359 | sudo modprobe fuse 2> /dev/null ||\ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 360 | warn "-- Note: modprobe fuse failed. gmergefs will not work" |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 361 | fi |
| 362 | |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 363 | # Turn off automounting of external media when we enter the |
| 364 | # chroot; thus we don't have to worry about being able to unmount |
| 365 | # from inside. |
Mike Frysinger | da7be78 | 2011-09-15 17:58:19 -0400 | [diff] [blame] | 366 | if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then |
| 367 | if [ "${SAVED_PREF}" != "false" ]; then |
| 368 | if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then |
| 369 | warn "-- Note: USB sticks may be automounted by your host OS." |
| 370 | warn "-- Note: If you plan to burn bootable media, you may need to" |
| 371 | warn "-- Note: unmount these devices manually, or run image_to_usb.sh" |
| 372 | warn "-- Note: outside the chroot." |
| 373 | fi |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 374 | fi |
| 375 | fi |
Mike Frysinger | da7be78 | 2011-09-15 17:58:19 -0400 | [diff] [blame] | 376 | # Always write the temp file so we can read it when exiting |
| 377 | echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}" |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 378 | |
Mike Frysinger | 94717e3 | 2011-09-21 00:10:44 -0400 | [diff] [blame] | 379 | # Configure committer username and email in chroot .gitconfig. Change |
| 380 | # to the root directory first so that random $PWD/.git/config settings |
| 381 | # do not get picked up. We want to stick to ~/.gitconfig only. |
| 382 | ident=$(cd /; git var GIT_COMMITTER_IDENT || :) |
| 383 | ident_name=${ident%% <*} |
| 384 | ident_email=${ident%%>*}; ident_email=${ident_email##*<} |
David James | 342f156 | 2011-07-15 15:21:18 -0700 | [diff] [blame] | 385 | git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ |
Mike Frysinger | 94717e3 | 2011-09-21 00:10:44 -0400 | [diff] [blame] | 386 | user.name "${ident_name}" || true |
David James | 342f156 | 2011-07-15 15:21:18 -0700 | [diff] [blame] | 387 | git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ |
Mike Frysinger | 94717e3 | 2011-09-21 00:10:44 -0400 | [diff] [blame] | 388 | user.email "${ident_email}" || true |
David James | 342f156 | 2011-07-15 15:21:18 -0700 | [diff] [blame] | 389 | |
Matt Tennant | d4316fe | 2011-08-30 12:06:42 -0700 | [diff] [blame] | 390 | # Copy ~/.gdata_cred.txt to chroot if it exists. This file contains |
| 391 | # credentials for reading/writing Google Docs on chromium.org. |
| 392 | if [ -f "$HOME/.gdata_cred.txt" ]; then |
| 393 | cp "$HOME/.gdata_cred.txt" "${FLAGS_chroot}/home/${USER}/.gdata_cred.txt" |
| 394 | fi |
| 395 | |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 396 | # Make sure user's requested locales are available |
| 397 | # http://crosbug.com/19139 |
Mike Frysinger | 4fc9c27 | 2011-08-17 15:23:19 -0400 | [diff] [blame] | 398 | # And make sure en_US{,.UTF-8} are always available as |
| 399 | # that what buildbot forces internally |
| 400 | locales=$(printf '%s\n' en_US en_US.UTF-8 ${LANG} \ |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 401 | $LC_{ADDRESS,ALL,COLLATE,CTYPE,IDENTIFICATION,MEASUREMENT,MESSAGES} \ |
| 402 | $LC_{MONETARY,NAME,NUMERIC,PAPER,TELEPHONE,TIME} | \ |
| 403 | sort -u | sed '/^C$/d') |
| 404 | gen_locales=() |
| 405 | for l in ${locales}; do |
| 406 | if [[ ${l} == *.* ]]; then |
| 407 | enc=${l#*.} |
| 408 | else |
| 409 | enc="ISO-8859-1" |
| 410 | fi |
| 411 | case $(echo ${enc//-} | tr '[:upper:]' '[:lower:]') in |
| 412 | utf8) enc="UTF-8";; |
| 413 | esac |
| 414 | gen_locales=("${gen_locales[@]}" "${l} ${enc}") |
| 415 | done |
Mike Frysinger | 4d258cd | 2011-09-15 16:17:49 -0400 | [diff] [blame] | 416 | if [[ ${#gen_locales[@]} -gt 0 ]] ; then |
| 417 | # Force LC_ALL=C to workaround slow string parsing in bash |
| 418 | # with long multibyte strings. Newer setups have this fixed, |
| 419 | # but locale-gen doesn't need to be run in any locale in the |
| 420 | # first place, so just go with C to keep it fast. |
| 421 | sudo -- chroot "$FLAGS_chroot" env LC_ALL=C locale-gen -q -u \ |
| 422 | -G "$(printf '%s\n' "${gen_locales[@]}")" |
| 423 | fi |
Mike Frysinger | 6601c52 | 2011-08-15 11:05:39 -0400 | [diff] [blame] | 424 | |
Dale Curtis | 9863173 | 2011-05-05 16:16:59 -0700 | [diff] [blame] | 425 | # Fix permissions on shared memory to allow non-root users access to POSIX |
Mike Frysinger | 8264917 | 2011-09-21 00:18:14 -0400 | [diff] [blame] | 426 | # semaphores. Avoid the sudo call if possible (sudo is slow). |
| 427 | if [ -n "$(find "${FLAGS_chroot}/dev/shm" ! -perm 777)" ] ; then |
| 428 | sudo chmod -R 777 "${FLAGS_chroot}/dev/shm" |
| 429 | fi |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 430 | ) 200>>"$LOCKFILE" || die "setup_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | function teardown_env { |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 434 | # Validate sudo timestamp before entering the critical section so that we |
| 435 | # don't stall for a password while we have the lockfile. |
Doug Anderson | 3d7fa3a | 2011-02-02 16:10:34 -0800 | [diff] [blame] | 436 | # Don't use sudo -v since that has issues on machines w/ no password. |
| 437 | sudo echo "" > /dev/null |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 438 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 439 | # Only teardown if we're the last enter_chroot to die |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 440 | ( |
| 441 | flock 200 |
| 442 | |
| 443 | # check each pid in $LOCKFILE to see if it's died unexpectedly |
| 444 | TMP_LOCKFILE="$LOCKFILE.tmp" |
| 445 | |
| 446 | echo -n > "$TMP_LOCKFILE" # Erase/reset temp file |
| 447 | cat "$LOCKFILE" | while read PID; do |
| 448 | if [ "$PID" = "$$" ]; then |
| 449 | # ourself, leave PROC_NAME empty |
| 450 | PROC_NAME="" |
| 451 | else |
| 452 | PROC_NAME=$(ps --pid $PID -o comm=) |
| 453 | fi |
| 454 | |
| 455 | if [ ! -z "$PROC_NAME" ]; then |
| 456 | # All good, keep going |
| 457 | echo "$PID" >> "$TMP_LOCKFILE" |
| 458 | fi |
| 459 | done |
| 460 | # Remove any dups from lock file while installing new one |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 461 | sort -u -n "$TMP_LOCKFILE" > "$LOCKFILE" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 462 | |
Mike Frysinger | da7be78 | 2011-09-15 17:58:19 -0400 | [diff] [blame] | 463 | SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") |
| 464 | if [ "${SAVED_PREF}" != "false" ]; then |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 465 | gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 466 | warn "could not re-set your automount preference." |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 467 | fi |
| 468 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 469 | if [ -s "$LOCKFILE" ]; then |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 470 | debug "At least one other pid is running in the chroot, so not" |
| 471 | debug "tearing down env." |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 472 | else |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 473 | debug "Stopping syncer process" |
Taylor Hutt | 25bf949 | 2011-07-27 13:54:35 -0700 | [diff] [blame] | 474 | # If another process entering the chroot is blocked on this |
| 475 | # flock in setup_env(), it can be a race condition. |
| 476 | # |
| 477 | # When this locked region is exited, the setup_env() flock can |
| 478 | # be entered before the script can exit and the /proc entry for |
| 479 | # the PID is removed. The newly-created chroot will not end up |
| 480 | # with a syncer process. To avoid that situation, remove the |
| 481 | # syncer PID file. |
| 482 | # |
| 483 | # The syncer PID file should also be removed because the kernel |
| 484 | # will reuse PIDs. It's possible that the PID in the syncer PID |
| 485 | # has been reused by another process; make sure we don't skip |
| 486 | # starting the syncer process when this occurs by deleting the |
| 487 | # PID file. |
Mike Frysinger | 61e4f28 | 2011-09-20 22:37:22 -0400 | [diff] [blame] | 488 | kill $(<"${SYNCERPIDFILE}") && \ |
Mike Frysinger | 470be99 | 2011-09-28 11:53:56 -0400 | [diff] [blame] | 489 | { rm -f "${SYNCERPIDFILE}" 2>/dev/null || \ |
| 490 | sudo rm -f "${SYNCERPIDFILE}" ; } || |
| 491 | debug "Unable to clean up syncer process."; |
Zdenek Behan | e28239d | 2011-04-07 00:37:20 +0200 | [diff] [blame] | 492 | |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 493 | debug "Unmounting chroot environment." |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 494 | safe_umount_tree "${MOUNTED_PATH}/" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 495 | fi |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 496 | ) 200>>"$LOCKFILE" || die "teardown_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 499 | if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 500 | setup_env |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame] | 501 | info "Make sure you run" |
| 502 | info " $0 --unmount" |
| 503 | info "before deleting $FLAGS_chroot" |
| 504 | info "or you'll end up deleting $FLAGS_trunk too!" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 505 | exit 0 |
| 506 | fi |
| 507 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 508 | if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 509 | teardown_env |
| 510 | exit 0 |
| 511 | fi |
| 512 | |
| 513 | # Make sure we unmount before exiting |
| 514 | trap teardown_env EXIT |
| 515 | setup_env |
| 516 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 517 | CHROOT_PASSTHRU=( |
| 518 | "BUILDBOT_BUILD=$FLAGS_build_number" |
| 519 | "CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL" |
| 520 | "CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-{DEV-BUILD}}" |
Raja Aluri | e891eea | 2010-11-16 20:12:56 -0800 | [diff] [blame] | 521 | |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 522 | # Set CHROMEOS_VERSION_TRACK, CHROMEOS_VERSION_AUSERVER, |
| 523 | # CHROMEOS_VERSION_DEVSERVER as environment variables to override the default |
| 524 | # assumptions (local AU server). These are used in cros_set_lsb_release, and |
| 525 | # are used by external Chromium OS builders. |
| 526 | |
| 527 | "CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK}" |
| 528 | "CHROMEOS_VERSION_AUSERVER=${CHROMEOS_VERSION_AUSERVER}" |
| 529 | "CHROMEOS_VERSION_DEVSERVER=${CHROMEOS_VERSION_DEVSERVER}" |
| 530 | "EXTERNAL_TRUNK_PATH=${FLAGS_trunk}" |
| 531 | "SSH_AGENT_PID=${SSH_AGENT_PID}" |
| 532 | "SSH_AUTH_SOCK=${SSH_AUTH_SOCK}" |
| 533 | ) |
Liam McLoughlin | 3b11b45 | 2011-03-14 16:04:07 -0700 | [diff] [blame] | 534 | |
Mario Limonciello | 7052ae1 | 2011-05-05 12:00:49 -0500 | [diff] [blame] | 535 | # Pass proxy variables into the environment. |
| 536 | for type in http_proxy ftp_proxy all_proxy GIT_PROXY_COMMAND GIT_SSH; do |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 537 | if [ -n "${!type}" ]; then |
| 538 | CHROOT_PASSTHRU+=( "${type}=${!type}" ) |
| 539 | fi |
Mario Limonciello | 7052ae1 | 2011-05-05 12:00:49 -0500 | [diff] [blame] | 540 | done |
| 541 | |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 542 | # Run command or interactive shell. Also include the non-chrooted path to |
| 543 | # the source trunk for scripts that may need to print it (e.g. |
| 544 | # build_image.sh). |
Brian Harring | 3576782 | 2012-02-01 23:50:45 -0800 | [diff] [blame] | 545 | |
| 546 | if [ $FLAGS_early_make_chroot -eq $FLAGS_TRUE ]; then |
| 547 | cmd=( /bin/bash -l -c 'env "$@"' -- ) |
| 548 | elif [ ! -x "${FLAGS_chroot}/usr/bin/sudo" ]; then |
| 549 | # Complain that sudo is missing. |
| 550 | error "Failing since the chroot lacks sudo." |
| 551 | error "Requested enter_chroot command was: $@" |
| 552 | exit 127 |
| 553 | else |
| 554 | cmd=( sudo -i -u "$USER" ) |
| 555 | fi |
| 556 | |
| 557 | sudo -- chroot "${FLAGS_chroot}" "${cmd[@]}" "${CHROOT_PASSTHRU[@]}" "$@" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 558 | |
| 559 | # Remove trap and explicitly unmount |
| 560 | trap - EXIT |
| 561 | teardown_env |