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" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 25 | |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 26 | 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] | 27 | DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts." |
| 28 | DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 29 | |
| 30 | # More useful help |
| 31 | FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- \"command\"] |
| 32 | |
| 33 | One or more VAR=value pairs can be specified to export variables into |
| 34 | the chroot environment. For example: |
| 35 | |
| 36 | $0 FOO=bar BAZ=bel |
| 37 | |
| 38 | If [-- \"command\"] is present, runs the command inside the chroot, |
| 39 | after changing directory to /$USER/trunk/src/scripts. Note that the |
| 40 | command should be enclosed in quotes to prevent interpretation by the |
| 41 | shell before getting into the chroot. For example: |
| 42 | |
| 43 | $0 -- \"./build_platform_packages.sh\" |
| 44 | |
| 45 | Otherwise, provides an interactive shell. |
| 46 | " |
| 47 | |
| 48 | # Parse command line flags |
| 49 | FLAGS "$@" || exit 1 |
| 50 | eval set -- "${FLAGS_ARGV}" |
| 51 | |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 52 | if [ $FLAGS_official_build -eq $FLAGS_TRUE ] |
| 53 | then |
| 54 | CHROMEOS_OFFICIAL=1 |
| 55 | fi |
| 56 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 57 | # Only now can we die on error. shflags functions leak non-zero error codes, |
| 58 | # so will die prematurely if 'set -e' is specified before now. |
| 59 | # TODO: replace shflags with something less error-prone, or contribute a fix. |
| 60 | set -e |
| 61 | |
| 62 | function setup_env { |
| 63 | echo "Mounting chroot environment." |
| 64 | |
| 65 | # Mount only if not already mounted |
| 66 | MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")" |
| 67 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 68 | then |
| 69 | sudo mount none -t proc "$MOUNTED_PATH" |
| 70 | fi |
| 71 | |
| 72 | MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/dev/pts")" |
| 73 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 74 | then |
| 75 | sudo mount none -t devpts "$MOUNTED_PATH" |
| 76 | fi |
| 77 | |
| 78 | MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")" |
| 79 | if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ] |
| 80 | then |
| 81 | sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" |
| 82 | fi |
| 83 | } |
| 84 | |
| 85 | function teardown_env { |
| 86 | echo "Unmounting chroot environment." |
| 87 | mount | grep "on $(readlink -f "$FLAGS_chroot")" | awk '{print $3}' \ |
| 88 | | xargs -r -L1 sudo umount |
| 89 | } |
| 90 | |
| 91 | if [ $FLAGS_mount -eq $FLAGS_TRUE ] |
| 92 | then |
| 93 | setup_env |
| 94 | echo "Make sure you run" |
| 95 | echo " $0 --unmount" |
| 96 | echo "before deleting $FLAGS_chroot" |
| 97 | echo "or you'll end up deleting $FLAGS_trunk too!" |
| 98 | exit 0 |
| 99 | fi |
| 100 | |
| 101 | if [ $FLAGS_unmount -eq $FLAGS_TRUE ] |
| 102 | then |
| 103 | teardown_env |
| 104 | exit 0 |
| 105 | fi |
| 106 | |
| 107 | # Make sure we unmount before exiting |
| 108 | trap teardown_env EXIT |
| 109 | setup_env |
| 110 | |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 111 | # Get the git revision to pass into the chroot. |
| 112 | # |
| 113 | # This must be determined outside the chroot because (1) there is no |
| 114 | # git inside the chroot, and (2) if there were it would likely be |
| 115 | # the wrong version, which would mess up the .git directories. |
| 116 | # |
| 117 | # Note that this fixes $CHROMEOS_REVISION at the time the chroot is |
| 118 | # entered. That's ok for the main use case of automated builds, |
| 119 | # which pass each command line into a separate call to enter_chroot |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 120 | # so always have up-to-date info. For developer builds, there may not |
| 121 | # be a single revision, since the developer may have |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 122 | # hand-sync'd some subdirs and edited files in others. |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 123 | # In that case, check against origin/HEAD and mark** revision. |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 124 | # Use git:8 chars of sha1 |
| 125 | REVISION=$(git rev-parse HEAD) |
| 126 | ORIGIN_REVISION=$(git rev-parse origin/HEAD) |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 127 | # Do not check for clean revision on official builds. They are coming directly |
| 128 | # from a branch rev and cannot compare to origin/HEAD. |
| 129 | if [ $FLAGS_official_build != $FLAGS_TRUE ] && \ |
| 130 | [ "$REVISION" != "$ORIGIN_REVISION" ] |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 131 | then |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 132 | # Mark dirty tree with "**" |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 133 | REVISION="${REVISION:0:8}**" |
David McMahon | 03aeb20 | 2009-12-08 12:47:08 -0800 | [diff] [blame] | 134 | else |
| 135 | REVISION="${REVISION:0:8}" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 136 | fi |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 137 | 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] | 138 | |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 139 | # Run command or interactive shell. Also include the non-chrooted path to |
| 140 | # the source trunk for scripts that may need to print it (e.g. |
| 141 | # build_image.sh). |
David McMahon | 857dbb5 | 2009-12-09 18:21:05 -0800 | [diff] [blame] | 142 | sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \ |
derat@google.com | 4e7a92b | 2009-11-21 23:44:14 +0000 | [diff] [blame] | 143 | EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" "$@" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 144 | |
| 145 | # Remove trap and explicitly unmount |
| 146 | trap - EXIT |
| 147 | teardown_env |