blob: 4fe71e4c5b42e02cd9cd6a83f85640e3d7aadb45 [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 Staaf856799a2011-01-26 11:23:50 -080020# Detect whether we're inside a chroot or not
21if [ -e /etc/debian_chroot ]
22then
23 INSIDE_CHROOT=1
24else
25 INSIDE_CHROOT=0
26fi
27
rspangler@google.comd74220d2009-10-09 20:56:14 +000028# Find root of source tree
29if [ "x$GCLIENT_ROOT" != "x" ]
30then
31 # GCLIENT_ROOT already set, so we're done
32 true
Anton Staaf856799a2011-01-26 11:23:50 -080033elif [ $INSIDE_CHROOT -eq 1 ]
34then
35 # If we are inside the chroot and we haven't been told otherwise, we assume
36 # that the GCLIENT_ROOT is ~/trunk.
37 GCLIENT_ROOT="${HOME}/trunk"
rspangler@google.comd74220d2009-10-09 20:56:14 +000038elif [ "x$COMMON_SH" != "x" ]
39then
40 # COMMON_SH set, so assume that's us
41 GCLIENT_ROOT="$(dirname "$COMMON_SH")/../.."
42elif [ "x$BASH_SOURCE" != "x" ]
43then
44 # Using bash, so we can find ourselves
45 GCLIENT_ROOT="$(dirname "$BASH_SOURCE")/../.."
46else
tedbo4f44d9e2010-01-08 17:26:11 -080047 # Using dash or sh, we don't know where we are. $0 refers to the calling
rspangler@google.comd74220d2009-10-09 20:56:14 +000048 # script, not ourselves, so that doesn't help us.
49 echo "Unable to determine location for common.sh. If you are sourcing"
50 echo "common.sh from a script run via dash or sh, you must do it in the"
51 echo "following way:"
52 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
53 echo ' . "$COMMON_SH"'
54 echo "where the first line is the relative path from your script to"
55 echo "common.sh."
56 exit 1
57fi
58
59# Canonicalize the directories for the root dir and the calling script.
60# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -080061# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000062# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080063# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000064# Note that 'realpath' is equivalent to 'readlink -f'.
65TOP_SCRIPT_DIR=`readlink -f $TOP_SCRIPT_DIR`
66GCLIENT_ROOT=`readlink -f $GCLIENT_ROOT`
67
68# Other directories should always be pathed down from GCLIENT_ROOT.
69SRC_ROOT="$GCLIENT_ROOT/src"
70SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
71SCRIPTS_DIR="$SRC_ROOT/scripts"
72
73# Load developer's custom settings. Default location is in scripts dir,
74# since that's available both inside and outside the chroot. By convention,
75# settings from this file are variables starting with 'CHROMEOS_'
76CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
77if [ -f $CHROMEOS_DEV_SETTINGS ]
78then
79 # Turn on exit-on-error during custom settings processing
80 SAVE_OPTS=`set +o`
81 set -e
82
83 # Read settings
84 . $CHROMEOS_DEV_SETTINGS
85
86 # Restore previous state of exit-on-error
87 eval "$SAVE_OPTS"
88fi
89
90# Load shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -070091if [[ -f /usr/lib/shflags ]]; then
92 . /usr/lib/shflags
Raja Aluric6e23cd2010-11-02 15:55:57 -070093elif [ -f ./lib/shflags/shflags ]; then
94 . "./lib/shflags/shflags"
Anush Elangovan56de1dd2010-08-02 23:56:07 -070095else
96 . "${SRC_ROOT}/scripts/lib/shflags/shflags"
97fi
rspangler@google.comd74220d2009-10-09 20:56:14 +000098
Bill Richardson10d27c22010-01-20 13:38:50 -080099# Our local mirror
100DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000101
Bill Richardson10d27c22010-01-20 13:38:50 -0800102# Upstream mirrors and build suites come in 2 flavors
103# DEV - development chroot, used to build the chromeos image
104# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000105
Bill Richardson10d27c22010-01-20 13:38:50 -0800106DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
107DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
108
109DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
110DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000111
112# Default location for chroot
113DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
114
115# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
116# they don't pollute the source directory.
117DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
118
David McMahon49302942010-02-18 16:55:35 -0800119# Set up a global ALL_BOARDS value
Raja Alurid44bcf92010-11-11 17:46:53 -0800120if [ -d $SRC_ROOT/overlays ]; then
121 ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500122fi
David McMahon49302942010-02-18 16:55:35 -0800123# Strip CR
124ALL_BOARDS=$(echo $ALL_BOARDS)
125# Set a default BOARD
126#DEFAULT_BOARD=x86-generic # or...
127DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
128
David Jamesff072012010-11-30 13:22:05 -0800129# Enable --fast by default.
David James03668642010-07-28 17:08:29 -0700130DEFAULT_FAST="${FLAGS_TRUE}"
David James03668642010-07-28 17:08:29 -0700131
rspangler@google.comd74220d2009-10-09 20:56:14 +0000132# Directory locations inside the dev chroot
133CHROOT_TRUNK_DIR="/home/$USER/trunk"
134
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700135# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800136# TODO: Is /usr/local/autotest-chrome still used by anyone?
Daniel Erate82f07c2010-12-21 13:39:22 -0800137DEFAULT_INSTALL_MASK="
138 *.a
139 *.la
140 /etc/init.d
141 /etc/runlevels
142 /lib/rc
143 /usr/bin/Xnest
144 /usr/bin/Xvfb
145 /usr/include
146 /usr/lib/debug
147 /usr/lib/gcc
148 /usr/lib/gtk-2.0/include
149 /usr/lib/pkgconfig
150 /usr/local/autotest
151 /usr/local/autotest-chrome
152 /usr/man
153 /usr/share/aclocal
154 /usr/share/doc
155 /usr/share/gettext
156 /usr/share/gtk-2.0
157 /usr/share/gtk-doc
158 /usr/share/info
159 /usr/share/man
160 /usr/share/openrc
161 /usr/share/pkgconfig
162 /usr/share/readline
163 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700164
Daniel Erate82f07c2010-12-21 13:39:22 -0800165FACTORY_INSTALL_MASK="
166 /opt/Qualcomm
167 /opt/Synaptics
168 /opt/google/chrome
169 /opt/google/o3d
170 /opt/google/talkplugin
171 /opt/netscape
172 /usr/lib/debug
173 /usr/lib/dri
174 /usr/lib/python2.6/test
175 /usr/local/autotest
176 /usr/local/autotest-chrome
177 /usr/local/autotest-pkgs
178 /usr/share/X11
179 /usr/share/chewing
180 /usr/share/fonts
181 /usr/share/ibus-pinyin
182 /usr/share/libhangul
183 /usr/share/locale
184 /usr/share/m17n
185 /usr/share/mime
186 /usr/share/sounds
187 /usr/share/tts
188 /usr/share/zoneinfo
189 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800190
David McMahonb7eb3a22010-02-09 14:07:40 -0800191# Check to ensure not running old scripts
192V_REVERSE=''
193V_VIDOFF=''
194case "$(basename $0)" in
195 build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh)
196 echo
197 echo "$V_REVERSE============================================================"
198 echo "=========================== WARNING ======================"
199 echo "============================================================$V_VIDOFF"
200 echo
201 echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:"
202 echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build"
203 echo
204 if [ "$USER" != "chrome-bot" ]
205 then
206 read -n1 -p "Press any key to continue using the OLD build system..."
207 echo
208 echo
209 fi
210 ;;
211esac
212
rspangler@google.comd74220d2009-10-09 20:56:14 +0000213# -----------------------------------------------------------------------------
214# Functions
215
Chris Sosaacada732010-02-23 15:20:03 -0800216function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700217 echo
218 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800219 echo
220 echo "*** No default board detected in " \
221 "$GCLIENT_ROOT/src/scripts/.default_board"
222 echo "*** Either run setup_board with default flag set"
223 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
224 echo
225}
226
227
228# Sets the default board variable for calling script
229function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700230 DEFAULT_BOARD=
231
Chris Sosaacada732010-02-23 15:20:03 -0800232 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
233 DEFAULT_BOARD=`cat "$GCLIENT_ROOT/src/scripts/.default_board"`
234 fi
235}
236
237
rspangler@google.comd74220d2009-10-09 20:56:14 +0000238# Make a package
239function make_pkg_common {
240 # Positional parameters from calling script. :? means "fail if unset".
241 set -e
242 PKG_BASE=${1:?}
243 shift
244 set +e
tedbo4f44d9e2010-01-08 17:26:11 -0800245
rspangler@google.comd74220d2009-10-09 20:56:14 +0000246 # All packages are built in the chroot
247 assert_inside_chroot
248
249 # Command line options
250 DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output"
251
252 # Parse command line and update positional args
253 FLAGS "$@" || exit 1
254 eval set -- "${FLAGS_ARGV}"
255
256 # Die on any errors
257 set -e
258
259 # Make output dir
260 OUT_DIR="$FLAGS_build_root/x86/local_packages"
261 mkdir -p "$OUT_DIR"
262
263 # Remove previous package from output dir
264 rm -f "$OUT_DIR"/${PKG_BASE}_*.deb
265
266 # Rebuild the package
267 pushd "$TOP_SCRIPT_DIR"
268 rm -f ../${PKG_BASE}_*.deb
269 dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS
270 mv ../${PKG_BASE}_*.deb "$OUT_DIR"
271 rm ../${PKG_BASE}_*.changes
272 popd
273}
274
Don Garrett640a0582010-05-04 16:54:28 -0700275# Enter a chroot and restart the current script if needed
276function restart_in_chroot_if_needed {
David Rochberg3b910702010-12-02 10:45:21 -0500277 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700278 if [ $INSIDE_CHROOT -ne 1 ]
279 then
David Rochbergb4f6b3c2010-12-17 11:55:40 -0500280 # Equivalent to enter_chroot.sh -- <current command>
Don Garrett640a0582010-05-04 16:54:28 -0700281 exec $SCRIPTS_DIR/enter_chroot.sh -- \
David Rochbergb4f6b3c2010-12-17 11:55:40 -0500282 $CHROOT_TRUNK_DIR/src/scripts/$(basename $0) "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700283 fi
284}
285
rspangler@google.comd74220d2009-10-09 20:56:14 +0000286# Fail unless we're inside the chroot. This guards against messing up your
287# workstation.
288function assert_inside_chroot {
289 if [ $INSIDE_CHROOT -ne 1 ]
290 then
291 echo "This script must be run inside the chroot. Run this first:"
292 echo " $SCRIPTS_DIR/enter_chroot.sh"
293 exit 1
294 fi
295}
296
297# Fail if we're inside the chroot. This guards against creating or entering
298# nested chroots, among other potential problems.
299function assert_outside_chroot {
300 if [ $INSIDE_CHROOT -ne 0 ]
301 then
302 echo "This script must be run outside the chroot."
303 exit 1
304 fi
305}
306
derat@google.com86dcc8e2009-11-21 19:49:49 +0000307function assert_not_root_user {
308 if [ `id -u` = 0 ]; then
309 echo "This script must be run as a non-root user."
310 exit 1
311 fi
312}
313
rspangler@google.comd74220d2009-10-09 20:56:14 +0000314# Install a package if it's not already installed
315function install_if_missing {
316 # Positional parameters from calling script. :? means "fail if unset".
317 PKG_NAME=${1:?}
318 shift
319
320 if [ -z `which $PKG_NAME` ]
321 then
322 echo "Can't find $PKG_NAME; attempting to install it."
323 sudo apt-get --yes --force-yes install $PKG_NAME
324 fi
325}
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800326
327# Returns true if the input file is whitelisted.
328#
329# $1 - The file to check
330is_whitelisted() {
David McMahonb7eb3a22010-02-09 14:07:40 -0800331 local file=$1
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800332 local whitelist="$FLAGS_whitelist"
333 test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1)
334
335 local checksum=$(md5sum "$file" | awk '{ print $1 }')
336 local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \
337 || /bin/true)
338 test $count -ne 0
339}
robotboy2ea03ac2010-02-11 15:30:55 -0800340
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700341# Check that all arguments are flags; that is, there are no remaining arguments
342# after parsing from shflags. Allow (with a warning) a single empty-string
343# argument.
344#
345# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
346# then change this function.
347#
348# Usage: check_flags_only_and_allow_null_arg "$@" && set --
349function check_flags_only_and_allow_null_arg {
350 do_shift=1
351 if [[ $# == 1 && -z "$@" ]]; then
352 echo "$0: warning: ignoring null argument" >&2
353 shift
354 do_shift=0
355 fi
356 if [[ $# -gt 0 ]]; then
357 echo "error: invalid arguments: \"$@\"" >&2
358 flags_help
359 exit 1
360 fi
361 return $do_shift
362}
363
robotboy2ea03ac2010-02-11 15:30:55 -0800364V_RED="\e[31m"
365V_YELLOW="\e[33m"
Chris Sosad9798522010-06-16 14:29:50 -0700366V_BOLD_GREEN="\e[1;32m"
Darin Petkovb2a21e72010-03-25 11:33:41 -0700367V_BOLD_RED="\e[1;31m"
368V_BOLD_YELLOW="\e[1;33m"
robotboy2ea03ac2010-02-11 15:30:55 -0800369
Chris Sosad9798522010-06-16 14:29:50 -0700370function info {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200371 echo -e >&2 "${V_BOLD_GREEN}INFO : $1${V_VIDOFF}"
Will Drewry55b42c92010-10-20 15:44:11 -0500372}
Chris Sosad9798522010-06-16 14:29:50 -0700373
robotboy2ea03ac2010-02-11 15:30:55 -0800374function warn {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200375 echo -e >&2 "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800376}
377
378function error {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200379 echo -e >&2 "${V_BOLD_RED}ERROR : $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800380}
David James546747b2010-03-23 15:19:43 -0700381
382function die {
383 error "$1"
384 exit 1
385}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700386
387# Retry an emerge command according to $FLAGS_retries
David Jamesfd7403a2010-07-09 12:00:17 -0700388# The $EMERGE_JOBS flags will only be added the first time the command is run
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700389function eretry () {
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700390 local i=
391 for i in $(seq $FLAGS_retries); do
392 echo Retrying $*
David Jamesfd7403a2010-07-09 12:00:17 -0700393 $* $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700394 done
David Jamesfd7403a2010-07-09 12:00:17 -0700395 $* && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700396 return 1
397}
398
399# Removes single quotes around parameter
400# Arguments:
401# $1 - string which optionally has surrounding quotes
402# Returns:
403# None, but prints the string without quotes.
404function remove_quotes() {
405 echo "$1" | sed -e "s/^'//; s/'$//"
406}
tedbo373c3902010-04-12 10:52:40 -0700407
408# Writes stdin to the given file name as root using sudo in overwrite mode.
409#
410# $1 - The output file name.
411function sudo_clobber() {
412 sudo tee "$1" > /dev/null
413}
414
415# Writes stdin to the given file name as root using sudo in append mode.
416#
417# $1 - The output file name.
418function sudo_append() {
419 sudo tee -a "$1" > /dev/null
420}
robotboy98912212010-04-12 14:08:14 -0700421
422# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
423#
424# $1 - The path to unmount.
425function safe_umount {
426 path=${1:?}
427 shift
428
429 if ! sudo umount -d "${path}"; then
430 warn "Failed to unmount ${path}"
431 warn "Doing a lazy unmount"
432
433 sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}"
434 fi
435}
Chris Sosa702618f2010-05-14 12:52:32 -0700436
Chris Sosad4455022010-05-20 10:14:06 -0700437# Fixes symlinks that are incorrectly prefixed with the build root ${1}
438# rather than the real running root '/'.
439# TODO(sosa) - Merge setup - cleanup below with this method.
440fix_broken_symlinks() {
441 local build_root="${1}"
442 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
443 for symlink in ${symlinks}; do
444 echo "Fixing ${symlink}"
445 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
446 # Trim spaces from target (bashism).
447 target=${target/ /}
448 # Make new target (removes rootfs prefix).
449 new_target=$(echo ${target} | sed "s#${build_root}##")
450
451 echo "Fixing symlink ${symlink}"
452 sudo unlink "${symlink}"
453 sudo ln -sf "${new_target}" "${symlink}"
454 done
455}
456
Chris Sosa702618f2010-05-14 12:52:32 -0700457# Sets up symlinks for the developer root. It is necessary to symlink
458# usr and local since the developer root is mounted at /usr/local and
459# applications expect to be installed under /usr/local/bin, etc.
460# This avoids packages installing into /usr/local/usr/local/bin.
461# ${1} specifies the symlink target for the developer root.
462# ${2} specifies the symlink target for the var directory.
463# ${3} specifies the location of the stateful partition.
464setup_symlinks_on_root() {
465 # Give args better names.
466 local dev_image_target=${1}
467 local var_target=${2}
468 local dev_image_root="${3}/dev_image"
469
470 # If our var target is actually the standard var, we are cleaning up the
471 # symlinks (could also check for /usr/local for the dev_image_target).
472 if [ ${var_target} = "/var" ]; then
473 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
474 else
475 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
476 fi
477
478 # Set up symlinks that should point to ${dev_image_target}.
479 for path in usr local; do
480 if [ -h "${dev_image_root}/${path}" ]; then
481 sudo unlink "${dev_image_root}/${path}"
482 elif [ -e "${dev_image_root}/${path}" ]; then
483 die "${dev_image_root}/${path} should be a symlink if exists"
484 fi
485 sudo ln -s ${dev_image_target} "${dev_image_root}/${path}"
486 done
487
488 # Setup var symlink.
489 if [ -h "${dev_image_root}/var" ]; then
490 sudo unlink "${dev_image_root}/var"
491 elif [ -e "${dev_image_root}/var" ]; then
492 die "${dev_image_root}/var should be a symlink if it exists"
493 fi
494
495 sudo ln -s "${var_target}" "${dev_image_root}/var"
496}
Nick Sandersd2509272010-06-16 03:50:04 -0700497
Will Drewry55b42c92010-10-20 15:44:11 -0500498# These two helpers clobber the ro compat value in our root filesystem.
499#
500# When the system is built with --enable_rootfs_verification, bit-precise
501# integrity checking is performed. That precision poses a usability issue on
502# systems that automount partitions with recognizable filesystems, such as
503# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
504# automatically updated even if no other writes are performed to the
505# filesystem. In addition, ext2+ does not support a "read-only" flag for a
506# given filesystem. That said, forward and backward compatibility of
507# filesystem features are supported by tracking if a new feature breaks r/w or
508# just write compatibility. We abuse the read-only compatibility flag[1] in
509# the filesystem header by setting the high order byte (le) to FF. This tells
510# the kernel that features R24-R31 are all enabled. Since those features are
511# undefined on all ext-based filesystem, all standard kernels will refuse to
512# mount the filesystem as read-write -- only read-only[2].
513#
514# [1] 32-bit flag we are modifying:
515# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
516# [2] Mount behavior is enforced here:
517# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
518#
519# N.B., if the high order feature bits are used in the future, we will need to
520# revisit this technique.
521disable_rw_mount() {
522 local rootfs="$1"
523 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500524 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
525 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500526 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
527 conv=notrunc count=1 bs=1
528}
529
530enable_rw_mount() {
531 local rootfs="$1"
532 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500533 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
534 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500535 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
536 conv=notrunc count=1 bs=1
537}
538
Nick Sandersd2509272010-06-16 03:50:04 -0700539# Get current timestamp. Assumes common.sh runs at startup.
540start_time=$(date +%s)
541
542# Print time elsapsed since start_time.
543print_time_elapsed() {
544 end_time=$(date +%s)
545 elapsed_seconds="$(( $end_time - $start_time ))"
546 minutes="$(( $elapsed_seconds / 60 ))"
547 seconds="$(( $elapsed_seconds % 60 ))"
Olof Johansson6d491382010-08-09 16:05:50 -0500548 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700549}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700550
551# This function is a place to put code to incrementally update the
552# chroot so that users don't need to fully re-make it. It should
553# be called from scripts that are run _outside_ the chroot.
554#
555# Please put date information so it's easy to keep track of when
556# old hacks can be retired and so that people can detect when a
557# hack triggered when it shouldn't have.
558#
559# ${1} specifies the location of the chroot.
560chroot_hacks_from_outside() {
561 # Give args better names.
562 local chroot_dir="${1}"
563
564 # Add root as a sudoer if not already done.
565 if ! sudo grep -q '^root ALL=(ALL) ALL$' "${chroot_dir}/etc/sudoers" ; then
566 info "Upgrading old chroot (pre 2010-10-19) - adding root to sudoers"
567 sudo bash -c "echo root ALL=\(ALL\) ALL >> \"${chroot_dir}/etc/sudoers\""
568 fi
569}
Anton Staaf9bcd8412011-01-24 15:27:14 -0800570
571# The board and variant command line options can be used in a number of ways
572# to specify the board and variant. The board can encode both pieces of
573# information separated by underscores. Or the variant can be passed using
574# the separate variant option. This function extracts the canonical board and
575# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
576# variables.
577get_board_and_variant() {
578 local flags_board="${1}"
579 local flags_variant="${2}"
580
581 BOARD=$(echo "$flags_board" | cut -d '_' -f 1)
582 VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)}
583
584 if [ -n "$VARIANT" ]; then
585 BOARD_VARIANT="${BOARD}_${VARIANT}"
586 else
587 BOARD_VARIANT="${BOARD}"
588 fi
589}