blob: 25abd9b4c3684fcea954e74c00f55b35b23dfd8f [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
20# Find root of source tree
21if [ "x$GCLIENT_ROOT" != "x" ]
22then
23 # GCLIENT_ROOT already set, so we're done
24 true
25elif [ "x$COMMON_SH" != "x" ]
26then
27 # COMMON_SH set, so assume that's us
28 GCLIENT_ROOT="$(dirname "$COMMON_SH")/../.."
29elif [ "x$BASH_SOURCE" != "x" ]
30then
31 # Using bash, so we can find ourselves
32 GCLIENT_ROOT="$(dirname "$BASH_SOURCE")/../.."
33else
tedbo4f44d9e2010-01-08 17:26:11 -080034 # Using dash or sh, we don't know where we are. $0 refers to the calling
rspangler@google.comd74220d2009-10-09 20:56:14 +000035 # script, not ourselves, so that doesn't help us.
36 echo "Unable to determine location for common.sh. If you are sourcing"
37 echo "common.sh from a script run via dash or sh, you must do it in the"
38 echo "following way:"
39 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
40 echo ' . "$COMMON_SH"'
41 echo "where the first line is the relative path from your script to"
42 echo "common.sh."
43 exit 1
44fi
45
46# Canonicalize the directories for the root dir and the calling script.
47# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -080048# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000049# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080050# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000051# Note that 'realpath' is equivalent to 'readlink -f'.
52TOP_SCRIPT_DIR=`readlink -f $TOP_SCRIPT_DIR`
53GCLIENT_ROOT=`readlink -f $GCLIENT_ROOT`
54
55# Other directories should always be pathed down from GCLIENT_ROOT.
56SRC_ROOT="$GCLIENT_ROOT/src"
57SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
58SCRIPTS_DIR="$SRC_ROOT/scripts"
59
60# Load developer's custom settings. Default location is in scripts dir,
61# since that's available both inside and outside the chroot. By convention,
62# settings from this file are variables starting with 'CHROMEOS_'
63CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
64if [ -f $CHROMEOS_DEV_SETTINGS ]
65then
66 # Turn on exit-on-error during custom settings processing
67 SAVE_OPTS=`set +o`
68 set -e
69
70 # Read settings
71 . $CHROMEOS_DEV_SETTINGS
72
73 # Restore previous state of exit-on-error
74 eval "$SAVE_OPTS"
75fi
76
77# Load shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -070078if [[ -f /usr/lib/shflags ]]; then
79 . /usr/lib/shflags
Raja Aluric6e23cd2010-11-02 15:55:57 -070080elif [ -f ./lib/shflags/shflags ]; then
81 . "./lib/shflags/shflags"
Anush Elangovan56de1dd2010-08-02 23:56:07 -070082else
83 . "${SRC_ROOT}/scripts/lib/shflags/shflags"
84fi
rspangler@google.comd74220d2009-10-09 20:56:14 +000085
Bill Richardson10d27c22010-01-20 13:38:50 -080086# Our local mirror
87DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +000088
Bill Richardson10d27c22010-01-20 13:38:50 -080089# Upstream mirrors and build suites come in 2 flavors
90# DEV - development chroot, used to build the chromeos image
91# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +000092
Bill Richardson10d27c22010-01-20 13:38:50 -080093DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
94DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
95
96DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
97DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +000098
99# Default location for chroot
100DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
101
102# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
103# they don't pollute the source directory.
104DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
105
David McMahon49302942010-02-18 16:55:35 -0800106# Set up a global ALL_BOARDS value
Sam Lefflera92ecd62010-02-23 08:25:38 -0800107ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g')
David McMahon49302942010-02-18 16:55:35 -0800108# Strip CR
109ALL_BOARDS=$(echo $ALL_BOARDS)
110# Set a default BOARD
111#DEFAULT_BOARD=x86-generic # or...
112DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
113
David James03668642010-07-28 17:08:29 -0700114# Enable --fast by default on non-official builds
115DEFAULT_FAST="${FLAGS_TRUE}"
116if [ "${CHROMEOS_OFFICIAL:-0}" = "1" ]; then
117 DEFAULT_FAST="${FLAGS_FALSE}"
118fi
119
rspangler@google.comd74220d2009-10-09 20:56:14 +0000120# Detect whether we're inside a chroot or not
121if [ -e /etc/debian_chroot ]
122then
123 INSIDE_CHROOT=1
124else
125 INSIDE_CHROOT=0
126fi
127
128# Directory locations inside the dev chroot
129CHROOT_TRUNK_DIR="/home/$USER/trunk"
130
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700131# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Sosa3d9a10b2010-04-13 15:00:46 -0700132DEFAULT_INSTALL_MASK="/usr/include /usr/man /usr/share/man /usr/share/doc \
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700133 /usr/share/gtk-doc /usr/share/gtk-2.0 /usr/lib/gtk-2.0/include \
134 /usr/share/info /usr/share/aclocal /usr/lib/gcc /usr/lib/pkgconfig \
135 /usr/share/pkgconfig /usr/share/gettext /usr/share/readline /etc/runlevels \
David James71fa14b2010-08-11 16:15:16 -0700136 /usr/share/openrc /lib/rc *.a *.la /etc/init.d /usr/lib/debug
137 /usr/local/autotest /usr/local/autotest-chrome"
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700138
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800139FACTORY_INSTALL_MASK="/opt/google/chrome /opt/google/o3d /opt/netscape \
Nick Sandersb3d04a02010-09-09 00:01:24 -0700140 /opt/google/talkplugin /opt/Qualcomm /opt/Synaptics \
141 /usr/lib/dri /usr/lib/python2.6/test \
142 /usr/share/chewing /usr/share/fonts \
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800143 /usr/share/ibus-pinyin /usr/share/libhangul /usr/share/locale \
144 /usr/share/m17n /usr/share/mime /usr/share/sounds /usr/share/tts \
David James71fa14b2010-08-11 16:15:16 -0700145 /usr/share/X11 /usr/share/zoneinfo /usr/lib/debug
146 /usr/local/autotest /usr/local/autotest-chrome"
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800147
David McMahonb7eb3a22010-02-09 14:07:40 -0800148# Check to ensure not running old scripts
149V_REVERSE=''
150V_VIDOFF=''
151case "$(basename $0)" in
152 build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh)
153 echo
154 echo "$V_REVERSE============================================================"
155 echo "=========================== WARNING ======================"
156 echo "============================================================$V_VIDOFF"
157 echo
158 echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:"
159 echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build"
160 echo
161 if [ "$USER" != "chrome-bot" ]
162 then
163 read -n1 -p "Press any key to continue using the OLD build system..."
164 echo
165 echo
166 fi
167 ;;
168esac
169
rspangler@google.comd74220d2009-10-09 20:56:14 +0000170# -----------------------------------------------------------------------------
171# Functions
172
Chris Sosaacada732010-02-23 15:20:03 -0800173function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700174 echo
175 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800176 echo
177 echo "*** No default board detected in " \
178 "$GCLIENT_ROOT/src/scripts/.default_board"
179 echo "*** Either run setup_board with default flag set"
180 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
181 echo
182}
183
184
185# Sets the default board variable for calling script
186function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700187 DEFAULT_BOARD=
188
Chris Sosaacada732010-02-23 15:20:03 -0800189 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
190 DEFAULT_BOARD=`cat "$GCLIENT_ROOT/src/scripts/.default_board"`
191 fi
192}
193
194
rspangler@google.comd74220d2009-10-09 20:56:14 +0000195# Make a package
196function make_pkg_common {
197 # Positional parameters from calling script. :? means "fail if unset".
198 set -e
199 PKG_BASE=${1:?}
200 shift
201 set +e
tedbo4f44d9e2010-01-08 17:26:11 -0800202
rspangler@google.comd74220d2009-10-09 20:56:14 +0000203 # All packages are built in the chroot
204 assert_inside_chroot
205
206 # Command line options
207 DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output"
208
209 # Parse command line and update positional args
210 FLAGS "$@" || exit 1
211 eval set -- "${FLAGS_ARGV}"
212
213 # Die on any errors
214 set -e
215
216 # Make output dir
217 OUT_DIR="$FLAGS_build_root/x86/local_packages"
218 mkdir -p "$OUT_DIR"
219
220 # Remove previous package from output dir
221 rm -f "$OUT_DIR"/${PKG_BASE}_*.deb
222
223 # Rebuild the package
224 pushd "$TOP_SCRIPT_DIR"
225 rm -f ../${PKG_BASE}_*.deb
226 dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS
227 mv ../${PKG_BASE}_*.deb "$OUT_DIR"
228 rm ../${PKG_BASE}_*.changes
229 popd
230}
231
Don Garrett640a0582010-05-04 16:54:28 -0700232# Enter a chroot and restart the current script if needed
233function restart_in_chroot_if_needed {
234 if [ $INSIDE_CHROOT -ne 1 ]
235 then
236 # Equivalent to enter_chroot.sh -- <current command>
237 exec $SCRIPTS_DIR/enter_chroot.sh -- \
238 $CHROOT_TRUNK_DIR/src/scripts/$(basename $0) $*
239 fi
240}
241
rspangler@google.comd74220d2009-10-09 20:56:14 +0000242# Fail unless we're inside the chroot. This guards against messing up your
243# workstation.
244function assert_inside_chroot {
245 if [ $INSIDE_CHROOT -ne 1 ]
246 then
247 echo "This script must be run inside the chroot. Run this first:"
248 echo " $SCRIPTS_DIR/enter_chroot.sh"
249 exit 1
250 fi
251}
252
253# Fail if we're inside the chroot. This guards against creating or entering
254# nested chroots, among other potential problems.
255function assert_outside_chroot {
256 if [ $INSIDE_CHROOT -ne 0 ]
257 then
258 echo "This script must be run outside the chroot."
259 exit 1
260 fi
261}
262
derat@google.com86dcc8e2009-11-21 19:49:49 +0000263function assert_not_root_user {
264 if [ `id -u` = 0 ]; then
265 echo "This script must be run as a non-root user."
266 exit 1
267 fi
268}
269
rspangler@google.comd74220d2009-10-09 20:56:14 +0000270# Install a package if it's not already installed
271function install_if_missing {
272 # Positional parameters from calling script. :? means "fail if unset".
273 PKG_NAME=${1:?}
274 shift
275
276 if [ -z `which $PKG_NAME` ]
277 then
278 echo "Can't find $PKG_NAME; attempting to install it."
279 sudo apt-get --yes --force-yes install $PKG_NAME
280 fi
281}
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800282
283# Returns true if the input file is whitelisted.
284#
285# $1 - The file to check
286is_whitelisted() {
David McMahonb7eb3a22010-02-09 14:07:40 -0800287 local file=$1
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800288 local whitelist="$FLAGS_whitelist"
289 test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1)
290
291 local checksum=$(md5sum "$file" | awk '{ print $1 }')
292 local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \
293 || /bin/true)
294 test $count -ne 0
295}
robotboy2ea03ac2010-02-11 15:30:55 -0800296
Luigi Semenzato1f82e122010-03-23 12:43:08 -0700297# Check that all arguments are flags; that is, there are no remaining arguments
298# after parsing from shflags. Allow (with a warning) a single empty-string
299# argument.
300#
301# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
302# then change this function.
303#
304# Usage: check_flags_only_and_allow_null_arg "$@" && set --
305function check_flags_only_and_allow_null_arg {
306 do_shift=1
307 if [[ $# == 1 && -z "$@" ]]; then
308 echo "$0: warning: ignoring null argument" >&2
309 shift
310 do_shift=0
311 fi
312 if [[ $# -gt 0 ]]; then
313 echo "error: invalid arguments: \"$@\"" >&2
314 flags_help
315 exit 1
316 fi
317 return $do_shift
318}
319
robotboy2ea03ac2010-02-11 15:30:55 -0800320V_RED="\e[31m"
321V_YELLOW="\e[33m"
Chris Sosad9798522010-06-16 14:29:50 -0700322V_BOLD_GREEN="\e[1;32m"
Darin Petkovb2a21e72010-03-25 11:33:41 -0700323V_BOLD_RED="\e[1;31m"
324V_BOLD_YELLOW="\e[1;33m"
robotboy2ea03ac2010-02-11 15:30:55 -0800325
Chris Sosad9798522010-06-16 14:29:50 -0700326function info {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200327 echo -e >&2 "${V_BOLD_GREEN}INFO : $1${V_VIDOFF}"
Will Drewry55b42c92010-10-20 15:44:11 -0500328}
Chris Sosad9798522010-06-16 14:29:50 -0700329
robotboy2ea03ac2010-02-11 15:30:55 -0800330function warn {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200331 echo -e >&2 "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800332}
333
334function error {
Sean O'Connord52c5ea2010-08-18 21:18:23 +0200335 echo -e >&2 "${V_BOLD_RED}ERROR : $1${V_VIDOFF}"
robotboy2ea03ac2010-02-11 15:30:55 -0800336}
David James546747b2010-03-23 15:19:43 -0700337
338function die {
339 error "$1"
340 exit 1
341}
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700342
343# Retry an emerge command according to $FLAGS_retries
David Jamesfd7403a2010-07-09 12:00:17 -0700344# The $EMERGE_JOBS flags will only be added the first time the command is run
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700345function eretry () {
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700346 local i=
347 for i in $(seq $FLAGS_retries); do
348 echo Retrying $*
David Jamesfd7403a2010-07-09 12:00:17 -0700349 $* $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700350 done
David Jamesfd7403a2010-07-09 12:00:17 -0700351 $* && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700352 return 1
353}
354
355# Removes single quotes around parameter
356# Arguments:
357# $1 - string which optionally has surrounding quotes
358# Returns:
359# None, but prints the string without quotes.
360function remove_quotes() {
361 echo "$1" | sed -e "s/^'//; s/'$//"
362}
tedbo373c3902010-04-12 10:52:40 -0700363
364# Writes stdin to the given file name as root using sudo in overwrite mode.
365#
366# $1 - The output file name.
367function sudo_clobber() {
368 sudo tee "$1" > /dev/null
369}
370
371# Writes stdin to the given file name as root using sudo in append mode.
372#
373# $1 - The output file name.
374function sudo_append() {
375 sudo tee -a "$1" > /dev/null
376}
robotboy98912212010-04-12 14:08:14 -0700377
378# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
379#
380# $1 - The path to unmount.
381function safe_umount {
382 path=${1:?}
383 shift
384
385 if ! sudo umount -d "${path}"; then
386 warn "Failed to unmount ${path}"
387 warn "Doing a lazy unmount"
388
389 sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}"
390 fi
391}
Chris Sosa702618f2010-05-14 12:52:32 -0700392
Chris Sosad4455022010-05-20 10:14:06 -0700393# Fixes symlinks that are incorrectly prefixed with the build root ${1}
394# rather than the real running root '/'.
395# TODO(sosa) - Merge setup - cleanup below with this method.
396fix_broken_symlinks() {
397 local build_root="${1}"
398 local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*")
399 for symlink in ${symlinks}; do
400 echo "Fixing ${symlink}"
401 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
402 # Trim spaces from target (bashism).
403 target=${target/ /}
404 # Make new target (removes rootfs prefix).
405 new_target=$(echo ${target} | sed "s#${build_root}##")
406
407 echo "Fixing symlink ${symlink}"
408 sudo unlink "${symlink}"
409 sudo ln -sf "${new_target}" "${symlink}"
410 done
411}
412
Chris Sosa702618f2010-05-14 12:52:32 -0700413# Sets up symlinks for the developer root. It is necessary to symlink
414# usr and local since the developer root is mounted at /usr/local and
415# applications expect to be installed under /usr/local/bin, etc.
416# This avoids packages installing into /usr/local/usr/local/bin.
417# ${1} specifies the symlink target for the developer root.
418# ${2} specifies the symlink target for the var directory.
419# ${3} specifies the location of the stateful partition.
420setup_symlinks_on_root() {
421 # Give args better names.
422 local dev_image_target=${1}
423 local var_target=${2}
424 local dev_image_root="${3}/dev_image"
425
426 # If our var target is actually the standard var, we are cleaning up the
427 # symlinks (could also check for /usr/local for the dev_image_target).
428 if [ ${var_target} = "/var" ]; then
429 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
430 else
431 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
432 fi
433
434 # Set up symlinks that should point to ${dev_image_target}.
435 for path in usr local; do
436 if [ -h "${dev_image_root}/${path}" ]; then
437 sudo unlink "${dev_image_root}/${path}"
438 elif [ -e "${dev_image_root}/${path}" ]; then
439 die "${dev_image_root}/${path} should be a symlink if exists"
440 fi
441 sudo ln -s ${dev_image_target} "${dev_image_root}/${path}"
442 done
443
444 # Setup var symlink.
445 if [ -h "${dev_image_root}/var" ]; then
446 sudo unlink "${dev_image_root}/var"
447 elif [ -e "${dev_image_root}/var" ]; then
448 die "${dev_image_root}/var should be a symlink if it exists"
449 fi
450
451 sudo ln -s "${var_target}" "${dev_image_root}/var"
452}
Nick Sandersd2509272010-06-16 03:50:04 -0700453
Will Drewry55b42c92010-10-20 15:44:11 -0500454# These two helpers clobber the ro compat value in our root filesystem.
455#
456# When the system is built with --enable_rootfs_verification, bit-precise
457# integrity checking is performed. That precision poses a usability issue on
458# systems that automount partitions with recognizable filesystems, such as
459# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
460# automatically updated even if no other writes are performed to the
461# filesystem. In addition, ext2+ does not support a "read-only" flag for a
462# given filesystem. That said, forward and backward compatibility of
463# filesystem features are supported by tracking if a new feature breaks r/w or
464# just write compatibility. We abuse the read-only compatibility flag[1] in
465# the filesystem header by setting the high order byte (le) to FF. This tells
466# the kernel that features R24-R31 are all enabled. Since those features are
467# undefined on all ext-based filesystem, all standard kernels will refuse to
468# mount the filesystem as read-write -- only read-only[2].
469#
470# [1] 32-bit flag we are modifying:
471# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
472# [2] Mount behavior is enforced here:
473# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
474#
475# N.B., if the high order feature bits are used in the future, we will need to
476# revisit this technique.
477disable_rw_mount() {
478 local rootfs="$1"
479 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500480 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
481 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500482 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
483 conv=notrunc count=1 bs=1
484}
485
486enable_rw_mount() {
487 local rootfs="$1"
488 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500489 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
490 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500491 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
492 conv=notrunc count=1 bs=1
493}
494
Nick Sandersd2509272010-06-16 03:50:04 -0700495# Get current timestamp. Assumes common.sh runs at startup.
496start_time=$(date +%s)
497
498# Print time elsapsed since start_time.
499print_time_elapsed() {
500 end_time=$(date +%s)
501 elapsed_seconds="$(( $end_time - $start_time ))"
502 minutes="$(( $elapsed_seconds / 60 ))"
503 seconds="$(( $elapsed_seconds % 60 ))"
Olof Johansson6d491382010-08-09 16:05:50 -0500504 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700505}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700506
507# This function is a place to put code to incrementally update the
508# chroot so that users don't need to fully re-make it. It should
509# be called from scripts that are run _outside_ the chroot.
510#
511# Please put date information so it's easy to keep track of when
512# old hacks can be retired and so that people can detect when a
513# hack triggered when it shouldn't have.
514#
515# ${1} specifies the location of the chroot.
516chroot_hacks_from_outside() {
517 # Give args better names.
518 local chroot_dir="${1}"
519
520 # Add root as a sudoer if not already done.
521 if ! sudo grep -q '^root ALL=(ALL) ALL$' "${chroot_dir}/etc/sudoers" ; then
522 info "Upgrading old chroot (pre 2010-10-19) - adding root to sudoers"
523 sudo bash -c "echo root ALL=\(ALL\) ALL >> \"${chroot_dir}/etc/sudoers\""
524 fi
525}