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