blob: 940e47a2b7e16574239ee4acd5677d9f18ab224c [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
derat@google.com86dcc8e2009-11-21 19:49:49 +000013# Script must be run outside the chroot and as a regular user.
rspangler@google.comd74220d2009-10-09 20:56:14 +000014assert_outside_chroot
derat@google.com86dcc8e2009-11-21 19:49:49 +000015assert_not_root_user
rspangler@google.comd74220d2009-10-09 20:56:14 +000016
17# Define command line flags
18# See http://code.google.com/p/shflags/wiki/Documentation10x
19DEFINE_string chroot "$DEFAULT_CHROOT_DIR" \
20 "The destination dir for the chroot environment." "d"
21DEFINE_string trunk "$GCLIENT_ROOT" \
22 "The source trunk to bind mount within the chroot." "s"
David McMahon03aeb202009-12-08 12:47:08 -080023DEFINE_string build_number "" \
24 "The build-bot build number (when called by buildbot only)." "b"
rspangler@google.comd74220d2009-10-09 20:56:14 +000025
26DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
27DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
rspangler@google.comd74220d2009-10-09 20:56:14 +000028
29# More useful help
30FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- \"command\"]
31
32One or more VAR=value pairs can be specified to export variables into
33the chroot environment. For example:
34
35 $0 FOO=bar BAZ=bel
36
37If [-- \"command\"] is present, runs the command inside the chroot,
38after changing directory to /$USER/trunk/src/scripts. Note that the
39command should be enclosed in quotes to prevent interpretation by the
40shell before getting into the chroot. For example:
41
42 $0 -- \"./build_platform_packages.sh\"
43
44Otherwise, provides an interactive shell.
45"
46
47# Parse command line flags
48FLAGS "$@" || exit 1
49eval set -- "${FLAGS_ARGV}"
50
51# Only now can we die on error. shflags functions leak non-zero error codes,
52# so will die prematurely if 'set -e' is specified before now.
53# TODO: replace shflags with something less error-prone, or contribute a fix.
54set -e
55
56function setup_env {
57 echo "Mounting chroot environment."
58
59 # Mount only if not already mounted
60 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")"
61 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ]
62 then
63 sudo mount none -t proc "$MOUNTED_PATH"
64 fi
65
66 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/dev/pts")"
67 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ]
68 then
69 sudo mount none -t devpts "$MOUNTED_PATH"
70 fi
71
72 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")"
73 if [ -z "$(mount | grep -F "on $MOUNTED_PATH")" ]
74 then
75 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH"
76 fi
77}
78
79function teardown_env {
80 echo "Unmounting chroot environment."
81 mount | grep "on $(readlink -f "$FLAGS_chroot")" | awk '{print $3}' \
82 | xargs -r -L1 sudo umount
83}
84
85if [ $FLAGS_mount -eq $FLAGS_TRUE ]
86then
87 setup_env
88 echo "Make sure you run"
89 echo " $0 --unmount"
90 echo "before deleting $FLAGS_chroot"
91 echo "or you'll end up deleting $FLAGS_trunk too!"
92 exit 0
93fi
94
95if [ $FLAGS_unmount -eq $FLAGS_TRUE ]
96then
97 teardown_env
98 exit 0
99fi
100
101# Make sure we unmount before exiting
102trap teardown_env EXIT
103setup_env
104
David McMahon03aeb202009-12-08 12:47:08 -0800105# Get the git revision to pass into the chroot.
106#
107# This must be determined outside the chroot because (1) there is no
108# git inside the chroot, and (2) if there were it would likely be
109# the wrong version, which would mess up the .git directories.
110#
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.
117# Use git:8 chars of sha1
118REVISION=$(git rev-parse HEAD)
119ORIGIN_REVISION=$(git rev-parse origin/HEAD)
120if [ "$REVISION" != "$ORIGIN_REVISION" ]
rspangler@google.comd74220d2009-10-09 20:56:14 +0000121then
David McMahon03aeb202009-12-08 12:47:08 -0800122 # Mark dirty tree with "**"
123 REVISION="${REVISION:0:8}**"
124else
125 REVISION="${REVISION:0:8}"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000126fi
David McMahon03aeb202009-12-08 12:47:08 -0800127REVISION="CHROMEOS_REVISION=$REVISION BUILDBOT_BUILD=$FLAGS_build_number"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000128
derat@google.com4e7a92b2009-11-21 23:44:14 +0000129# Run command or interactive shell. Also include the non-chrooted path to
130# the source trunk for scripts that may need to print it (e.g.
131# build_image.sh).
132sudo chroot "$FLAGS_chroot" sudo -i -u $USER $REVISION \
133 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000134
135# Remove trap and explicitly unmount
136trap - EXIT
137teardown_env