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