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