blob: 1d25074316d2a336046e3a5311cc1d6de5b02a37 [file] [log] [blame]
David McMahon49302942010-02-18 16:55:35 -08001# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
rspangler@google.comd74220d2009-10-09 20:56:14 +00002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Common constants for build scripts
6# This must evaluate properly for both /bin/bash and /bin/sh
7
8# All scripts should die on error unless commands are specifically excepted
9# by prefixing with '!' or surrounded by 'set +e' / 'set -e'.
10# TODO: Re-enable this once shflags is less prone to dying.
11#set -e
12
13# The number of jobs to pass to tools that can run in parallel (such as make
14# and dpkg-buildpackage
Greg Spencer798d75f2011-02-01 22:04:49 -080015NUM_JOBS=$(grep -c "^processor" /proc/cpuinfo)
rspangler@google.comd74220d2009-10-09 20:56:14 +000016
Simon Glass142ca062011-02-09 13:39:43 -080017# True if we have the 'pv' utility - also set up COMMON_PV_CAT for convenience
18COMMON_PV_OK=1
19COMMON_PV_CAT=pv
20pv -V >/dev/null 2>&1 || COMMON_PV_OK=0
21if [ $COMMON_PV_OK -eq 0 ]; then
22 COMMON_PV_CAT=cat
23fi
24
Greg Spencer798d75f2011-02-01 22:04:49 -080025# Make sure we have the location and name of the calling script, using
26# the current value if it is already set.
27SCRIPT_LOCATION=${SCRIPT_LOCATION:-$(dirname "$(readlink -f "$0")")}
28SCRIPT_NAME=${SCRIPT_NAME:-$(basename "$0")}
rspangler@google.comd74220d2009-10-09 20:56:14 +000029
Anton Staaf30acb0b2011-01-26 16:00:20 -080030# Detect whether we're inside a chroot or not
31if [ -e /etc/debian_chroot ]
rspangler@google.comd74220d2009-10-09 20:56:14 +000032then
Anton Staaf30acb0b2011-01-26 16:00:20 -080033 INSIDE_CHROOT=1
rspangler@google.comd74220d2009-10-09 20:56:14 +000034else
Anton Staaf30acb0b2011-01-26 16:00:20 -080035 INSIDE_CHROOT=0
rspangler@google.comd74220d2009-10-09 20:56:14 +000036fi
37
Anton Staaf30acb0b2011-01-26 16:00:20 -080038# Construct a list of possible locations for the source tree. This list is
39# based on various environment variables and globals that may have been set
40# by the calling script.
41function get_gclient_root_list() {
42 if [ $INSIDE_CHROOT -eq 1 ]; then
43 echo "/home/${USER}/trunk"
44
45 if [ -n "${SUDO_USER}" ]; then echo "/home/${SUDO_USER}/trunk"; fi
46 fi
47
48 if [ -n "${COMMON_SH}" ]; then echo "$(dirname "$COMMON_SH")/../.."; fi
49 if [ -n "${BASH_SOURCE}" ]; then echo "$(dirname "$BASH_SOURCE")/../.."; fi
50}
51
52# Based on the list of possible source locations we set GCLIENT_ROOT if it is
53# not already defined by looking for a src directory in each seach path
54# location. If we do not find a valid looking root we error out.
55function get_gclient_root() {
56 if [ -n "${GCLIENT_ROOT}" ]; then
57 return
58 fi
59
60 for path in $(get_gclient_root_list); do
61 if [ -d "${path}/src" ]; then
62 GCLIENT_ROOT=${path}
63 break
64 fi
65 done
66
67 if [ -z "${GCLIENT_ROOT}" ]; then
68 # Using dash or sh, we don't know where we are. $0 refers to the calling
69 # script, not ourselves, so that doesn't help us.
70 echo "Unable to determine location for common.sh. If you are sourcing"
71 echo "common.sh from a script run via dash or sh, you must do it in the"
72 echo "following way:"
73 echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
74 echo ' . "$COMMON_SH"'
75 echo "where the first line is the relative path from your script to"
76 echo "common.sh."
77 exit 1
78 fi
79}
80
81# Find root of source tree
82get_gclient_root
83
rspangler@google.comd74220d2009-10-09 20:56:14 +000084# Canonicalize the directories for the root dir and the calling script.
85# readlink is part of coreutils and should be present even in a bare chroot.
tedbo4f44d9e2010-01-08 17:26:11 -080086# This is better than just using
rspangler@google.comd74220d2009-10-09 20:56:14 +000087# FOO = "$(cd $FOO ; pwd)"
tedbo4f44d9e2010-01-08 17:26:11 -080088# since that leaves symbolic links intact.
rspangler@google.comd74220d2009-10-09 20:56:14 +000089# Note that 'realpath' is equivalent to 'readlink -f'.
Greg Spencer798d75f2011-02-01 22:04:49 -080090SCRIPT_LOCATION=$(readlink -f $SCRIPT_LOCATION)
91GCLIENT_ROOT=$(readlink -f $GCLIENT_ROOT)
rspangler@google.comd74220d2009-10-09 20:56:14 +000092
93# Other directories should always be pathed down from GCLIENT_ROOT.
94SRC_ROOT="$GCLIENT_ROOT/src"
95SRC_INTERNAL="$GCLIENT_ROOT/src-internal"
96SCRIPTS_DIR="$SRC_ROOT/scripts"
97
98# Load developer's custom settings. Default location is in scripts dir,
99# since that's available both inside and outside the chroot. By convention,
100# settings from this file are variables starting with 'CHROMEOS_'
101CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}"
Greg Spencer798d75f2011-02-01 22:04:49 -0800102if [ -f $CHROMEOS_DEV_SETTINGS ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000103 # Turn on exit-on-error during custom settings processing
Greg Spencer798d75f2011-02-01 22:04:49 -0800104 SAVE_OPTS=$(set +o)
rspangler@google.comd74220d2009-10-09 20:56:14 +0000105 set -e
106
107 # Read settings
108 . $CHROMEOS_DEV_SETTINGS
109
110 # Restore previous state of exit-on-error
111 eval "$SAVE_OPTS"
112fi
113
114# Load shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -0700115if [[ -f /usr/lib/shflags ]]; then
116 . /usr/lib/shflags
Raja Aluric6e23cd2010-11-02 15:55:57 -0700117elif [ -f ./lib/shflags/shflags ]; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800118 . ./lib/shflags/shflags
Anush Elangovan56de1dd2010-08-02 23:56:07 -0700119else
120 . "${SRC_ROOT}/scripts/lib/shflags/shflags"
121fi
rspangler@google.comd74220d2009-10-09 20:56:14 +0000122
Bill Richardson10d27c22010-01-20 13:38:50 -0800123# Our local mirror
124DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000125
Bill Richardson10d27c22010-01-20 13:38:50 -0800126# Upstream mirrors and build suites come in 2 flavors
127# DEV - development chroot, used to build the chromeos image
128# IMG - bootable image, to run on actual hardware
rspangler@google.comd74220d2009-10-09 20:56:14 +0000129
Bill Richardson10d27c22010-01-20 13:38:50 -0800130DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
131DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
132
133DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
134DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
rspangler@google.comd74220d2009-10-09 20:56:14 +0000135
136# Default location for chroot
137DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"$GCLIENT_ROOT/chroot"}
138
139# All output files from build should go under $DEFAULT_BUILD_ROOT, so that
140# they don't pollute the source directory.
141DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"}
142
David McMahon49302942010-02-18 16:55:35 -0800143# Set up a global ALL_BOARDS value
Raja Alurid44bcf92010-11-11 17:46:53 -0800144if [ -d $SRC_ROOT/overlays ]; then
145 ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g')
David Rochberg3b910702010-12-02 10:45:21 -0500146fi
David McMahon49302942010-02-18 16:55:35 -0800147# Strip CR
148ALL_BOARDS=$(echo $ALL_BOARDS)
149# Set a default BOARD
150#DEFAULT_BOARD=x86-generic # or...
151DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}')
152
David Jamesff072012010-11-30 13:22:05 -0800153# Enable --fast by default.
Greg Spencer798d75f2011-02-01 22:04:49 -0800154DEFAULT_FAST=${FLAGS_TRUE}
David James03668642010-07-28 17:08:29 -0700155
Simon Glass142ca062011-02-09 13:39:43 -0800156
157# Standard filenames
158CHROMEOS_IMAGE_NAME="chromiumos_image.bin"
159CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin"
Chris Sosab885b802011-02-16 15:33:11 -0800160CHROMEOS_FACTORY_TEST_IMAGE_NAME="chromiumos_factory_image.bin"
Simon Glass142ca062011-02-09 13:39:43 -0800161
rspangler@google.comd74220d2009-10-09 20:56:14 +0000162# Directory locations inside the dev chroot
163CHROOT_TRUNK_DIR="/home/$USER/trunk"
164
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700165# Install make for portage ebuilds. Used by build_image and gmergefs.
Chris Masoned11ce172010-11-09 14:22:08 -0800166# TODO: Is /usr/local/autotest-chrome still used by anyone?
Daniel Erate82f07c2010-12-21 13:39:22 -0800167DEFAULT_INSTALL_MASK="
168 *.a
169 *.la
170 /etc/init.d
171 /etc/runlevels
172 /lib/rc
173 /usr/bin/Xnest
174 /usr/bin/Xvfb
175 /usr/include
176 /usr/lib/debug
177 /usr/lib/gcc
178 /usr/lib/gtk-2.0/include
179 /usr/lib/pkgconfig
180 /usr/local/autotest
181 /usr/local/autotest-chrome
182 /usr/man
183 /usr/share/aclocal
184 /usr/share/doc
185 /usr/share/gettext
186 /usr/share/gtk-2.0
187 /usr/share/gtk-doc
188 /usr/share/info
189 /usr/share/man
190 /usr/share/openrc
191 /usr/share/pkgconfig
192 /usr/share/readline
193 "
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700194
Daniel Erate82f07c2010-12-21 13:39:22 -0800195FACTORY_INSTALL_MASK="
196 /opt/Qualcomm
197 /opt/Synaptics
198 /opt/google/chrome
199 /opt/google/o3d
200 /opt/google/talkplugin
201 /opt/netscape
202 /usr/lib/debug
203 /usr/lib/dri
204 /usr/lib/python2.6/test
205 /usr/local/autotest
206 /usr/local/autotest-chrome
207 /usr/local/autotest-pkgs
208 /usr/share/X11
209 /usr/share/chewing
210 /usr/share/fonts
211 /usr/share/ibus-pinyin
212 /usr/share/libhangul
213 /usr/share/locale
214 /usr/share/m17n
215 /usr/share/mime
216 /usr/share/sounds
217 /usr/share/tts
218 /usr/share/zoneinfo
219 "
Tom Wai-Hong Tamf87a3672010-05-17 16:06:33 +0800220
David McMahonb7eb3a22010-02-09 14:07:40 -0800221# Check to ensure not running old scripts
222V_REVERSE=''
223V_VIDOFF=''
224case "$(basename $0)" in
225 build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh)
226 echo
227 echo "$V_REVERSE============================================================"
228 echo "=========================== WARNING ======================"
229 echo "============================================================$V_VIDOFF"
230 echo
231 echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:"
232 echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build"
233 echo
Greg Spencer798d75f2011-02-01 22:04:49 -0800234 if [ "$USER" != "chrome-bot" ]; then
David McMahonb7eb3a22010-02-09 14:07:40 -0800235 read -n1 -p "Press any key to continue using the OLD build system..."
236 echo
237 echo
238 fi
239 ;;
240esac
241
rspangler@google.comd74220d2009-10-09 20:56:14 +0000242# -----------------------------------------------------------------------------
243# Functions
244
Chris Sosaacada732010-02-23 15:20:03 -0800245function setup_board_warning {
tedbo373c3902010-04-12 10:52:40 -0700246 echo
247 echo "$V_REVERSE================= WARNING ======================$V_VIDOFF"
Chris Sosaacada732010-02-23 15:20:03 -0800248 echo
249 echo "*** No default board detected in " \
250 "$GCLIENT_ROOT/src/scripts/.default_board"
251 echo "*** Either run setup_board with default flag set"
252 echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board"
253 echo
254}
255
256
257# Sets the default board variable for calling script
258function get_default_board {
tedbo373c3902010-04-12 10:52:40 -0700259 DEFAULT_BOARD=
260
Chris Sosaacada732010-02-23 15:20:03 -0800261 if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then
Greg Spencer798d75f2011-02-01 22:04:49 -0800262 DEFAULT_BOARD=$(cat "$GCLIENT_ROOT/src/scripts/.default_board")
Chris Sosaacada732010-02-23 15:20:03 -0800263 fi
264}
265
266
rspangler@google.comd74220d2009-10-09 20:56:14 +0000267# Make a package
268function make_pkg_common {
269 # Positional parameters from calling script. :? means "fail if unset".
270 set -e
271 PKG_BASE=${1:?}
272 shift
273 set +e
tedbo4f44d9e2010-01-08 17:26:11 -0800274
rspangler@google.comd74220d2009-10-09 20:56:14 +0000275 # All packages are built in the chroot
276 assert_inside_chroot
277
278 # Command line options
279 DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output"
280
281 # Parse command line and update positional args
282 FLAGS "$@" || exit 1
283 eval set -- "${FLAGS_ARGV}"
284
285 # Die on any errors
286 set -e
287
288 # Make output dir
Greg Spencer798d75f2011-02-01 22:04:49 -0800289 local out_dir="$FLAGS_build_root/x86/local_packages"
290 mkdir -p "$out_dir"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000291
292 # Remove previous package from output dir
Greg Spencer798d75f2011-02-01 22:04:49 -0800293 rm -f "$out_dir"/${PKG_BASE}_*.deb
rspangler@google.comd74220d2009-10-09 20:56:14 +0000294
295 # Rebuild the package
Greg Spencer798d75f2011-02-01 22:04:49 -0800296 pushd "$SCRIPT_LOCATION"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000297 rm -f ../${PKG_BASE}_*.deb
298 dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS
Greg Spencer798d75f2011-02-01 22:04:49 -0800299 mv ../${PKG_BASE}_*.deb "$out_dir"
rspangler@google.comd74220d2009-10-09 20:56:14 +0000300 rm ../${PKG_BASE}_*.changes
301 popd
302}
303
Don Garrett640a0582010-05-04 16:54:28 -0700304# Enter a chroot and restart the current script if needed
305function restart_in_chroot_if_needed {
David Rochberg3b910702010-12-02 10:45:21 -0500306 # NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
Greg Spencer798d75f2011-02-01 22:04:49 -0800307 if [ $INSIDE_CHROOT -ne 1 ]; then
308 local abspath=$(readlink -f "$0")
309 # strip everything up to (and including) /scripts/ from abspath
310 local path_from_scripts="${abspath##*/scripts/}"
Don Garrett640a0582010-05-04 16:54:28 -0700311 exec $SCRIPTS_DIR/enter_chroot.sh -- \
Greg Spencer798d75f2011-02-01 22:04:49 -0800312 "$CHROOT_TRUNK_DIR/src/scripts/$path_from_scripts" "$@"
Don Garrett640a0582010-05-04 16:54:28 -0700313 fi
314}
315
rspangler@google.comd74220d2009-10-09 20:56:14 +0000316# Fail unless we're inside the chroot. This guards against messing up your
317# workstation.
318function assert_inside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800319 if [ $INSIDE_CHROOT -ne 1 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000320 echo "This script must be run inside the chroot. Run this first:"
321 echo " $SCRIPTS_DIR/enter_chroot.sh"
322 exit 1
323 fi
324}
325
326# Fail if we're inside the chroot. This guards against creating or entering
327# nested chroots, among other potential problems.
328function assert_outside_chroot {
Greg Spencer798d75f2011-02-01 22:04:49 -0800329 if [ $INSIDE_CHROOT -ne 0 ]; then
rspangler@google.comd74220d2009-10-09 20:56:14 +0000330 echo "This script must be run outside the chroot."
331 exit 1
332 fi
333}
334
derat@google.com86dcc8e2009-11-21 19:49:49 +0000335function assert_not_root_user {
Greg Spencer798d75f2011-02-01 22:04:49 -0800336 if [ $(id -u) = 0 ]; then
derat@google.com86dcc8e2009-11-21 19:49:49 +0000337 echo "This script must be run as a non-root user."
338 exit 1
339 fi
340}
341
Steve VanDeBogart8174ba02010-01-15 19:45:30 -0800342# 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 () {
Greg Spencer798d75f2011-02-01 22:04:49 -0800405 local i
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700406 for i in $(seq $FLAGS_retries); do
Greg Spencer798d75f2011-02-01 22:04:49 -0800407 echo "Retrying $@"
408 "$@" $EMERGE_JOBS && return 0
Chris Sosaaa1a7fd2010-04-02 14:06:29 -0700409 done
Greg Spencer798d75f2011-02-01 22:04:49 -0800410 "$@" && 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}/*")
Greg Spencer798d75f2011-02-01 22:04:49 -0800458 local symlink
Chris Sosad4455022010-05-20 10:14:06 -0700459 for symlink in ${symlinks}; do
460 echo "Fixing ${symlink}"
461 local target=$(ls -l "${symlink}" | cut -f 2 -d '>')
462 # Trim spaces from target (bashism).
463 target=${target/ /}
464 # Make new target (removes rootfs prefix).
465 new_target=$(echo ${target} | sed "s#${build_root}##")
466
467 echo "Fixing symlink ${symlink}"
468 sudo unlink "${symlink}"
469 sudo ln -sf "${new_target}" "${symlink}"
470 done
471}
472
Chris Sosa702618f2010-05-14 12:52:32 -0700473# Sets up symlinks for the developer root. It is necessary to symlink
474# usr and local since the developer root is mounted at /usr/local and
475# applications expect to be installed under /usr/local/bin, etc.
476# This avoids packages installing into /usr/local/usr/local/bin.
477# ${1} specifies the symlink target for the developer root.
478# ${2} specifies the symlink target for the var directory.
479# ${3} specifies the location of the stateful partition.
480setup_symlinks_on_root() {
481 # Give args better names.
482 local dev_image_target=${1}
483 local var_target=${2}
484 local dev_image_root="${3}/dev_image"
485
486 # If our var target is actually the standard var, we are cleaning up the
487 # symlinks (could also check for /usr/local for the dev_image_target).
488 if [ ${var_target} = "/var" ]; then
489 echo "Cleaning up /usr/local symlinks for ${dev_image_root}"
490 else
491 echo "Setting up symlinks for /usr/local for ${dev_image_root}"
492 fi
493
494 # Set up symlinks that should point to ${dev_image_target}.
Greg Spencer798d75f2011-02-01 22:04:49 -0800495 local path
Chris Sosa702618f2010-05-14 12:52:32 -0700496 for path in usr local; do
497 if [ -h "${dev_image_root}/${path}" ]; then
498 sudo unlink "${dev_image_root}/${path}"
499 elif [ -e "${dev_image_root}/${path}" ]; then
500 die "${dev_image_root}/${path} should be a symlink if exists"
501 fi
502 sudo ln -s ${dev_image_target} "${dev_image_root}/${path}"
503 done
504
505 # Setup var symlink.
506 if [ -h "${dev_image_root}/var" ]; then
507 sudo unlink "${dev_image_root}/var"
508 elif [ -e "${dev_image_root}/var" ]; then
509 die "${dev_image_root}/var should be a symlink if it exists"
510 fi
511
512 sudo ln -s "${var_target}" "${dev_image_root}/var"
513}
Nick Sandersd2509272010-06-16 03:50:04 -0700514
Will Drewry55b42c92010-10-20 15:44:11 -0500515# These two helpers clobber the ro compat value in our root filesystem.
516#
517# When the system is built with --enable_rootfs_verification, bit-precise
518# integrity checking is performed. That precision poses a usability issue on
519# systems that automount partitions with recognizable filesystems, such as
520# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
521# automatically updated even if no other writes are performed to the
522# filesystem. In addition, ext2+ does not support a "read-only" flag for a
523# given filesystem. That said, forward and backward compatibility of
524# filesystem features are supported by tracking if a new feature breaks r/w or
525# just write compatibility. We abuse the read-only compatibility flag[1] in
526# the filesystem header by setting the high order byte (le) to FF. This tells
527# the kernel that features R24-R31 are all enabled. Since those features are
528# undefined on all ext-based filesystem, all standard kernels will refuse to
529# mount the filesystem as read-write -- only read-only[2].
530#
531# [1] 32-bit flag we are modifying:
532# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
533# [2] Mount behavior is enforced here:
534# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
535#
536# N.B., if the high order feature bits are used in the future, we will need to
537# revisit this technique.
538disable_rw_mount() {
539 local rootfs="$1"
540 local offset="${2-0}" # in bytes
Will Drewry9b7cb512010-10-20 18:11:24 -0500541 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
542 printf '\377' |
Will Drewry55b42c92010-10-20 15:44:11 -0500543 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
544 conv=notrunc count=1 bs=1
545}
546
547enable_rw_mount() {
548 local rootfs="$1"
549 local offset="${2-0}"
Will Drewry9b7cb512010-10-20 18:11:24 -0500550 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
551 printf '\000' |
Will Drewry55b42c92010-10-20 15:44:11 -0500552 sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \
553 conv=notrunc count=1 bs=1
554}
555
Nick Sandersd2509272010-06-16 03:50:04 -0700556# Get current timestamp. Assumes common.sh runs at startup.
557start_time=$(date +%s)
558
559# Print time elsapsed since start_time.
560print_time_elapsed() {
Greg Spencer798d75f2011-02-01 22:04:49 -0800561 local end_time=$(date +%s)
562 local elapsed_seconds=$(($end_time - $start_time))
563 local minutes=$(($elapsed_seconds / 60))
564 local seconds=$(($elapsed_seconds % 60))
Olof Johansson6d491382010-08-09 16:05:50 -0500565 echo "Elapsed time: ${minutes}m${seconds}s"
Nick Sandersd2509272010-06-16 03:50:04 -0700566}
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700567
568# This function is a place to put code to incrementally update the
569# chroot so that users don't need to fully re-make it. It should
570# be called from scripts that are run _outside_ the chroot.
571#
572# Please put date information so it's easy to keep track of when
573# old hacks can be retired and so that people can detect when a
574# hack triggered when it shouldn't have.
575#
576# ${1} specifies the location of the chroot.
577chroot_hacks_from_outside() {
578 # Give args better names.
Greg Spencer798d75f2011-02-01 22:04:49 -0800579 local chroot_dir=$1
Doug Anderson0c9e88d2010-10-19 14:49:39 -0700580
581 # Add root as a sudoer if not already done.
582 if ! sudo grep -q '^root ALL=(ALL) ALL$' "${chroot_dir}/etc/sudoers" ; then
583 info "Upgrading old chroot (pre 2010-10-19) - adding root to sudoers"
584 sudo bash -c "echo root ALL=\(ALL\) ALL >> \"${chroot_dir}/etc/sudoers\""
585 fi
586}
Anton Staaf9bcd8412011-01-24 15:27:14 -0800587
588# The board and variant command line options can be used in a number of ways
589# to specify the board and variant. The board can encode both pieces of
590# information separated by underscores. Or the variant can be passed using
591# the separate variant option. This function extracts the canonical board and
592# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
593# variables.
594get_board_and_variant() {
595 local flags_board="${1}"
596 local flags_variant="${2}"
597
598 BOARD=$(echo "$flags_board" | cut -d '_' -f 1)
599 VARIANT=${flags_variant:-$(echo "$flags_board" | cut -s -d '_' -f 2)}
600
601 if [ -n "$VARIANT" ]; then
602 BOARD_VARIANT="${BOARD}_${VARIANT}"
603 else
604 BOARD_VARIANT="${BOARD}"
605 fi
606}
Simon Glass142ca062011-02-09 13:39:43 -0800607
608# This function converts a chromiumos image into a test image, either
609# in place or by copying to a new test image filename first. It honors
610# the following flags (see mod_image_for_test.sh)
611#
612# --factory
613# --factory_install
614# --force_copy
615#
616# On entry, pass the directory containing the image, and the image filename
617# On exit, it puts the pathname of the resulting test image into
618# CHROMEOS_RETURN_VAL
619# (yes this is ugly, but perhaps less ugly than the alternatives)
620#
621# Usage:
622# SRC_IMAGE=$(prepare_test_image "directory" "imagefile")
623prepare_test_image() {
624 # If we're asked to modify the image for test, then let's make a copy and
625 # modify that instead.
626 # Check for manufacturing image.
627 local args
628
629 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then
630 args="--factory"
631 fi
632
633 # Check for install shim.
634 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then
635 args="--factory_install"
636 fi
637
638 # Check for forcing copy of image
639 if [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ]; then
640 args="${args} --force_copy"
641 fi
642
643 # Modify the image for test, creating a new test image
644 "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} \
645 --image="$1/$2" --noinplace ${args}
646
647 # From now on we use the just-created test image
648 CHROMEOS_RETURN_VAL="$1/${CHROMEOS_TEST_IMAGE_NAME}"
649}