blob: 89cb389f2d4cddabced66bbaa54fa2e6ec5e4895 [file] [log] [blame]
David McMahon49302942010-02-18 16:55:35 -08001# Copyright (c) 2010 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
Anton Staaf30acb0b2011-01-26 16:00:20 -080038# Construct a list of possible locations for the source tree. This list is
39# based on various environment variables and globals that may have been set
40# by the calling script.
41function get_gclient_root_list() {
42 if [ $INSIDE_CHROOT -eq 1 ]; then
43 echo "/home/${USER}/trunk"
44
45 if [ -n "${SUDO_USER}" ]; then echo "/home/${SUDO_USER}/trunk"; fi
46 fi
47
48 if [ -n "${COMMON_SH}" ]; then echo "$(dirname "$COMMON_SH")/../.."; fi
49 if [ -n "${BASH_SOURCE}" ]; then echo "$(dirname "$BASH_SOURCE")/../.."; fi
50}
51
52# Based on the list of possible source locations we set GCLIENT_ROOT if it is
53# not already defined by looking for a src directory in each seach path
54# location. If we do not find a valid looking root we error out.
55function get_gclient_root() {
56 if [ -n "${GCLIENT_ROOT}" ]; then
57 return
58 fi
59
60 for path in $(get_gclient_root_list); do
61 if [ -d "${path}/src" ]; then
62 GCLIENT_ROOT=${path}
63 break
64 fi
65 done
66
67 if [ -z "${GCLIENT_ROOT}" ]; then
68 # Using dash or sh, we don't know where we are. $0 refers to the calling
69 # script, not ourselves, so that doesn't help us.
70 echo "Unable to determine location for common.sh. If you are sourcing"
71 echo "common.sh from a script run via dash or sh, you must do it in the"
72 echo "following way:"
73 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
74 echo ' . "$COMMON_SH"'
75 echo "where the first line is the relative path from your script to"
76 echo "common.sh."
77 exit 1
78 fi
79}
80
81# Find root of source tree
82get_gclient_root
83
rspangler@google.comd74220d2009-10-09 20:56:14 +000084# Canonicalize the directories for the root dir and the calling script.
85# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -080086# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000087# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080088# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000089# Note that 'realpath' is equivalent to 'readlink -f'.
Mike Frysingera1a06ab2011-08-10 11:40:30 -040090SCRIPT_LOCATION=$(readlink -f "$SCRIPT_LOCATION")
91GCLIENT_ROOT=$(readlink -f "$GCLIENT_ROOT")
rspangler@google.comd74220d2009-10-09 20:56:14 +000092
93# Other directories should always be pathed down from GCLIENT_ROOT.
94SRC_ROOT="$GCLIENT_ROOT/src"
95SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
96SCRIPTS_DIR="$SRC_ROOT/scripts"
97
98# Load developer's custom settings. Default location is in scripts dir,
99# since that's available both inside and outside the chroot. By convention,
100# settings from this file are variables starting with 'CHROMEOS_'
101CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400102if [ -f "$CHROMEOS_DEV_SETTINGS" ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000103 # Turn on exit-on-error during custom settings processing
Greg Spencer798d75f2011-02-01 22:04:49 -0800104 SAVE_OPTS=$(set +o)
rspangler@google.comd74220d2009-10-09 20:56:14 +0000105 set -e
106
107 # Read settings
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400108 . "$CHROMEOS_DEV_SETTINGS"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000109
110 # Restore previous state of exit-on-error
111 eval "$SAVE_OPTS"
112fi
113
114# Load shflags
Zdenek Behan07d24222011-11-02 00:46:25 +0000115# NOTE: This code snippet is in particular used by the au-generator (which
116# stores shflags in ./lib/shflags/) and should not be touched.
117if [ -f "${SCRIPTS_DIR}/lib/shflags/shflags" ]; then
118 . "${SCRIPTS_DIR}/lib/shflags/shflags"
119else
120 . ./lib/shflags/shflags || die "Couldn't find shflags"
121fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000122
Bill Richardson10d27c22010-01-20 13:38:50 -0800123# Our local mirror
124DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000125
Bill Richardson10d27c22010-01-20 13:38:50 -0800126# Upstream mirrors and build suites come in 2 flavors
127# DEV - development chroot, used to build the chromeos image
128# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000129
Bill Richardson10d27c22010-01-20 13:38:50 -0800130DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
131DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
132
133DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
134DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000135
136# Default location for chroot
137DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
138
139# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
140# they don't pollute the source directory.
141DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
142
David McMahon49302942010-02-18 16:55:35 -0800143# Set up a global ALL_BOARDS value
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400144if [ -d "$SRC_ROOT/overlays" ]; then
145 ALL_BOARDS=$(cd "$SRC_ROOT/overlays"; \
146 ls -1d overlay-* 2>&- | sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500147fi
David McMahon49302942010-02-18 16:55:35 -0800148# Strip CR
149ALL_BOARDS=$(echo $ALL_BOARDS)
150# Set a default BOARD
151#DEFAULT_BOARD=x86-generic # or...
152DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
153
David Jamesff072012010-11-30 13:22:05 -0800154# Enable --fast by default.
Greg Spencer798d75f2011-02-01 22:04:49 -0800155DEFAULT_FAST=${FLAGS_TRUE}
David James03668642010-07-28 17:08:29 -0700156
Chris Sosab0f57322011-10-25 03:07:23 +0000157# Directory to store built images. Should be set by sourcing script when used.
158BUILD_DIR=
Simon Glass142ca062011-02-09 13:39:43 -0800159
160# Standard filenames
Chris Sosab0f57322011-10-25 03:07:23 +0000161CHROMEOS_BASE_IMAGE_NAME="chromiumos_base_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800162CHROMEOS_IMAGE_NAME="chromiumos_image.bin"
Chris Sosab0f57322011-10-25 03:07:23 +0000163CHROMEOS_DEVELOPER_IMAGE_NAME="chromiumos_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800164CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin"
Chris Sosab885b802011-02-16 15:33:11 -0800165CHROMEOS_FACTORY_TEST_IMAGE_NAME="chromiumos_factory_image.bin"
Chris Sosab0f57322011-10-25 03:07:23 +0000166CHROMEOS_FACTORY_INSTALL_SHIM_NAME="factory_install_shim.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800167
rspangler@google.comd74220d2009-10-09 20:56:14 +0000168# Directory locations inside the dev chroot
169CHROOT_TRUNK_DIR="/home/$USER/trunk"
170
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700171# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800172# TODO: Is /usr/local/autotest-chrome still used by anyone?
Daniel Erate82f07c2010-12-21 13:39:22 -0800173DEFAULT_INSTALL_MASK="
174 *.a
175 *.la
176 /etc/init.d
177 /etc/runlevels
178 /lib/rc
179 /usr/bin/Xnest
180 /usr/bin/Xvfb
181 /usr/include
182 /usr/lib/debug
183 /usr/lib/gcc
184 /usr/lib/gtk-2.0/include
185 /usr/lib/pkgconfig
186 /usr/local/autotest
187 /usr/local/autotest-chrome
188 /usr/man
189 /usr/share/aclocal
190 /usr/share/doc
191 /usr/share/gettext
192 /usr/share/gtk-2.0
193 /usr/share/gtk-doc
194 /usr/share/info
195 /usr/share/man
196 /usr/share/openrc
197 /usr/share/pkgconfig
198 /usr/share/readline
Chris Wolfed13775f2011-07-26 16:34:38 -0400199 /usr/src
Daniel Erate82f07c2010-12-21 13:39:22 -0800200 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700201
Daniel Erate82f07c2010-12-21 13:39:22 -0800202FACTORY_INSTALL_MASK="
203 /opt/Qualcomm
204 /opt/Synaptics
205 /opt/google/chrome
206 /opt/google/o3d
207 /opt/google/talkplugin
208 /opt/netscape
209 /usr/lib/debug
210 /usr/lib/dri
211 /usr/lib/python2.6/test
212 /usr/local/autotest
213 /usr/local/autotest-chrome
214 /usr/local/autotest-pkgs
215 /usr/share/X11
216 /usr/share/chewing
217 /usr/share/fonts
218 /usr/share/ibus-pinyin
219 /usr/share/libhangul
220 /usr/share/locale
221 /usr/share/m17n
222 /usr/share/mime
223 /usr/share/sounds
224 /usr/share/tts
225 /usr/share/zoneinfo
226 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800227
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700228# Determine and set up variables needed for fancy color output (if supported).
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700229V_BOLD_RED=
230V_BOLD_GREEN=
231V_BOLD_YELLOW=
Mike Frysingerc2474ae2011-08-11 18:27:44 -0400232V_REVERSE=
233V_VIDOFF=
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700234
235if tput colors >/dev/null 2>&1; then
Mike Frysingerc2474ae2011-08-11 18:27:44 -0400236 # order matters: we want VIDOFF last so that when we trace with `set -x`,
237 # our terminal doesn't bleed colors as bash dumps the values of vars.
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700238 V_BOLD_RED="$(tput bold; tput setaf 1)"
239 V_BOLD_GREEN="$(tput bold; tput setaf 2)"
240 V_BOLD_YELLOW="$(tput bold; tput setaf 3)"
Mike Frysingerc2474ae2011-08-11 18:27:44 -0400241 V_REVERSE="$(tput rev)"
242 V_VIDOFF="$(tput sgr0)"
Gaurav Shah786ceeb2011-07-26 15:03:37 -0700243fi
David McMahonb7eb3a22010-02-09 14:07:40 -0800244
rspangler@google.comd74220d2009-10-09 20:56:14 +0000245# -----------------------------------------------------------------------------
246# Functions
247
Chris Sosaacada732010-02-23 15:20:03 -0800248function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700249 echo
250 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800251 echo
252 echo "*** No default board detected in " \
253 "$GCLIENT_ROOT/src/scripts/.default_board"
254 echo "*** Either run setup_board with default flag set"
255 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
256 echo
257}
258
259
260# Sets the default board variable for calling script
261function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700262 DEFAULT_BOARD=
263
Chris Sosaacada732010-02-23 15:20:03 -0800264 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800265 DEFAULT_BOARD=$(cat "$GCLIENT_ROOT/src/scripts/.default_board")
Chris Sosaacada732010-02-23 15:20:03 -0800266 fi
267}
268
269
Don Garrett640a0582010-05-04 16:54:28 -0700270# Enter a chroot and restart the current script if needed
271function restart_in_chroot_if_needed {
David Rochberg3b910702010-12-02 10:45:21 -0500272 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Greg Spencer798d75f2011-02-01 22:04:49 -0800273 if [ $INSIDE_CHROOT -ne 1 ]; then
Chris Sosafd2cdec2011-03-24 16:06:59 -0700274 # Get inside_chroot path for script.
275 local chroot_path="$(reinterpret_path_for_chroot "$0")"
Zdenek Behan2811c162011-08-13 00:47:38 +0200276 exec $GCLIENT_ROOT/chromite/bin/cros_sdk -- "$chroot_path" "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700277 fi
278}
279
rspangler@google.comd74220d2009-10-09 20:56:14 +0000280# Fail unless we're inside the chroot. This guards against messing up your
281# workstation.
282function assert_inside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800283 if [ $INSIDE_CHROOT -ne 1 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000284 echo "This script must be run inside the chroot. Run this first:"
Zdenek Behan2811c162011-08-13 00:47:38 +0200285 echo " cros_sdk"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000286 exit 1
287 fi
288}
289
290# Fail if we're inside the chroot. This guards against creating or entering
291# nested chroots, among other potential problems.
292function assert_outside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800293 if [ $INSIDE_CHROOT -ne 0 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000294 echo "This script must be run outside the chroot."
295 exit 1
296 fi
297}
298
derat@google.com86dcc8e2009-11-21 19:49:49 +0000299function assert_not_root_user {
Greg Spencer798d75f2011-02-01 22:04:49 -0800300 if [ $(id -u) = 0 ]; then
derat@google.com86dcc8e2009-11-21 19:49:49 +0000301 echo "This script must be run as a non-root user."
302 exit 1
303 fi
304}
305
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700306# Check that all arguments are flags; that is, there are no remaining arguments
307# after parsing from shflags. Allow (with a warning) a single empty-string
308# argument.
309#
310# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
311# then change this function.
312#
313# Usage: check_flags_only_and_allow_null_arg "$@" && set --
314function check_flags_only_and_allow_null_arg {
315 do_shift=1
316 if [[ $# == 1 && -z "$@" ]]; then
317 echo "$0: warning: ignoring null argument" >&2
318 shift
319 do_shift=0
320 fi
321 if [[ $# -gt 0 ]]; then
322 echo "error: invalid arguments: \"$@\"" >&2
323 flags_help
324 exit 1
325 fi
326 return $do_shift
327}
328
Chris Sosad9798522010-06-16 14:29:50 -0700329function info {
Chris Sosa131eaf52011-10-27 16:15:53 -0700330 echo -e >&2 "${V_BOLD_GREEN}INFO ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}"
Will Drewry55b42c92010-10-20 15:44:11 -0500331}
Chris Sosad9798522010-06-16 14:29:50 -0700332
robotboy2ea03ac2010-02-11 15:30:55 -0800333function warn {
Chris Sosa131eaf52011-10-27 16:15:53 -0700334 echo -e >&2 "${V_BOLD_YELLOW}WARNING ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800335}
336
337function error {
Chris Sosa131eaf52011-10-27 16:15:53 -0700338 echo -e >&2 "${V_BOLD_RED}ERROR ${CROS_LOG_PREFIX:-""}: $@${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800339}
David James546747b2010-03-23 15:19:43 -0700340
341function die {
Chris Sosa131eaf52011-10-27 16:15:53 -0700342 error "$@"
David James546747b2010-03-23 15:19:43 -0700343 exit 1
344}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700345
346# Retry an emerge command according to $FLAGS_retries
David Jamesfd7403a2010-07-09 12:00:17 -0700347# The $EMERGE_JOBS flags will only be added the first time the command is run
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700348function eretry () {
Greg Spencer798d75f2011-02-01 22:04:49 -0800349 local i
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700350 for i in $(seq $FLAGS_retries); do
Greg Spencer798d75f2011-02-01 22:04:49 -0800351 echo "Retrying $@"
352 "$@" $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700353 done
Greg Spencer798d75f2011-02-01 22:04:49 -0800354 "$@" && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700355 return 1
356}
357
358# Removes single quotes around parameter
359# Arguments:
360# $1 - string which optionally has surrounding quotes
361# Returns:
362# None, but prints the string without quotes.
363function remove_quotes() {
364 echo "$1" | sed -e "s/^'//; s/'$//"
365}
tedbo373c3902010-04-12 10:52:40 -0700366
367# Writes stdin to the given file name as root using sudo in overwrite mode.
368#
369# $1 - The output file name.
370function sudo_clobber() {
371 sudo tee "$1" > /dev/null
372}
373
374# Writes stdin to the given file name as root using sudo in append mode.
375#
376# $1 - The output file name.
377function sudo_append() {
378 sudo tee -a "$1" > /dev/null
379}
robotboy98912212010-04-12 14:08:14 -0700380
Mike Frysinger286b5922011-09-28 11:59:53 -0400381# Execute multiple commands in a single sudo. Generally will speed things
382# up by avoiding multiple calls to `sudo`. If any commands fail, we will
383# call die with the failing command. We can handle a max of ~100 commands,
384# but hopefully no one will ever try that many at once.
385#
386# $@ - The commands to execute, one per arg.
387function sudo_multi() {
388 local i cmds
389
390 # Construct the shell code to execute. It'll be of the form:
391 # ... && ( ( command ) || exit <command index> ) && ...
392 # This way we know which command exited. The exit status of
393 # the underlying command is lost, but we never cared about it
394 # in the first place (other than it is non zero), so oh well.
395 for (( i = 1; i <= $#; ++i )); do
396 cmds+=" && ( ( ${!i} ) || exit $(( i + 10 )) )"
397 done
398
399 # Execute our constructed shell code.
400 sudo -- sh -c ":${cmds[*]}" && i=0 || i=$?
401
402 # See if this failed, and if so, print out the failing command.
403 if [[ $i -gt 10 ]]; then
404 : $(( i -= 10 ))
405 die "sudo_multi failed: ${!i}"
406 elif [[ $i -ne 0 ]]; then
407 die "sudo_multi failed for unknown reason $i"
408 fi
409}
410
Mike Frysinger1aa61242011-09-15 17:46:44 -0400411# Locate all mounts below a specified directory.
412#
413# $1 - The root tree.
414function sub_mounts() {
415 # Assume that `mount` outputs a list of mount points in the order
416 # that things were mounted (since it always has and hopefully always
417 # will). As such, we have to unmount in reverse order to cleanly
418 # unmount submounts (think /dev/pts and /dev).
419 mount | \
Mike Frysingerb2e897b2011-09-22 17:04:35 -0400420 awk -v path="$1" -v len="${#1}" \
421 '(substr($3, 1, len) == path) { print $3 }' | \
Mike Frysinger1aa61242011-09-15 17:46:44 -0400422 tac
423}
424
robotboy98912212010-04-12 14:08:14 -0700425# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
426#
427# $1 - The path to unmount.
Mike Frysinger1aa61242011-09-15 17:46:44 -0400428function safe_umount_tree {
429 local mounts=$(sub_mounts "$1")
robotboy98912212010-04-12 14:08:14 -0700430
Mike Frysingere8aec372011-09-21 00:03:22 -0400431 # Hmm, this shouldn't normally happen, but anything is possible.
432 if [ -z "${mounts}" ] ; then
433 return 0
434 fi
435
Mike Frysinger1aa61242011-09-15 17:46:44 -0400436 # First try to unmount in one shot to speed things up.
437 if sudo umount -d ${mounts}; then
438 return 0
439 fi
robotboy98912212010-04-12 14:08:14 -0700440
Mike Frysinger1aa61242011-09-15 17:46:44 -0400441 # Well that didn't work, so lazy unmount remaining ones.
442 mounts=$(sub_mounts "$1")
443 warn "Failed to unmount ${mounts}"
444 warn "Doing a lazy unmount"
445 if ! sudo umount -d -l ${mounts}; then
446 mounts=$(sub_mounts "$1")
447 die "Failed to lazily unmount ${mounts}"
robotboy98912212010-04-12 14:08:14 -0700448 fi
449}
Chris Sosa702618f2010-05-14 12:52:32 -0700450
Chris Sosad4455022010-05-20 10:14:06 -0700451# Fixes symlinks that are incorrectly prefixed with the build root ${1}
452# rather than the real running root '/'.
453# TODO(sosa) - Merge setup - cleanup below with this method.
454fix_broken_symlinks() {
455 local build_root="${1}"
456 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
Greg Spencer798d75f2011-02-01 22:04:49 -0800457 local symlink
Chris Sosad4455022010-05-20 10:14:06 -0700458 for symlink in ${symlinks}; do
459 echo "Fixing ${symlink}"
460 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
461 # Trim spaces from target (bashism).
462 target=${target/ /}
463 # Make new target (removes rootfs prefix).
464 new_target=$(echo ${target} | sed "s#${build_root}##")
465
466 echo "Fixing symlink ${symlink}"
467 sudo unlink "${symlink}"
468 sudo ln -sf "${new_target}" "${symlink}"
469 done
470}
471
Chris Sosa702618f2010-05-14 12:52:32 -0700472# Sets up symlinks for the developer root. It is necessary to symlink
473# usr and local since the developer root is mounted at /usr/local and
474# applications expect to be installed under /usr/local/bin, etc.
475# This avoids packages installing into /usr/local/usr/local/bin.
476# ${1} specifies the symlink target for the developer root.
477# ${2} specifies the symlink target for the var directory.
478# ${3} specifies the location of the stateful partition.
479setup_symlinks_on_root() {
480 # Give args better names.
481 local dev_image_target=${1}
482 local var_target=${2}
483 local dev_image_root="${3}/dev_image"
484
485 # If our var target is actually the standard var, we are cleaning up the
486 # symlinks (could also check for /usr/local for the dev_image_target).
487 if [ ${var_target} = "/var" ]; then
488 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
489 else
490 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
491 fi
492
493 # Set up symlinks that should point to ${dev_image_target}.
Greg Spencer798d75f2011-02-01 22:04:49 -0800494 local path
Chris Sosa702618f2010-05-14 12:52:32 -0700495 for path in usr local; do
496 if [ -h "${dev_image_root}/${path}" ]; then
497 sudo unlink "${dev_image_root}/${path}"
498 elif [ -e "${dev_image_root}/${path}" ]; then
499 die "${dev_image_root}/${path} should be a symlink if exists"
500 fi
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400501 sudo ln -s "${dev_image_target}" "${dev_image_root}/${path}"
Chris Sosa702618f2010-05-14 12:52:32 -0700502 done
503
504 # Setup var symlink.
505 if [ -h "${dev_image_root}/var" ]; then
506 sudo unlink "${dev_image_root}/var"
507 elif [ -e "${dev_image_root}/var" ]; then
508 die "${dev_image_root}/var should be a symlink if it exists"
509 fi
510
511 sudo ln -s "${var_target}" "${dev_image_root}/var"
512}
Nick Sandersd2509272010-06-16 03:50:04 -0700513
Will Drewry55b42c92010-10-20 15:44:11 -0500514# These two helpers clobber the ro compat value in our root filesystem.
515#
516# When the system is built with --enable_rootfs_verification, bit-precise
517# integrity checking is performed. That precision poses a usability issue on
518# systems that automount partitions with recognizable filesystems, such as
519# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
520# automatically updated even if no other writes are performed to the
521# filesystem. In addition, ext2+ does not support a "read-only" flag for a
522# given filesystem. That said, forward and backward compatibility of
523# filesystem features are supported by tracking if a new feature breaks r/w or
524# just write compatibility. We abuse the read-only compatibility flag[1] in
525# the filesystem header by setting the high order byte (le) to FF. This tells
526# the kernel that features R24-R31 are all enabled. Since those features are
527# undefined on all ext-based filesystem, all standard kernels will refuse to
528# mount the filesystem as read-write -- only read-only[2].
529#
530# [1] 32-bit flag we are modifying:
531# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
532# [2] Mount behavior is enforced here:
533# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
534#
535# N.B., if the high order feature bits are used in the future, we will need to
536# revisit this technique.
537disable_rw_mount() {
538 local rootfs="$1"
539 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500540 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
541 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500542 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
543 conv=notrunc count=1 bs=1
544}
545
546enable_rw_mount() {
547 local rootfs="$1"
548 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500549 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
550 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500551 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
552 conv=notrunc count=1 bs=1
553}
554
Nick Sandersd2509272010-06-16 03:50:04 -0700555# Get current timestamp. Assumes common.sh runs at startup.
556start_time=$(date +%s)
557
558# Print time elsapsed since start_time.
559print_time_elapsed() {
Greg Spencer798d75f2011-02-01 22:04:49 -0800560 local end_time=$(date +%s)
561 local elapsed_seconds=$(($end_time - $start_time))
562 local minutes=$(($elapsed_seconds / 60))
563 local seconds=$(($elapsed_seconds % 60))
Olof Johansson6d491382010-08-09 16:05:50 -0500564 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700565}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700566
Anton Staaf9bcd8412011-01-24 15:27:14 -0800567# The board and variant command line options can be used in a number of ways
568# to specify the board and variant. The board can encode both pieces of
569# information separated by underscores. Or the variant can be passed using
570# the separate variant option. This function extracts the canonical board and
571# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
572# variables.
573get_board_and_variant() {
574 local flags_board="${1}"
575 local flags_variant="${2}"
576
577 BOARD=$(echo "$flags_board" | cut -d '_' -f 1)
578 VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)}
579
580 if [ -n "$VARIANT" ]; then
581 BOARD_VARIANT="${BOARD}_${VARIANT}"
582 else
583 BOARD_VARIANT="${BOARD}"
584 fi
585}
Simon Glass142ca062011-02-09 13:39:43 -0800586
587# This function converts a chromiumos image into a test image, either
588# in place or by copying to a new test image filename first. It honors
589# the following flags (see mod_image_for_test.sh)
590#
591# --factory
592# --factory_install
593# --force_copy
594#
595# On entry, pass the directory containing the image, and the image filename
596# On exit, it puts the pathname of the resulting test image into
597# CHROMEOS_RETURN_VAL
598# (yes this is ugly, but perhaps less ugly than the alternatives)
599#
600# Usage:
601# SRC_IMAGE=$(prepare_test_image "directory" "imagefile")
602prepare_test_image() {
603 # If we're asked to modify the image for test, then let's make a copy and
604 # modify that instead.
605 # Check for manufacturing image.
606 local args
607
608 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
609 args="--factory"
610 fi
611
612 # Check for install shim.
613 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
614 args="--factory_install"
615 fi
616
617 # Check for forcing copy of image
618 if [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ]; then
619 args="${args} --force_copy"
620 fi
621
622 # Modify the image for test, creating a new test image
623 "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} \
624 --image="$1/$2" --noinplace ${args}
625
626 # From now on we use the just-created test image
Simon Glass6e448ae2011-03-03 11:20:54 -0800627 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
628 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_FACTORY_TEST_IMAGE_NAME}"
629 else
630 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_TEST_IMAGE_NAME}"
631 fi
Simon Glass142ca062011-02-09 13:39:43 -0800632}
Anton Staaf6f5262d2011-03-02 09:35:54 -0800633
634# Check that the specified file exists. If the file path is empty or the file
635# doesn't exist on the filesystem generate useful error messages. Otherwise
636# show the user the name and path of the file that will be used. The padding
637# parameter can be used to tabulate multiple name:path pairs. For example:
638#
639# check_for_file "really long name" "...:" "file.foo"
640# check_for_file "short name" ".........:" "another.bar"
641#
642# Results in the following output:
643#
644# Using really long name...: file.foo
645# Using short name.........: another.bar
646#
647# If tabulation is not required then passing "" for padding generates the
648# output "Using <name> <path>"
649check_for_file() {
650 local name=$1
651 local padding=$2
652 local path=$3
653
654 if [ -z "${path}" ]; then
655 die "No ${name} file specified."
656 fi
657
658 if [ ! -e "${path}" ]; then
659 die "No ${name} file found at: ${path}"
660 else
661 info "Using ${name}${padding} ${path}"
662 fi
663}
664
665# Check that the specified tool exists. If it does not exist in the PATH
666# generate a useful error message indicating how to install the ebuild
667# that contains the required tool.
668check_for_tool() {
669 local tool=$1
670 local ebuild=$2
671
672 if ! which "${tool}" >/dev/null ; then
673 error "The ${tool} utility was not found in your path. Run the following"
674 error "command in your chroot to install it: sudo -E emerge ${ebuild}"
675 exit 1
676 fi
677}
Chris Sosafd2cdec2011-03-24 16:06:59 -0700678
679# Reinterprets path from outside the chroot for use inside.
680# Returns "" if "" given.
681# $1 - The path to reinterpret.
682function reinterpret_path_for_chroot() {
683 if [ $INSIDE_CHROOT -ne 1 ]; then
684 if [ -z "${1}" ]; then
685 echo ""
686 else
687 local path_abs_path=$(readlink -f "${1}")
688 local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}")
689
690 # Strip the repository root from the path.
691 local relative_path=$(echo ${path_abs_path} \
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400692 | sed "s:${gclient_root_abs_path}/::")
Chris Sosafd2cdec2011-03-24 16:06:59 -0700693
694 if [ "${relative_path}" = "${path_abs_path}" ]; then
695 die "Error reinterpreting path. Path ${1} is not within source tree."
696 fi
697
698 # Prepend the chroot repository path.
699 echo "/home/${USER}/trunk/${relative_path}"
700 fi
701 else
702 # Path is already inside the chroot :).
703 echo "${1}"
704 fi
705}
Gabe Black83d8b822011-08-01 17:50:09 -0700706
David James0ea96e42011-08-03 11:53:50 -0700707function emerge_custom_kernel() {
708 local install_root="$1"
709 local root=${FLAGS_build_root}/${FLAGS_board}
710 local tmp_pkgdir=${root}/custom-packages
711
712 # Clean up any leftover state in custom directories.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400713 sudo rm -rf "${tmp_pkgdir}"
David James0ea96e42011-08-03 11:53:50 -0700714
715 # Update chromeos-initramfs to contain the latest binaries from the build
716 # tree. This is basically just packaging up already-built binaries from
717 # $root. We are careful not to muck with the existing prebuilts so that
718 # prebuilts can be uploaded in parallel.
719 # TODO(davidjames): Implement ABI deps so that chromeos-initramfs will be
720 # rebuilt automatically when its dependencies change.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400721 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD -1 \
David James0ea96e42011-08-03 11:53:50 -0700722 chromeos-base/chromeos-initramfs || die "Cannot emerge chromeos-initramfs"
723
724 # Verify all dependencies of the kernel are installed. This should be a
725 # no-op, but it's good to check in case a developer didn't run
726 # build_packages.
727 local kernel=$(portageq-${FLAGS_board} expand_virtual ${root} virtual/kernel)
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400728 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --onlydeps \
David James0ea96e42011-08-03 11:53:50 -0700729 ${kernel} || die "Cannot emerge kernel dependencies"
730
731 # Build the kernel. This uses the standard root so that we can pick up the
732 # initramfs from there. But we don't actually install the kernel to the
733 # standard root, because that'll muck up the kernel debug symbols there,
734 # which we want to upload in parallel.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400735 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --buildpkgonly \
David James0ea96e42011-08-03 11:53:50 -0700736 ${kernel} || die "Cannot emerge kernel"
737
738 # Install the custom kernel to the provided install root.
Mike Frysingera1a06ab2011-08-10 11:40:30 -0400739 sudo -E PKGDIR="${tmp_pkgdir}" $EMERGE_BOARD_CMD --usepkgonly \
David James0ea96e42011-08-03 11:53:50 -0700740 --root=${install_root} ${kernel} || die "Cannot emerge kernel to root"
741}