blob: 7235f23ce92723d9dc6b18e95358a65e99988ab2 [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
Alexey Marinichev63c42aa2009-12-21 11:42:39 -080015NUM_JOBS=`grep -c "^processor" /proc/cpuinfo`
rspangler@google.comd74220d2009-10-09 20:56:14 +000016
17# Store location of the calling script.
18TOP_SCRIPT_DIR="${TOP_SCRIPT_DIR:-$(dirname $0)}"
19
Anton Staaf30acb0b2011-01-26 16:00:20 -080020# Detect whether we're inside a chroot or not
21if [ -e /etc/debian_chroot ]
rspangler@google.comd74220d2009-10-09 20:56:14 +000022then
Anton Staaf30acb0b2011-01-26 16:00:20 -080023 INSIDE_CHROOT=1
rspangler@google.comd74220d2009-10-09 20:56:14 +000024else
Anton Staaf30acb0b2011-01-26 16:00:20 -080025 INSIDE_CHROOT=0
rspangler@google.comd74220d2009-10-09 20:56:14 +000026fi
27
Anton Staaf30acb0b2011-01-26 16:00:20 -080028# Construct a list of possible locations for the source tree. This list is
29# based on various environment variables and globals that may have been set
30# by the calling script.
31function get_gclient_root_list() {
32 if [ $INSIDE_CHROOT -eq 1 ]; then
33 echo "/home/${USER}/trunk"
34
35 if [ -n "${SUDO_USER}" ]; then echo "/home/${SUDO_USER}/trunk"; fi
36 fi
37
38 if [ -n "${COMMON_SH}" ]; then echo "$(dirname "$COMMON_SH")/../.."; fi
39 if [ -n "${BASH_SOURCE}" ]; then echo "$(dirname "$BASH_SOURCE")/../.."; fi
40}
41
42# Based on the list of possible source locations we set GCLIENT_ROOT if it is
43# not already defined by looking for a src directory in each seach path
44# location. If we do not find a valid looking root we error out.
45function get_gclient_root() {
46 if [ -n "${GCLIENT_ROOT}" ]; then
47 return
48 fi
49
50 for path in $(get_gclient_root_list); do
51 if [ -d "${path}/src" ]; then
52 GCLIENT_ROOT=${path}
53 break
54 fi
55 done
56
57 if [ -z "${GCLIENT_ROOT}" ]; then
58 # Using dash or sh, we don't know where we are. $0 refers to the calling
59 # script, not ourselves, so that doesn't help us.
60 echo "Unable to determine location for common.sh. If you are sourcing"
61 echo "common.sh from a script run via dash or sh, you must do it in the"
62 echo "following way:"
63 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
64 echo ' . "$COMMON_SH"'
65 echo "where the first line is the relative path from your script to"
66 echo "common.sh."
67 exit 1
68 fi
69}
70
71# Find root of source tree
72get_gclient_root
73
rspangler@google.comd74220d2009-10-09 20:56:14 +000074# Canonicalize the directories for the root dir and the calling script.
75# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -080076# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000077# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080078# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000079# Note that 'realpath' is equivalent to 'readlink -f'.
80TOP_SCRIPT_DIR=`readlink -f $TOP_SCRIPT_DIR`
81GCLIENT_ROOT=`readlink -f $GCLIENT_ROOT`
82
83# Other directories should always be pathed down from GCLIENT_ROOT.
84SRC_ROOT="$GCLIENT_ROOT/src"
85SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
86SCRIPTS_DIR="$SRC_ROOT/scripts"
87
88# Load developer's custom settings. Default location is in scripts dir,
89# since that's available both inside and outside the chroot. By convention,
90# settings from this file are variables starting with 'CHROMEOS_'
91CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
92if [ -f $CHROMEOS_DEV_SETTINGS ]
93then
94 # Turn on exit-on-error during custom settings processing
95 SAVE_OPTS=`set +o`
96 set -e
97
98 # Read settings
99 . $CHROMEOS_DEV_SETTINGS
100
101 # Restore previous state of exit-on-error
102 eval "$SAVE_OPTS"
103fi
104
105# Load shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -0700106if [[ -f /usr/lib/shflags ]]; then
107 . /usr/lib/shflags
Raja Aluric6e23cd2010-11-02 15:55:57 -0700108elif [ -f ./lib/shflags/shflags ]; then
109 . "./lib/shflags/shflags"
Anush Elangovan56de1dd2010-08-02 23:56:07 -0700110else
111 . "${SRC_ROOT}/scripts/lib/shflags/shflags"
112fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000113
Bill Richardson10d27c22010-01-20 13:38:50 -0800114# Our local mirror
115DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000116
Bill Richardson10d27c22010-01-20 13:38:50 -0800117# Upstream mirrors and build suites come in 2 flavors
118# DEV - development chroot, used to build the chromeos image
119# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000120
Bill Richardson10d27c22010-01-20 13:38:50 -0800121DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
122DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
123
124DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
125DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000126
127# Default location for chroot
128DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
129
130# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
131# they don't pollute the source directory.
132DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
133
David McMahon49302942010-02-18 16:55:35 -0800134# Set up a global ALL_BOARDS value
Raja Alurid44bcf92010-11-11 17:46:53 -0800135if [ -d $SRC_ROOT/overlays ]; then
136 ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500137fi
David McMahon49302942010-02-18 16:55:35 -0800138# Strip CR
139ALL_BOARDS=$(echo $ALL_BOARDS)
140# Set a default BOARD
141#DEFAULT_BOARD=x86-generic # or...
142DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
143
David Jamesff072012010-11-30 13:22:05 -0800144# Enable --fast by default.
David James03668642010-07-28 17:08:29 -0700145DEFAULT_FAST="${FLAGS_TRUE}"
David James03668642010-07-28 17:08:29 -0700146
rspangler@google.comd74220d2009-10-09 20:56:14 +0000147# Directory locations inside the dev chroot
148CHROOT_TRUNK_DIR="/home/$USER/trunk"
149
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700150# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800151# TODO: Is /usr/local/autotest-chrome still used by anyone?
Daniel Erate82f07c2010-12-21 13:39:22 -0800152DEFAULT_INSTALL_MASK="
153 *.a
154 *.la
155 /etc/init.d
156 /etc/runlevels
157 /lib/rc
158 /usr/bin/Xnest
159 /usr/bin/Xvfb
160 /usr/include
161 /usr/lib/debug
162 /usr/lib/gcc
163 /usr/lib/gtk-2.0/include
164 /usr/lib/pkgconfig
165 /usr/local/autotest
166 /usr/local/autotest-chrome
167 /usr/man
168 /usr/share/aclocal
169 /usr/share/doc
170 /usr/share/gettext
171 /usr/share/gtk-2.0
172 /usr/share/gtk-doc
173 /usr/share/info
174 /usr/share/man
175 /usr/share/openrc
176 /usr/share/pkgconfig
177 /usr/share/readline
178 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700179
Daniel Erate82f07c2010-12-21 13:39:22 -0800180FACTORY_INSTALL_MASK="
181 /opt/Qualcomm
182 /opt/Synaptics
183 /opt/google/chrome
184 /opt/google/o3d
185 /opt/google/talkplugin
186 /opt/netscape
187 /usr/lib/debug
188 /usr/lib/dri
189 /usr/lib/python2.6/test
190 /usr/local/autotest
191 /usr/local/autotest-chrome
192 /usr/local/autotest-pkgs
193 /usr/share/X11
194 /usr/share/chewing
195 /usr/share/fonts
196 /usr/share/ibus-pinyin
197 /usr/share/libhangul
198 /usr/share/locale
199 /usr/share/m17n
200 /usr/share/mime
201 /usr/share/sounds
202 /usr/share/tts
203 /usr/share/zoneinfo
204 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800205
David McMahonb7eb3a22010-02-09 14:07:40 -0800206# Check to ensure not running old scripts
207V_REVERSE=''
208V_VIDOFF=''
209case "$(basename $0)" in
210 build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh)
211 echo
212 echo "$V_REVERSE============================================================"
213 echo "=========================== WARNING ======================"
214 echo "============================================================$V_VIDOFF"
215 echo
216 echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:"
217 echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build"
218 echo
219 if [ "$USER" != "chrome-bot" ]
220 then
221 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
248 DEFAULT_BOARD=`cat "$GCLIENT_ROOT/src/scripts/.default_board"`
249 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
275 OUT_DIR="$FLAGS_build_root/x86/local_packages"
276 mkdir -p "$OUT_DIR"
277
278 # Remove previous package from output dir
279 rm -f "$OUT_DIR"/${PKG_BASE}_*.deb
280
281 # Rebuild the package
282 pushd "$TOP_SCRIPT_DIR"
283 rm -f ../${PKG_BASE}_*.deb
284 dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS
285 mv ../${PKG_BASE}_*.deb "$OUT_DIR"
286 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 "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700293 if [ $INSIDE_CHROOT -ne 1 ]
294 then
David Rochbergb4f6b3c2010-12-17 11:55:40 -0500295 # Equivalent to enter_chroot.sh -- <current command>
Don Garrett640a0582010-05-04 16:54:28 -0700296 exec $SCRIPTS_DIR/enter_chroot.sh -- \
David Rochbergb4f6b3c2010-12-17 11:55:40 -0500297 $CHROOT_TRUNK_DIR/src/scripts/$(basename $0) "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700298 fi
299}
300
rspangler@google.comd74220d2009-10-09 20:56:14 +0000301# Fail unless we're inside the chroot. This guards against messing up your
302# workstation.
303function assert_inside_chroot {
304 if [ $INSIDE_CHROOT -ne 1 ]
305 then
306 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 {
315 if [ $INSIDE_CHROOT -ne 0 ]
316 then
317 echo "This script must be run outside the chroot."
318 exit 1
319 fi
320}
321
derat@google.com86dcc8e2009-11-21 19:49:49 +0000322function assert_not_root_user {
323 if [ `id -u` = 0 ]; then
324 echo "This script must be run as a non-root user."
325 exit 1
326 fi
327}
328
rspangler@google.comd74220d2009-10-09 20:56:14 +0000329# Install a package if it's not already installed
330function install_if_missing {
331 # Positional parameters from calling script. :? means "fail if unset".
332 PKG_NAME=${1:?}
333 shift
334
335 if [ -z `which $PKG_NAME` ]
336 then
337 echo "Can't find $PKG_NAME; attempting to install it."
338 sudo apt-get --yes --force-yes install $PKG_NAME
339 fi
340}
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800341
342# Returns true if the input file is whitelisted.
343#
344# $1 - The file to check
345is_whitelisted() {
David McMahonb7eb3a22010-02-09 14:07:40 -0800346 local file=$1
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800347 local whitelist="$FLAGS_whitelist"
348 test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1)
349
350 local checksum=$(md5sum "$file" | awk '{ print $1 }')
351 local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \
352 || /bin/true)
353 test $count -ne 0
354}
robotboy2ea03ac2010-02-11 15:30:55 -0800355
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700356# Check that all arguments are flags; that is, there are no remaining arguments
357# after parsing from shflags. Allow (with a warning) a single empty-string
358# argument.
359#
360# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
361# then change this function.
362#
363# Usage: check_flags_only_and_allow_null_arg "$@" && set --
364function check_flags_only_and_allow_null_arg {
365 do_shift=1
366 if [[ $# == 1 && -z "$@" ]]; then
367 echo "$0: warning: ignoring null argument" >&2
368 shift
369 do_shift=0
370 fi
371 if [[ $# -gt 0 ]]; then
372 echo "error: invalid arguments: \"$@\"" >&2
373 flags_help
374 exit 1
375 fi
376 return $do_shift
377}
378
robotboy2ea03ac2010-02-11 15:30:55 -0800379V_RED="\e[31m"
380V_YELLOW="\e[33m"
Chris Sosad9798522010-06-16 14:29:50 -0700381V_BOLD_GREEN="\e[1;32m"
Darin Petkovb2a21e72010-03-25 11:33:41 -0700382V_BOLD_RED="\e[1;31m"
383V_BOLD_YELLOW="\e[1;33m"
robotboy2ea03ac2010-02-11 15:30:55 -0800384
Chris Sosad9798522010-06-16 14:29:50 -0700385function info {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200386 echo -e >&2 "${V_BOLD_GREEN}INFO : $1${V_VIDOFF}"
Will Drewry55b42c92010-10-20 15:44:11 -0500387}
Chris Sosad9798522010-06-16 14:29:50 -0700388
robotboy2ea03ac2010-02-11 15:30:55 -0800389function warn {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200390 echo -e >&2 "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800391}
392
393function error {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200394 echo -e >&2 "${V_BOLD_RED}ERROR : $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800395}
David James546747b2010-03-23 15:19:43 -0700396
397function die {
398 error "$1"
399 exit 1
400}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700401
402# Retry an emerge command according to $FLAGS_retries
David Jamesfd7403a2010-07-09 12:00:17 -0700403# The $EMERGE_JOBS flags will only be added the first time the command is run
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700404function eretry () {
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700405 local i=
406 for i in $(seq $FLAGS_retries); do
407 echo Retrying $*
David Jamesfd7403a2010-07-09 12:00:17 -0700408 $* $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700409 done
David Jamesfd7403a2010-07-09 12:00:17 -0700410 $* && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700411 return 1
412}
413
414# Removes single quotes around parameter
415# Arguments:
416# $1 - string which optionally has surrounding quotes
417# Returns:
418# None, but prints the string without quotes.
419function remove_quotes() {
420 echo "$1" | sed -e "s/^'//; s/'$//"
421}
tedbo373c3902010-04-12 10:52:40 -0700422
423# Writes stdin to the given file name as root using sudo in overwrite mode.
424#
425# $1 - The output file name.
426function sudo_clobber() {
427 sudo tee "$1" > /dev/null
428}
429
430# Writes stdin to the given file name as root using sudo in append mode.
431#
432# $1 - The output file name.
433function sudo_append() {
434 sudo tee -a "$1" > /dev/null
435}
robotboy98912212010-04-12 14:08:14 -0700436
437# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
438#
439# $1 - The path to unmount.
440function safe_umount {
441 path=${1:?}
442 shift
443
444 if ! sudo umount -d "${path}"; then
445 warn "Failed to unmount ${path}"
446 warn "Doing a lazy unmount"
447
448 sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}"
449 fi
450}
Chris Sosa702618f2010-05-14 12:52:32 -0700451
Chris Sosad4455022010-05-20 10:14:06 -0700452# Fixes symlinks that are incorrectly prefixed with the build root ${1}
453# rather than the real running root '/'.
454# TODO(sosa) - Merge setup - cleanup below with this method.
455fix_broken_symlinks() {
456 local build_root="${1}"
457 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
458 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}.
494 for path in usr local; do
495 if [ -h "${dev_image_root}/${path}" ]; then
496 sudo unlink "${dev_image_root}/${path}"
497 elif [ -e "${dev_image_root}/${path}" ]; then
498 die "${dev_image_root}/${path} should be a symlink if exists"
499 fi
500 sudo ln -s ${dev_image_target} "${dev_image_root}/${path}"
501 done
502
503 # Setup var symlink.
504 if [ -h "${dev_image_root}/var" ]; then
505 sudo unlink "${dev_image_root}/var"
506 elif [ -e "${dev_image_root}/var" ]; then
507 die "${dev_image_root}/var should be a symlink if it exists"
508 fi
509
510 sudo ln -s "${var_target}" "${dev_image_root}/var"
511}
Nick Sandersd2509272010-06-16 03:50:04 -0700512
Will Drewry55b42c92010-10-20 15:44:11 -0500513# These two helpers clobber the ro compat value in our root filesystem.
514#
515# When the system is built with --enable_rootfs_verification, bit-precise
516# integrity checking is performed. That precision poses a usability issue on
517# systems that automount partitions with recognizable filesystems, such as
518# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
519# automatically updated even if no other writes are performed to the
520# filesystem. In addition, ext2+ does not support a "read-only" flag for a
521# given filesystem. That said, forward and backward compatibility of
522# filesystem features are supported by tracking if a new feature breaks r/w or
523# just write compatibility. We abuse the read-only compatibility flag[1] in
524# the filesystem header by setting the high order byte (le) to FF. This tells
525# the kernel that features R24-R31 are all enabled. Since those features are
526# undefined on all ext-based filesystem, all standard kernels will refuse to
527# mount the filesystem as read-write -- only read-only[2].
528#
529# [1] 32-bit flag we are modifying:
530# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
531# [2] Mount behavior is enforced here:
532# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
533#
534# N.B., if the high order feature bits are used in the future, we will need to
535# revisit this technique.
536disable_rw_mount() {
537 local rootfs="$1"
538 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500539 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
540 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500541 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
542 conv=notrunc count=1 bs=1
543}
544
545enable_rw_mount() {
546 local rootfs="$1"
547 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500548 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
549 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500550 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
551 conv=notrunc count=1 bs=1
552}
553
Nick Sandersd2509272010-06-16 03:50:04 -0700554# Get current timestamp. Assumes common.sh runs at startup.
555start_time=$(date +%s)
556
557# Print time elsapsed since start_time.
558print_time_elapsed() {
559 end_time=$(date +%s)
560 elapsed_seconds="$(( $end_time - $start_time ))"
561 minutes="$(( $elapsed_seconds / 60 ))"
562 seconds="$(( $elapsed_seconds % 60 ))"
Olof Johansson6d491382010-08-09 16:05:50 -0500563 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700564}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700565
566# This function is a place to put code to incrementally update the
567# chroot so that users don't need to fully re-make it. It should
568# be called from scripts that are run _outside_ the chroot.
569#
570# Please put date information so it's easy to keep track of when
571# old hacks can be retired and so that people can detect when a
572# hack triggered when it shouldn't have.
573#
574# ${1} specifies the location of the chroot.
575chroot_hacks_from_outside() {
576 # Give args better names.
577 local chroot_dir="${1}"
578
579 # Add root as a sudoer if not already done.
580 if ! sudo grep -q '^root ALL=(ALL) ALL$' "${chroot_dir}/etc/sudoers" ; then
581 info "Upgrading old chroot (pre 2010-10-19) - adding root to sudoers"
582 sudo bash -c "echo root ALL=\(ALL\) ALL >> \"${chroot_dir}/etc/sudoers\""
583 fi
584}
Anton Staaf9bcd8412011-01-24 15:27:14 -0800585
586# The board and variant command line options can be used in a number of ways
587# to specify the board and variant. The board can encode both pieces of
588# information separated by underscores. Or the variant can be passed using
589# the separate variant option. This function extracts the canonical board and
590# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
591# variables.
592get_board_and_variant() {
593 local flags_board="${1}"
594 local flags_variant="${2}"
595
596 BOARD=$(echo "$flags_board" | cut -d '_' -f 1)
597 VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)}
598
599 if [ -n "$VARIANT" ]; then
600 BOARD_VARIANT="${BOARD}_${VARIANT}"
601 else
602 BOARD_VARIANT="${BOARD}"
603 fi
604}