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 | |
| 9 | # Load common constants. This should be the first executable line. |
| 10 | # The path to common.sh should be relative to your script's location. |
| 11 | . "$(dirname "$0")/common.sh" |
| 12 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 13 | # 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] | 14 | assert_outside_chroot |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 15 | assert_not_root_user |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 16 | |
| 17 | # Define command line flags |
| 18 | # See http://code.google.com/p/shflags/wiki/Documentation10x |
| 19 | DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \ |
| 20 | "The destination dir for the chroot environment." "d" |
| 21 | DEFINE_string trunk "$GCLIENT_ROOT" \ |
| 22 | "The source trunk to bind mount within the chroot." "s" |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 23 | DEFINE_string build_number "" \ |
| 24 | "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] | 25 | DEFINE_string chrome_root "" \ |
| 26 | "The root of your chrome browser source. Should contain a 'src' subdir." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 27 | |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 28 | DEFINE_boolean official_build $FLAGS_FALSE "Set CHROMEOS_OFFICIAL=1 for release builds." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 29 | DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts." |
| 30 | DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 31 | |
| 32 | # More useful help |
| 33 | FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- \"command\"] |
| 34 | |
| 35 | One or more VAR=value pairs can be specified to export variables into |
| 36 | the chroot environment. For example: |
| 37 | |
| 38 | $0 FOO=bar BAZ=bel |
| 39 | |
| 40 | If [-- \"command\"] is present, runs the command inside the chroot, |
| 41 | after changing directory to /$USER/trunk/src/scripts. Note that the |
| 42 | command should be enclosed in quotes to prevent interpretation by the |
| 43 | shell before getting into the chroot. For example: |
| 44 | |
| 45 | $0 -- \"./build_platform_packages.sh\" |
| 46 | |
| 47 | Otherwise, provides an interactive shell. |
| 48 | " |
| 49 | |
| 50 | # Parse command line flags |
| 51 | FLAGS "$@" || exit 1 |
| 52 | eval set -- "${FLAGS_ARGV}" |
| 53 | |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 54 | if [ $FLAGS_official_build -eq $FLAGS_TRUE ] |
| 55 | then |
| 56 | CHROMEOS_OFFICIAL=1 |
| 57 | fi |
| 58 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 59 | # Only now can we die on error. shflags functions leak non-zero error codes, |
| 60 | # so will die prematurely if 'set -e' is specified before now. |
| 61 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
| 62 | set -e |
| 63 | |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 64 | INNER_CHROME_ROOT="/home/$USER/chrome_root" # inside chroot |
| 65 | CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 66 | INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 67 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 68 | sudo chmod 0777 "$FLAGS_chroot/var/lock" |
| 69 | |
| 70 | LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot" |
| 71 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 72 | function setup_env { |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 73 | ( |
| 74 | flock 200 |
| 75 | echo $$ >> "$LOCKFILE" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 76 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 77 | echo "Mounting chroot environment." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 78 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 79 | # Mount only if not already mounted |
| 80 | MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" |
| 81 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 82 | then |
| 83 | sudo mount none -t proc "$MOUNTED_PATH" |
| 84 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 85 | |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 86 | MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/dev/pts")" |
| 87 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 88 | then |
| 89 | sudo mount none -t devpts "$MOUNTED_PATH" |
| 90 | fi |
| 91 | |
| 92 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" |
| 93 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 94 | then |
| 95 | sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" |
| 96 | fi |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 97 | |
| 98 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")" |
| 99 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 100 | then |
Andrew de los Reyes | c1e8d27 | 2010-02-13 12:39:21 -0800 | [diff] [blame^] | 101 | ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")" |
Andrew de los Reyes | 6d0ca16 | 2010-02-12 14:36:08 -0800 | [diff] [blame] | 102 | if [ -z "$CHROME_ROOT" ]; then |
| 103 | ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \ |
| 104 | 2>/dev/null)" |
| 105 | fi |
| 106 | if [[ ( -z "$CHROME_ROOT" ) || ( ! -d "${CHROME_ROOT}/src" ) ]]; then |
| 107 | echo "Not mounting chrome source" |
| 108 | sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
| 109 | else |
| 110 | echo "Mounting chrome source at: $INNER_CHROME_ROOT" |
| 111 | echo "$CHROME_ROOT" | \ |
| 112 | sudo dd of="${FLAGS_chroot}${CHROME_ROOT_CONFIG}" |
| 113 | mkdir -p "$MOUNTED_PATH" |
| 114 | sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" |
| 115 | fi |
| 116 | fi |
Andrew de los Reyes | 5d0248f | 2010-02-12 16:12:31 -0800 | [diff] [blame] | 117 | |
| 118 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")" |
| 119 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 120 | then |
| 121 | if [ $(which gclient 2>/dev/null) ]; then |
| 122 | echo "Mounting depot_tools" |
| 123 | DEPOT_TOOLS=$(dirname $(which gclient) ) |
| 124 | mkdir -p "$MOUNTED_PATH" |
| 125 | sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH" |
| 126 | fi |
| 127 | fi |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 128 | ) 200>>"$LOCKFILE" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | function teardown_env { |
Andrew de los Reyes | c9317ea | 2010-02-10 13:16:36 -0800 | [diff] [blame] | 132 | # Only teardown if we're the last enter_chroot to die |
| 133 | |
| 134 | ( |
| 135 | flock 200 |
| 136 | |
| 137 | # check each pid in $LOCKFILE to see if it's died unexpectedly |
| 138 | TMP_LOCKFILE="$LOCKFILE.tmp" |
| 139 | |
| 140 | echo -n > "$TMP_LOCKFILE" # Erase/reset temp file |
| 141 | cat "$LOCKFILE" | while read PID; do |
| 142 | if [ "$PID" = "$$" ]; then |
| 143 | # ourself, leave PROC_NAME empty |
| 144 | PROC_NAME="" |
| 145 | else |
| 146 | PROC_NAME=$(ps --pid $PID -o comm=) |
| 147 | fi |
| 148 | |
| 149 | if [ ! -z "$PROC_NAME" ]; then |
| 150 | # All good, keep going |
| 151 | echo "$PID" >> "$TMP_LOCKFILE" |
| 152 | fi |
| 153 | done |
| 154 | # Remove any dups from lock file while installing new one |
| 155 | sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE" |
| 156 | |
| 157 | if [ -s "$LOCKFILE" ]; then |
| 158 | echo "At least one other pid is running in the chroot, so not" |
| 159 | echo "tearing down env." |
| 160 | else |
| 161 | echo "Unmounting chroot environment." |
| 162 | mount | grep "on $(readlink -f "$FLAGS_chroot")" | awk '{print $3}' \ |
| 163 | | xargs -r -L1 sudo umount |
| 164 | fi |
| 165 | ) 200>>"$LOCKFILE" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if [ $FLAGS_mount -eq $FLAGS_TRUE ] |
| 169 | then |
| 170 | setup_env |
| 171 | echo "Make sure you run" |
| 172 | echo " $0 --unmount" |
| 173 | echo "before deleting $FLAGS_chroot" |
| 174 | echo "or you'll end up deleting $FLAGS_trunk too!" |
| 175 | exit 0 |
| 176 | fi |
| 177 | |
| 178 | if [ $FLAGS_unmount -eq $FLAGS_TRUE ] |
| 179 | then |
| 180 | teardown_env |
| 181 | exit 0 |
| 182 | fi |
| 183 | |
| 184 | # Make sure we unmount before exiting |
| 185 | trap teardown_env EXIT |
| 186 | setup_env |
| 187 | |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 188 | # Get the git revision to pass into the chroot. |
| 189 | # |
| 190 | # This must be determined outside the chroot because (1) there is no |
| 191 | # git inside the chroot, and (2) if there were it would likely be |
| 192 | # the wrong version, which would mess up the .git directories. |
| 193 | # |
| 194 | # Note that this fixes $CHROMEOS_REVISION at the time the chroot is |
| 195 | # entered. That's ok for the main use case of automated builds, |
| 196 | # which pass each command line into a separate call to enter_chroot |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 197 | # so always have up-to-date info. For developer builds, there may not |
| 198 | # be a single revision, since the developer may have |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 199 | # hand-sync'd some subdirs and edited files in others. |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 200 | # In that case, check against origin/HEAD and mark** revision. |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 201 | # Use git:8 chars of sha1 |
| 202 | REVISION=$(git rev-parse HEAD) |
| 203 | ORIGIN_REVISION=$(git rev-parse origin/HEAD) |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 204 | # Do not check for clean revision on official builds. They are coming directly |
| 205 | # from a branch rev and cannot compare to origin/HEAD. |
| 206 | if [ $FLAGS_official_build != $FLAGS_TRUE ] && \ |
| 207 | [ "$REVISION" != "$ORIGIN_REVISION" ] |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 208 | then |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 209 | # Mark dirty tree with "**" |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 210 | REVISION="${REVISION:0:8}**" |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 211 | else |
| 212 | REVISION="${REVISION:0:8}" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 213 | fi |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 214 | CHROOT_PASSTHRU="CHROMEOS_REVISION=$REVISION BUILDBOT_BUILD=$FLAGS_build_number CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 215 | |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 216 | # Run command or interactive shell. Also include the non-chrooted path to |
| 217 | # the source trunk for scripts that may need to print it (e.g. |
| 218 | # build_image.sh). |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 219 | sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ |
Colin Watson | 3a3566b | 2010-01-07 07:36:32 +0000 | [diff] [blame] | 220 | EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C "$@" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 221 | |
| 222 | # Remove trap and explicitly unmount |
| 223 | trap - EXIT |
| 224 | teardown_env |