blob: bc06ddfc4a958b4dae12d959c7dca90140b125fe [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."
Elly Jones7990a062010-09-02 09:23:23 -040034DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
rspangler@google.comd74220d2009-10-09 20:56:14 +000035
36# More useful help
Doug Anderson9362fa82010-12-16 14:44:12 -080037FLAGS_HELP="USAGE: $0 [flags] [VAR=value] [-- command [arg1] [arg2] ...]
rspangler@google.comd74220d2009-10-09 20:56:14 +000038
39One or more VAR=value pairs can be specified to export variables into
40the chroot environment. For example:
41
42 $0 FOO=bar BAZ=bel
43
Doug Anderson9362fa82010-12-16 14:44:12 -080044If [-- command] is present, runs the command inside the chroot,
45after changing directory to /$USER/trunk/src/scripts. Note that neither
46the command nor args should include single quotes. For example:
rspangler@google.comd74220d2009-10-09 20:56:14 +000047
Doug Anderson9362fa82010-12-16 14:44:12 -080048 $0 -- ./build_platform_packages.sh
rspangler@google.comd74220d2009-10-09 20:56:14 +000049
50Otherwise, provides an interactive shell.
51"
52
Doug Anderson9362fa82010-12-16 14:44:12 -080053# Double up on the first '--' argument. Why? For enter_chroot, we want to
54# emulate the behavior of sudo for setting environment vars. That is, we want:
55# ./enter_chroot [flags] [VAR=val] [-- command]
56# ...but shflags ends up eating the '--' out of the command line and gives
57# us back "VAR=val" and "command" together in one chunk. By doubling up, we
58# end up getting what we want back from shflags.
59#
60# Examples of how people might be using enter_chroot:
61# 1. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 -- cmd arg1 arg2
62# Set env vars and run cmd w/ args
63# 2. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2
64# Set env vars and run shell
65# 3. ./enter_chroot [chroot_flags] -- cmd arg1 arg2
66# Run cmd w/ args
67# 4. ./enter_chroot [chroot_flags] VAR1=val1 VAR2=val2 cmd arg1 arg2
68# Like #1 _if_ args aren't flags (if they are, enter_chroot will claim them)
69# 5. ./enter_chroot [chroot_flags] cmd arg1 arg2
70# Like #3 _if_ args aren't flags (if they are, enter_chroot will claim them)
71_FLAGS_FIXED=''
72_SAW_DASHDASH=0
73while [ $# -gt 0 ]; do
74 _FLAGS_FIXED="${_FLAGS_FIXED:+${_FLAGS_FIXED} }'$1'"
75 if [ $_SAW_DASHDASH -eq 0 ] && [[ "$1" == "--" ]]; then
76 _FLAGS_FIXED="${_FLAGS_FIXED:+${_FLAGS_FIXED} }'--'"
77 _SAW_DASHDASH=1
78 fi
79 shift
80done
81eval set -- "${_FLAGS_FIXED}"
82
83
rspangler@google.comd74220d2009-10-09 20:56:14 +000084# Parse command line flags
85FLAGS "$@" || exit 1
86eval set -- "${FLAGS_ARGV}"
87
David McMahon857dbb52009-12-09 18:21:05 -080088if [ $FLAGS_official_build -eq $FLAGS_TRUE ]
89then
90 CHROMEOS_OFFICIAL=1
91fi
92
rspangler@google.comd74220d2009-10-09 20:56:14 +000093# Only now can we die on error. shflags functions leak non-zero error codes,
94# so will die prematurely if 'set -e' is specified before now.
95# TODO: replace shflags with something less error-prone, or contribute a fix.
96set -e
97
Sean Parent2898f752010-05-25 15:06:33 -070098INNER_CHROME_ROOT=$FLAGS_chrome_root_mount # inside chroot
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -080099CHROME_ROOT_CONFIG="/var/cache/chrome_root" # inside chroot
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800100INNER_DEPOT_TOOLS_ROOT="/home/$USER/depot_tools" # inside chroot
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700101FUSE_DEVICE="/dev/fuse"
Chris Masone162f6542010-05-12 14:58:37 -0700102AUTOMOUNT_PREF="/apps/nautilus/preferences/media_automount"
103SAVED_AUTOMOUNT_PREF_FILE="/tmp/.automount_pref"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800104
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800105sudo chmod 0777 "$FLAGS_chroot/var/lock"
106
107LOCKFILE="$FLAGS_chroot/var/lock/enter_chroot"
108
rspangler@google.comd74220d2009-10-09 20:56:14 +0000109function setup_env {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800110 (
111 flock 200
112 echo $$ >> "$LOCKFILE"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000113
Sean Ocd8b1d12010-09-02 17:34:49 +0200114 info "Mounting chroot environment."
rspangler@google.comd74220d2009-10-09 20:56:14 +0000115
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800116 # Mount only if not already mounted
117 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/proc")"
David James546747b2010-03-23 15:19:43 -0700118 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800119 then
David James546747b2010-03-23 15:19:43 -0700120 sudo mount none -t proc "$MOUNTED_PATH" || \
121 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800122 fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000123
Antoine Laboure9e585f2010-04-01 15:57:57 -0700124 MOUNTED_PATH="$(readlink -f "$FLAGS_chroot/sys")"
125 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
126 then
127 sudo mount none -t sysfs "$MOUNTED_PATH" || \
128 die "Could not mount $MOUNTED_PATH"
129 fi
130
robotboy152a1ab2010-04-26 14:07:27 -0700131 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev")"
David James546747b2010-03-23 15:19:43 -0700132 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800133 then
robotboy152a1ab2010-04-26 14:07:27 -0700134 sudo mount --bind /dev "$MOUNTED_PATH" || \
David James546747b2010-03-23 15:19:43 -0700135 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800136 fi
137
Elly Jones7990a062010-09-02 09:23:23 -0400138 if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
139 TARGET_DIR="$(readlink -f "${FLAGS_chroot}/home/${USER}/.ssh")"
140 if [ -n "${SSH_AUTH_SOCK}" \
141 -a -d "${HOME}/.ssh" ]
142 then
143 mkdir -p "${TARGET_DIR}"
144 cp -r "${HOME}/.ssh/known_hosts" "${TARGET_DIR}"
145 ASOCK="$(dirname "${SSH_AUTH_SOCK}")"
146 mkdir -p "${FLAGS_chroot}/${ASOCK}"
147 sudo mount --bind "${ASOCK}" "${FLAGS_chroot}/${ASOCK}" || \
148 die "Count not mount ${ASOCK}"
149 fi
150 fi
151
Zdenek Behan6f17b5e2010-06-10 16:50:52 -0700152 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/dev/pts")"
153 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
154 then
155 sudo mount none -t devpts "$MOUNTED_PATH" || \
156 die "Could not mount $MOUNTED_PATH"
157 fi
158
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800159 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}$CHROOT_TRUNK_DIR")"
David James546747b2010-03-23 15:19:43 -0700160 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800161 then
David James546747b2010-03-23 15:19:43 -0700162 sudo mount --bind "$FLAGS_trunk" "$MOUNTED_PATH" || \
163 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800164 fi
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800165
166 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_CHROME_ROOT}")"
David James546747b2010-03-23 15:19:43 -0700167 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800168 then
Andrew de los Reyesc1e8d272010-02-13 12:39:21 -0800169 ! CHROME_ROOT="$(readlink -f "$FLAGS_chrome_root")"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800170 if [ -z "$CHROME_ROOT" ]; then
171 ! CHROME_ROOT="$(cat "${FLAGS_chroot}${CHROME_ROOT_CONFIG}" \
172 2>/dev/null)"
173 fi
174 if [[ ( -z "$CHROME_ROOT" ) || ( ! -d "${CHROME_ROOT}/src" ) ]]; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200175 info "Not mounting chrome source"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800176 sudo rm -f "${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
177 else
Sean Ocd8b1d12010-09-02 17:34:49 +0200178 info "Mounting chrome source at: $INNER_CHROME_ROOT"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800179 echo "$CHROME_ROOT" | \
180 sudo dd of="${FLAGS_chroot}${CHROME_ROOT_CONFIG}"
181 mkdir -p "$MOUNTED_PATH"
David James546747b2010-03-23 15:19:43 -0700182 sudo mount --bind "$CHROME_ROOT" "$MOUNTED_PATH" || \
183 die "Could not mount $MOUNTED_PATH"
Andrew de los Reyes6d0ca162010-02-12 14:36:08 -0800184 fi
185 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800186
187 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}${INNER_DEPOT_TOOLS_ROOT}")"
David James546747b2010-03-23 15:19:43 -0700188 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800189 then
190 if [ $(which gclient 2>/dev/null) ]; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200191 info "Mounting depot_tools"
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800192 DEPOT_TOOLS=$(dirname $(which gclient) )
193 mkdir -p "$MOUNTED_PATH"
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800194 if ! sudo mount --bind "$DEPOT_TOOLS" "$MOUNTED_PATH"; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200195 warn "depot_tools failed to mount; perhaps it's on NFS?"
196 warn "This may impact chromium build."
Andrew de los Reyese8b63152010-02-16 14:18:34 -0800197 fi
Andrew de los Reyes5d0248f2010-02-12 16:12:31 -0800198 fi
Chris Sosa317d8eb2010-04-05 15:45:28 -0700199 fi
200
robotboy152a1ab2010-04-26 14:07:27 -0700201 # Install fuse module.
202 if [ -c "${FUSE_DEVICE}" ] ; then
Chris Sosa317d8eb2010-04-05 15:45:28 -0700203 sudo modprobe fuse 2> /dev/null ||\
Sean Ocd8b1d12010-09-02 17:34:49 +0200204 warn "-- Note: modprobe fuse failed. gmergefs will not work"
Chris Sosa317d8eb2010-04-05 15:45:28 -0700205 fi
206
Chris Masone162f6542010-05-12 14:58:37 -0700207 # Turn off automounting of external media when we enter the
208 # chroot; thus we don't have to worry about being able to unmount
209 # from inside.
210 if [ $(which gconftool-2 2>/dev/null) ]; then
211 gconftool-2 -g ${AUTOMOUNT_PREF} > \
212 "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
213 if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200214 warn "-- Note: USB sticks may be automounted by your host OS."
215 warn "-- Note: If you plan to burn bootable media, you may need to"
216 warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
217 warn "-- Note: outside the chroot."
Chris Masone162f6542010-05-12 14:58:37 -0700218 fi
219 fi
220
David James546747b2010-03-23 15:19:43 -0700221 ) 200>>"$LOCKFILE" || die "setup_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000222}
223
224function teardown_env {
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800225 # Only teardown if we're the last enter_chroot to die
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800226 (
227 flock 200
228
229 # check each pid in $LOCKFILE to see if it's died unexpectedly
230 TMP_LOCKFILE="$LOCKFILE.tmp"
231
232 echo -n > "$TMP_LOCKFILE" # Erase/reset temp file
233 cat "$LOCKFILE" | while read PID; do
234 if [ "$PID" = "$$" ]; then
235 # ourself, leave PROC_NAME empty
236 PROC_NAME=""
237 else
238 PROC_NAME=$(ps --pid $PID -o comm=)
239 fi
240
241 if [ ! -z "$PROC_NAME" ]; then
242 # All good, keep going
243 echo "$PID" >> "$TMP_LOCKFILE"
244 fi
245 done
246 # Remove any dups from lock file while installing new one
247 sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE"
248
Chris Masone162f6542010-05-12 14:58:37 -0700249 if [ $(which gconftool-2 2>/dev/null) ]; then
250 SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
251 gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
Sean Ocd8b1d12010-09-02 17:34:49 +0200252 warn "could not re-set your automount preference."
Chris Masone162f6542010-05-12 14:58:37 -0700253 fi
254
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800255 if [ -s "$LOCKFILE" ]; then
Sean Ocd8b1d12010-09-02 17:34:49 +0200256 info "At least one other pid is running in the chroot, so not"
257 info "tearing down env."
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800258 else
David James546747b2010-03-23 15:19:43 -0700259 MOUNTED_PATH=$(readlink -f "$FLAGS_chroot")
Sean Ocd8b1d12010-09-02 17:34:49 +0200260 info "Unmounting chroot environment."
Zdenek Behan6f17b5e2010-06-10 16:50:52 -0700261 # sort the list of mounts in reverse order, to ensure umount of
262 # cascading mounts in proper order
263 for i in \
264 $(mount | grep -F "on $MOUNTED_PATH/" | sort -r | awk '{print $3}'); do
robotboy98912212010-04-12 14:08:14 -0700265 safe_umount "$i"
David James546747b2010-03-23 15:19:43 -0700266 done
Andrew de los Reyesc9317ea2010-02-10 13:16:36 -0800267 fi
David James546747b2010-03-23 15:19:43 -0700268 ) 200>>"$LOCKFILE" || die "teardown_env failed"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000269}
270
271if [ $FLAGS_mount -eq $FLAGS_TRUE ]
272then
273 setup_env
Sean Ocd8b1d12010-09-02 17:34:49 +0200274 info "Make sure you run"
275 info " $0 --unmount"
276 info "before deleting $FLAGS_chroot"
277 info "or you'll end up deleting $FLAGS_trunk too!"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000278 exit 0
279fi
280
281if [ $FLAGS_unmount -eq $FLAGS_TRUE ]
282then
283 teardown_env
284 exit 0
285fi
286
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700287# Apply any hacks needed to update the chroot.
288chroot_hacks_from_outside "${FLAGS_chroot}"
289
290
rspangler@google.comd74220d2009-10-09 20:56:14 +0000291# Make sure we unmount before exiting
292trap teardown_env EXIT
293setup_env
294
David McMahon03aeb202009-12-08 12:47:08 -0800295# Get the git revision to pass into the chroot.
296#
297# This must be determined outside the chroot because (1) there is no
298# git inside the chroot, and (2) if there were it would likely be
299# the wrong version, which would mess up the .git directories.
300#
301# Note that this fixes $CHROMEOS_REVISION at the time the chroot is
302# entered. That's ok for the main use case of automated builds,
303# which pass each command line into a separate call to enter_chroot
David McMahon857dbb52009-12-09 18:21:05 -0800304# so always have up-to-date info. For developer builds, there may not
305# be a single revision, since the developer may have
David McMahon03aeb202009-12-08 12:47:08 -0800306# hand-sync'd some subdirs and edited files in others.
David McMahon857dbb52009-12-09 18:21:05 -0800307# In that case, check against origin/HEAD and mark** revision.
David McMahon03aeb202009-12-08 12:47:08 -0800308# Use git:8 chars of sha1
David Rochbergc1a3e562010-10-04 09:44:57 -0400309REVISION=$(cd ${FLAGS_trunk}/src/scripts ; git rev-parse --short=8 HEAD)
David McMahon857dbb52009-12-09 18:21:05 -0800310CHROOT_PASSTHRU="CHROMEOS_REVISION=$REVISION BUILDBOT_BUILD=$FLAGS_build_number CHROMEOS_OFFICIAL=$CHROMEOS_OFFICIAL"
Raja Aluri8f2a9952010-11-17 15:03:00 -0800311CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
312CHROMEOS_RELEASE_APPID=${CHROMEOS_RELEASE_APPID:-"{DEV-BUILD}"}"
hexxeh72d0b082010-12-15 13:17:37 -0800313CHROOT_PASSTHRU="${CHROOT_PASSTHRU} \
314CHROMEOS_VERSION_TRACK=$CHROMEOS_VERSION_TRACK CHROMEOS_VERSION_AUSERVER=$CHROMEOS_VERSION_AUSERVER CHROMEOS_VERSION_DEVSERVER=$CHROMEOS_VERSION_DEVSERVER"
Raja Alurie891eea2010-11-16 20:12:56 -0800315
Raja Aluri32759cf2010-08-30 18:44:39 -0700316if [ -d "$HOME/.subversion" ]; then
317 # Bind mounting .subversion into chroot
Sean Ocd8b1d12010-09-02 17:34:49 +0200318 info "mounting ~/.subversion into chroot"
Raja Aluri32759cf2010-08-30 18:44:39 -0700319 MOUNTED_PATH="$(readlink -f "${FLAGS_chroot}/home/${USER}/.subversion")"
320 if [ -z "$(mount | grep -F "on $MOUNTED_PATH ")" ]; then
321 mkdir -p "$MOUNTED_PATH"
322 sudo mount --bind "$HOME/.subversion" "$MOUNTED_PATH" || \
323 die "Could not mount $MOUNTED_PATH"
324 fi
325fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000326
David James0fd8af42010-09-27 10:04:57 -0700327# Configure committer username and email in chroot .gitconfig
328git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all user.name \
329 "$(cd /tmp; git var GIT_COMMITTER_IDENT | sed -e 's/ *<.*//')"
330git config -f ${FLAGS_chroot}/home/${USER}/.gitconfig --replace-all user.email \
331 "$(cd /tmp; git var GIT_COMMITTER_IDENT | sed -e 's/.*<\([^>]*\)>.*/\1/')"
332
derat@google.com4e7a92b2009-11-21 23:44:14 +0000333# Run command or interactive shell. Also include the non-chrooted path to
334# the source trunk for scripts that may need to print it (e.g.
335# build_image.sh).
Doug Anderson9362fa82010-12-16 14:44:12 -0800336sudo -- chroot "$FLAGS_chroot" sudo -i -u $USER $CHROOT_PASSTHRU \
Elly Jones7990a062010-09-02 09:23:23 -0400337 EXTERNAL_TRUNK_PATH="${FLAGS_trunk}" LANG=C SSH_AGENT_PID="${SSH_AGENT_PID}" \
Doug Anderson9362fa82010-12-16 14:44:12 -0800338 SSH_AUTH_SOCK="${SSH_AUTH_SOCK}" "$@"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000339
340# Remove trap and explicitly unmount
341trap - EXIT
342teardown_env