blob: e32ef1e18d2fcb7a3c16181e8addc1237e6954ae [file] [log] [blame]
Chris Sosa2b736002012-02-12 16:16:08 -08001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rspangler@google.comd74220d2009-10-09 20:56:14 +00002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Common constants for build scripts
6# This must evaluate properly for both /bin/bash and /bin/sh
7
8# All scripts should die on error unless commands are specifically excepted
9# by prefixing with '!' or surrounded by 'set +e' / 'set -e'.
10# TODO: Re-enable this once shflags is less prone to dying.
11#set -e
12
13# The number of jobs to pass to tools that can run in parallel (such as make
14# and dpkg-buildpackage
Greg Spencer798d75f2011-02-01 22:04:49 -080015NUM_JOBS=$(grep -c "^processor" /proc/cpuinfo)
rspangler@google.comd74220d2009-10-09 20:56:14 +000016
Simon Glass142ca062011-02-09 13:39:43 -080017# True if we have the 'pv' utility - also set up COMMON_PV_CAT for convenience
18COMMON_PV_OK=1
19COMMON_PV_CAT=pv
20pv -V >/dev/null 2>&1 || COMMON_PV_OK=0
21if [ $COMMON_PV_OK -eq 0 ]; then
22 COMMON_PV_CAT=cat
23fi
24
Greg Spencer798d75f2011-02-01 22:04:49 -080025# Make sure we have the location and name of the calling script, using
26# the current value if it is already set.
27SCRIPT_LOCATION=${SCRIPT_LOCATION:-$(dirname "$(readlink -f "$0")")}
28SCRIPT_NAME=${SCRIPT_NAME:-$(basename "$0")}
rspangler@google.comd74220d2009-10-09 20:56:14 +000029
Anton Staaf30acb0b2011-01-26 16:00:20 -080030# Detect whether we're inside a chroot or not
31if [ -e /etc/debian_chroot ]
rspangler@google.comd74220d2009-10-09 20:56:14 +000032then
Anton Staaf30acb0b2011-01-26 16:00:20 -080033 INSIDE_CHROOT=1
rspangler@google.comd74220d2009-10-09 20:56:14 +000034else
Anton Staaf30acb0b2011-01-26 16:00:20 -080035 INSIDE_CHROOT=0
rspangler@google.comd74220d2009-10-09 20:56:14 +000036fi
37
Mike Frysinger669b28b2012-02-07 18:01:00 -050038# Determine and set up variables needed for fancy color output (if supported).
39V_BOLD_RED=
40V_BOLD_GREEN=
41V_BOLD_YELLOW=
42V_REVERSE=
43V_VIDOFF=
44
45if tput colors >/dev/null 2>&1; then
46 # order matters: we want VIDOFF last so that when we trace with `set -x`,
47 # our terminal doesn't bleed colors as bash dumps the values of vars.
48 V_BOLD_RED="$(tput bold; tput setaf 1)"
49 V_BOLD_GREEN="$(tput bold; tput setaf 2)"
50 V_BOLD_YELLOW="$(tput bold; tput setaf 3)"
51 V_REVERSE="$(tput rev)"
52 V_VIDOFF="$(tput sgr0)"
53fi
54
55# Declare these asap so that code below can safely assume they exist.
56function info {
57 echo -e >&2 "${V_BOLD_GREEN}INFO ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}"
58}
59
60function warn {
61 echo -e >&2 "${V_BOLD_YELLOW}WARNING ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}"
62}
63
64function error {
65 echo -e >&2 "${V_BOLD_RED}ERROR ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}"
66}
67
68function die {
69 error "$@"
70 exit 1
71}
72
Anton Staaf30acb0b2011-01-26 16:00:20 -080073# Construct a list of possible locations for the source tree. This list is
74# based on various environment variables and globals that may have been set
75# by the calling script.
76function get_gclient_root_list() {
77 if [ $INSIDE_CHROOT -eq 1 ]; then
78 echo "/home/${USER}/trunk"
79
80 if [ -n "${SUDO_USER}" ]; then echo "/home/${SUDO_USER}/trunk"; fi
81 fi
82
83 if [ -n "${COMMON_SH}" ]; then echo "$(dirname "$COMMON_SH")/../.."; fi
84 if [ -n "${BASH_SOURCE}" ]; then echo "$(dirname "$BASH_SOURCE")/../.."; fi
85}
86
87# Based on the list of possible source locations we set GCLIENT_ROOT if it is
88# not already defined by looking for a src directory in each seach path
89# location. If we do not find a valid looking root we error out.
90function get_gclient_root() {
91 if [ -n "${GCLIENT_ROOT}" ]; then
92 return
93 fi
94
95 for path in $(get_gclient_root_list); do
96 if [ -d "${path}/src" ]; then
97 GCLIENT_ROOT=${path}
98 break
99 fi
100 done
101
102 if [ -z "${GCLIENT_ROOT}" ]; then
103 # Using dash or sh, we don't know where we are. $0 refers to the calling
104 # script, not ourselves, so that doesn't help us.
105 echo "Unable to determine location for common.sh. If you are sourcing"
106 echo "common.sh from a script run via dash or sh, you must do it in the"
107 echo "following way:"
108 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
109 echo ' . "$COMMON_SH"'
110 echo "where the first line is the relative path from your script to"
111 echo "common.sh."
112 exit 1
113 fi
114}
115
116# Find root of source tree
117get_gclient_root
118
rspangler@google.comd74220d2009-10-09 20:56:14 +0000119# Canonicalize the directories for the root dir and the calling script.
120# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -0800121# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +0000122# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -0800123# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +0000124# Note that 'realpath' is equivalent to 'readlink -f'.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400125SCRIPT_LOCATION=$(readlink -f "$SCRIPT_LOCATION")
126GCLIENT_ROOT=$(readlink -f "$GCLIENT_ROOT")
rspangler@google.comd74220d2009-10-09 20:56:14 +0000127
128# Other directories should always be pathed down from GCLIENT_ROOT.
129SRC_ROOT="$GCLIENT_ROOT/src"
130SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
131SCRIPTS_DIR="$SRC_ROOT/scripts"
132
133# Load developer's custom settings. Default location is in scripts dir,
134# since that's available both inside and outside the chroot. By convention,
135# settings from this file are variables starting with 'CHROMEOS_'
136CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400137if [ -f "$CHROMEOS_DEV_SETTINGS" ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000138 # Turn on exit-on-error during custom settings processing
Greg Spencer798d75f2011-02-01 22:04:49 -0800139 SAVE_OPTS=$(set +o)
rspangler@google.comd74220d2009-10-09 20:56:14 +0000140 set -e
141
142 # Read settings
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400143 . "$CHROMEOS_DEV_SETTINGS"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000144
145 # Restore previous state of exit-on-error
146 eval "$SAVE_OPTS"
147fi
148
149# Load shflags
Zdenek Behan07d24222011-11-02 00:46:25 +0000150# NOTE: This code snippet is in particular used by the au-generator (which
151# stores shflags in ./lib/shflags/) and should not be touched.
152if [ -f "${SCRIPTS_DIR}/lib/shflags/shflags" ]; then
Mike Frysinger77c674b2012-02-07 18:05:07 -0500153 . "${SCRIPTS_DIR}/lib/shflags/shflags" || die "Couldn't find shflags"
Zdenek Behan07d24222011-11-02 00:46:25 +0000154else
155 . ./lib/shflags/shflags || die "Couldn't find shflags"
156fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000157
Bill Richardson10d27c22010-01-20 13:38:50 -0800158# Our local mirror
159DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000160
Bill Richardson10d27c22010-01-20 13:38:50 -0800161# Upstream mirrors and build suites come in 2 flavors
162# DEV - development chroot, used to build the chromeos image
163# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000164
Bill Richardson10d27c22010-01-20 13:38:50 -0800165DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
166DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
167
168DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
169DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000170
171# Default location for chroot
172DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
173
174# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
175# they don't pollute the source directory.
176DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
177
David McMahon49302942010-02-18 16:55:35 -0800178# Set up a global ALL_BOARDS value
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400179if [ -d "$SRC_ROOT/overlays" ]; then
180 ALL_BOARDS=$(cd "$SRC_ROOT/overlays"; \
181 ls -1d overlay-* 2>&- | sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500182fi
David McMahon49302942010-02-18 16:55:35 -0800183# Strip CR
184ALL_BOARDS=$(echo $ALL_BOARDS)
185# Set a default BOARD
186#DEFAULT_BOARD=x86-generic # or...
187DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
188
David Jamesff072012010-11-30 13:22:05 -0800189# Enable --fast by default.
Greg Spencer798d75f2011-02-01 22:04:49 -0800190DEFAULT_FAST=${FLAGS_TRUE}
David James03668642010-07-28 17:08:29 -0700191
Chris Sosab0f57322011-10-25 03:07:23 +0000192# Directory to store built images. Should be set by sourcing script when used.
193BUILD_DIR=
Simon Glass142ca062011-02-09 13:39:43 -0800194
195# Standard filenames
Chris Sosab0f57322011-10-25 03:07:23 +0000196CHROMEOS_BASE_IMAGE_NAME="chromiumos_base_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800197CHROMEOS_IMAGE_NAME="chromiumos_image.bin"
Chris Sosab0f57322011-10-25 03:07:23 +0000198CHROMEOS_DEVELOPER_IMAGE_NAME="chromiumos_image.bin"
Gilad Arnold08366272012-02-08 10:46:26 -0800199CHROMEOS_RECOVERY_IMAGE_NAME="recovery_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800200CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin"
Chris Sosab885b802011-02-16 15:33:11 -0800201CHROMEOS_FACTORY_TEST_IMAGE_NAME="chromiumos_factory_image.bin"
Chris Sosab0f57322011-10-25 03:07:23 +0000202CHROMEOS_FACTORY_INSTALL_SHIM_NAME="factory_install_shim.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800203
rspangler@google.comd74220d2009-10-09 20:56:14 +0000204# Directory locations inside the dev chroot
205CHROOT_TRUNK_DIR="/home/$USER/trunk"
206
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700207# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800208# TODO: Is /usr/local/autotest-chrome still used by anyone?
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800209COMMON_INSTALL_MASK="
Daniel Erate82f07c2010-12-21 13:39:22 -0800210 *.a
211 *.la
212 /etc/init.d
213 /etc/runlevels
214 /lib/rc
215 /usr/bin/Xnest
216 /usr/bin/Xvfb
217 /usr/include
218 /usr/lib/debug
219 /usr/lib/gcc
220 /usr/lib/gtk-2.0/include
221 /usr/lib/pkgconfig
Daniel Erate82f07c2010-12-21 13:39:22 -0800222 /usr/local/autotest-chrome
223 /usr/man
224 /usr/share/aclocal
225 /usr/share/doc
226 /usr/share/gettext
227 /usr/share/gtk-2.0
228 /usr/share/gtk-doc
229 /usr/share/info
230 /usr/share/man
231 /usr/share/openrc
232 /usr/share/pkgconfig
233 /usr/share/readline
Chris Wolfed13775f2011-07-26 16:34:38 -0400234 /usr/src
Daniel Erate82f07c2010-12-21 13:39:22 -0800235 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700236
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800237# Mask for base, dev, and test images (build_image, build_image --test)
238DEFAULT_INSTALL_MASK="
239 $COMMON_INSTALL_MASK
240 /usr/local/autotest
Joseph Hwangca63e042012-03-24 20:38:21 +0800241 /lib/modules/*/kernel/drivers/input/misc/uinput.ko
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800242 "
243
244# Mask for factory test image (build_image --factory)
245FACTORY_TEST_INSTALL_MASK="
246 $COMMON_INSTALL_MASK
247 */.svn
248 */CVS
249 /usr/local/autotest/[^c]*
250 /usr/local/autotest/conmux
251 /usr/local/autotest/client/deps/chrome_test
252 /usr/local/autotest/client/deps/piglit
253 /usr/local/autotest/client/deps/pyauto_dep
254 /usr/local/autotest/client/deps/realtimecomm_*
255 /usr/local/autotest/client/site_tests/desktopui_PageCyclerTests
256 /usr/local/autotest/client/site_tests/graphics_WebGLConformance
257 /usr/local/autotest/client/site_tests/platform_ToolchainOptions
258 /usr/local/autotest/client/site_tests/realtimecomm_GTalk*
259 "
260
Chris Sosac9422fa2012-03-05 15:58:07 -0800261# Mask for factory install shim (build_image factory_install)
Hung-Te Lind32c59f2012-01-19 19:54:01 +0800262FACTORY_SHIM_INSTALL_MASK="
263 $DEFAULT_INSTALL_MASK
264 /opt/[^g]*
Daniel Erate82f07c2010-12-21 13:39:22 -0800265 /opt/google/chrome
266 /opt/google/o3d
267 /opt/google/talkplugin
Daniel Erate82f07c2010-12-21 13:39:22 -0800268 /usr/lib/dri
269 /usr/lib/python2.6/test
Daniel Erate82f07c2010-12-21 13:39:22 -0800270 /usr/local/autotest-pkgs
271 /usr/share/X11
272 /usr/share/chewing
273 /usr/share/fonts
274 /usr/share/ibus-pinyin
275 /usr/share/libhangul
276 /usr/share/locale
277 /usr/share/m17n
278 /usr/share/mime
279 /usr/share/sounds
280 /usr/share/tts
281 /usr/share/zoneinfo
282 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800283
rspangler@google.comd74220d2009-10-09 20:56:14 +0000284# -----------------------------------------------------------------------------
285# Functions
286
Chris Sosaacada732010-02-23 15:20:03 -0800287function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700288 echo
289 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800290 echo
291 echo "*** No default board detected in " \
292 "$GCLIENT_ROOT/src/scripts/.default_board"
293 echo "*** Either run setup_board with default flag set"
294 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
295 echo
296}
297
298
299# Sets the default board variable for calling script
300function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700301 DEFAULT_BOARD=
302
Chris Sosaacada732010-02-23 15:20:03 -0800303 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800304 DEFAULT_BOARD=$(cat "$GCLIENT_ROOT/src/scripts/.default_board")
Mike Frysingerbc36d042011-12-19 16:01:09 -0500305 # Check for user typos like whitespace.
306 if [[ -n ${DEFAULT_BOARD//[a-zA-Z0-9-_]} ]] ; then
307 die ".default_board: invalid name detected; please fix:" \
308 "'${DEFAULT_BOARD}'"
309 fi
Chris Sosaacada732010-02-23 15:20:03 -0800310 fi
311}
312
313
Don Garrett640a0582010-05-04 16:54:28 -0700314# Enter a chroot and restart the current script if needed
315function restart_in_chroot_if_needed {
David Rochberg3b910702010-12-02 10:45:21 -0500316 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Greg Spencer798d75f2011-02-01 22:04:49 -0800317 if [ $INSIDE_CHROOT -ne 1 ]; then
Chris Sosafd2cdec2011-03-24 16:06:59 -0700318 # Get inside_chroot path for script.
319 local chroot_path="$(reinterpret_path_for_chroot "$0")"
Zdenek Behan2811c162011-08-13 00:47:38 +0200320 exec $GCLIENT_ROOT/chromite/bin/cros_sdk -- "$chroot_path" "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700321 fi
322}
323
rspangler@google.comd74220d2009-10-09 20:56:14 +0000324# Fail unless we're inside the chroot. This guards against messing up your
325# workstation.
326function assert_inside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800327 if [ $INSIDE_CHROOT -ne 1 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000328 echo "This script must be run inside the chroot. Run this first:"
Zdenek Behan2811c162011-08-13 00:47:38 +0200329 echo " cros_sdk"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000330 exit 1
331 fi
332}
333
334# Fail if we're inside the chroot. This guards against creating or entering
335# nested chroots, among other potential problems.
336function assert_outside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800337 if [ $INSIDE_CHROOT -ne 0 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000338 echo "This script must be run outside the chroot."
339 exit 1
340 fi
341}
342
derat@google.com86dcc8e2009-11-21 19:49:49 +0000343function assert_not_root_user {
Greg Spencer798d75f2011-02-01 22:04:49 -0800344 if [ $(id -u) = 0 ]; then
derat@google.com86dcc8e2009-11-21 19:49:49 +0000345 echo "This script must be run as a non-root user."
346 exit 1
347 fi
348}
349
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700350# Check that all arguments are flags; that is, there are no remaining arguments
351# after parsing from shflags. Allow (with a warning) a single empty-string
352# argument.
353#
354# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
355# then change this function.
356#
357# Usage: check_flags_only_and_allow_null_arg "$@" && set --
358function check_flags_only_and_allow_null_arg {
359 do_shift=1
360 if [[ $# == 1 && -z "$@" ]]; then
361 echo "$0: warning: ignoring null argument" >&2
362 shift
363 do_shift=0
364 fi
365 if [[ $# -gt 0 ]]; then
366 echo "error: invalid arguments: \"$@\"" >&2
367 flags_help
368 exit 1
369 fi
370 return $do_shift
371}
372
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700373# Removes single quotes around parameter
374# Arguments:
375# $1 - string which optionally has surrounding quotes
376# Returns:
377# None, but prints the string without quotes.
378function remove_quotes() {
379 echo "$1" | sed -e "s/^'//; s/'$//"
380}
tedbo373c3902010-04-12 10:52:40 -0700381
382# Writes stdin to the given file name as root using sudo in overwrite mode.
383#
384# $1 - The output file name.
385function sudo_clobber() {
386 sudo tee "$1" > /dev/null
387}
388
389# Writes stdin to the given file name as root using sudo in append mode.
390#
391# $1 - The output file name.
392function sudo_append() {
393 sudo tee -a "$1" > /dev/null
394}
robotboy98912212010-04-12 14:08:14 -0700395
Mike Frysinger286b5922011-09-28 11:59:53 -0400396# Execute multiple commands in a single sudo. Generally will speed things
397# up by avoiding multiple calls to `sudo`. If any commands fail, we will
398# call die with the failing command. We can handle a max of ~100 commands,
399# but hopefully no one will ever try that many at once.
400#
401# $@ - The commands to execute, one per arg.
402function sudo_multi() {
403 local i cmds
404
405 # Construct the shell code to execute. It'll be of the form:
406 # ... && ( ( command ) || exit <command index> ) && ...
407 # This way we know which command exited. The exit status of
408 # the underlying command is lost, but we never cared about it
409 # in the first place (other than it is non zero), so oh well.
410 for (( i = 1; i <= $#; ++i )); do
411 cmds+=" && ( ( ${!i} ) || exit $(( i + 10 )) )"
412 done
413
414 # Execute our constructed shell code.
415 sudo -- sh -c ":${cmds[*]}" && i=0 || i=$?
416
417 # See if this failed, and if so, print out the failing command.
418 if [[ $i -gt 10 ]]; then
419 : $(( i -= 10 ))
420 die "sudo_multi failed: ${!i}"
421 elif [[ $i -ne 0 ]]; then
422 die "sudo_multi failed for unknown reason $i"
423 fi
424}
425
Mike Frysinger1aa61242011-09-15 17:46:44 -0400426# Locate all mounts below a specified directory.
427#
428# $1 - The root tree.
429function sub_mounts() {
430 # Assume that `mount` outputs a list of mount points in the order
431 # that things were mounted (since it always has and hopefully always
432 # will). As such, we have to unmount in reverse order to cleanly
433 # unmount submounts (think /dev/pts and /dev).
Zdenek Behan1d5d3b52012-05-01 01:58:48 +0200434 awk -v path="$1" -v len="${#1}" \
435 '(substr($2, 1, len) == path) { print $2 }' /proc/mounts | \
Mike Frysinger1aa61242011-09-15 17:46:44 -0400436 tac
437}
438
robotboy98912212010-04-12 14:08:14 -0700439# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
440#
441# $1 - The path to unmount.
Mike Frysinger1aa61242011-09-15 17:46:44 -0400442function safe_umount_tree {
443 local mounts=$(sub_mounts "$1")
robotboy98912212010-04-12 14:08:14 -0700444
Mike Frysingere8aec372011-09-21 00:03:22 -0400445 # Hmm, this shouldn't normally happen, but anything is possible.
446 if [ -z "${mounts}" ] ; then
447 return 0
448 fi
449
Mike Frysinger1aa61242011-09-15 17:46:44 -0400450 # First try to unmount in one shot to speed things up.
451 if sudo umount -d ${mounts}; then
452 return 0
453 fi
robotboy98912212010-04-12 14:08:14 -0700454
Mike Frysinger1aa61242011-09-15 17:46:44 -0400455 # Well that didn't work, so lazy unmount remaining ones.
456 mounts=$(sub_mounts "$1")
457 warn "Failed to unmount ${mounts}"
458 warn "Doing a lazy unmount"
459 if ! sudo umount -d -l ${mounts}; then
460 mounts=$(sub_mounts "$1")
461 die "Failed to lazily unmount ${mounts}"
robotboy98912212010-04-12 14:08:14 -0700462 fi
463}
Chris Sosa702618f2010-05-14 12:52:32 -0700464
Chris Sosad4455022010-05-20 10:14:06 -0700465# Fixes symlinks that are incorrectly prefixed with the build root ${1}
466# rather than the real running root '/'.
467# TODO(sosa) - Merge setup - cleanup below with this method.
468fix_broken_symlinks() {
469 local build_root="${1}"
470 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
Greg Spencer798d75f2011-02-01 22:04:49 -0800471 local symlink
Chris Sosad4455022010-05-20 10:14:06 -0700472 for symlink in ${symlinks}; do
473 echo "Fixing ${symlink}"
474 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
475 # Trim spaces from target (bashism).
476 target=${target/ /}
477 # Make new target (removes rootfs prefix).
478 new_target=$(echo ${target} | sed "s#${build_root}##")
479
480 echo "Fixing symlink ${symlink}"
481 sudo unlink "${symlink}"
482 sudo ln -sf "${new_target}" "${symlink}"
483 done
484}
485
Chris Sosa702618f2010-05-14 12:52:32 -0700486# Sets up symlinks for the developer root. It is necessary to symlink
487# usr and local since the developer root is mounted at /usr/local and
488# applications expect to be installed under /usr/local/bin, etc.
489# This avoids packages installing into /usr/local/usr/local/bin.
490# ${1} specifies the symlink target for the developer root.
491# ${2} specifies the symlink target for the var directory.
492# ${3} specifies the location of the stateful partition.
493setup_symlinks_on_root() {
494 # Give args better names.
495 local dev_image_target=${1}
496 local var_target=${2}
497 local dev_image_root="${3}/dev_image"
498
499 # If our var target is actually the standard var, we are cleaning up the
500 # symlinks (could also check for /usr/local for the dev_image_target).
501 if [ ${var_target} = "/var" ]; then
502 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
503 else
504 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
505 fi
506
507 # Set up symlinks that should point to ${dev_image_target}.
Greg Spencer798d75f2011-02-01 22:04:49 -0800508 local path
Chris Sosa702618f2010-05-14 12:52:32 -0700509 for path in usr local; do
510 if [ -h "${dev_image_root}/${path}" ]; then
511 sudo unlink "${dev_image_root}/${path}"
512 elif [ -e "${dev_image_root}/${path}" ]; then
513 die "${dev_image_root}/${path} should be a symlink if exists"
514 fi
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400515 sudo ln -s "${dev_image_target}" "${dev_image_root}/${path}"
Chris Sosa702618f2010-05-14 12:52:32 -0700516 done
517
518 # Setup var symlink.
519 if [ -h "${dev_image_root}/var" ]; then
520 sudo unlink "${dev_image_root}/var"
521 elif [ -e "${dev_image_root}/var" ]; then
522 die "${dev_image_root}/var should be a symlink if it exists"
523 fi
524
525 sudo ln -s "${var_target}" "${dev_image_root}/var"
526}
Nick Sandersd2509272010-06-16 03:50:04 -0700527
Will Drewry55b42c92010-10-20 15:44:11 -0500528# These two helpers clobber the ro compat value in our root filesystem.
529#
530# When the system is built with --enable_rootfs_verification, bit-precise
531# integrity checking is performed. That precision poses a usability issue on
532# systems that automount partitions with recognizable filesystems, such as
533# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
534# automatically updated even if no other writes are performed to the
535# filesystem. In addition, ext2+ does not support a "read-only" flag for a
536# given filesystem. That said, forward and backward compatibility of
537# filesystem features are supported by tracking if a new feature breaks r/w or
538# just write compatibility. We abuse the read-only compatibility flag[1] in
539# the filesystem header by setting the high order byte (le) to FF. This tells
540# the kernel that features R24-R31 are all enabled. Since those features are
541# undefined on all ext-based filesystem, all standard kernels will refuse to
542# mount the filesystem as read-write -- only read-only[2].
543#
544# [1] 32-bit flag we are modifying:
545# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
546# [2] Mount behavior is enforced here:
547# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
548#
549# N.B., if the high order feature bits are used in the future, we will need to
550# revisit this technique.
551disable_rw_mount() {
552 local rootfs="$1"
553 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500554 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
555 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500556 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
557 conv=notrunc count=1 bs=1
558}
559
560enable_rw_mount() {
561 local rootfs="$1"
562 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500563 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
564 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500565 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
566 conv=notrunc count=1 bs=1
567}
568
Nick Sandersd2509272010-06-16 03:50:04 -0700569# Get current timestamp. Assumes common.sh runs at startup.
570start_time=$(date +%s)
571
572# Print time elsapsed since start_time.
573print_time_elapsed() {
Greg Spencer798d75f2011-02-01 22:04:49 -0800574 local end_time=$(date +%s)
575 local elapsed_seconds=$(($end_time - $start_time))
576 local minutes=$(($elapsed_seconds / 60))
577 local seconds=$(($elapsed_seconds % 60))
Olof Johansson6d491382010-08-09 16:05:50 -0500578 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700579}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700580
Anton Staaf9bcd8412011-01-24 15:27:14 -0800581# The board and variant command line options can be used in a number of ways
582# to specify the board and variant. The board can encode both pieces of
583# information separated by underscores. Or the variant can be passed using
584# the separate variant option. This function extracts the canonical board and
585# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
586# variables.
587get_board_and_variant() {
588 local flags_board="${1}"
589 local flags_variant="${2}"
590
591 BOARD=$(echo "$flags_board" | cut -d '_' -f 1)
592 VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)}
593
594 if [ -n "$VARIANT" ]; then
595 BOARD_VARIANT="${BOARD}_${VARIANT}"
596 else
597 BOARD_VARIANT="${BOARD}"
598 fi
599}
Simon Glass142ca062011-02-09 13:39:43 -0800600
601# This function converts a chromiumos image into a test image, either
602# in place or by copying to a new test image filename first. It honors
603# the following flags (see mod_image_for_test.sh)
604#
605# --factory
606# --factory_install
607# --force_copy
608#
609# On entry, pass the directory containing the image, and the image filename
610# On exit, it puts the pathname of the resulting test image into
611# CHROMEOS_RETURN_VAL
612# (yes this is ugly, but perhaps less ugly than the alternatives)
613#
614# Usage:
615# SRC_IMAGE=$(prepare_test_image "directory" "imagefile")
616prepare_test_image() {
617 # If we're asked to modify the image for test, then let's make a copy and
618 # modify that instead.
619 # Check for manufacturing image.
620 local args
621
622 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
623 args="--factory"
624 fi
625
626 # Check for install shim.
627 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
628 args="--factory_install"
629 fi
630
631 # Check for forcing copy of image
632 if [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ]; then
633 args="${args} --force_copy"
634 fi
635
636 # Modify the image for test, creating a new test image
637 "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} \
638 --image="$1/$2" --noinplace ${args}
639
640 # From now on we use the just-created test image
Simon Glass6e448ae2011-03-03 11:20:54 -0800641 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
642 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
643 else
644 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_TEST_IMAGE_NAME}"
645 fi
Simon Glass142ca062011-02-09 13:39:43 -0800646}
Anton Staaf6f5262d2011-03-02 09:35:54 -0800647
648# Check that the specified file exists. If the file path is empty or the file
649# doesn't exist on the filesystem generate useful error messages. Otherwise
650# show the user the name and path of the file that will be used. The padding
651# parameter can be used to tabulate multiple name:path pairs. For example:
652#
653# check_for_file "really long name" "...:" "file.foo"
654# check_for_file "short name" ".........:" "another.bar"
655#
656# Results in the following output:
657#
658# Using really long name...: file.foo
659# Using short name.........: another.bar
660#
661# If tabulation is not required then passing "" for padding generates the
662# output "Using <name> <path>"
663check_for_file() {
664 local name=$1
665 local padding=$2
666 local path=$3
667
668 if [ -z "${path}" ]; then
669 die "No ${name} file specified."
670 fi
671
672 if [ ! -e "${path}" ]; then
673 die "No ${name} file found at: ${path}"
674 else
675 info "Using ${name}${padding} ${path}"
676 fi
677}
678
679# Check that the specified tool exists. If it does not exist in the PATH
680# generate a useful error message indicating how to install the ebuild
681# that contains the required tool.
682check_for_tool() {
683 local tool=$1
684 local ebuild=$2
685
686 if ! which "${tool}" >/dev/null ; then
687 error "The ${tool} utility was not found in your path. Run the following"
688 error "command in your chroot to install it: sudo -E emerge ${ebuild}"
689 exit 1
690 fi
691}
Chris Sosafd2cdec2011-03-24 16:06:59 -0700692
693# Reinterprets path from outside the chroot for use inside.
694# Returns "" if "" given.
695# $1 - The path to reinterpret.
696function reinterpret_path_for_chroot() {
697 if [ $INSIDE_CHROOT -ne 1 ]; then
698 if [ -z "${1}" ]; then
699 echo ""
700 else
701 local path_abs_path=$(readlink -f "${1}")
702 local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}")
703
704 # Strip the repository root from the path.
705 local relative_path=$(echo ${path_abs_path} \
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400706 | sed "s:${gclient_root_abs_path}/::")
Chris Sosafd2cdec2011-03-24 16:06:59 -0700707
708 if [ "${relative_path}" = "${path_abs_path}" ]; then
709 die "Error reinterpreting path. Path ${1} is not within source tree."
710 fi
711
712 # Prepend the chroot repository path.
713 echo "/home/${USER}/trunk/${relative_path}"
714 fi
715 else
716 # Path is already inside the chroot :).
717 echo "${1}"
718 fi
719}
Gabe Black83d8b822011-08-01 17:50:09 -0700720
David James0ea96e42011-08-03 11:53:50 -0700721function emerge_custom_kernel() {
722 local install_root="$1"
David Jamesdee866c2012-03-15 14:53:19 -0700723 local root=/build/${FLAGS_board}
David James0ea96e42011-08-03 11:53:50 -0700724 local tmp_pkgdir=${root}/custom-packages
725
726 # Clean up any leftover state in custom directories.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400727 sudo rm -rf "${tmp_pkgdir}"
David James0ea96e42011-08-03 11:53:50 -0700728
729 # Update chromeos-initramfs to contain the latest binaries from the build
730 # tree. This is basically just packaging up already-built binaries from
731 # $root. We are careful not to muck with the existing prebuilts so that
732 # prebuilts can be uploaded in parallel.
733 # TODO(davidjames): Implement ABI deps so that chromeos-initramfs will be
734 # rebuilt automatically when its dependencies change.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400735 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD -1 \
David James0ea96e42011-08-03 11:53:50 -0700736 chromeos-base/chromeos-initramfs || die "Cannot emerge chromeos-initramfs"
737
738 # Verify all dependencies of the kernel are installed. This should be a
739 # no-op, but it's good to check in case a developer didn't run
Mike Frysinger0957a182012-03-21 23:17:14 -0400740 # build_packages. We need the expand_virtual call to workaround a bug
741 # in portage where it only installs the virtual pkg.
742 local kernel=$(portageq-${FLAGS_board} expand_virtual ${root} \
743 virtual/linux-sources)
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400744 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --onlydeps \
David James0ea96e42011-08-03 11:53:50 -0700745 ${kernel} || die "Cannot emerge kernel dependencies"
746
747 # Build the kernel. This uses the standard root so that we can pick up the
748 # initramfs from there. But we don't actually install the kernel to the
749 # standard root, because that'll muck up the kernel debug symbols there,
750 # which we want to upload in parallel.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400751 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --buildpkgonly \
David James0ea96e42011-08-03 11:53:50 -0700752 ${kernel} || die "Cannot emerge kernel"
753
754 # Install the custom kernel to the provided install root.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400755 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --usepkgonly \
David James0ea96e42011-08-03 11:53:50 -0700756 --root=${install_root} ${kernel} || die "Cannot emerge kernel to root"
757}
Brian Harringfeb04f72012-02-03 21:22:50 -0800758
759function enable_strict_sudo {
760 if [ -z "$CROS_SUDO_KEEP_ALIVE" ]; then
761 echo "$0 was somehow invoked in a way that the sudo keep alive could"
762 echo "not be found. Failing due to this. See crosbug.com/18393."
763 exit 126
764 fi
765 function sudo {
766 `type -P sudo` -n "$@"
767 }
768}
Gilad Arnold207a7c72012-02-09 10:19:16 -0800769
Chris Wolfe21a27b72012-02-27 13:00:51 -0500770# Checks that stdin and stderr are both terminals.
771# If so, we assume that there is a live user we can interact with.
772# This check can be overridden by setting the CROS_NO_PROMPT environment
773# variable to a non-empty value.
774function is_interactive() {
775 [ -z "${CROS_NO_PROMPT}" -a -t 0 -a -t 2 ]
776}
777
778function assert_interactive() {
779 if ! is_interactive; then
780 die "Script ${0##*/} tried to get user input on a non-interactive terminal."
781 fi
782}
783
Gilad Arnold207a7c72012-02-09 10:19:16 -0800784# Selection menu with a default option: this is similar to bash's select
785# built-in, only that in case of an empty selection it'll return the default
786# choice. Like select, it uses PS3 as the prompt.
787#
788# $1: name of variable to be assigned the selected value; it better not be of
789# the form choose_foo to avoid conflict with local variables.
790# $2: default value to return in case of an empty user entry.
791# $3: value to return in case of an invalid choice.
792# $...: options for selection.
793#
794# Usage example:
795#
796# PS3="Select one [1]: "
797# choose reply "foo" "ERROR" "foo" "bar" "foobar"
798#
799# This will present the following menu and prompt:
800#
801# 1) foo
802# 2) bar
803# 3) foobar
804# Select one [1]:
805#
806# The return value will be stored in a variable named 'reply'. If the input is
807# 1, 2 or 3, the return value will be "foo", "bar" or "foobar", respectively.
808# If it is empty (i.e. the user clicked Enter) it will be "foo". Anything else
809# will return "ERROR".
810function choose() {
811 typeset -i choose_i=1
812
813 # Retrieve output variable name and default return value.
814 local choose_reply=$1
815 local choose_default="$2"
816 local choose_invalid="$3"
817 shift 3
818
819 # Select a return value
820 unset REPLY
821 if [ $# -gt 0 ]; then
Chris Wolfe21a27b72012-02-27 13:00:51 -0500822 assert_interactive
823
Gilad Arnold207a7c72012-02-09 10:19:16 -0800824 # Actual options provided, present a menu and prompt for a choice.
825 local choose_opt
826 for choose_opt in "$@"; do
Chris Wolfe21a27b72012-02-27 13:00:51 -0500827 echo "$choose_i) $choose_opt" >&2
Gilad Arnold207a7c72012-02-09 10:19:16 -0800828 choose_i=choose_i+1
829 done
830 read -p "$PS3"
831 fi
832 # Filter out strings containing non-digits.
833 if [ "${REPLY}" != "${REPLY%%[!0-9]*}" ]; then
834 REPLY=0
835 fi
836 choose_i="${REPLY}"
837
838 if [ $choose_i -ge 1 -a $choose_i -le $# ]; then
839 # Valid choice, return the corresponding value.
840 eval ${choose_reply}="${!choose_i}"
841 elif [ -z "${REPLY}" ]; then
842 # Empty choice, return default value.
843 eval ${choose_reply}="${choose_default}"
844 else
845 # Invalid choice, return corresponding value.
846 eval ${choose_reply}="${choose_invalid}"
847 fi
848}
David James855afb72012-03-14 20:04:59 -0700849
850# Display --help if requested. This is used to hide options from help
851# that are not intended for developer use.
852#
853# How to use:
854# 1) Declare the options that you want to appear in help.
855# 2) Call this function.
856# 3) Declare the options that you don't want to appear in help.
857#
858# See build_packages for example usage.
859function show_help_if_requested() {
860 for opt in "$@"; do
861 if [ "$opt" = "-h" ] || [ "$opt" = "--help" ]; then
862 flags_help
863 exit 0
864 fi
865 done
866}