blob: 6ad51fa44c251f4e11f8f478994c19e320481bc4 [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"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080025DEFINE_string chrome_root "" \
26 "The root of your chrome browser source. Should contain a 'src' subdir."
rspangler@google.comd74220d2009-10-09 20:56:14 +000027
David McMahon857dbb52009-12-09 18:21:05 -080028DEFINE_boolean official_build $FLAGS_FALSE "Set CHROMEOS_OFFICIAL=1 for release builds."
rspangler@google.comd74220d2009-10-09 20:56:14 +000029DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
30DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
rspangler@google.comd74220d2009-10-09 20:56:14 +000031
32# More useful help
33FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- \"command\"]
34
35One or more VAR=value pairs can be specified to export variables into
36the chroot environment. For example:
37
38 $0 FOO=bar BAZ=bel
39
40If [-- \"command\"] is present, runs the command inside the chroot,
41after changing directory to /$USER/trunk/src/scripts. Note that the
42command should be enclosed in quotes to prevent interpretation by the
43shell before getting into the chroot. For example:
44
45 $0 -- \"./build_platform_packages.sh\"
46
47Otherwise, provides an interactive shell.
48"
49
50# Parse command line flags
51FLAGS "$@" || exit 1
52eval set -- "${FLAGS_ARGV}"
53
David McMahon857dbb52009-12-09 18:21:05 -080054if [ $FLAGS_official_build -eq $FLAGS_TRUE ]
55then
56 CHROMEOS_OFFICIAL=1
57fi
58
rspangler@google.comd74220d2009-10-09 20:56:14 +000059# Only now can we die on error. shflags functions leak non-zero error codes,
60# so will die prematurely if 'set -e' is specified before now.
61# TODO: replace shflags with something less error-prone, or contribute a fix.
62set -e
63
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080064INNER_CHROME_ROOT="/home/$USER/chrome_root" # inside chroot
65CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080066INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080067
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080068sudo chmod 0777 "$FLAGS_chroot/var/lock"
69
70LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
71
rspangler@google.comd74220d2009-10-09 20:56:14 +000072function setup_env {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080073 (
74 flock 200
75 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +000076
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080077 echo "Mounting chroot environment."
rspangler@google.comd74220d2009-10-09 20:56:14 +000078
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080079 # Mount only if not already mounted
80 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")"
David James546747b2010-03-23 15:19:43 -070081 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080082 then
David James546747b2010-03-23 15:19:43 -070083 sudo mount none -t proc "$MOUNTED_PATH" || \
84 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080085 fi
rspangler@google.comd74220d2009-10-09 20:56:14 +000086
Antoine Laboure9e585f2010-04-01 15:57:57 -070087 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/sys")"
88 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
89 then
90 sudo mount none -t sysfs "$MOUNTED_PATH" || \
91 die "Could not mount $MOUNTED_PATH"
92 fi
93
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080094 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/dev/pts")"
David James546747b2010-03-23 15:19:43 -070095 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080096 then
David James546747b2010-03-23 15:19:43 -070097 sudo mount none -t devpts "$MOUNTED_PATH" || \
98 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080099 fi
100
101 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")"
David James546747b2010-03-23 15:19:43 -0700102 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800103 then
David James546747b2010-03-23 15:19:43 -0700104 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" || \
105 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800106 fi
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800107
108 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")"
David James546747b2010-03-23 15:19:43 -0700109 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800110 then
Andrew de los Reyesc1e8d272010-02-13 12:39:21 -0800111 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800112 if [ -z "$CHROME_ROOT" ]; then
113 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
114 2>/dev/null)"
115 fi
116 if [[ ( -z "$CHROME_ROOT" ) || ( ! -d "${CHROME_ROOT}/src" ) ]]; then
117 echo "Not mounting chrome source"
118 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
119 else
120 echo "Mounting chrome source at: $INNER_CHROME_ROOT"
121 echo "$CHROME_ROOT" | \
122 sudo dd of="${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
123 mkdir -p "$MOUNTED_PATH"
David James546747b2010-03-23 15:19:43 -0700124 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \
125 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800126 fi
127 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800128
129 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")"
David James546747b2010-03-23 15:19:43 -0700130 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800131 then
132 if [ $(which gclient 2>/dev/null) ]; then
133 echo "Mounting depot_tools"
134 DEPOT_TOOLS=$(dirname $(which gclient) )
135 mkdir -p "$MOUNTED_PATH"
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800136 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then
137 echo "depot_tools failed to mount; perhaps it's on NFS?"
138 echo "This may impact chromium build."
139 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800140 fi
141 fi
David James546747b2010-03-23 15:19:43 -0700142 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000143}
144
145function teardown_env {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800146 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800147 (
148 flock 200
149
150 # check each pid in $LOCKFILE to see if it's died unexpectedly
151 TMP_LOCKFILE="$LOCKFILE.tmp"
152
153 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
154 cat "$LOCKFILE" | while read PID; do
155 if [ "$PID" = "$$" ]; then
156 # ourself, leave PROC_NAME empty
157 PROC_NAME=""
158 else
159 PROC_NAME=$(ps --pid $PID -o comm=)
160 fi
161
162 if [ ! -z "$PROC_NAME" ]; then
163 # All good, keep going
164 echo "$PID" >> "$TMP_LOCKFILE"
165 fi
166 done
167 # Remove any dups from lock file while installing new one
168 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE"
169
170 if [ -s "$LOCKFILE" ]; then
171 echo "At least one other pid is running in the chroot, so not"
172 echo "tearing down env."
173 else
David James546747b2010-03-23 15:19:43 -0700174 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800175 echo "Unmounting chroot environment."
David James546747b2010-03-23 15:19:43 -0700176 for i in $(mount | grep -F "on $MOUNTED_PATH/" | awk '{print $3}'); do
177 sudo umount "$i" || die "Failed to umount $i"
178 done
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800179 fi
David James546747b2010-03-23 15:19:43 -0700180 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000181}
182
183if [ $FLAGS_mount -eq $FLAGS_TRUE ]
184then
185 setup_env
186 echo "Make sure you run"
187 echo " $0 --unmount"
188 echo "before deleting $FLAGS_chroot"
189 echo "or you'll end up deleting $FLAGS_trunk too!"
190 exit 0
191fi
192
193if [ $FLAGS_unmount -eq $FLAGS_TRUE ]
194then
195 teardown_env
196 exit 0
197fi
198
199# Make sure we unmount before exiting
200trap teardown_env EXIT
201setup_env
202
David McMahon03aeb202009-12-08 12:47:08 -0800203# Get the git revision to pass into the chroot.
204#
205# This must be determined outside the chroot because (1) there is no
206# git inside the chroot, and (2) if there were it would likely be
207# the wrong version, which would mess up the .git directories.
208#
209# Note that this fixes $CHROMEOS_REVISION at the time the chroot is
210# entered. That's ok for the main use case of automated builds,
211# which pass each command line into a separate call to enter_chroot
David McMahon857dbb52009-12-09 18:21:05 -0800212# so always have up-to-date info. For developer builds, there may not
213# be a single revision, since the developer may have
David McMahon03aeb202009-12-08 12:47:08 -0800214# hand-sync'd some subdirs and edited files in others.
David McMahon857dbb52009-12-09 18:21:05 -0800215# In that case, check against origin/HEAD and mark** revision.
David McMahon03aeb202009-12-08 12:47:08 -0800216# Use git:8 chars of sha1
217REVISION=$(git rev-parse HEAD)
218ORIGIN_REVISION=$(git rev-parse origin/HEAD)
David McMahon857dbb52009-12-09 18:21:05 -0800219# Do not check for clean revision on official builds. They are coming directly
220# from a branch rev and cannot compare to origin/HEAD.
221if [ $FLAGS_official_build != $FLAGS_TRUE ] && \
222 [ "$REVISION" != "$ORIGIN_REVISION" ]
rspangler@google.comd74220d2009-10-09 20:56:14 +0000223then
David McMahon03aeb202009-12-08 12:47:08 -0800224 # Mark dirty tree with "**"
David McMahon857dbb52009-12-09 18:21:05 -0800225 REVISION="${REVISION:0:8}**"
David McMahon03aeb202009-12-08 12:47:08 -0800226else
227 REVISION="${REVISION:0:8}"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000228fi
David McMahon857dbb52009-12-09 18:21:05 -0800229CHROOT_PASSTHRU="CHROMEOS_REVISION=$REVISION BUILDBOT_BUILD=$FLAGS_build_number CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000230
derat@google.com4e7a92b2009-11-21 23:44:14 +0000231# Run command or interactive shell. Also include the non-chrooted path to
232# the source trunk for scripts that may need to print it (e.g.
233# build_image.sh).
David McMahon857dbb52009-12-09 18:21:05 -0800234sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
Colin Watson3a3566b2010-01-07 07:36:32 +0000235 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000236
237# Remove trap and explicitly unmount
238trap - EXIT
239teardown_env