blob: f8dcd1d2df7ea50eb1b3c28b40f759f54a1451ca [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."
Sean Parent2898f752010-05-25 15:06:33 -070027DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \
28 "The mount point of the chrome broswer source in the chroot."
rspangler@google.comd74220d2009-10-09 20:56:14 +000029
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070030DEFINE_boolean official_build $FLAGS_FALSE \
31 "Set CHROMEOS_OFFICIAL=1 for release builds."
rspangler@google.comd74220d2009-10-09 20:56:14 +000032DEFINE_boolean mount $FLAGS_FALSE "Only set up mounts."
33DEFINE_boolean unmount $FLAGS_FALSE "Only tear down mounts."
rspangler@google.comd74220d2009-10-09 20:56:14 +000034
35# More useful help
36FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- \"command\"]
37
38One or more VAR=value pairs can be specified to export variables into
39the chroot environment. For example:
40
41 $0 FOO=bar BAZ=bel
42
43If [-- \"command\"] is present, runs the command inside the chroot,
44after changing directory to /$USER/trunk/src/scripts. Note that the
45command should be enclosed in quotes to prevent interpretation by the
46shell before getting into the chroot. For example:
47
48 $0 -- \"./build_platform_packages.sh\"
49
50Otherwise, provides an interactive shell.
51"
52
53# Parse command line flags
54FLAGS "$@" || exit 1
55eval set -- "${FLAGS_ARGV}"
56
David McMahon857dbb52009-12-09 18:21:05 -080057if [ $FLAGS_official_build -eq $FLAGS_TRUE ]
58then
59 CHROMEOS_OFFICIAL=1
60fi
61
rspangler@google.comd74220d2009-10-09 20:56:14 +000062# Only now can we die on error. shflags functions leak non-zero error codes,
63# so will die prematurely if 'set -e' is specified before now.
64# TODO: replace shflags with something less error-prone, or contribute a fix.
65set -e
66
Sean Parent2898f752010-05-25 15:06:33 -070067INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080068CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -080069INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -070070FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -070071AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
72SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080073
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080074sudo chmod 0777 "$FLAGS_chroot/var/lock"
75
76LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
77
rspangler@google.comd74220d2009-10-09 20:56:14 +000078function setup_env {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080079 (
80 flock 200
81 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +000082
Sean O'Connord52c5ea2010-08-18 21:18:23 +020083 echo >&2 "Mounting chroot environment."
rspangler@google.comd74220d2009-10-09 20:56:14 +000084
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080085 # Mount only if not already mounted
86 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")"
David James546747b2010-03-23 15:19:43 -070087 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080088 then
David James546747b2010-03-23 15:19:43 -070089 sudo mount none -t proc "$MOUNTED_PATH" || \
90 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -080091 fi
rspangler@google.comd74220d2009-10-09 20:56:14 +000092
Antoine Laboure9e585f2010-04-01 15:57:57 -070093 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/sys")"
94 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
95 then
96 sudo mount none -t sysfs "$MOUNTED_PATH" || \
97 die "Could not mount $MOUNTED_PATH"
98 fi
99
robotboy152a1ab2010-04-26 14:07:27 -0700100 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev")"
David James546747b2010-03-23 15:19:43 -0700101 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800102 then
robotboy152a1ab2010-04-26 14:07:27 -0700103 sudo mount --bind /dev "$MOUNTED_PATH" || \
David James546747b2010-03-23 15:19:43 -0700104 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800105 fi
106
Zdenek Behan6f17b5e2010-06-10 16:50:52 -0700107 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev/pts")"
108 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
109 then
110 sudo mount none -t devpts "$MOUNTED_PATH" || \
111 die "Could not mount $MOUNTED_PATH"
112 fi
113
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800114 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")"
David James546747b2010-03-23 15:19:43 -0700115 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800116 then
David James546747b2010-03-23 15:19:43 -0700117 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" || \
118 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800119 fi
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800120
121 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")"
David James546747b2010-03-23 15:19:43 -0700122 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800123 then
Andrew de los Reyesc1e8d272010-02-13 12:39:21 -0800124 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800125 if [ -z "$CHROME_ROOT" ]; then
126 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
127 2>/dev/null)"
128 fi
129 if [[ ( -z "$CHROME_ROOT" ) || ( ! -d "${CHROME_ROOT}/src" ) ]]; then
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200130 echo >&2 "Not mounting chrome source"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800131 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
132 else
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200133 echo >&2 "Mounting chrome source at: $INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800134 echo "$CHROME_ROOT" | \
135 sudo dd of="${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
136 mkdir -p "$MOUNTED_PATH"
David James546747b2010-03-23 15:19:43 -0700137 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \
138 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800139 fi
140 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800141
142 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")"
David James546747b2010-03-23 15:19:43 -0700143 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800144 then
145 if [ $(which gclient 2>/dev/null) ]; then
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200146 echo >&2 "Mounting depot_tools"
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800147 DEPOT_TOOLS=$(dirname $(which gclient) )
148 mkdir -p "$MOUNTED_PATH"
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800149 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200150 echo >&2 "depot_tools failed to mount; perhaps it's on NFS?"
151 echo >&2 "This may impact chromium build."
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800152 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800153 fi
Chris Sosa317d8eb2010-04-05 15:45:28 -0700154 fi
155
robotboy152a1ab2010-04-26 14:07:27 -0700156 # Install fuse module.
157 if [ -c "${FUSE_DEVICE}" ] ; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700158 sudo modprobe fuse 2> /dev/null ||\
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200159 echo >&2 "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700160 fi
161
Chris Masone162f6542010-05-12 14:58:37 -0700162 # Turn off automounting of external media when we enter the
163 # chroot; thus we don't have to worry about being able to unmount
164 # from inside.
165 if [ $(which gconftool-2 2>/dev/null) ]; then
166 gconftool-2 -g ${AUTOMOUNT_PREF} > \
167 "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
168 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200169 echo >&2 "-- Note: USB sticks may be automounted by your host OS."
170 echo >&2 "-- Note: If you plan to burn bootable media, you may need to"
171 echo >&2 "-- Note: unmount these devices manually, or run image_to_usb.sh"
172 echo >&2 "-- Note: outside the chroot."
Chris Masone162f6542010-05-12 14:58:37 -0700173 fi
174 fi
175
David James546747b2010-03-23 15:19:43 -0700176 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000177}
178
179function teardown_env {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800180 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800181 (
182 flock 200
183
184 # check each pid in $LOCKFILE to see if it's died unexpectedly
185 TMP_LOCKFILE="$LOCKFILE.tmp"
186
187 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
188 cat "$LOCKFILE" | while read PID; do
189 if [ "$PID" = "$$" ]; then
190 # ourself, leave PROC_NAME empty
191 PROC_NAME=""
192 else
193 PROC_NAME=$(ps --pid $PID -o comm=)
194 fi
195
196 if [ ! -z "$PROC_NAME" ]; then
197 # All good, keep going
198 echo "$PID" >> "$TMP_LOCKFILE"
199 fi
200 done
201 # Remove any dups from lock file while installing new one
202 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE"
203
Chris Masone162f6542010-05-12 14:58:37 -0700204 if [ $(which gconftool-2 2>/dev/null) ]; then
205 SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
206 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200207 echo >&2 "could not re-set your automount preference."
Chris Masone162f6542010-05-12 14:58:37 -0700208 fi
209
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800210 if [ -s "$LOCKFILE" ]; then
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200211 echo >&2 "At least one other pid is running in the chroot, so not"
212 echo >&2 "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800213 else
David James546747b2010-03-23 15:19:43 -0700214 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200215 echo >&2 "Unmounting chroot environment."
Zdenek Behan6f17b5e2010-06-10 16:50:52 -0700216 # sort the list of mounts in reverse order, to ensure umount of
217 # cascading mounts in proper order
218 for i in \
219 $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do
robotboy98912212010-04-12 14:08:14 -0700220 safe_umount "$i"
David James546747b2010-03-23 15:19:43 -0700221 done
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800222 fi
David James546747b2010-03-23 15:19:43 -0700223 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000224}
225
226if [ $FLAGS_mount -eq $FLAGS_TRUE ]
227then
228 setup_env
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200229 echo >&2 "Make sure you run"
230 echo >&2 " $0 --unmount"
231 echo >&2 "before deleting $FLAGS_chroot"
232 echo >&2 "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000233 exit 0
234fi
235
236if [ $FLAGS_unmount -eq $FLAGS_TRUE ]
237then
238 teardown_env
239 exit 0
240fi
241
242# Make sure we unmount before exiting
243trap teardown_env EXIT
244setup_env
245
David McMahon03aeb202009-12-08 12:47:08 -0800246# Get the git revision to pass into the chroot.
247#
248# This must be determined outside the chroot because (1) there is no
249# git inside the chroot, and (2) if there were it would likely be
250# the wrong version, which would mess up the .git directories.
251#
252# Note that this fixes $CHROMEOS_REVISION at the time the chroot is
253# entered. That's ok for the main use case of automated builds,
254# which pass each command line into a separate call to enter_chroot
David McMahon857dbb52009-12-09 18:21:05 -0800255# so always have up-to-date info. For developer builds, there may not
256# be a single revision, since the developer may have
David McMahon03aeb202009-12-08 12:47:08 -0800257# hand-sync'd some subdirs and edited files in others.
David McMahon857dbb52009-12-09 18:21:05 -0800258# In that case, check against origin/HEAD and mark** revision.
David McMahon03aeb202009-12-08 12:47:08 -0800259# Use git:8 chars of sha1
Anush Elangovan587d7492010-06-10 15:39:19 -0700260REVISION=$(git rev-parse --short=8 HEAD)
David McMahon857dbb52009-12-09 18:21:05 -0800261CHROOT_PASSTHRU="CHROMEOS_REVISION=$REVISION BUILDBOT_BUILD=$FLAGS_build_number CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000262
derat@google.com4e7a92b2009-11-21 23:44:14 +0000263# Run command or interactive shell. Also include the non-chrooted path to
264# the source trunk for scripts that may need to print it (e.g.
265# build_image.sh).
David McMahon857dbb52009-12-09 18:21:05 -0800266sudo chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
Allen Martin07bfc7a2010-07-13 15:44:59 -0700267 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C -- "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000268
269# Remove trap and explicitly unmount
270trap - EXIT
271teardown_env