Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 1 | #!/bin/bash |
Chris Sosa | 2b73600 | 2012-02-12 16:16:08 -0800 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 6 | # All scripts should die on error unless commands are specifically excepted |
| 7 | # by prefixing with '!' or surrounded by 'set +e' / 'set -e'. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 8 | |
| 9 | # The number of jobs to pass to tools that can run in parallel (such as make |
| 10 | # and dpkg-buildpackage |
Alex Deymo | 135faa1 | 2015-01-21 11:20:13 -0800 | [diff] [blame] | 11 | if [[ -z ${NUM_JOBS:-} ]]; then |
Brian Harring | 28bb01f | 2012-05-09 15:26:09 -0700 | [diff] [blame] | 12 | NUM_JOBS=$(grep -c "^processor" /proc/cpuinfo) |
| 13 | fi |
| 14 | # Ensure that any sub scripts we invoke get the max proc count. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 15 | export NUM_JOBS |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 16 | |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 17 | # Make sure we have the location and name of the calling script, using |
| 18 | # the current value if it is already set. |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 19 | : ${SCRIPT_LOCATION:=$(dirname "$(readlink -f -- "$0")")} |
| 20 | : ${SCRIPT_NAME:=$(basename -- "$0")} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 21 | |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 22 | # Detect whether we're inside a chroot or not |
David James | 47e0e8a | 2015-03-11 15:24:10 -0700 | [diff] [blame] | 23 | CHROOT_VERSION_FILE=/etc/cros_chroot_version |
| 24 | if [[ -e ${CHROOT_VERSION_FILE} ]]; then |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 25 | INSIDE_CHROOT=1 |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 26 | else |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 27 | INSIDE_CHROOT=0 |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 28 | fi |
| 29 | |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 30 | # Determine and set up variables needed for fancy color output (if supported). |
| 31 | V_BOLD_RED= |
| 32 | V_BOLD_GREEN= |
| 33 | V_BOLD_YELLOW= |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 34 | V_VIDOFF= |
| 35 | |
Mike Frysinger | 8e477cd | 2019-08-21 23:59:12 -0400 | [diff] [blame] | 36 | if [[ -t 1 ]]; then |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 37 | # order matters: we want VIDOFF last so that when we trace with `set -x`, |
| 38 | # our terminal doesn't bleed colors as bash dumps the values of vars. |
Mike Frysinger | 8e477cd | 2019-08-21 23:59:12 -0400 | [diff] [blame] | 39 | V_BOLD_RED=$'\e[1;31m' |
| 40 | V_BOLD_GREEN=$'\e[1;32m' |
| 41 | V_BOLD_YELLOW=$'\e[1;33m' |
| 42 | V_VIDOFF=$'\e[m' |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 43 | fi |
| 44 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 45 | # Turn on bash debug support if available for backtraces. |
| 46 | shopt -s extdebug 2>/dev/null |
Sergey Frolov | 0ca9602 | 2021-02-09 10:19:00 -0700 | [diff] [blame] | 47 | set +E |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 48 | |
Marc Herbert | ff9fa31 | 2015-01-16 17:18:53 -0800 | [diff] [blame] | 49 | # Output a backtrace. Optional parameter allows hiding the last |
| 50 | # frame(s) so functions like "die()" can hide their additional |
| 51 | # frame(s) if they wish. |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 52 | _dump_trace() { |
Marc Herbert | ff9fa31 | 2015-01-16 17:18:53 -0800 | [diff] [blame] | 53 | # Default = 0 hidden frames: show everything except dump_trace |
| 54 | # frame itself. |
| 55 | local hidden_frames=${1:-0} |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 56 | local j n p func src line args |
| 57 | p=${#BASH_ARGV[@]} |
Marc Herbert | ff9fa31 | 2015-01-16 17:18:53 -0800 | [diff] [blame] | 58 | |
Marc Herbert | f51cf8f | 2015-01-20 13:56:00 -0800 | [diff] [blame] | 59 | error "$(date)" |
Marc Herbert | ee1fcbb | 2015-05-28 10:37:46 -0700 | [diff] [blame] | 60 | error "$(ps f -o pgid,ppid,pid,etime,cputime,%cpu,command)" |
Marc Herbert | f51cf8f | 2015-01-20 13:56:00 -0800 | [diff] [blame] | 61 | |
Marc Herbert | ff9fa31 | 2015-01-16 17:18:53 -0800 | [diff] [blame] | 62 | # Frame 0 is ourselves so it's always suppressed / does not count. |
| 63 | for (( n = ${#FUNCNAME[@]}; n > hidden_frames; --n )); do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 64 | func=${FUNCNAME[${n} - 1]} |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 65 | line=${BASH_LINENO[${n} - 1]} |
| 66 | args= |
| 67 | if [[ -z ${BASH_ARGC[${n} -1]} ]]; then |
| 68 | args='(args unknown, no debug available)' |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 69 | else |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 70 | for (( j = 0; j < ${BASH_ARGC[${n} -1]}; ++j )); do |
| 71 | args="${args:+${args} }'${BASH_ARGV[$(( p - j - 1 ))]}'" |
| 72 | done |
| 73 | ! (( p -= ${BASH_ARGC[${n} - 1]} )) |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 74 | fi |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 75 | if [[ ${n} == ${#FUNCNAME[@]} ]]; then |
Marc Herbert | ee1fcbb | 2015-05-28 10:37:46 -0700 | [diff] [blame] | 76 | error "Arguments of $$: ${0##/*} ${args}" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 77 | error "Backtrace: (most recent call is last)" |
| 78 | else |
Marc Herbert | 2e8c590 | 2015-01-15 20:17:31 -0800 | [diff] [blame] | 79 | src=${BASH_SOURCE[${n}]##*/} |
Marc Herbert | fa6cbe7 | 2015-01-20 14:09:09 -0800 | [diff] [blame] | 80 | curr_func=${FUNCNAME[${n}]} |
| 81 | error "$(printf ' %s:%s:%s(), called: %s %s ' \ |
| 82 | "${src}" "${line}" "${curr_func}" "${func}" "${args}")" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 83 | fi |
| 84 | done |
| 85 | } |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 86 | |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 87 | # Declare these asap so that code below can safely assume they exist. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 88 | _message() { |
Evan Benn | 4b6aba5 | 2020-03-02 18:07:48 +1100 | [diff] [blame] | 89 | local prefix |
| 90 | prefix="$(date +%H:%M:%S) $1" |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 91 | shift |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 92 | if [[ $# -eq 0 ]]; then |
| 93 | echo -e "${prefix}${CROS_LOG_PREFIX:-""}:${V_VIDOFF}" >&2 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 94 | return |
| 95 | fi |
| 96 | ( |
| 97 | # Handle newlines in the message, prefixing each chunk correctly. |
| 98 | # Do this in a subshell to avoid having to track IFS/set -f state. |
| 99 | IFS=" |
| 100 | " |
| 101 | set +f |
| 102 | set -- $* |
| 103 | IFS=' ' |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 104 | if [[ $# -eq 0 ]]; then |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 105 | # Empty line was requested. |
| 106 | set -- '' |
| 107 | fi |
| 108 | for line in "$@"; do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 109 | echo -e "${prefix}${CROS_LOG_PREFIX:-}: ${line}${V_VIDOFF}" >&2 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 110 | done |
| 111 | ) |
| 112 | } |
| 113 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 114 | info() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 115 | _message "${V_BOLD_GREEN}INFO " "$*" |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 118 | warn() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 119 | _message "${V_BOLD_YELLOW}WARNING " "$*" |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 122 | error() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 123 | _message "${V_BOLD_RED}ERROR " "$*" |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Mike Frysinger | 5cf035f | 2021-01-26 00:57:46 -0500 | [diff] [blame] | 126 | # Log the command we're about to run, then run it. |
| 127 | # Useful for long running commands that often need debugging from builders. |
| 128 | info_run() { |
| 129 | info "Running: $*" |
| 130 | "$@" |
| 131 | } |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 132 | |
| 133 | # For all die functions, they must explicitly force set +eu; |
Alex Deymo | f923595 | 2015-01-15 11:15:09 -0800 | [diff] [blame] | 134 | # no reason to have them cause their own crash if we're in the middle |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 135 | # of reporting an error condition then exiting. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 136 | die_err_trap() { |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 137 | local result=${1:-$?} |
| 138 | local command=${2:-${BASH_COMMAND:-command unknown}} |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 139 | set +e +u |
| 140 | |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 141 | if [[ ${result} == "0" ]]; then |
| 142 | # Let callers simplify by setting us as an EXIT trap handler. |
| 143 | return 0 |
| 144 | fi |
| 145 | |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 146 | # Per the message, bash misreports 127 as 1 during err trap sometimes. |
| 147 | # Note this fact to ensure users don't place too much faith in the |
| 148 | # exit code in that case. |
| 149 | set -- "Command '${command}' exited with nonzero code: ${result}" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 150 | if [[ ${result} -eq 1 ]] && [[ -z $(type -t ${command}) ]]; then |
| 151 | set -- "$@" \ |
| 152 | '(Note bash sometimes misreports "command not found" as exit code 1 '\ |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 153 | 'instead of 127)' |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 154 | fi |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 155 | _dump_trace 1 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 156 | error |
| 157 | error "Command failed:" |
| 158 | DIE_PREFIX=' ' |
| 159 | die_notrace "$@" |
| 160 | } |
| 161 | |
| 162 | # Exit this script due to a failure, outputting a backtrace in the process. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 163 | die() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 164 | set +e +u |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 165 | _dump_trace 1 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 166 | error |
| 167 | error "Error was:" |
| 168 | DIE_PREFIX=' ' |
| 169 | die_notrace "$@" |
| 170 | } |
| 171 | |
| 172 | # Exit this script w/out a backtrace. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 173 | die_notrace() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 174 | set +e +u |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 175 | if [[ $# -eq 0 ]]; then |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 176 | set -- '(no error message given)' |
| 177 | fi |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 178 | local line |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 179 | for line in "$@"; do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 180 | error "${DIE_PREFIX}${line}" |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 181 | done |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 182 | exit 1 |
| 183 | } |
| 184 | |
David James | bf13ea8 | 2013-05-08 14:17:25 -0700 | [diff] [blame] | 185 | # Check for a single string in a list of space-separated strings. |
| 186 | # e.g. has "foo" "foo bar baz" is true, but has "f" "foo bar baz" is not. |
| 187 | has() { [[ " ${*:2} " == *" $1 "* ]]; } |
| 188 | |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 189 | # Directory locations inside the dev chroot; try the new default, |
| 190 | # falling back to user specific paths if the upgrade has yet to |
| 191 | # happen. |
| 192 | _user="${USER}" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 193 | [[ ${USER} == "root" ]] && _user="${SUDO_USER}" |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 194 | _CHROOT_TRUNK_DIRS=( "/home/${_user}/trunk" /mnt/host/source ) |
| 195 | _DEPOT_TOOLS_DIRS=( "/home/${_user}/depot_tools" /mnt/host/depot_tools ) |
| 196 | unset _user |
| 197 | |
| 198 | _process_mount_pt() { |
| 199 | # Given 4 arguments; the root path, the variable to set, |
| 200 | # the old location, and the new; finally, forcing the upgrade is doable |
| 201 | # via if a 5th arg is provided. |
| 202 | # This will then try to migrate the old to new if we can do so right now |
| 203 | # (else leaving symlinks in place w/in the new), and will set $1 to the |
| 204 | # new location. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 205 | local base=${1:-/} var=$2 old=$3 new=$4 force=${5:-false} |
| 206 | local _sudo=$([[ ${USER} != "root" ]] && echo sudo) |
| 207 | local val=${new} |
Mike Frysinger | 2d9a97d | 2015-02-25 01:38:56 -0500 | [diff] [blame] | 208 | if ${force} || [[ -L ${base}/${new} ]] || [[ ! -e ${base}/${new} ]]; then |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 209 | # Ok, it's either a symlink or this is the first run. Upgrade if we can- |
| 210 | # specifically, if we're outside the chroot and we can rmdir the old. |
| 211 | # If we cannot rmdir the old, that's due to a mount being bound to that |
| 212 | # point (even if we can't see it, it's there)- thus fallback to adding |
| 213 | # compat links. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 214 | if ${force} || ( [[ ${INSIDE_CHROOT} -eq 0 ]] && \ |
| 215 | ${_sudo} rmdir "${base}/${old}" 2>/dev/null ); then |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 216 | ${_sudo} rm -f "${base}/${new}" || : |
| 217 | ${_sudo} mkdir -p "${base}/${new}" "$(dirname "${base}/${old}" )" |
| 218 | ${_sudo} ln -s "${new}" "${base}/${old}" |
| 219 | else |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 220 | if [[ ! -L ${base}/${new} ]]; then |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 221 | # We can't do the upgrade right now; install compatibility links. |
| 222 | ${_sudo} mkdir -p "$(dirname "${base}/${new}")" "${base}/${old}" |
| 223 | ${_sudo} ln -s "${old}" "${base}/${new}" |
| 224 | fi |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 225 | val=${old} |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 226 | fi |
| 227 | fi |
| 228 | eval "${var}=\"${val}\"" |
| 229 | } |
| 230 | |
| 231 | set_chroot_trunk_dir() { |
| 232 | # This takes two optional arguments; the first being the path to the chroot |
| 233 | # base; this is only used by enter_chroot. The second argument is whether |
| 234 | # or not to force the new pathways; this is only used by make_chroot. Passing |
| 235 | # a non-null value for $2 forces the new paths. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 236 | if [[ ${INSIDE_CHROOT} -eq 0 ]] && [[ -z ${1-} ]]; then |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 237 | # Can't do the upgrade, thus skip trying to do so. |
| 238 | CHROOT_TRUNK_DIR="${_CHROOT_TRUNK_DIRS[1]}" |
| 239 | DEPOT_TOOLS_DIR="${_DEPOT_TOOLS_DIRS[1]}" |
| 240 | return |
| 241 | fi |
Alex Deymo | 76277de | 2015-01-22 11:02:13 -0800 | [diff] [blame] | 242 | _process_mount_pt "${1:-}" CHROOT_TRUNK_DIR "${_CHROOT_TRUNK_DIRS[@]}" \ |
| 243 | ${2:+true} |
| 244 | _process_mount_pt "${1:-}" DEPOT_TOOLS_DIR "${_DEPOT_TOOLS_DIRS[@]}" \ |
| 245 | ${2:+true} |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | set_chroot_trunk_dir |
| 249 | |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 250 | # Construct a list of possible locations for the source tree. This list is |
| 251 | # based on various environment variables and globals that may have been set |
| 252 | # by the calling script. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 253 | get_gclient_root_list() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 254 | if [[ ${INSIDE_CHROOT} -eq 1 ]]; then |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 255 | echo "${CHROOT_TRUNK_DIR}" |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 256 | fi |
| 257 | |
Alex Deymo | 135faa1 | 2015-01-21 11:20:13 -0800 | [diff] [blame] | 258 | if [[ -n ${COMMON_SH:-} ]]; then echo "$(dirname "${COMMON_SH}")/../.."; fi |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 259 | if [[ -n ${BASH_SOURCE} ]]; then echo "$(dirname "${BASH_SOURCE}")/../.."; fi |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | # Based on the list of possible source locations we set GCLIENT_ROOT if it is |
| 263 | # not already defined by looking for a src directory in each seach path |
| 264 | # location. If we do not find a valid looking root we error out. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 265 | get_gclient_root() { |
Alex Deymo | 135faa1 | 2015-01-21 11:20:13 -0800 | [diff] [blame] | 266 | if [[ -n ${GCLIENT_ROOT:-} ]]; then |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 267 | return |
| 268 | fi |
| 269 | |
| 270 | for path in $(get_gclient_root_list); do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 271 | if [[ -d ${path}/src ]]; then |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 272 | GCLIENT_ROOT=${path} |
| 273 | break |
| 274 | fi |
| 275 | done |
| 276 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 277 | if [[ -z ${GCLIENT_ROOT} ]]; then |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 278 | # Using dash or sh, we don't know where we are. $0 refers to the calling |
| 279 | # script, not ourselves, so that doesn't help us. |
| 280 | echo "Unable to determine location for common.sh. If you are sourcing" |
| 281 | echo "common.sh from a script run via dash or sh, you must do it in the" |
| 282 | echo "following way:" |
| 283 | echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"' |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 284 | echo ' . "${COMMON_SH}"' |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 285 | echo "where the first line is the relative path from your script to" |
| 286 | echo "common.sh." |
| 287 | exit 1 |
| 288 | fi |
| 289 | } |
| 290 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 291 | # Populate the ENVIRONMENT_WHITELIST array. |
| 292 | load_environment_whitelist() { |
| 293 | set -f |
| 294 | ENVIRONMENT_WHITELIST=( |
| 295 | $("${GCLIENT_ROOT}/chromite/scripts/cros_env_whitelist") |
| 296 | ) |
| 297 | set +f |
| 298 | } |
| 299 | |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 300 | # Find root of source tree |
| 301 | get_gclient_root |
| 302 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 303 | # Canonicalize the directories for the root dir and the calling script. |
| 304 | # 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] | 305 | # This is better than just using |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 306 | # FOO="$(cd ${FOO} ; pwd)" |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 307 | # since that leaves symbolic links intact. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 308 | # Note that 'realpath' is equivalent to 'readlink -f'. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 309 | SCRIPT_LOCATION=$(readlink -f "${SCRIPT_LOCATION}") |
| 310 | GCLIENT_ROOT=$(readlink -f "${GCLIENT_ROOT}") |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 311 | |
| 312 | # Other directories should always be pathed down from GCLIENT_ROOT. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 313 | SRC_ROOT="${GCLIENT_ROOT}/src" |
| 314 | SRC_INTERNAL="${GCLIENT_ROOT}/src-internal" |
| 315 | SCRIPTS_DIR="${SRC_ROOT}/scripts" |
| 316 | BUILD_LIBRARY_DIR="${SCRIPTS_DIR}/build_library" |
Mike Frysinger | 66accd2 | 2017-09-13 03:50:30 -0400 | [diff] [blame] | 317 | CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin" |
Alex Klein | 999443c | 2018-10-17 12:02:05 -0600 | [diff] [blame] | 318 | IMAGES_DIR="${GCLIENT_ROOT}/src/build/images" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 319 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 320 | # Load shflags |
Zdenek Behan | 07d2422 | 2011-11-02 00:46:25 +0000 | [diff] [blame] | 321 | # NOTE: This code snippet is in particular used by the au-generator (which |
| 322 | # stores shflags in ./lib/shflags/) and should not be touched. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 323 | if [[ -f ${SCRIPTS_DIR}/lib/shflags/shflags ]]; then |
Mike Frysinger | 77c674b | 2012-02-07 18:05:07 -0500 | [diff] [blame] | 324 | . "${SCRIPTS_DIR}/lib/shflags/shflags" || die "Couldn't find shflags" |
Zdenek Behan | 07d2422 | 2011-11-02 00:46:25 +0000 | [diff] [blame] | 325 | else |
| 326 | . ./lib/shflags/shflags || die "Couldn't find shflags" |
| 327 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 328 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 329 | # Our local mirror |
| 330 | DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 331 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 332 | # Upstream mirrors and build suites come in 2 flavors |
| 333 | # DEV - development chroot, used to build the chromeos image |
| 334 | # IMG - bootable image, to run on actual hardware |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 335 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 336 | DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 337 | DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"} |
| 338 | |
| 339 | DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 340 | DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 341 | |
| 342 | # Default location for chroot |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 343 | DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"${GCLIENT_ROOT}/chroot"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 344 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 345 | # All output files from build should go under ${DEFAULT_BUILD_ROOT}, so that |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 346 | # they don't pollute the source directory. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 347 | DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"${SRC_ROOT}/build"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 348 | |
Chris Ching | 4bc95a1 | 2016-11-22 13:44:13 -0700 | [diff] [blame] | 349 | # Default location for event files |
| 350 | DEFAULT_EVENT_DIR=${DEFAULT_EVENT_DIR:-"${DEFAULT_BUILD_ROOT}/events"} |
| 351 | |
| 352 | # Default event file. Format is YYYYDD.HHMM.json |
| 353 | DEFAULT_EVENT_FILE=${DEFAULT_EVENT_FILE:-"${DEFAULT_EVENT_DIR}/$(date +%Y%m%d.%H%M.).json"} |
| 354 | |
Mike Frysinger | c17a493 | 2012-03-12 17:07:40 -0400 | [diff] [blame] | 355 | # Sets the default board variable for calling script. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 356 | if [[ -f ${GCLIENT_ROOT}/src/scripts/.default_board ]]; then |
| 357 | DEFAULT_BOARD=$(<"${GCLIENT_ROOT}/src/scripts/.default_board") |
Mike Frysinger | c17a493 | 2012-03-12 17:07:40 -0400 | [diff] [blame] | 358 | # Check for user typos like whitespace. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 359 | if [[ -n ${DEFAULT_BOARD//[a-zA-Z0-9-_]} ]]; then |
Mike Frysinger | c17a493 | 2012-03-12 17:07:40 -0400 | [diff] [blame] | 360 | die ".default_board: invalid name detected; please fix:" \ |
| 361 | "'${DEFAULT_BOARD}'" |
| 362 | fi |
| 363 | fi |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 364 | |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 365 | # Directory to store built images. Should be set by sourcing script when used. |
| 366 | BUILD_DIR= |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 367 | |
Mike Frysinger | 34808e5 | 2017-01-11 21:14:08 -0500 | [diff] [blame] | 368 | # Path to the verified boot directory where we get signing related keys/scripts. |
| 369 | VBOOT_DIR="${CHROOT_TRUNK_DIR}/src/platform/vboot_reference" |
| 370 | VBOOT_TESTKEYS_DIR="${VBOOT_DIR}/tests/testkeys" |
Mike Frysinger | 40cea03 | 2017-03-12 19:48:20 -0400 | [diff] [blame] | 371 | # We load these from the chroot rather than directly from the vboot source repo |
| 372 | # so we work correctly even in a minilayout. |
| 373 | VBOOT_DEVKEYS_DIR="/usr/share/vboot/devkeys" |
| 374 | VBOOT_SIGNING_DIR="/usr/share/vboot/bin" |
Mike Frysinger | 34808e5 | 2017-01-11 21:14:08 -0500 | [diff] [blame] | 375 | |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 376 | # Standard filenames |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 377 | CHROMEOS_BASE_IMAGE_NAME="chromiumos_base_image.bin" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 378 | CHROMEOS_IMAGE_NAME="chromiumos_image.bin" |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 379 | CHROMEOS_DEVELOPER_IMAGE_NAME="chromiumos_image.bin" |
Gilad Arnold | 0836627 | 2012-02-08 10:46:26 -0800 | [diff] [blame] | 380 | CHROMEOS_RECOVERY_IMAGE_NAME="recovery_image.bin" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 381 | CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin" |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 382 | CHROMEOS_FACTORY_INSTALL_SHIM_NAME="factory_install_shim.bin" |
Bertrand SIMONNET | b17cd57 | 2015-04-01 10:27:37 -0700 | [diff] [blame] | 383 | SYSROOT_SETTINGS_FILE="/var/cache/edb/chromeos" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 384 | |
Rahul Chaudhry | 306f688 | 2015-05-13 15:14:13 -0700 | [diff] [blame] | 385 | # Install mask for portage ebuilds. Used by build_image and gmergefs. |
Chris Masone | d11ce17 | 2010-11-09 14:22:08 -0800 | [diff] [blame] | 386 | # TODO: Is /usr/local/autotest-chrome still used by anyone? |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 387 | COMMON_INSTALL_MASK=" |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 388 | *.a |
Alex Deymo | 1856c4f | 2014-09-24 08:15:34 -0700 | [diff] [blame] | 389 | *.c |
| 390 | *.cc |
François Degros | 848a90c | 2021-02-08 09:48:11 +1100 | [diff] [blame] | 391 | *.cmake |
Rahul Chaudhry | 306f688 | 2015-05-13 15:14:13 -0700 | [diff] [blame] | 392 | *.go |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 393 | *.la |
Rene Bolldorf | 3222822 | 2012-07-21 14:18:22 -0400 | [diff] [blame] | 394 | *.h |
Brian Norris | ef0880d | 2016-05-19 12:39:56 -0700 | [diff] [blame] | 395 | *.hh |
Rene Bolldorf | 3222822 | 2012-07-21 14:18:22 -0400 | [diff] [blame] | 396 | *.hpp |
Kevin Cernekee | 6ab57a9 | 2015-06-25 15:43:06 -0700 | [diff] [blame] | 397 | *.h++ |
Nam T. Nguyen | aa15f14 | 2015-05-15 14:27:50 -0700 | [diff] [blame] | 398 | *.hxx |
Ryo Hashimoto | 44e6a25 | 2019-10-10 16:12:02 +0900 | [diff] [blame] | 399 | *.proto |
Mike Frysinger | 97c6ac9 | 2014-06-16 00:06:37 -0400 | [diff] [blame] | 400 | */.keep* |
Shuhei Takahashi | c62d307 | 2019-01-29 18:04:35 +0900 | [diff] [blame] | 401 | /build/libexec/tast |
Andrew | 7e8ecf7 | 2020-02-07 12:54:25 -0800 | [diff] [blame] | 402 | /build/rootfs/dlc |
Matt Delco | 80f3d78 | 2019-11-15 14:04:31 -0800 | [diff] [blame] | 403 | /build/share |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 404 | /etc/init.d |
| 405 | /etc/runlevels |
Qijiang Fan | 6834cba | 2018-04-04 16:41:56 +0900 | [diff] [blame] | 406 | /etc/selinux/intermediates |
Mike Frysinger | 6b20e66 | 2021-02-15 08:21:52 -0500 | [diff] [blame] | 407 | /etc/xinetd.d |
Hung-Te Lin | 76272be | 2012-08-07 19:10:09 +0800 | [diff] [blame] | 408 | /firmware |
Mike Frysinger | 785f09d | 2013-09-25 22:11:21 -0400 | [diff] [blame] | 409 | /lib/modules/*/vdso |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 410 | /lib/rc |
Tomasz Figa | 435f0e5 | 2017-01-06 14:28:06 +0900 | [diff] [blame] | 411 | /opt/google/containers/android/vendor/lib*/pkgconfig |
Benjamin Gordon | ffcb2b1 | 2017-10-24 13:02:46 -0600 | [diff] [blame] | 412 | /opt/google/containers/android/build |
Nam T. Nguyen | f1de1a7 | 2015-05-27 08:37:46 -0700 | [diff] [blame] | 413 | /usr/bin/*-config |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 414 | /usr/bin/Xnest |
| 415 | /usr/bin/Xvfb |
Sergei Datsenko | 29869d9 | 2019-11-12 14:26:27 +1100 | [diff] [blame] | 416 | /usr/include |
Stephen Barber | 717c583 | 2020-01-17 16:14:41 -0800 | [diff] [blame] | 417 | /usr/lib/cros_rust_registry |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 418 | /usr/lib/debug |
Rahul Chaudhry | 306f688 | 2015-05-13 15:14:13 -0700 | [diff] [blame] | 419 | /usr/lib/gopath |
Mike Frysinger | 27b5edb | 2012-08-08 18:52:06 -0400 | [diff] [blame] | 420 | /usr/lib*/pkgconfig |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 421 | /usr/local/autotest-chrome |
| 422 | /usr/man |
| 423 | /usr/share/aclocal |
Brian Norris | 0f3c4e2 | 2016-06-21 12:23:18 -0700 | [diff] [blame] | 424 | /usr/share/cups/drv |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 425 | /usr/share/doc |
| 426 | /usr/share/gettext |
| 427 | /usr/share/gtk-2.0 |
| 428 | /usr/share/gtk-doc |
| 429 | /usr/share/info |
| 430 | /usr/share/man |
Brian Norris | 0f3c4e2 | 2016-06-21 12:23:18 -0700 | [diff] [blame] | 431 | /usr/share/ppd |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 432 | /usr/share/openrc |
| 433 | /usr/share/pkgconfig |
Ryan Harrison | b8bd594 | 2013-02-02 13:39:43 -0500 | [diff] [blame] | 434 | /usr/share/profiling |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 435 | /usr/share/readline |
Chris Wolfe | d13775f | 2011-07-26 16:34:38 -0400 | [diff] [blame] | 436 | /usr/src |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 437 | " |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 438 | |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 439 | # Mask for base, dev, and test images (build_image, build_image --test) |
| 440 | DEFAULT_INSTALL_MASK=" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 441 | ${COMMON_INSTALL_MASK} |
Don Garrett | ac1cd0d | 2013-07-18 18:04:06 -0700 | [diff] [blame] | 442 | /boot/config-* |
| 443 | /boot/System.map-* |
Aviv Keshet | a1838ba | 2013-07-23 10:58:23 -0700 | [diff] [blame] | 444 | /usr/local/build/autotest |
Olof Johansson | 1222b2e | 2012-08-08 15:27:35 -0700 | [diff] [blame] | 445 | /lib/modules/*/build |
| 446 | /lib/modules/*/source |
Kees Cook | 74f163b | 2012-12-18 15:46:51 -0800 | [diff] [blame] | 447 | test_*.ko |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 448 | " |
| 449 | |
Chris Sosa | c9422fa | 2012-03-05 15:58:07 -0800 | [diff] [blame] | 450 | # Mask for factory install shim (build_image factory_install) |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 451 | FACTORY_SHIM_INSTALL_MASK=" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 452 | ${DEFAULT_INSTALL_MASK} |
Cheng-Han Yang | c924c2c | 2018-11-02 17:02:29 +0800 | [diff] [blame] | 453 | /opt/google/chrome |
| 454 | /opt/google/containers |
Lepton Wu | 29127c2 | 2020-05-05 17:45:07 -0700 | [diff] [blame] | 455 | /opt/google/vms |
Vic Yang | 982556a | 2012-12-11 11:41:54 +0800 | [diff] [blame] | 456 | /usr/lib64/dri |
Hung-Te Lin | 7f721ec | 2017-07-18 15:14:48 +0800 | [diff] [blame] | 457 | /usr/lib/dri |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 458 | /usr/share/X11 |
Hung-Te Lin | 7f721ec | 2017-07-18 15:14:48 +0800 | [diff] [blame] | 459 | /usr/share/chromeos-assets/[^i]* |
| 460 | /usr/share/chromeos-assets/i[^m]* |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 461 | /usr/share/fonts |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 462 | /usr/share/locale |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 463 | /usr/share/mime |
Vic Yang | 982556a | 2012-12-11 11:41:54 +0800 | [diff] [blame] | 464 | /usr/share/oem |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 465 | /usr/share/sounds |
| 466 | /usr/share/tts |
| 467 | /usr/share/zoneinfo |
| 468 | " |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 469 | |
Sabin Floares | e790ea7 | 2016-11-21 14:56:41 +0200 | [diff] [blame] | 470 | # Mask for images without systemd. |
| 471 | SYSTEMD_INSTALL_MASK=" |
Chris Morin | 83226ab | 2018-12-07 19:53:48 -0800 | [diff] [blame] | 472 | /lib/systemd/network |
| 473 | /usr/lib/systemd/system |
Sabin Floares | e790ea7 | 2016-11-21 14:56:41 +0200 | [diff] [blame] | 474 | " |
| 475 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 476 | # ----------------------------------------------------------------------------- |
| 477 | # Functions |
| 478 | |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 479 | # Enter a chroot and restart the current script if needed |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 480 | restart_in_chroot_if_needed() { |
David Rochberg | 3b91070 | 2010-12-02 10:45:21 -0500 | [diff] [blame] | 481 | # NB: Pass in ARGV: restart_in_chroot_if_needed "$@" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 482 | if [[ ${INSIDE_CHROOT} -ne 1 ]]; then |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 483 | # Get inside_chroot path for script. |
| 484 | local chroot_path="$(reinterpret_path_for_chroot "$0")" |
Mike Frysinger | 66accd2 | 2017-09-13 03:50:30 -0400 | [diff] [blame] | 485 | exec "${CHROMITE_BIN}/cros_sdk" -- "${chroot_path}" "$@" |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 486 | fi |
| 487 | } |
| 488 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 489 | # Fail unless we're inside the chroot. This guards against messing up your |
| 490 | # workstation. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 491 | assert_inside_chroot() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 492 | if [[ ${INSIDE_CHROOT} -ne 1 ]]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 493 | echo "This script must be run inside the chroot. Run this first:" |
Zdenek Behan | 2811c16 | 2011-08-13 00:47:38 +0200 | [diff] [blame] | 494 | echo " cros_sdk" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 495 | exit 1 |
| 496 | fi |
| 497 | } |
| 498 | |
| 499 | # Fail if we're inside the chroot. This guards against creating or entering |
| 500 | # nested chroots, among other potential problems. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 501 | assert_outside_chroot() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 502 | if [[ ${INSIDE_CHROOT} -ne 0 ]]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 503 | echo "This script must be run outside the chroot." |
| 504 | exit 1 |
| 505 | fi |
| 506 | } |
| 507 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 508 | assert_not_root_user() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 509 | if [[ ${UID:-$(id -u)} == 0 ]]; then |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 510 | echo "This script must be run as a non-root user." |
| 511 | exit 1 |
| 512 | fi |
| 513 | } |
| 514 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 515 | assert_root_user() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 516 | if [[ ${UID:-$(id -u)} != 0 ]] || [[ ${SUDO_USER:-root} == "root" ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 517 | die_notrace "This script must be run using sudo from a non-root user." |
| 518 | fi |
| 519 | } |
| 520 | |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 521 | # Writes stdin to the given file name as root using sudo in overwrite mode. |
| 522 | # |
| 523 | # $1 - The output file name. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 524 | sudo_clobber() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 525 | sudo tee "$1" >/dev/null |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 528 | # Execute multiple commands in a single sudo. Generally will speed things |
| 529 | # up by avoiding multiple calls to `sudo`. If any commands fail, we will |
| 530 | # call die with the failing command. We can handle a max of ~100 commands, |
| 531 | # but hopefully no one will ever try that many at once. |
| 532 | # |
| 533 | # $@ - The commands to execute, one per arg. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 534 | sudo_multi() { |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 535 | local i cmds |
| 536 | |
| 537 | # Construct the shell code to execute. It'll be of the form: |
| 538 | # ... && ( ( command ) || exit <command index> ) && ... |
| 539 | # This way we know which command exited. The exit status of |
| 540 | # the underlying command is lost, but we never cared about it |
| 541 | # in the first place (other than it is non zero), so oh well. |
| 542 | for (( i = 1; i <= $#; ++i )); do |
| 543 | cmds+=" && ( ( ${!i} ) || exit $(( i + 10 )) )" |
| 544 | done |
| 545 | |
| 546 | # Execute our constructed shell code. |
| 547 | sudo -- sh -c ":${cmds[*]}" && i=0 || i=$? |
| 548 | |
| 549 | # See if this failed, and if so, print out the failing command. |
| 550 | if [[ $i -gt 10 ]]; then |
| 551 | : $(( i -= 10 )) |
| 552 | die "sudo_multi failed: ${!i}" |
| 553 | elif [[ $i -ne 0 ]]; then |
| 554 | die "sudo_multi failed for unknown reason $i" |
| 555 | fi |
| 556 | } |
| 557 | |
Chris Masone | bbccc24 | 2014-02-08 16:23:53 -0800 | [diff] [blame] | 558 | # Clears out stale shadow-utils locks in the given target root. |
| 559 | sudo_clear_shadow_locks() { |
| 560 | info "Clearing shadow utils lockfiles under $1" |
| 561 | sudo rm -f "$1/etc/"{passwd,group,shadow,gshadow}.lock* |
| 562 | } |
| 563 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 564 | # Locate all mounts below a specified directory. |
| 565 | # |
| 566 | # $1 - The root tree. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 567 | sub_mounts() { |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 568 | # Assume that `mount` outputs a list of mount points in the order |
| 569 | # that things were mounted (since it always has and hopefully always |
| 570 | # will). As such, we have to unmount in reverse order to cleanly |
| 571 | # unmount submounts (think /dev/pts and /dev). |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 572 | awk -v path="$1" -v len="${#1}" \ |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 573 | '(substr($2, 1, len+1) == path || |
| 574 | substr($2, 1, len+1) == (path "/")) { print $2 }' /proc/mounts | \ |
Zdenek Behan | 52970f4 | 2012-08-30 22:36:40 +0200 | [diff] [blame] | 575 | tac | \ |
| 576 | sed -e 's/\\040(deleted)$//' |
| 577 | # Hack(zbehan): If a bind mount's source is mysteriously removed, |
| 578 | # we'd end up with an orphaned mount with the above string in its name. |
| 579 | # It can only be seen through /proc/mounts and will stick around even |
| 580 | # when it should be gone already. crosbug.com/31250 |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 581 | } |
| 582 | |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 583 | # Unmounts a directory, if the unmount fails, warn, and then lazily unmount. |
| 584 | # |
| 585 | # $1 - The path to unmount. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 586 | safe_umount_tree() { |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 587 | local mount_point="$1" |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 588 | |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 589 | local mounts=( $(sub_mounts "${mount_point}") ) |
| 590 | |
| 591 | # Silently return if the mount_point was already unmounted. |
| 592 | if [[ ${#mounts[@]} -eq 0 ]]; then |
Mike Frysinger | e8aec37 | 2011-09-21 00:03:22 -0400 | [diff] [blame] | 593 | return 0 |
| 594 | fi |
| 595 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 596 | # First try to unmount in one shot to speed things up. |
Chirantan Ekbote | 30823e9 | 2016-06-29 11:39:03 -0700 | [diff] [blame] | 597 | if LC_ALL=C safe_umount -d "${mounts[@]}"; then |
| 598 | return 0 |
David James | d9b6798 | 2012-10-26 09:46:50 -0700 | [diff] [blame] | 599 | fi |
| 600 | |
| 601 | # Well that didn't work, so lazy unmount remaining ones. |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 602 | warn "Failed to unmount ${mounts[@]}, these are the processes using the" \ |
| 603 | "mount points." |
| 604 | sudo fuser -vm "${mount_point}" || true |
| 605 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 606 | warn "Doing a lazy unmount" |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 607 | if ! safe_umount -d -l "${mounts[@]}"; then |
| 608 | mounts=( $(sub_mounts "${mount_point}") ) |
| 609 | die "Failed to lazily unmount ${mounts[@]}" |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 610 | fi |
| 611 | } |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 612 | |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 613 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 614 | # Run umount as root. |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 615 | safe_umount() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 616 | $([[ ${UID:-$(id -u)} != 0 ]] && echo sudo) umount "$@" |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 619 | # Setup a loopback device for a file and scan for partitions, with retries. |
| 620 | # |
| 621 | # $1 - The file to back the new loopback device. |
| 622 | # $2-$N - Additional arguments to pass to losetup. |
| 623 | loopback_partscan() { |
| 624 | local lb_dev image="$1" |
| 625 | shift |
LaMont Jones | 005f77b | 2019-10-11 14:04:55 -0600 | [diff] [blame] | 626 | |
George Engelbrecht | fcd236f | 2019-10-28 18:12:56 -0600 | [diff] [blame] | 627 | # We set up a binary backoff for adding the partitions. We give 10 attempts |
| 628 | # each time nth time we fail, backing off by 1<<n. The maximum time then |
| 629 | # spent sleeping will be sum(2^n) from [0...10] which is 1023 seconds, |
| 630 | # or ~37 minutes. |
| 631 | local i partx_out partx_d_out sleep_seconds=1 |
| 632 | for (( i = 0; i <= 10; i++ )); do |
| 633 | # Flush any dirty pages in the image before we do partx commands. |
| 634 | info "Running sync -f ${image}" |
| 635 | sync -f "${image}" |
LaMont Jones | 005f77b | 2019-10-11 14:04:55 -0600 | [diff] [blame] | 636 | |
George Engelbrecht | fcd236f | 2019-10-28 18:12:56 -0600 | [diff] [blame] | 637 | # This (perhaps) mounts the partitions as well. |
| 638 | lb_dev=$(sudo losetup --show -f "$@" "${image}") |
Mike Frysinger | c97934e | 2019-09-02 02:48:28 -0400 | [diff] [blame] | 639 | |
George Engelbrecht | fcd236f | 2019-10-28 18:12:56 -0600 | [diff] [blame] | 640 | # Try to clean the slate by removing any existing parts (best effort). |
| 641 | partx_d_out=$(sudo partx -v -d "${lb_dev}") || true |
Mike Frysinger | c97934e | 2019-09-02 02:48:28 -0400 | [diff] [blame] | 642 | |
George Engelbrecht | fcd236f | 2019-10-28 18:12:56 -0600 | [diff] [blame] | 643 | # Try to add the partitions back. |
| 644 | if ! partx_out=$(sudo partx -v -a "${lb_dev}"); then |
| 645 | local proc_parts |
| 646 | warn "Adding partitions with 'partx -v -a ${lb_dev}' failed." |
| 647 | warn "partx -d output:\n${partx_d_out}" |
| 648 | warn "partx -a output:\n${partx_out}" |
| 649 | proc_parts=$(cat /proc/partitions) |
| 650 | warn "/proc/partitions before detaching loopback:\n ${proc_parts}" |
| 651 | # Detach the image. |
| 652 | sudo losetup -d "${lb_dev}" || true |
| 653 | proc_parts=$(cat /proc/partitions) |
| 654 | warn "/proc/partitions after detaching loopback:\n ${proc_parts}" |
| 655 | warn "Sleeping ${sleep_seconds} before trying again." |
| 656 | sleep "${sleep_seconds}" |
| 657 | : $(( sleep_seconds <<= 1 )) |
| 658 | else |
| 659 | break |
| 660 | fi |
| 661 | done |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 662 | |
Gabe Black | 7bb6c5a | 2015-03-16 20:23:06 -0700 | [diff] [blame] | 663 | echo "${lb_dev}" |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | # Detach a loopback device set up earlier. |
| 667 | # |
Gabe Black | 01ba4de | 2015-03-11 16:34:56 -0700 | [diff] [blame] | 668 | # $1 - The loop device to detach. |
| 669 | # $2-$N - Additional arguments to pass to losetup. |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 670 | loopback_detach() { |
Mike Frysinger | af64ed5 | 2015-04-06 13:20:39 -0400 | [diff] [blame] | 671 | # Retry the deletes before we detach. crbug.com/469259 |
| 672 | local i |
| 673 | for (( i = 0; i < 10; i++ )); do |
Mike Frysinger | ad3b200 | 2019-08-21 23:35:59 -0400 | [diff] [blame] | 674 | if sudo partx -d "$1"; then |
Mike Frysinger | af64ed5 | 2015-04-06 13:20:39 -0400 | [diff] [blame] | 675 | break |
| 676 | fi |
| 677 | warn "Sleeping & retrying ..." |
| 678 | sync |
| 679 | sleep 1 |
| 680 | done |
Mike Frysinger | ad3b200 | 2019-08-21 23:35:59 -0400 | [diff] [blame] | 681 | sudo losetup --detach "$@" |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 684 | # Sets up symlinks for the developer root. It is necessary to symlink |
| 685 | # usr and local since the developer root is mounted at /usr/local and |
| 686 | # applications expect to be installed under /usr/local/bin, etc. |
| 687 | # This avoids packages installing into /usr/local/usr/local/bin. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 688 | # $1 specifies the symlink target for the developer root. |
| 689 | # $2 specifies the symlink target for the var directory. |
| 690 | # $3 specifies the location of the stateful partition. |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 691 | setup_symlinks_on_root() { |
| 692 | # Give args better names. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 693 | local dev_image_target=$1 |
| 694 | local var_target=$2 |
| 695 | local dev_image_root="$3/dev_image" |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 696 | |
Mike Frysinger | 9e15538 | 2019-07-12 21:11:00 -0400 | [diff] [blame] | 697 | # Make sure the dev_image dir itself exists. |
| 698 | if [[ ! -d "${dev_image_root}" ]]; then |
| 699 | sudo mkdir "${dev_image_root}" |
| 700 | fi |
| 701 | |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 702 | # If our var target is actually the standard var, we are cleaning up the |
| 703 | # symlinks (could also check for /usr/local for the dev_image_target). |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 704 | if [[ ${var_target} == "/var" ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 705 | echo "Cleaning up /usr/local symlinks for ${dev_image_root}" |
| 706 | else |
| 707 | echo "Setting up symlinks for /usr/local for ${dev_image_root}" |
| 708 | fi |
| 709 | |
| 710 | # Set up symlinks that should point to ${dev_image_target}. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 711 | local path |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 712 | for path in usr local; do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 713 | if [[ -h ${dev_image_root}/${path} ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 714 | sudo unlink "${dev_image_root}/${path}" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 715 | elif [[ -e ${dev_image_root}/${path} ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 716 | die "${dev_image_root}/${path} should be a symlink if exists" |
| 717 | fi |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 718 | sudo ln -s "${dev_image_target}" "${dev_image_root}/${path}" |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 719 | done |
| 720 | |
| 721 | # Setup var symlink. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 722 | if [[ -h ${dev_image_root}/var ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 723 | sudo unlink "${dev_image_root}/var" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 724 | elif [[ -e ${dev_image_root}/var ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 725 | die "${dev_image_root}/var should be a symlink if it exists" |
| 726 | fi |
| 727 | |
| 728 | sudo ln -s "${var_target}" "${dev_image_root}/var" |
| 729 | } |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 730 | |
| 731 | # Get current timestamp. Assumes common.sh runs at startup. |
| 732 | start_time=$(date +%s) |
| 733 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 734 | # Get time elapsed since start_time in seconds. |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 735 | _get_elapsed_seconds() { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 736 | local end_time=$(date +%s) |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 737 | local elapsed_seconds=$(( end_time - start_time )) |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 738 | echo ${elapsed_seconds} |
| 739 | } |
| 740 | |
| 741 | # Print time elapsed since start_time. |
| 742 | print_time_elapsed() { |
| 743 | # Optional first arg to specify elapsed_seconds. If not given, will |
| 744 | # recalculate elapsed time to now. Optional second arg to specify |
| 745 | # command name associated with elapsed time. |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 746 | local elapsed_seconds=${1:-$(_get_elapsed_seconds)} |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 747 | local cmd_base=${2:-} |
| 748 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 749 | local minutes=$(( elapsed_seconds / 60 )) |
| 750 | local seconds=$(( elapsed_seconds % 60 )) |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 751 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 752 | if [[ -n ${cmd_base} ]]; then |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 753 | info "Elapsed time (${cmd_base}): ${minutes}m${seconds}s" |
| 754 | else |
| 755 | info "Elapsed time: ${minutes}m${seconds}s" |
| 756 | fi |
| 757 | } |
| 758 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 759 | command_completed() { |
| 760 | # Call print_elapsed_time regardless. |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 761 | local run_time=$(_get_elapsed_seconds) |
Mike Frysinger | 3131d41 | 2019-03-02 14:09:37 -0500 | [diff] [blame] | 762 | local cmd_base=$(basename "$0") |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 763 | print_time_elapsed ${run_time} ${cmd_base} |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 764 | } |
Doug Anderson | 0c9e88d | 2010-10-19 14:49:39 -0700 | [diff] [blame] | 765 | |
Albert Chaulk | 5d190f7 | 2013-07-12 11:42:18 -0700 | [diff] [blame] | 766 | # Load configuration files that allow board-specific overrides of default |
| 767 | # functionality to be specified in overlays. |
Steve Fung | d3b1f5f | 2015-03-29 21:47:24 -0700 | [diff] [blame] | 768 | # $1 - File to load. |
Albert Chaulk | 5d190f7 | 2013-07-12 11:42:18 -0700 | [diff] [blame] | 769 | load_board_specific_script() { |
Steve Fung | 88897cc | 2015-03-25 13:38:38 -0700 | [diff] [blame] | 770 | local file=$1 overlay |
| 771 | [[ $# -ne 1 ]] && die "load_board_specific_script requires exactly 1 param" |
| 772 | for overlay in ${BOARD_OVERLAY}; do |
Albert Chaulk | 5d190f7 | 2013-07-12 11:42:18 -0700 | [diff] [blame] | 773 | local setup_sh="${overlay}/scripts/${file}" |
| 774 | if [[ -e ${setup_sh} ]]; then |
| 775 | source "${setup_sh}" |
| 776 | fi |
| 777 | done |
| 778 | } |
| 779 | |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 780 | # Reinterprets path from outside the chroot for use inside. |
| 781 | # Returns "" if "" given. |
| 782 | # $1 - The path to reinterpret. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 783 | reinterpret_path_for_chroot() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 784 | if [[ ${INSIDE_CHROOT} -ne 1 ]]; then |
| 785 | if [[ -z $1 ]]; then |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 786 | echo "" |
| 787 | else |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 788 | local path_abs_path=$(readlink -f "$1") |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 789 | local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}") |
| 790 | |
| 791 | # Strip the repository root from the path. |
| 792 | local relative_path=$(echo ${path_abs_path} \ |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 793 | | sed "s:${gclient_root_abs_path}/::") |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 794 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 795 | if [[ ${relative_path} == "${path_abs_path}" ]]; then |
| 796 | die "Error reinterpreting path. Path $1 is not within source tree." |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 797 | fi |
| 798 | |
| 799 | # Prepend the chroot repository path. |
Chris Sosa | ed2ff4b | 2013-08-30 12:58:41 -0700 | [diff] [blame] | 800 | echo "/mnt/host/source/${relative_path}" |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 801 | fi |
| 802 | else |
| 803 | # Path is already inside the chroot :). |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 804 | echo "$1" |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 805 | fi |
| 806 | } |
Gabe Black | 83d8b82 | 2011-08-01 17:50:09 -0700 | [diff] [blame] | 807 | |
Allen Webb | f7abe57 | 2021-05-13 13:33:15 -0500 | [diff] [blame] | 808 | # Echo a list of cross-* ebuilds to exclude from eclean. |
| 809 | get_eclean_exclusions() { |
| 810 | qlist -IC ^cross- |
| 811 | } |
| 812 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 813 | switch_to_strict_mode() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 814 | # Set up strict execution mode; note that the trap |
| 815 | # must follow switch_to_strict_mode, else it will have no effect. |
| 816 | set -e |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 817 | trap 'die_err_trap' ERR |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 818 | if [[ $# -ne 0 ]]; then |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 819 | set "$@" |
| 820 | fi |
| 821 | } |
| 822 | |
| 823 | # TODO: Re-enable this once shflags is set -e safe. |
| 824 | #switch_to_strict_mode |