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