rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 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 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 9 | # --- BEGIN COMMON.SH BOILERPLATE --- |
| 10 | # Load common CrOS utilities. Inside the chroot this file is installed in |
| 11 | # /usr/lib/crosutils. Outside the chroot we find it relative to the script's |
| 12 | # location. |
| 13 | find_common_sh() { |
| 14 | local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")")) |
| 15 | local path |
| 16 | |
| 17 | SCRIPT_ROOT= |
| 18 | for path in "${common_paths[@]}"; do |
| 19 | if [ -r "${path}/common.sh" ]; then |
| 20 | SCRIPT_ROOT=${path} |
| 21 | break |
| 22 | fi |
| 23 | done |
| 24 | } |
| 25 | |
| 26 | find_common_sh |
| 27 | . "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1) |
| 28 | # --- END COMMON.SH BOILERPLATE --- |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 29 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 30 | # 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] | 31 | assert_outside_chroot |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 32 | assert_not_root_user |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 33 | |
| 34 | # Define command line flags |
| 35 | # See http://code.google.com/p/shflags/wiki/Documentation10x |
| 36 | DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \ |
| 37 | "The destination dir for the chroot environment." "d" |
| 38 | DEFINE_string trunk "$GCLIENT_ROOT" \ |
| 39 | "The source trunk to bind mount within the chroot." "s" |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 40 | DEFINE_string build_number "" \ |
| 41 | "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] | 42 | DEFINE_string chrome_root "" \ |
| 43 | "The root of your chrome browser source. Should contain a 'src' subdir." |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 44 | DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \ |
| 45 | "The mount point of the chrome broswer source in the chroot." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 46 | |
Chris Sosa | 558940d | 2011-02-01 13:07:55 -0800 | [diff] [blame] | 47 | DEFINE_boolean git_config $FLAGS_TRUE \ |
| 48 | "Config git to work with your user/pass in the chroot." |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 49 | DEFINE_boolean official_build $FLAGS_FALSE \ |
| 50 | "Set CHROMEOS_OFFICIAL=1 for release builds." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 51 | DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts." |
| 52 | DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts." |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 53 | DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent." |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 54 | DEFINE_boolean verbose $FLAGS_FALSE "Print out actions taken" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 55 | |
| 56 | # More useful help |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 57 | FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...] |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 58 | |
| 59 | One or more VAR=value pairs can be specified to export variables into |
| 60 | the chroot environment. For example: |
| 61 | |
| 62 | $0 FOO=bar BAZ=bel |
| 63 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 64 | If [-- command] is present, runs the command inside the chroot, |
| 65 | after changing directory to /$USER/trunk/src/scripts. Note that neither |
| 66 | the command nor args should include single quotes. For example: |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 67 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 68 | $0 -- ./build_platform_packages.sh |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 69 | |
| 70 | Otherwise, provides an interactive shell. |
| 71 | " |
| 72 | |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 73 | # 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^] | 74 | function debug { |
Don Garrett | ad3f059 | 2011-02-04 14:59:56 -0800 | [diff] [blame] | 75 | if [ $FLAGS_verbose -eq $FLAGS_TRUE ]; then |
| 76 | info "$1" |
| 77 | fi |
| 78 | } |
| 79 | |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 80 | # Double up on the first '--' argument. Why? For enter_chroot, we want to |
| 81 | # emulate the behavior of sudo for setting environment vars. That is, we want: |
| 82 | # ./enter_chroot [flags] [VAR=val] [-- command] |
| 83 | # ...but shflags ends up eating the '--' out of the command line and gives |
| 84 | # us back "VAR=val" and "command" together in one chunk. By doubling up, we |
| 85 | # end up getting what we want back from shflags. |
| 86 | # |
| 87 | # Examples of how people might be using enter_chroot: |
| 88 | # 1. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 -- cmd arg1 arg2 |
| 89 | # Set env vars and run cmd w/ args |
| 90 | # 2. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 |
| 91 | # Set env vars and run shell |
| 92 | # 3. ./enter_chroot [chroot_flags] -- cmd arg1 arg2 |
| 93 | # Run cmd w/ args |
| 94 | # 4. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 cmd arg1 arg2 |
| 95 | # Like #1 _if_ args aren't flags (if they are, enter_chroot will claim them) |
| 96 | # 5. ./enter_chroot [chroot_flags] cmd arg1 arg2 |
| 97 | # Like #3 _if_ args aren't flags (if they are, enter_chroot will claim them) |
| 98 | _FLAGS_FIXED='' |
| 99 | _SAW_DASHDASH=0 |
| 100 | while [ $# -gt 0 ]; do |
| 101 | _FLAGS_FIXED="${_FLAGS_FIXED:+${_FLAGS_FIXED} }'$1'" |
| 102 | if [ $_SAW_DASHDASH -eq 0 ] && [[ "$1" == "--" ]]; then |
| 103 | _FLAGS_FIXED="${_FLAGS_FIXED:+${_FLAGS_FIXED} }'--'" |
| 104 | _SAW_DASHDASH=1 |
| 105 | fi |
| 106 | shift |
| 107 | done |
| 108 | eval set -- "${_FLAGS_FIXED}" |
| 109 | |
| 110 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 111 | # Parse command line flags |
| 112 | FLAGS "$@" || exit 1 |
| 113 | eval set -- "${FLAGS_ARGV}" |
| 114 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 115 | if [ $FLAGS_official_build -eq $FLAGS_TRUE ]; then |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 116 | CHROMEOS_OFFICIAL=1 |
| 117 | fi |
| 118 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 119 | # Only now can we die on error. shflags functions leak non-zero error codes, |
| 120 | # so will die prematurely if 'set -e' is specified before now. |
| 121 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
| 122 | set -e |
| 123 | |
Sean Parent | 2898f75 | 2010-05-25 15:06:33 -0700 | [diff] [blame] | 124 | INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 125 | CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 126 | INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 127 | FUSE_DEVICE="/dev/fuse" |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 128 | AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount" |
| 129 | SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 130 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 131 | sudo chmod 0777 "$FLAGS_chroot/var/lock" |
| 132 | |
| 133 | LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
| 134 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 135 | function setup_env { |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 136 | # Validate sudo timestamp before entering the critical section so that we |
| 137 | # don't stall for a password while we have the lockfile. |
Doug Anderson | 3d7fa3a | 2011-02-02 16:10:34 -0800 | [diff] [blame] | 138 | # Don't use sudo -v since that has issues on machines w/ no password. |
| 139 | sudo echo "" > /dev/null |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 140 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 141 | ( |
| 142 | flock 200 |
| 143 | echo $$ >> "$LOCKFILE" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 144 | |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame^] | 145 | debug "Mounting chroot environment." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 146 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 147 | # Mount only if not already mounted |
| 148 | MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 149 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 150 | sudo mount none -t proc "$MOUNTED_PATH" || \ |
| 151 | die "Could not mount $MOUNTED_PATH" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 152 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 153 | |
Antoine Labour | e9e585f | 2010-04-01 15:57:57 -0700 | [diff] [blame] | 154 | MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/sys")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 155 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
Antoine Labour | e9e585f | 2010-04-01 15:57:57 -0700 | [diff] [blame] | 156 | sudo mount none -t sysfs "$MOUNTED_PATH" || \ |
| 157 | die "Could not mount $MOUNTED_PATH" |
| 158 | fi |
| 159 | |
robotboy | 152a1ab | 2010-04-26 14:07:27 -0700 | [diff] [blame] | 160 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 161 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
robotboy | 152a1ab | 2010-04-26 14:07:27 -0700 | [diff] [blame] | 162 | sudo mount --bind /dev "$MOUNTED_PATH" || \ |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 163 | die "Could not mount $MOUNTED_PATH" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 164 | fi |
| 165 | |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 166 | if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then |
| 167 | TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 168 | if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 169 | mkdir -p "${TARGET_DIR}" |
| 170 | cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}" |
Zdenek Behan | 7580571 | 2011-01-05 00:56:32 +0100 | [diff] [blame] | 171 | cp -r "${HOME}/.ssh/config" "${TARGET_DIR}" |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 172 | ASOCK="$(dirname "${SSH_AUTH_SOCK}")" |
| 173 | mkdir -p "${FLAGS_chroot}/${ASOCK}" |
| 174 | sudo mount --bind "${ASOCK}" "${FLAGS_chroot}/${ASOCK}" || \ |
| 175 | die "Count not mount ${ASOCK}" |
| 176 | fi |
| 177 | fi |
| 178 | |
Zdenek Behan | 6f17b5e | 2010-06-10 16:50:52 -0700 | [diff] [blame] | 179 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev/pts")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 180 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
Zdenek Behan | 6f17b5e | 2010-06-10 16:50:52 -0700 | [diff] [blame] | 181 | sudo mount none -t devpts "$MOUNTED_PATH" || \ |
| 182 | die "Could not mount $MOUNTED_PATH" |
| 183 | fi |
| 184 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 185 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 186 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 187 | sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" || \ |
| 188 | die "Could not mount $MOUNTED_PATH" |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 189 | fi |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 190 | |
| 191 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 192 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
Andrew de los Reyes | c1e8d27 | 2010-02-13 12:39:21 -0800 | [diff] [blame] | 193 | ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 194 | if [ -z "$CHROME_ROOT" ]; then |
| 195 | ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
| 196 | 2>/dev/null)" |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame^] | 197 | CHROME_ROOT_AUTO=1 |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 198 | fi |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame^] | 199 | if [[ ( -n "$CHROME_ROOT" ) ]]; then |
| 200 | if [[ ( ! -d "${CHROME_ROOT}/src" ) ]]; then |
| 201 | error "Not mounting chrome source" |
| 202 | sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
| 203 | if [[ ! "$CHROME_ROOT_AUTO" ]]; then |
| 204 | exit 1 |
| 205 | fi |
| 206 | else |
| 207 | debug "Mounting chrome source at: $INNER_CHROME_ROOT" |
| 208 | sudo bash -c "echo '$CHROME_ROOT' > \ |
| 209 | '${FLAGS_chroot}${CHROME_ROOT_CONFIG}'" |
| 210 | mkdir -p "$MOUNTED_PATH" |
| 211 | sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \ |
| 212 | die "Could not mount $MOUNTED_PATH" |
| 213 | fi |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 214 | fi |
| 215 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 216 | |
| 217 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 218 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 219 | if [ $(which gclient 2>/dev/null) ]; then |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame^] | 220 | debug "Mounting depot_tools" |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 221 | DEPOT_TOOLS=$(dirname "$(which gclient)") |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 222 | mkdir -p "$MOUNTED_PATH" |
Andrew de los Reyes | e8b6315 | 2010-02-16 14:18:34 -0800 | [diff] [blame] | 223 | if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 224 | warn "depot_tools failed to mount; perhaps it's on NFS?" |
| 225 | warn "This may impact chromium build." |
Andrew de los Reyes | e8b6315 | 2010-02-16 14:18:34 -0800 | [diff] [blame] | 226 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 227 | fi |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 228 | fi |
| 229 | |
robotboy | 152a1ab | 2010-04-26 14:07:27 -0700 | [diff] [blame] | 230 | # Install fuse module. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 231 | if [ -c "${FUSE_DEVICE}" ]; then |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 232 | sudo modprobe fuse 2> /dev/null ||\ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 233 | warn "-- Note: modprobe fuse failed. gmergefs will not work" |
Chris Sosa | 317d8eb | 2010-04-05 15:45:28 -0700 | [diff] [blame] | 234 | fi |
| 235 | |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 236 | # Turn off automounting of external media when we enter the |
| 237 | # chroot; thus we don't have to worry about being able to unmount |
| 238 | # from inside. |
| 239 | if [ $(which gconftool-2 2>/dev/null) ]; then |
| 240 | gconftool-2 -g ${AUTOMOUNT_PREF} > \ |
| 241 | "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}" |
| 242 | if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 243 | warn "-- Note: USB sticks may be automounted by your host OS." |
| 244 | warn "-- Note: If you plan to burn bootable media, you may need to" |
| 245 | warn "-- Note: unmount these devices manually, or run image_to_usb.sh" |
| 246 | warn "-- Note: outside the chroot." |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 247 | fi |
| 248 | fi |
| 249 | |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 250 | ) 200>>"$LOCKFILE" || die "setup_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | function teardown_env { |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 254 | # Validate sudo timestamp before entering the critical section so that we |
| 255 | # don't stall for a password while we have the lockfile. |
Doug Anderson | 3d7fa3a | 2011-02-02 16:10:34 -0800 | [diff] [blame] | 256 | # Don't use sudo -v since that has issues on machines w/ no password. |
| 257 | sudo echo "" > /dev/null |
Doug Anderson | a8d9cc1 | 2011-02-02 15:47:00 -0800 | [diff] [blame] | 258 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 259 | # 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] | 260 | ( |
| 261 | flock 200 |
| 262 | |
| 263 | # check each pid in $LOCKFILE to see if it's died unexpectedly |
| 264 | TMP_LOCKFILE="$LOCKFILE.tmp" |
| 265 | |
| 266 | echo -n > "$TMP_LOCKFILE" # Erase/reset temp file |
| 267 | cat "$LOCKFILE" | while read PID; do |
| 268 | if [ "$PID" = "$$" ]; then |
| 269 | # ourself, leave PROC_NAME empty |
| 270 | PROC_NAME="" |
| 271 | else |
| 272 | PROC_NAME=$(ps --pid $PID -o comm=) |
| 273 | fi |
| 274 | |
| 275 | if [ ! -z "$PROC_NAME" ]; then |
| 276 | # All good, keep going |
| 277 | echo "$PID" >> "$TMP_LOCKFILE" |
| 278 | fi |
| 279 | done |
| 280 | # Remove any dups from lock file while installing new one |
| 281 | sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE" |
| 282 | |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 283 | if [ $(which gconftool-2 2>/dev/null) ]; then |
| 284 | SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}") |
| 285 | gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \ |
Sean O | cd8b1d1 | 2010-09-02 17:34:49 +0200 | [diff] [blame] | 286 | warn "could not re-set your automount preference." |
Chris Masone | 162f654 | 2010-05-12 14:58:37 -0700 | [diff] [blame] | 287 | fi |
| 288 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 289 | if [ -s "$LOCKFILE" ]; then |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame^] | 290 | debug "At least one other pid is running in the chroot, so not" |
| 291 | debug "tearing down env." |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 292 | else |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 293 | MOUNTED_PATH=$(readlink -f "$FLAGS_chroot") |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame^] | 294 | debug "Unmounting chroot environment." |
Zdenek Behan | 6f17b5e | 2010-06-10 16:50:52 -0700 | [diff] [blame] | 295 | # sort the list of mounts in reverse order, to ensure umount of |
| 296 | # cascading mounts in proper order |
| 297 | for i in \ |
| 298 | $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 299 | safe_umount "$i" |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 300 | done |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 301 | fi |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 302 | ) 200>>"$LOCKFILE" || die "teardown_env failed" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 305 | if [ $FLAGS_mount -eq $FLAGS_TRUE ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 306 | setup_env |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame^] | 307 | info "Make sure you run" |
| 308 | info " $0 --unmount" |
| 309 | info "before deleting $FLAGS_chroot" |
| 310 | info "or you'll end up deleting $FLAGS_trunk too!" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 311 | exit 0 |
| 312 | fi |
| 313 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 314 | if [ $FLAGS_unmount -eq $FLAGS_TRUE ]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 315 | teardown_env |
| 316 | exit 0 |
| 317 | fi |
| 318 | |
Doug Anderson | 0c9e88d | 2010-10-19 14:49:39 -0700 | [diff] [blame] | 319 | # Apply any hacks needed to update the chroot. |
| 320 | chroot_hacks_from_outside "${FLAGS_chroot}" |
| 321 | |
| 322 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 323 | # Make sure we unmount before exiting |
| 324 | trap teardown_env EXIT |
| 325 | setup_env |
| 326 | |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 327 | # Get the git revision to pass into the chroot. |
| 328 | # |
| 329 | # This must be determined outside the chroot because (1) there is no |
| 330 | # git inside the chroot, and (2) if there were it would likely be |
| 331 | # the wrong version, which would mess up the .git directories. |
| 332 | # |
| 333 | # Note that this fixes $CHROMEOS_REVISION at the time the chroot is |
| 334 | # entered. That's ok for the main use case of automated builds, |
| 335 | # which pass each command line into a separate call to enter_chroot |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 336 | # so always have up-to-date info. For developer builds, there may not |
| 337 | # be a single revision, since the developer may have |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 338 | # hand-sync'd some subdirs and edited files in others. |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 339 | # In that case, check against origin/HEAD and mark** revision. |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 340 | # Use git:8 chars of sha1 |
David Rochberg | c1a3e56 | 2010-10-04 09:44:57 -0400 | [diff] [blame] | 341 | REVISION=$(cd ${FLAGS_trunk}/src/scripts ; git rev-parse --short=8 HEAD) |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 342 | CHROOT_PASSTHRU="CHROMEOS_REVISION=$REVISION BUILDBOT_BUILD=$FLAGS_build_number CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL" |
Raja Aluri | 8f2a995 | 2010-11-17 15:03:00 -0800 | [diff] [blame] | 343 | CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \ |
| 344 | CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}" |
hexxeh | 72d0b08 | 2010-12-15 13:17:37 -0800 | [diff] [blame] | 345 | CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \ |
| 346 | CHROMEOS_VERSION_TRACK=$CHROMEOS_VERSION_TRACK CHROMEOS_VERSION_AUSERVER=$CHROMEOS_VERSION_AUSERVER CHROMEOS_VERSION_DEVSERVER=$CHROMEOS_VERSION_DEVSERVER" |
Raja Aluri | e891eea | 2010-11-16 20:12:56 -0800 | [diff] [blame] | 347 | |
Raja Aluri | 32759cf | 2010-08-30 18:44:39 -0700 | [diff] [blame] | 348 | if [ -d "$HOME/.subversion" ]; then |
| 349 | # Bind mounting .subversion into chroot |
Don Garrett | a0e7ea1 | 2011-02-07 18:39:59 -0800 | [diff] [blame^] | 350 | debug "mounting ~/.subversion into chroot" |
Raja Aluri | 32759cf | 2010-08-30 18:44:39 -0700 | [diff] [blame] | 351 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/home/${USER}/.subversion")" |
| 352 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then |
| 353 | mkdir -p "$MOUNTED_PATH" |
| 354 | sudo mount --bind "$HOME/.subversion" "$MOUNTED_PATH" || \ |
| 355 | die "Could not mount $MOUNTED_PATH" |
| 356 | fi |
| 357 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 358 | |
David James | 0fd8af4 | 2010-09-27 10:04:57 -0700 | [diff] [blame] | 359 | # Configure committer username and email in chroot .gitconfig |
Chris Sosa | 558940d | 2011-02-01 13:07:55 -0800 | [diff] [blame] | 360 | if [ $FLAGS_git_config -eq $FLAGS_TRUE ]; then |
| 361 | git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ |
| 362 | user.name "$(cd /tmp; git var GIT_COMMITTER_IDENT | sed -e 's/ *<.*//')" |
| 363 | git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all \ |
| 364 | user.email "$(cd /tmp; git var GIT_COMMITTER_IDENT | \ |
| 365 | sed -e 's/.*<\([^>]*\)>.*/\1/')" |
| 366 | fi |
David James | 0fd8af4 | 2010-09-27 10:04:57 -0700 | [diff] [blame] | 367 | |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 368 | # Run command or interactive shell. Also include the non-chrooted path to |
| 369 | # the source trunk for scripts that may need to print it (e.g. |
| 370 | # build_image.sh). |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 371 | sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ |
Elly Jones | 7990a06 | 2010-09-02 09:23:23 -0400 | [diff] [blame] | 372 | EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \ |
Doug Anderson | 9362fa8 | 2010-12-16 14:44:12 -0800 | [diff] [blame] | 373 | SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 374 | |
| 375 | # Remove trap and explicitly unmount |
| 376 | trap - EXIT |
| 377 | teardown_env |