blob: 5f3495fdbb5dd942d6ee75a39547d282b014b3a1 [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
Greg Spencer798d75f2011-02-01 22:04:49 -080017# Make sure we have the location and name of the calling script, using
18# the current value if it is already set.
19SCRIPT_LOCATION=${SCRIPT_LOCATION:-$(dirname "$(readlink -f "$0")")}
20SCRIPT_NAME=${SCRIPT_NAME:-$(basename "$0")}
rspangler@google.comd74220d2009-10-09 20:56:14 +000021
Anton Staaf30acb0b2011-01-26 16:00:20 -080022# Detect whether we're inside a chroot or not
23if [ -e /etc/debian_chroot ]
rspangler@google.comd74220d2009-10-09 20:56:14 +000024then
Anton Staaf30acb0b2011-01-26 16:00:20 -080025 INSIDE_CHROOT=1
rspangler@google.comd74220d2009-10-09 20:56:14 +000026else
Anton Staaf30acb0b2011-01-26 16:00:20 -080027 INSIDE_CHROOT=0
rspangler@google.comd74220d2009-10-09 20:56:14 +000028fi
29
Anton Staaf30acb0b2011-01-26 16:00:20 -080030# Construct a list of possible locations for the source tree. This list is
31# based on various environment variables and globals that may have been set
32# by the calling script.
33function get_gclient_root_list() {
34 if [ $INSIDE_CHROOT -eq 1 ]; then
35 echo "/home/${USER}/trunk"
36
37 if [ -n "${SUDO_USER}" ]; then echo "/home/${SUDO_USER}/trunk"; fi
38 fi
39
40 if [ -n "${COMMON_SH}" ]; then echo "$(dirname "$COMMON_SH")/../.."; fi
41 if [ -n "${BASH_SOURCE}" ]; then echo "$(dirname "$BASH_SOURCE")/../.."; fi
42}
43
44# Based on the list of possible source locations we set GCLIENT_ROOT if it is
45# not already defined by looking for a src directory in each seach path
46# location. If we do not find a valid looking root we error out.
47function get_gclient_root() {
48 if [ -n "${GCLIENT_ROOT}" ]; then
49 return
50 fi
51
52 for path in $(get_gclient_root_list); do
53 if [ -d "${path}/src" ]; then
54 GCLIENT_ROOT=${path}
55 break
56 fi
57 done
58
59 if [ -z "${GCLIENT_ROOT}" ]; then
60 # Using dash or sh, we don't know where we are. $0 refers to the calling
61 # script, not ourselves, so that doesn't help us.
62 echo "Unable to determine location for common.sh. If you are sourcing"
63 echo "common.sh from a script run via dash or sh, you must do it in the"
64 echo "following way:"
65 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
66 echo ' . "$COMMON_SH"'
67 echo "where the first line is the relative path from your script to"
68 echo "common.sh."
69 exit 1
70 fi
71}
72
73# Find root of source tree
74get_gclient_root
75
rspangler@google.comd74220d2009-10-09 20:56:14 +000076# Canonicalize the directories for the root dir and the calling script.
77# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -080078# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000079# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080080# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000081# Note that 'realpath' is equivalent to 'readlink -f'.
Greg Spencer798d75f2011-02-01 22:04:49 -080082SCRIPT_LOCATION=$(readlink -f $SCRIPT_LOCATION)
83GCLIENT_ROOT=$(readlink -f $GCLIENT_ROOT)
rspangler@google.comd74220d2009-10-09 20:56:14 +000084
85# Other directories should always be pathed down from GCLIENT_ROOT.
86SRC_ROOT="$GCLIENT_ROOT/src"
87SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
88SCRIPTS_DIR="$SRC_ROOT/scripts"
89
90# Load developer's custom settings. Default location is in scripts dir,
91# since that's available both inside and outside the chroot. By convention,
92# settings from this file are variables starting with 'CHROMEOS_'
93CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
Greg Spencer798d75f2011-02-01 22:04:49 -080094if [ -f $CHROMEOS_DEV_SETTINGS ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +000095 # Turn on exit-on-error during custom settings processing
Greg Spencer798d75f2011-02-01 22:04:49 -080096 SAVE_OPTS=$(set +o)
rspangler@google.comd74220d2009-10-09 20:56:14 +000097 set -e
98
99 # Read settings
100 . $CHROMEOS_DEV_SETTINGS
101
102 # Restore previous state of exit-on-error
103 eval "$SAVE_OPTS"
104fi
105
106# Load shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -0700107if [[ -f /usr/lib/shflags ]]; then
108 . /usr/lib/shflags
Raja Aluric6e23cd2010-11-02 15:55:57 -0700109elif [ -f ./lib/shflags/shflags ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800110 . ./lib/shflags/shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -0700111else
112 . "${SRC_ROOT}/scripts/lib/shflags/shflags"
113fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000114
Bill Richardson10d27c22010-01-20 13:38:50 -0800115# Our local mirror
116DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000117
Bill Richardson10d27c22010-01-20 13:38:50 -0800118# Upstream mirrors and build suites come in 2 flavors
119# DEV - development chroot, used to build the chromeos image
120# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000121
Bill Richardson10d27c22010-01-20 13:38:50 -0800122DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
123DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
124
125DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
126DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000127
128# Default location for chroot
129DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
130
131# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
132# they don't pollute the source directory.
133DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
134
David McMahon49302942010-02-18 16:55:35 -0800135# Set up a global ALL_BOARDS value
Raja Alurid44bcf92010-11-11 17:46:53 -0800136if [ -d $SRC_ROOT/overlays ]; then
137 ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500138fi
David McMahon49302942010-02-18 16:55:35 -0800139# Strip CR
140ALL_BOARDS=$(echo $ALL_BOARDS)
141# Set a default BOARD
142#DEFAULT_BOARD=x86-generic # or...
143DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
144
David Jamesff072012010-11-30 13:22:05 -0800145# Enable --fast by default.
Greg Spencer798d75f2011-02-01 22:04:49 -0800146DEFAULT_FAST=${FLAGS_TRUE}
David James03668642010-07-28 17:08:29 -0700147
rspangler@google.comd74220d2009-10-09 20:56:14 +0000148# Directory locations inside the dev chroot
149CHROOT_TRUNK_DIR="/home/$USER/trunk"
150
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700151# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800152# TODO: Is /usr/local/autotest-chrome still used by anyone?
Daniel Erate82f07c2010-12-21 13:39:22 -0800153DEFAULT_INSTALL_MASK="
154 *.a
155 *.la
156 /etc/init.d
157 /etc/runlevels
158 /lib/rc
159 /usr/bin/Xnest
160 /usr/bin/Xvfb
161 /usr/include
162 /usr/lib/debug
163 /usr/lib/gcc
164 /usr/lib/gtk-2.0/include
165 /usr/lib/pkgconfig
166 /usr/local/autotest
167 /usr/local/autotest-chrome
168 /usr/man
169 /usr/share/aclocal
170 /usr/share/doc
171 /usr/share/gettext
172 /usr/share/gtk-2.0
173 /usr/share/gtk-doc
174 /usr/share/info
175 /usr/share/man
176 /usr/share/openrc
177 /usr/share/pkgconfig
178 /usr/share/readline
179 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700180
Daniel Erate82f07c2010-12-21 13:39:22 -0800181FACTORY_INSTALL_MASK="
182 /opt/Qualcomm
183 /opt/Synaptics
184 /opt/google/chrome
185 /opt/google/o3d
186 /opt/google/talkplugin
187 /opt/netscape
188 /usr/lib/debug
189 /usr/lib/dri
190 /usr/lib/python2.6/test
191 /usr/local/autotest
192 /usr/local/autotest-chrome
193 /usr/local/autotest-pkgs
194 /usr/share/X11
195 /usr/share/chewing
196 /usr/share/fonts
197 /usr/share/ibus-pinyin
198 /usr/share/libhangul
199 /usr/share/locale
200 /usr/share/m17n
201 /usr/share/mime
202 /usr/share/sounds
203 /usr/share/tts
204 /usr/share/zoneinfo
205 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800206
David McMahonb7eb3a22010-02-09 14:07:40 -0800207# Check to ensure not running old scripts
208V_REVERSE=''
209V_VIDOFF=''
210case "$(basename $0)" in
211 build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh)
212 echo
213 echo "$V_REVERSE============================================================"
214 echo "=========================== WARNING ======================"
215 echo "============================================================$V_VIDOFF"
216 echo
217 echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:"
218 echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build"
219 echo
Greg Spencer798d75f2011-02-01 22:04:49 -0800220 if [ "$USER" != "chrome-bot" ]; then
David McMahonb7eb3a22010-02-09 14:07:40 -0800221 read -n1 -p "Press any key to continue using the OLD build system..."
222 echo
223 echo
224 fi
225 ;;
226esac
227
rspangler@google.comd74220d2009-10-09 20:56:14 +0000228# -----------------------------------------------------------------------------
229# Functions
230
Chris Sosaacada732010-02-23 15:20:03 -0800231function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700232 echo
233 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800234 echo
235 echo "*** No default board detected in " \
236 "$GCLIENT_ROOT/src/scripts/.default_board"
237 echo "*** Either run setup_board with default flag set"
238 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
239 echo
240}
241
242
243# Sets the default board variable for calling script
244function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700245 DEFAULT_BOARD=
246
Chris Sosaacada732010-02-23 15:20:03 -0800247 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800248 DEFAULT_BOARD=$(cat "$GCLIENT_ROOT/src/scripts/.default_board")
Chris Sosaacada732010-02-23 15:20:03 -0800249 fi
250}
251
252
rspangler@google.comd74220d2009-10-09 20:56:14 +0000253# Make a package
254function make_pkg_common {
255 # Positional parameters from calling script. :? means "fail if unset".
256 set -e
257 PKG_BASE=${1:?}
258 shift
259 set +e
tedbo4f44d9e2010-01-08 17:26:11 -0800260
rspangler@google.comd74220d2009-10-09 20:56:14 +0000261 # All packages are built in the chroot
262 assert_inside_chroot
263
264 # Command line options
265 DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output"
266
267 # Parse command line and update positional args
268 FLAGS "$@" || exit 1
269 eval set -- "${FLAGS_ARGV}"
270
271 # Die on any errors
272 set -e
273
274 # Make output dir
Greg Spencer798d75f2011-02-01 22:04:49 -0800275 local out_dir="$FLAGS_build_root/x86/local_packages"
276 mkdir -p "$out_dir"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000277
278 # Remove previous package from output dir
Greg Spencer798d75f2011-02-01 22:04:49 -0800279 rm -f "$out_dir"/${PKG_BASE}_*.deb
rspangler@google.comd74220d2009-10-09 20:56:14 +0000280
281 # Rebuild the package
Greg Spencer798d75f2011-02-01 22:04:49 -0800282 pushd "$SCRIPT_LOCATION"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000283 rm -f ../${PKG_BASE}_*.deb
284 dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS
Greg Spencer798d75f2011-02-01 22:04:49 -0800285 mv ../${PKG_BASE}_*.deb "$out_dir"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000286 rm ../${PKG_BASE}_*.changes
287 popd
288}
289
Don Garrett640a0582010-05-04 16:54:28 -0700290# Enter a chroot and restart the current script if needed
291function restart_in_chroot_if_needed {
David Rochberg3b910702010-12-02 10:45:21 -0500292 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Greg Spencer798d75f2011-02-01 22:04:49 -0800293 if [ $INSIDE_CHROOT -ne 1 ]; then
294 local abspath=$(readlink -f "$0")
295 # strip everything up to (and including) /scripts/ from abspath
296 local path_from_scripts="${abspath##*/scripts/}"
Don Garrett640a0582010-05-04 16:54:28 -0700297 exec $SCRIPTS_DIR/enter_chroot.sh -- \
Greg Spencer798d75f2011-02-01 22:04:49 -0800298 "$CHROOT_TRUNK_DIR/src/scripts/$path_from_scripts" "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700299 fi
300}
301
rspangler@google.comd74220d2009-10-09 20:56:14 +0000302# Fail unless we're inside the chroot. This guards against messing up your
303# workstation.
304function assert_inside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800305 if [ $INSIDE_CHROOT -ne 1 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000306 echo "This script must be run inside the chroot. Run this first:"
307 echo " $SCRIPTS_DIR/enter_chroot.sh"
308 exit 1
309 fi
310}
311
312# Fail if we're inside the chroot. This guards against creating or entering
313# nested chroots, among other potential problems.
314function assert_outside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800315 if [ $INSIDE_CHROOT -ne 0 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000316 echo "This script must be run outside the chroot."
317 exit 1
318 fi
319}
320
derat@google.com86dcc8e2009-11-21 19:49:49 +0000321function assert_not_root_user {
Greg Spencer798d75f2011-02-01 22:04:49 -0800322 if [ $(id -u) = 0 ]; then
derat@google.com86dcc8e2009-11-21 19:49:49 +0000323 echo "This script must be run as a non-root user."
324 exit 1
325 fi
326}
327
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800328# Returns true if the input file is whitelisted.
329#
330# $1 - The file to check
331is_whitelisted() {
David McMahonb7eb3a22010-02-09 14:07:40 -0800332 local file=$1
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800333 local whitelist="$FLAGS_whitelist"
334 test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1)
335
336 local checksum=$(md5sum "$file" | awk '{ print $1 }')
337 local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \
338 || /bin/true)
339 test $count -ne 0
340}
robotboy2ea03ac2010-02-11 15:30:55 -0800341
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700342# Check that all arguments are flags; that is, there are no remaining arguments
343# after parsing from shflags. Allow (with a warning) a single empty-string
344# argument.
345#
346# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
347# then change this function.
348#
349# Usage: check_flags_only_and_allow_null_arg "$@" && set --
350function check_flags_only_and_allow_null_arg {
351 do_shift=1
352 if [[ $# == 1 && -z "$@" ]]; then
353 echo "$0: warning: ignoring null argument" >&2
354 shift
355 do_shift=0
356 fi
357 if [[ $# -gt 0 ]]; then
358 echo "error: invalid arguments: \"$@\"" >&2
359 flags_help
360 exit 1
361 fi
362 return $do_shift
363}
364
robotboy2ea03ac2010-02-11 15:30:55 -0800365V_RED="\e[31m"
366V_YELLOW="\e[33m"
Chris Sosad9798522010-06-16 14:29:50 -0700367V_BOLD_GREEN="\e[1;32m"
Darin Petkovb2a21e72010-03-25 11:33:41 -0700368V_BOLD_RED="\e[1;31m"
369V_BOLD_YELLOW="\e[1;33m"
robotboy2ea03ac2010-02-11 15:30:55 -0800370
Chris Sosad9798522010-06-16 14:29:50 -0700371function info {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200372 echo -e >&2 "${V_BOLD_GREEN}INFO : $1${V_VIDOFF}"
Will Drewry55b42c92010-10-20 15:44:11 -0500373}
Chris Sosad9798522010-06-16 14:29:50 -0700374
robotboy2ea03ac2010-02-11 15:30:55 -0800375function warn {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200376 echo -e >&2 "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800377}
378
379function error {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200380 echo -e >&2 "${V_BOLD_RED}ERROR : $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800381}
David James546747b2010-03-23 15:19:43 -0700382
383function die {
384 error "$1"
385 exit 1
386}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700387
388# Retry an emerge command according to $FLAGS_retries
David Jamesfd7403a2010-07-09 12:00:17 -0700389# The $EMERGE_JOBS flags will only be added the first time the command is run
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700390function eretry () {
Greg Spencer798d75f2011-02-01 22:04:49 -0800391 local i
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700392 for i in $(seq $FLAGS_retries); do
Greg Spencer798d75f2011-02-01 22:04:49 -0800393 echo "Retrying $@"
394 "$@" $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700395 done
Greg Spencer798d75f2011-02-01 22:04:49 -0800396 "$@" && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700397 return 1
398}
399
400# Removes single quotes around parameter
401# Arguments:
402# $1 - string which optionally has surrounding quotes
403# Returns:
404# None, but prints the string without quotes.
405function remove_quotes() {
406 echo "$1" | sed -e "s/^'//; s/'$//"
407}
tedbo373c3902010-04-12 10:52:40 -0700408
409# Writes stdin to the given file name as root using sudo in overwrite mode.
410#
411# $1 - The output file name.
412function sudo_clobber() {
413 sudo tee "$1" > /dev/null
414}
415
416# Writes stdin to the given file name as root using sudo in append mode.
417#
418# $1 - The output file name.
419function sudo_append() {
420 sudo tee -a "$1" > /dev/null
421}
robotboy98912212010-04-12 14:08:14 -0700422
423# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
424#
425# $1 - The path to unmount.
426function safe_umount {
427 path=${1:?}
428 shift
429
430 if ! sudo umount -d "${path}"; then
431 warn "Failed to unmount ${path}"
432 warn "Doing a lazy unmount"
433
434 sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}"
435 fi
436}
Chris Sosa702618f2010-05-14 12:52:32 -0700437
Chris Sosad4455022010-05-20 10:14:06 -0700438# Fixes symlinks that are incorrectly prefixed with the build root ${1}
439# rather than the real running root '/'.
440# TODO(sosa) - Merge setup - cleanup below with this method.
441fix_broken_symlinks() {
442 local build_root="${1}"
443 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
Greg Spencer798d75f2011-02-01 22:04:49 -0800444 local symlink
Chris Sosad4455022010-05-20 10:14:06 -0700445 for symlink in ${symlinks}; do
446 echo "Fixing ${symlink}"
447 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
448 # Trim spaces from target (bashism).
449 target=${target/ /}
450 # Make new target (removes rootfs prefix).
451 new_target=$(echo ${target} | sed "s#${build_root}##")
452
453 echo "Fixing symlink ${symlink}"
454 sudo unlink "${symlink}"
455 sudo ln -sf "${new_target}" "${symlink}"
456 done
457}
458
Chris Sosa702618f2010-05-14 12:52:32 -0700459# Sets up symlinks for the developer root. It is necessary to symlink
460# usr and local since the developer root is mounted at /usr/local and
461# applications expect to be installed under /usr/local/bin, etc.
462# This avoids packages installing into /usr/local/usr/local/bin.
463# ${1} specifies the symlink target for the developer root.
464# ${2} specifies the symlink target for the var directory.
465# ${3} specifies the location of the stateful partition.
466setup_symlinks_on_root() {
467 # Give args better names.
468 local dev_image_target=${1}
469 local var_target=${2}
470 local dev_image_root="${3}/dev_image"
471
472 # If our var target is actually the standard var, we are cleaning up the
473 # symlinks (could also check for /usr/local for the dev_image_target).
474 if [ ${var_target} = "/var" ]; then
475 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
476 else
477 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
478 fi
479
480 # Set up symlinks that should point to ${dev_image_target}.
Greg Spencer798d75f2011-02-01 22:04:49 -0800481 local path
Chris Sosa702618f2010-05-14 12:52:32 -0700482 for path in usr local; do
483 if [ -h "${dev_image_root}/${path}" ]; then
484 sudo unlink "${dev_image_root}/${path}"
485 elif [ -e "${dev_image_root}/${path}" ]; then
486 die "${dev_image_root}/${path} should be a symlink if exists"
487 fi
488 sudo ln -s ${dev_image_target} "${dev_image_root}/${path}"
489 done
490
491 # Setup var symlink.
492 if [ -h "${dev_image_root}/var" ]; then
493 sudo unlink "${dev_image_root}/var"
494 elif [ -e "${dev_image_root}/var" ]; then
495 die "${dev_image_root}/var should be a symlink if it exists"
496 fi
497
498 sudo ln -s "${var_target}" "${dev_image_root}/var"
499}
Nick Sandersd2509272010-06-16 03:50:04 -0700500
Will Drewry55b42c92010-10-20 15:44:11 -0500501# These two helpers clobber the ro compat value in our root filesystem.
502#
503# When the system is built with --enable_rootfs_verification, bit-precise
504# integrity checking is performed. That precision poses a usability issue on
505# systems that automount partitions with recognizable filesystems, such as
506# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
507# automatically updated even if no other writes are performed to the
508# filesystem. In addition, ext2+ does not support a "read-only" flag for a
509# given filesystem. That said, forward and backward compatibility of
510# filesystem features are supported by tracking if a new feature breaks r/w or
511# just write compatibility. We abuse the read-only compatibility flag[1] in
512# the filesystem header by setting the high order byte (le) to FF. This tells
513# the kernel that features R24-R31 are all enabled. Since those features are
514# undefined on all ext-based filesystem, all standard kernels will refuse to
515# mount the filesystem as read-write -- only read-only[2].
516#
517# [1] 32-bit flag we are modifying:
518# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
519# [2] Mount behavior is enforced here:
520# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
521#
522# N.B., if the high order feature bits are used in the future, we will need to
523# revisit this technique.
524disable_rw_mount() {
525 local rootfs="$1"
526 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500527 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
528 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500529 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
530 conv=notrunc count=1 bs=1
531}
532
533enable_rw_mount() {
534 local rootfs="$1"
535 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500536 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
537 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500538 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
539 conv=notrunc count=1 bs=1
540}
541
Nick Sandersd2509272010-06-16 03:50:04 -0700542# Get current timestamp. Assumes common.sh runs at startup.
543start_time=$(date +%s)
544
545# Print time elsapsed since start_time.
546print_time_elapsed() {
Greg Spencer798d75f2011-02-01 22:04:49 -0800547 local end_time=$(date +%s)
548 local elapsed_seconds=$(($end_time - $start_time))
549 local minutes=$(($elapsed_seconds / 60))
550 local seconds=$(($elapsed_seconds % 60))
Olof Johansson6d491382010-08-09 16:05:50 -0500551 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700552}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700553
554# This function is a place to put code to incrementally update the
555# chroot so that users don't need to fully re-make it. It should
556# be called from scripts that are run _outside_ the chroot.
557#
558# Please put date information so it's easy to keep track of when
559# old hacks can be retired and so that people can detect when a
560# hack triggered when it shouldn't have.
561#
562# ${1} specifies the location of the chroot.
563chroot_hacks_from_outside() {
564 # Give args better names.
Greg Spencer798d75f2011-02-01 22:04:49 -0800565 local chroot_dir=$1
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700566
567 # Add root as a sudoer if not already done.
568 if ! sudo grep -q '^root ALL=(ALL) ALL$' "${chroot_dir}/etc/sudoers" ; then
569 info "Upgrading old chroot (pre 2010-10-19) - adding root to sudoers"
570 sudo bash -c "echo root ALL=\(ALL\) ALL >> \"${chroot_dir}/etc/sudoers\""
571 fi
572}
Anton Staaf9bcd8412011-01-24 15:27:14 -0800573
574# The board and variant command line options can be used in a number of ways
575# to specify the board and variant. The board can encode both pieces of
576# information separated by underscores. Or the variant can be passed using
577# the separate variant option. This function extracts the canonical board and
578# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
579# variables.
580get_board_and_variant() {
581 local flags_board="${1}"
582 local flags_variant="${2}"
583
584 BOARD=$(echo "$flags_board" | cut -d '_' -f 1)
585 VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)}
586
587 if [ -n "$VARIANT" ]; then
588 BOARD_VARIANT="${BOARD}_${VARIANT}"
589 else
590 BOARD_VARIANT="${BOARD}"
591 fi
592}