David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 1 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 2 | # 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 Marinichev | 63c42aa | 2009-12-21 11:42:39 -0800 | [diff] [blame] | 15 | NUM_JOBS=`grep -c "^processor" /proc/cpuinfo` |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 16 | |
| 17 | # Store location of the calling script. |
| 18 | TOP_SCRIPT_DIR="${TOP_SCRIPT_DIR:-$(dirname $0)}" |
| 19 | |
| 20 | # Find root of source tree |
| 21 | if [ "x$GCLIENT_ROOT" != "x" ] |
| 22 | then |
| 23 | # GCLIENT_ROOT already set, so we're done |
| 24 | true |
| 25 | elif [ "x$COMMON_SH" != "x" ] |
| 26 | then |
| 27 | # COMMON_SH set, so assume that's us |
| 28 | GCLIENT_ROOT="$(dirname "$COMMON_SH")/../.." |
| 29 | elif [ "x$BASH_SOURCE" != "x" ] |
| 30 | then |
| 31 | # Using bash, so we can find ourselves |
| 32 | GCLIENT_ROOT="$(dirname "$BASH_SOURCE")/../.." |
| 33 | else |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 34 | # Using dash or sh, we don't know where we are. $0 refers to the calling |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 35 | # 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 |
| 44 | fi |
| 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. |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 48 | # This is better than just using |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 49 | # FOO = "$(cd $FOO ; pwd)" |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 50 | # since that leaves symbolic links intact. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 51 | # Note that 'realpath' is equivalent to 'readlink -f'. |
| 52 | TOP_SCRIPT_DIR=`readlink -f $TOP_SCRIPT_DIR` |
| 53 | GCLIENT_ROOT=`readlink -f $GCLIENT_ROOT` |
| 54 | |
| 55 | # Other directories should always be pathed down from GCLIENT_ROOT. |
| 56 | SRC_ROOT="$GCLIENT_ROOT/src" |
| 57 | SRC_INTERNAL="$GCLIENT_ROOT/src-internal" |
| 58 | SCRIPTS_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_' |
| 63 | CHROMEOS_DEV_SETTINGS="${CHROMEOS_DEV_SETTINGS:-$SCRIPTS_DIR/.chromeos_dev}" |
| 64 | if [ -f $CHROMEOS_DEV_SETTINGS ] |
| 65 | then |
| 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" |
| 75 | fi |
| 76 | |
| 77 | # Load shflags |
Anush Elangovan | 56de1dd | 2010-08-02 23:56:07 -0700 | [diff] [blame] | 78 | if [[ -f /usr/lib/shflags ]]; then |
| 79 | . /usr/lib/shflags |
Raja Aluri | c6e23cd | 2010-11-02 15:55:57 -0700 | [diff] [blame] | 80 | elif [ -f ./lib/shflags/shflags ]; then |
| 81 | . "./lib/shflags/shflags" |
Anush Elangovan | 56de1dd | 2010-08-02 23:56:07 -0700 | [diff] [blame] | 82 | else |
| 83 | . "${SRC_ROOT}/scripts/lib/shflags/shflags" |
| 84 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 85 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 86 | # Our local mirror |
| 87 | DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 88 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 89 | # 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.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 92 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 93 | DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 94 | DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"} |
| 95 | |
| 96 | DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 97 | DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 98 | |
| 99 | # Default location for chroot |
| 100 | DEFAULT_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. |
| 104 | DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"$SRC_ROOT/build"} |
| 105 | |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 106 | # Set up a global ALL_BOARDS value |
Sam Leffler | a92ecd6 | 2010-02-23 08:25:38 -0800 | [diff] [blame] | 107 | ALL_BOARDS=$(cd $SRC_ROOT/overlays;ls -1d overlay-* 2>&-|sed 's,overlay-,,g') |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 108 | # Strip CR |
| 109 | ALL_BOARDS=$(echo $ALL_BOARDS) |
| 110 | # Set a default BOARD |
| 111 | #DEFAULT_BOARD=x86-generic # or... |
| 112 | DEFAULT_BOARD=$(echo $ALL_BOARDS | awk '{print $NF}') |
| 113 | |
David James | 0366864 | 2010-07-28 17:08:29 -0700 | [diff] [blame] | 114 | # Enable --fast by default on non-official builds |
| 115 | DEFAULT_FAST="${FLAGS_TRUE}" |
| 116 | if [ "${CHROMEOS_OFFICIAL:-0}" = "1" ]; then |
| 117 | DEFAULT_FAST="${FLAGS_FALSE}" |
| 118 | fi |
| 119 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 120 | # Detect whether we're inside a chroot or not |
| 121 | if [ -e /etc/debian_chroot ] |
| 122 | then |
| 123 | INSIDE_CHROOT=1 |
| 124 | else |
| 125 | INSIDE_CHROOT=0 |
| 126 | fi |
| 127 | |
| 128 | # Directory locations inside the dev chroot |
| 129 | CHROOT_TRUNK_DIR="/home/$USER/trunk" |
| 130 | |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 131 | # Install make for portage ebuilds. Used by build_image and gmergefs. |
Chris Masone | d11ce17 | 2010-11-09 14:22:08 -0800 | [diff] [blame^] | 132 | # TODO: Is /usr/local/autotest-chrome still used by anyone? |
Chris Sosa | 3d9a10b | 2010-04-13 15:00:46 -0700 | [diff] [blame] | 133 | DEFAULT_INSTALL_MASK="/usr/include /usr/man /usr/share/man /usr/share/doc \ |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 134 | /usr/share/gtk-doc /usr/share/gtk-2.0 /usr/lib/gtk-2.0/include \ |
| 135 | /usr/share/info /usr/share/aclocal /usr/lib/gcc /usr/lib/pkgconfig \ |
| 136 | /usr/share/pkgconfig /usr/share/gettext /usr/share/readline /etc/runlevels \ |
David James | 71fa14b | 2010-08-11 16:15:16 -0700 | [diff] [blame] | 137 | /usr/share/openrc /lib/rc *.a *.la /etc/init.d /usr/lib/debug |
| 138 | /usr/local/autotest /usr/local/autotest-chrome" |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 139 | |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 140 | FACTORY_INSTALL_MASK="/opt/google/chrome /opt/google/o3d /opt/netscape \ |
Nick Sanders | b3d04a0 | 2010-09-09 00:01:24 -0700 | [diff] [blame] | 141 | /opt/google/talkplugin /opt/Qualcomm /opt/Synaptics \ |
| 142 | /usr/lib/dri /usr/lib/python2.6/test \ |
| 143 | /usr/share/chewing /usr/share/fonts \ |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 144 | /usr/share/ibus-pinyin /usr/share/libhangul /usr/share/locale \ |
| 145 | /usr/share/m17n /usr/share/mime /usr/share/sounds /usr/share/tts \ |
David James | 71fa14b | 2010-08-11 16:15:16 -0700 | [diff] [blame] | 146 | /usr/share/X11 /usr/share/zoneinfo /usr/lib/debug |
Chris Masone | d11ce17 | 2010-11-09 14:22:08 -0800 | [diff] [blame^] | 147 | /usr/local/autotest /usr/local/autotest-chrome /usr/local/autotest-pkgs" |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 148 | |
David McMahon | b7eb3a2 | 2010-02-09 14:07:40 -0800 | [diff] [blame] | 149 | # Check to ensure not running old scripts |
| 150 | V_REVERSE='[7m' |
| 151 | V_VIDOFF='[m' |
| 152 | case "$(basename $0)" in |
| 153 | build_image.sh|build_platform_packages.sh|customize_rootfs.sh|make_chroot.sh) |
| 154 | echo |
| 155 | echo "$V_REVERSE============================================================" |
| 156 | echo "=========================== WARNING ======================" |
| 157 | echo "============================================================$V_VIDOFF" |
| 158 | echo |
| 159 | echo "RUNNING OLD BUILD SYSTEM SCRIPTS. RUN THE PORTAGE-BASED BUILD HERE:" |
| 160 | echo "http://www.chromium.org/chromium-os/building-chromium-os/portage-based-build" |
| 161 | echo |
| 162 | if [ "$USER" != "chrome-bot" ] |
| 163 | then |
| 164 | read -n1 -p "Press any key to continue using the OLD build system..." |
| 165 | echo |
| 166 | echo |
| 167 | fi |
| 168 | ;; |
| 169 | esac |
| 170 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 171 | # ----------------------------------------------------------------------------- |
| 172 | # Functions |
| 173 | |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 174 | function setup_board_warning { |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 175 | echo |
| 176 | echo "$V_REVERSE================= WARNING ======================$V_VIDOFF" |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 177 | echo |
| 178 | echo "*** No default board detected in " \ |
| 179 | "$GCLIENT_ROOT/src/scripts/.default_board" |
| 180 | echo "*** Either run setup_board with default flag set" |
| 181 | echo "*** or echo |board_name| > $GCLIENT_ROOT/src/scripts/.default_board" |
| 182 | echo |
| 183 | } |
| 184 | |
| 185 | |
| 186 | # Sets the default board variable for calling script |
| 187 | function get_default_board { |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 188 | DEFAULT_BOARD= |
| 189 | |
Chris Sosa | acada73 | 2010-02-23 15:20:03 -0800 | [diff] [blame] | 190 | if [ -f "$GCLIENT_ROOT/src/scripts/.default_board" ] ; then |
| 191 | DEFAULT_BOARD=`cat "$GCLIENT_ROOT/src/scripts/.default_board"` |
| 192 | fi |
| 193 | } |
| 194 | |
| 195 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 196 | # Make a package |
| 197 | function make_pkg_common { |
| 198 | # Positional parameters from calling script. :? means "fail if unset". |
| 199 | set -e |
| 200 | PKG_BASE=${1:?} |
| 201 | shift |
| 202 | set +e |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 203 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 204 | # All packages are built in the chroot |
| 205 | assert_inside_chroot |
| 206 | |
| 207 | # Command line options |
| 208 | DEFINE_string build_root "$DEFAULT_BUILD_ROOT" "Root of build output" |
| 209 | |
| 210 | # Parse command line and update positional args |
| 211 | FLAGS "$@" || exit 1 |
| 212 | eval set -- "${FLAGS_ARGV}" |
| 213 | |
| 214 | # Die on any errors |
| 215 | set -e |
| 216 | |
| 217 | # Make output dir |
| 218 | OUT_DIR="$FLAGS_build_root/x86/local_packages" |
| 219 | mkdir -p "$OUT_DIR" |
| 220 | |
| 221 | # Remove previous package from output dir |
| 222 | rm -f "$OUT_DIR"/${PKG_BASE}_*.deb |
| 223 | |
| 224 | # Rebuild the package |
| 225 | pushd "$TOP_SCRIPT_DIR" |
| 226 | rm -f ../${PKG_BASE}_*.deb |
| 227 | dpkg-buildpackage -b -tc -us -uc -j$NUM_JOBS |
| 228 | mv ../${PKG_BASE}_*.deb "$OUT_DIR" |
| 229 | rm ../${PKG_BASE}_*.changes |
| 230 | popd |
| 231 | } |
| 232 | |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 233 | # Enter a chroot and restart the current script if needed |
| 234 | function restart_in_chroot_if_needed { |
| 235 | if [ $INSIDE_CHROOT -ne 1 ] |
| 236 | then |
| 237 | # Equivalent to enter_chroot.sh -- <current command> |
| 238 | exec $SCRIPTS_DIR/enter_chroot.sh -- \ |
| 239 | $CHROOT_TRUNK_DIR/src/scripts/$(basename $0) $* |
| 240 | fi |
| 241 | } |
| 242 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 243 | # Fail unless we're inside the chroot. This guards against messing up your |
| 244 | # workstation. |
| 245 | function assert_inside_chroot { |
| 246 | if [ $INSIDE_CHROOT -ne 1 ] |
| 247 | then |
| 248 | echo "This script must be run inside the chroot. Run this first:" |
| 249 | echo " $SCRIPTS_DIR/enter_chroot.sh" |
| 250 | exit 1 |
| 251 | fi |
| 252 | } |
| 253 | |
| 254 | # Fail if we're inside the chroot. This guards against creating or entering |
| 255 | # nested chroots, among other potential problems. |
| 256 | function assert_outside_chroot { |
| 257 | if [ $INSIDE_CHROOT -ne 0 ] |
| 258 | then |
| 259 | echo "This script must be run outside the chroot." |
| 260 | exit 1 |
| 261 | fi |
| 262 | } |
| 263 | |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 264 | function assert_not_root_user { |
| 265 | if [ `id -u` = 0 ]; then |
| 266 | echo "This script must be run as a non-root user." |
| 267 | exit 1 |
| 268 | fi |
| 269 | } |
| 270 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 271 | # Install a package if it's not already installed |
| 272 | function install_if_missing { |
| 273 | # Positional parameters from calling script. :? means "fail if unset". |
| 274 | PKG_NAME=${1:?} |
| 275 | shift |
| 276 | |
| 277 | if [ -z `which $PKG_NAME` ] |
| 278 | then |
| 279 | echo "Can't find $PKG_NAME; attempting to install it." |
| 280 | sudo apt-get --yes --force-yes install $PKG_NAME |
| 281 | fi |
| 282 | } |
Steve VanDeBogart | 8174ba0 | 2010-01-15 19:45:30 -0800 | [diff] [blame] | 283 | |
| 284 | # Returns true if the input file is whitelisted. |
| 285 | # |
| 286 | # $1 - The file to check |
| 287 | is_whitelisted() { |
David McMahon | b7eb3a2 | 2010-02-09 14:07:40 -0800 | [diff] [blame] | 288 | local file=$1 |
Steve VanDeBogart | 8174ba0 | 2010-01-15 19:45:30 -0800 | [diff] [blame] | 289 | local whitelist="$FLAGS_whitelist" |
| 290 | test -f "$whitelist" || (echo "Whitelist file missing ($whitelist)" && exit 1) |
| 291 | |
| 292 | local checksum=$(md5sum "$file" | awk '{ print $1 }') |
| 293 | local count=$(sed -e "s/#.*$//" "${whitelist}" | grep -c "$checksum" \ |
| 294 | || /bin/true) |
| 295 | test $count -ne 0 |
| 296 | } |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 297 | |
Luigi Semenzato | 1f82e12 | 2010-03-23 12:43:08 -0700 | [diff] [blame] | 298 | # Check that all arguments are flags; that is, there are no remaining arguments |
| 299 | # after parsing from shflags. Allow (with a warning) a single empty-string |
| 300 | # argument. |
| 301 | # |
| 302 | # TODO: fix buildbot so that it doesn't pass the empty-string parameter, |
| 303 | # then change this function. |
| 304 | # |
| 305 | # Usage: check_flags_only_and_allow_null_arg "$@" && set -- |
| 306 | function check_flags_only_and_allow_null_arg { |
| 307 | do_shift=1 |
| 308 | if [[ $# == 1 && -z "$@" ]]; then |
| 309 | echo "$0: warning: ignoring null argument" >&2 |
| 310 | shift |
| 311 | do_shift=0 |
| 312 | fi |
| 313 | if [[ $# -gt 0 ]]; then |
| 314 | echo "error: invalid arguments: \"$@\"" >&2 |
| 315 | flags_help |
| 316 | exit 1 |
| 317 | fi |
| 318 | return $do_shift |
| 319 | } |
| 320 | |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 321 | V_RED="\e[31m" |
| 322 | V_YELLOW="\e[33m" |
Chris Sosa | d979852 | 2010-06-16 14:29:50 -0700 | [diff] [blame] | 323 | V_BOLD_GREEN="\e[1;32m" |
Darin Petkov | b2a21e7 | 2010-03-25 11:33:41 -0700 | [diff] [blame] | 324 | V_BOLD_RED="\e[1;31m" |
| 325 | V_BOLD_YELLOW="\e[1;33m" |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 326 | |
Chris Sosa | d979852 | 2010-06-16 14:29:50 -0700 | [diff] [blame] | 327 | function info { |
Sean O'Connor | d52c5ea | 2010-08-18 21:18:23 +0200 | [diff] [blame] | 328 | echo -e >&2 "${V_BOLD_GREEN}INFO : $1${V_VIDOFF}" |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 329 | } |
Chris Sosa | d979852 | 2010-06-16 14:29:50 -0700 | [diff] [blame] | 330 | |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 331 | function warn { |
Sean O'Connor | d52c5ea | 2010-08-18 21:18:23 +0200 | [diff] [blame] | 332 | echo -e >&2 "${V_BOLD_YELLOW}WARNING: $1${V_VIDOFF}" |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | function error { |
Sean O'Connor | d52c5ea | 2010-08-18 21:18:23 +0200 | [diff] [blame] | 336 | echo -e >&2 "${V_BOLD_RED}ERROR : $1${V_VIDOFF}" |
robotboy | 2ea03ac | 2010-02-11 15:30:55 -0800 | [diff] [blame] | 337 | } |
David James | 546747b | 2010-03-23 15:19:43 -0700 | [diff] [blame] | 338 | |
| 339 | function die { |
| 340 | error "$1" |
| 341 | exit 1 |
| 342 | } |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 343 | |
| 344 | # Retry an emerge command according to $FLAGS_retries |
David James | fd7403a | 2010-07-09 12:00:17 -0700 | [diff] [blame] | 345 | # The $EMERGE_JOBS flags will only be added the first time the command is run |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 346 | function eretry () { |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 347 | local i= |
| 348 | for i in $(seq $FLAGS_retries); do |
| 349 | echo Retrying $* |
David James | fd7403a | 2010-07-09 12:00:17 -0700 | [diff] [blame] | 350 | $* $EMERGE_JOBS && return 0 |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 351 | done |
David James | fd7403a | 2010-07-09 12:00:17 -0700 | [diff] [blame] | 352 | $* && return 0 |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 353 | return 1 |
| 354 | } |
| 355 | |
| 356 | # Removes single quotes around parameter |
| 357 | # Arguments: |
| 358 | # $1 - string which optionally has surrounding quotes |
| 359 | # Returns: |
| 360 | # None, but prints the string without quotes. |
| 361 | function remove_quotes() { |
| 362 | echo "$1" | sed -e "s/^'//; s/'$//" |
| 363 | } |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 364 | |
| 365 | # Writes stdin to the given file name as root using sudo in overwrite mode. |
| 366 | # |
| 367 | # $1 - The output file name. |
| 368 | function sudo_clobber() { |
| 369 | sudo tee "$1" > /dev/null |
| 370 | } |
| 371 | |
| 372 | # Writes stdin to the given file name as root using sudo in append mode. |
| 373 | # |
| 374 | # $1 - The output file name. |
| 375 | function sudo_append() { |
| 376 | sudo tee -a "$1" > /dev/null |
| 377 | } |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 378 | |
| 379 | # Unmounts a directory, if the unmount fails, warn, and then lazily unmount. |
| 380 | # |
| 381 | # $1 - The path to unmount. |
| 382 | function safe_umount { |
| 383 | path=${1:?} |
| 384 | shift |
| 385 | |
| 386 | if ! sudo umount -d "${path}"; then |
| 387 | warn "Failed to unmount ${path}" |
| 388 | warn "Doing a lazy unmount" |
| 389 | |
| 390 | sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}" |
| 391 | fi |
| 392 | } |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 393 | |
Chris Sosa | d445502 | 2010-05-20 10:14:06 -0700 | [diff] [blame] | 394 | # Fixes symlinks that are incorrectly prefixed with the build root ${1} |
| 395 | # rather than the real running root '/'. |
| 396 | # TODO(sosa) - Merge setup - cleanup below with this method. |
| 397 | fix_broken_symlinks() { |
| 398 | local build_root="${1}" |
| 399 | local symlinks=$(find "${build_root}/usr/local" -lname "${build_root}/*") |
| 400 | for symlink in ${symlinks}; do |
| 401 | echo "Fixing ${symlink}" |
| 402 | local target=$(ls -l "${symlink}" | cut -f 2 -d '>') |
| 403 | # Trim spaces from target (bashism). |
| 404 | target=${target/ /} |
| 405 | # Make new target (removes rootfs prefix). |
| 406 | new_target=$(echo ${target} | sed "s#${build_root}##") |
| 407 | |
| 408 | echo "Fixing symlink ${symlink}" |
| 409 | sudo unlink "${symlink}" |
| 410 | sudo ln -sf "${new_target}" "${symlink}" |
| 411 | done |
| 412 | } |
| 413 | |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 414 | # Sets up symlinks for the developer root. It is necessary to symlink |
| 415 | # usr and local since the developer root is mounted at /usr/local and |
| 416 | # applications expect to be installed under /usr/local/bin, etc. |
| 417 | # This avoids packages installing into /usr/local/usr/local/bin. |
| 418 | # ${1} specifies the symlink target for the developer root. |
| 419 | # ${2} specifies the symlink target for the var directory. |
| 420 | # ${3} specifies the location of the stateful partition. |
| 421 | setup_symlinks_on_root() { |
| 422 | # Give args better names. |
| 423 | local dev_image_target=${1} |
| 424 | local var_target=${2} |
| 425 | local dev_image_root="${3}/dev_image" |
| 426 | |
| 427 | # If our var target is actually the standard var, we are cleaning up the |
| 428 | # symlinks (could also check for /usr/local for the dev_image_target). |
| 429 | if [ ${var_target} = "/var" ]; then |
| 430 | echo "Cleaning up /usr/local symlinks for ${dev_image_root}" |
| 431 | else |
| 432 | echo "Setting up symlinks for /usr/local for ${dev_image_root}" |
| 433 | fi |
| 434 | |
| 435 | # Set up symlinks that should point to ${dev_image_target}. |
| 436 | for path in usr local; do |
| 437 | if [ -h "${dev_image_root}/${path}" ]; then |
| 438 | sudo unlink "${dev_image_root}/${path}" |
| 439 | elif [ -e "${dev_image_root}/${path}" ]; then |
| 440 | die "${dev_image_root}/${path} should be a symlink if exists" |
| 441 | fi |
| 442 | sudo ln -s ${dev_image_target} "${dev_image_root}/${path}" |
| 443 | done |
| 444 | |
| 445 | # Setup var symlink. |
| 446 | if [ -h "${dev_image_root}/var" ]; then |
| 447 | sudo unlink "${dev_image_root}/var" |
| 448 | elif [ -e "${dev_image_root}/var" ]; then |
| 449 | die "${dev_image_root}/var should be a symlink if it exists" |
| 450 | fi |
| 451 | |
| 452 | sudo ln -s "${var_target}" "${dev_image_root}/var" |
| 453 | } |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 454 | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 455 | # These two helpers clobber the ro compat value in our root filesystem. |
| 456 | # |
| 457 | # When the system is built with --enable_rootfs_verification, bit-precise |
| 458 | # integrity checking is performed. That precision poses a usability issue on |
| 459 | # systems that automount partitions with recognizable filesystems, such as |
| 460 | # ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be |
| 461 | # automatically updated even if no other writes are performed to the |
| 462 | # filesystem. In addition, ext2+ does not support a "read-only" flag for a |
| 463 | # given filesystem. That said, forward and backward compatibility of |
| 464 | # filesystem features are supported by tracking if a new feature breaks r/w or |
| 465 | # just write compatibility. We abuse the read-only compatibility flag[1] in |
| 466 | # the filesystem header by setting the high order byte (le) to FF. This tells |
| 467 | # the kernel that features R24-R31 are all enabled. Since those features are |
| 468 | # undefined on all ext-based filesystem, all standard kernels will refuse to |
| 469 | # mount the filesystem as read-write -- only read-only[2]. |
| 470 | # |
| 471 | # [1] 32-bit flag we are modifying: |
| 472 | # http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417 |
| 473 | # [2] Mount behavior is enforced here: |
| 474 | # http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857 |
| 475 | # |
| 476 | # N.B., if the high order feature bits are used in the future, we will need to |
| 477 | # revisit this technique. |
| 478 | disable_rw_mount() { |
| 479 | local rootfs="$1" |
| 480 | local offset="${2-0}" # in bytes |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 481 | local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 482 | printf '\377' | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 483 | sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \ |
| 484 | conv=notrunc count=1 bs=1 |
| 485 | } |
| 486 | |
| 487 | enable_rw_mount() { |
| 488 | local rootfs="$1" |
| 489 | local offset="${2-0}" |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 490 | local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 491 | printf '\000' | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 492 | sudo dd of="$rootfs" seek=$((offset + ro_compat_offset)) \ |
| 493 | conv=notrunc count=1 bs=1 |
| 494 | } |
| 495 | |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 496 | # Get current timestamp. Assumes common.sh runs at startup. |
| 497 | start_time=$(date +%s) |
| 498 | |
| 499 | # Print time elsapsed since start_time. |
| 500 | print_time_elapsed() { |
| 501 | end_time=$(date +%s) |
| 502 | elapsed_seconds="$(( $end_time - $start_time ))" |
| 503 | minutes="$(( $elapsed_seconds / 60 ))" |
| 504 | seconds="$(( $elapsed_seconds % 60 ))" |
Olof Johansson | 6d49138 | 2010-08-09 16:05:50 -0500 | [diff] [blame] | 505 | echo "Elapsed time: ${minutes}m${seconds}s" |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 506 | } |
Doug Anderson | 0c9e88d | 2010-10-19 14:49:39 -0700 | [diff] [blame] | 507 | |
| 508 | # This function is a place to put code to incrementally update the |
| 509 | # chroot so that users don't need to fully re-make it. It should |
| 510 | # be called from scripts that are run _outside_ the chroot. |
| 511 | # |
| 512 | # Please put date information so it's easy to keep track of when |
| 513 | # old hacks can be retired and so that people can detect when a |
| 514 | # hack triggered when it shouldn't have. |
| 515 | # |
| 516 | # ${1} specifies the location of the chroot. |
| 517 | chroot_hacks_from_outside() { |
| 518 | # Give args better names. |
| 519 | local chroot_dir="${1}" |
| 520 | |
| 521 | # Add root as a sudoer if not already done. |
| 522 | if ! sudo grep -q '^root ALL=(ALL) ALL$' "${chroot_dir}/etc/sudoers" ; then |
| 523 | info "Upgrading old chroot (pre 2010-10-19) - adding root to sudoers" |
| 524 | sudo bash -c "echo root ALL=\(ALL\) ALL >> \"${chroot_dir}/etc/sudoers\"" |
| 525 | fi |
| 526 | } |