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 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 47 | |
Marc Herbert | ff9fa31 | 2015-01-16 17:18:53 -0800 | [diff] [blame] | 48 | # Output a backtrace. Optional parameter allows hiding the last |
| 49 | # frame(s) so functions like "die()" can hide their additional |
| 50 | # frame(s) if they wish. |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 51 | _dump_trace() { |
Marc Herbert | ff9fa31 | 2015-01-16 17:18:53 -0800 | [diff] [blame] | 52 | # Default = 0 hidden frames: show everything except dump_trace |
| 53 | # frame itself. |
| 54 | local hidden_frames=${1:-0} |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 55 | local j n p func src line args |
| 56 | p=${#BASH_ARGV[@]} |
Marc Herbert | ff9fa31 | 2015-01-16 17:18:53 -0800 | [diff] [blame] | 57 | |
Marc Herbert | f51cf8f | 2015-01-20 13:56:00 -0800 | [diff] [blame] | 58 | error "$(date)" |
Marc Herbert | ee1fcbb | 2015-05-28 10:37:46 -0700 | [diff] [blame] | 59 | error "$(ps f -o pgid,ppid,pid,etime,cputime,%cpu,command)" |
Marc Herbert | f51cf8f | 2015-01-20 13:56:00 -0800 | [diff] [blame] | 60 | |
Marc Herbert | ff9fa31 | 2015-01-16 17:18:53 -0800 | [diff] [blame] | 61 | # Frame 0 is ourselves so it's always suppressed / does not count. |
| 62 | for (( n = ${#FUNCNAME[@]}; n > hidden_frames; --n )); do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 63 | func=${FUNCNAME[${n} - 1]} |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 64 | line=${BASH_LINENO[${n} - 1]} |
| 65 | args= |
| 66 | if [[ -z ${BASH_ARGC[${n} -1]} ]]; then |
| 67 | args='(args unknown, no debug available)' |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 68 | else |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 69 | for (( j = 0; j < ${BASH_ARGC[${n} -1]}; ++j )); do |
| 70 | args="${args:+${args} }'${BASH_ARGV[$(( p - j - 1 ))]}'" |
| 71 | done |
| 72 | ! (( p -= ${BASH_ARGC[${n} - 1]} )) |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 73 | fi |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 74 | if [[ ${n} == ${#FUNCNAME[@]} ]]; then |
Marc Herbert | ee1fcbb | 2015-05-28 10:37:46 -0700 | [diff] [blame] | 75 | error "Arguments of $$: ${0##/*} ${args}" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 76 | error "Backtrace: (most recent call is last)" |
| 77 | else |
Marc Herbert | 2e8c590 | 2015-01-15 20:17:31 -0800 | [diff] [blame] | 78 | src=${BASH_SOURCE[${n}]##*/} |
Marc Herbert | fa6cbe7 | 2015-01-20 14:09:09 -0800 | [diff] [blame] | 79 | curr_func=${FUNCNAME[${n}]} |
| 80 | error "$(printf ' %s:%s:%s(), called: %s %s ' \ |
| 81 | "${src}" "${line}" "${curr_func}" "${func}" "${args}")" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 82 | fi |
| 83 | done |
| 84 | } |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 85 | |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 86 | # Declare these asap so that code below can safely assume they exist. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 87 | _message() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 88 | local prefix=$1 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 89 | shift |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 90 | if [[ $# -eq 0 ]]; then |
| 91 | echo -e "${prefix}${CROS_LOG_PREFIX:-""}:${V_VIDOFF}" >&2 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 92 | return |
| 93 | fi |
| 94 | ( |
| 95 | # Handle newlines in the message, prefixing each chunk correctly. |
| 96 | # Do this in a subshell to avoid having to track IFS/set -f state. |
| 97 | IFS=" |
| 98 | " |
| 99 | set +f |
| 100 | set -- $* |
| 101 | IFS=' ' |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 102 | if [[ $# -eq 0 ]]; then |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 103 | # Empty line was requested. |
| 104 | set -- '' |
| 105 | fi |
| 106 | for line in "$@"; do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 107 | echo -e "${prefix}${CROS_LOG_PREFIX:-}: ${line}${V_VIDOFF}" >&2 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 108 | done |
| 109 | ) |
| 110 | } |
| 111 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 112 | info() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 113 | _message "${V_BOLD_GREEN}INFO " "$*" |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 116 | warn() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 117 | _message "${V_BOLD_YELLOW}WARNING " "$*" |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 120 | error() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 121 | _message "${V_BOLD_RED}ERROR " "$*" |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 124 | |
| 125 | # For all die functions, they must explicitly force set +eu; |
Alex Deymo | f923595 | 2015-01-15 11:15:09 -0800 | [diff] [blame] | 126 | # 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] | 127 | # of reporting an error condition then exiting. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 128 | die_err_trap() { |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 129 | local result=${1:-$?} |
| 130 | local command=${2:-${BASH_COMMAND:-command unknown}} |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 131 | set +e +u |
| 132 | |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 133 | if [[ ${result} == "0" ]]; then |
| 134 | # Let callers simplify by setting us as an EXIT trap handler. |
| 135 | return 0 |
| 136 | fi |
| 137 | |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 138 | # Per the message, bash misreports 127 as 1 during err trap sometimes. |
| 139 | # Note this fact to ensure users don't place too much faith in the |
| 140 | # exit code in that case. |
| 141 | set -- "Command '${command}' exited with nonzero code: ${result}" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 142 | if [[ ${result} -eq 1 ]] && [[ -z $(type -t ${command}) ]]; then |
| 143 | set -- "$@" \ |
| 144 | '(Note bash sometimes misreports "command not found" as exit code 1 '\ |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 145 | 'instead of 127)' |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 146 | fi |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 147 | _dump_trace 1 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 148 | error |
| 149 | error "Command failed:" |
| 150 | DIE_PREFIX=' ' |
| 151 | die_notrace "$@" |
| 152 | } |
| 153 | |
| 154 | # 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] | 155 | die() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 156 | set +e +u |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 157 | _dump_trace 1 |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 158 | error |
| 159 | error "Error was:" |
| 160 | DIE_PREFIX=' ' |
| 161 | die_notrace "$@" |
| 162 | } |
| 163 | |
| 164 | # Exit this script w/out a backtrace. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 165 | die_notrace() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 166 | set +e +u |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 167 | if [[ $# -eq 0 ]]; then |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 168 | set -- '(no error message given)' |
| 169 | fi |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 170 | local line |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 171 | for line in "$@"; do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 172 | error "${DIE_PREFIX}${line}" |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 173 | done |
Mike Frysinger | 669b28b | 2012-02-07 18:01:00 -0500 | [diff] [blame] | 174 | exit 1 |
| 175 | } |
| 176 | |
David James | bf13ea8 | 2013-05-08 14:17:25 -0700 | [diff] [blame] | 177 | # Check for a single string in a list of space-separated strings. |
| 178 | # e.g. has "foo" "foo bar baz" is true, but has "f" "foo bar baz" is not. |
| 179 | has() { [[ " ${*:2} " == *" $1 "* ]]; } |
| 180 | |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 181 | # Directory locations inside the dev chroot; try the new default, |
| 182 | # falling back to user specific paths if the upgrade has yet to |
| 183 | # happen. |
| 184 | _user="${USER}" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 185 | [[ ${USER} == "root" ]] && _user="${SUDO_USER}" |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 186 | _CHROOT_TRUNK_DIRS=( "/home/${_user}/trunk" /mnt/host/source ) |
| 187 | _DEPOT_TOOLS_DIRS=( "/home/${_user}/depot_tools" /mnt/host/depot_tools ) |
| 188 | unset _user |
| 189 | |
| 190 | _process_mount_pt() { |
| 191 | # Given 4 arguments; the root path, the variable to set, |
| 192 | # the old location, and the new; finally, forcing the upgrade is doable |
| 193 | # via if a 5th arg is provided. |
| 194 | # This will then try to migrate the old to new if we can do so right now |
| 195 | # (else leaving symlinks in place w/in the new), and will set $1 to the |
| 196 | # new location. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 197 | local base=${1:-/} var=$2 old=$3 new=$4 force=${5:-false} |
| 198 | local _sudo=$([[ ${USER} != "root" ]] && echo sudo) |
| 199 | local val=${new} |
Mike Frysinger | 2d9a97d | 2015-02-25 01:38:56 -0500 | [diff] [blame] | 200 | if ${force} || [[ -L ${base}/${new} ]] || [[ ! -e ${base}/${new} ]]; then |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 201 | # Ok, it's either a symlink or this is the first run. Upgrade if we can- |
| 202 | # specifically, if we're outside the chroot and we can rmdir the old. |
| 203 | # If we cannot rmdir the old, that's due to a mount being bound to that |
| 204 | # point (even if we can't see it, it's there)- thus fallback to adding |
| 205 | # compat links. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 206 | if ${force} || ( [[ ${INSIDE_CHROOT} -eq 0 ]] && \ |
| 207 | ${_sudo} rmdir "${base}/${old}" 2>/dev/null ); then |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 208 | ${_sudo} rm -f "${base}/${new}" || : |
| 209 | ${_sudo} mkdir -p "${base}/${new}" "$(dirname "${base}/${old}" )" |
| 210 | ${_sudo} ln -s "${new}" "${base}/${old}" |
| 211 | else |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 212 | if [[ ! -L ${base}/${new} ]]; then |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 213 | # We can't do the upgrade right now; install compatibility links. |
| 214 | ${_sudo} mkdir -p "$(dirname "${base}/${new}")" "${base}/${old}" |
| 215 | ${_sudo} ln -s "${old}" "${base}/${new}" |
| 216 | fi |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 217 | val=${old} |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 218 | fi |
| 219 | fi |
| 220 | eval "${var}=\"${val}\"" |
| 221 | } |
| 222 | |
| 223 | set_chroot_trunk_dir() { |
| 224 | # This takes two optional arguments; the first being the path to the chroot |
| 225 | # base; this is only used by enter_chroot. The second argument is whether |
| 226 | # or not to force the new pathways; this is only used by make_chroot. Passing |
| 227 | # a non-null value for $2 forces the new paths. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 228 | if [[ ${INSIDE_CHROOT} -eq 0 ]] && [[ -z ${1-} ]]; then |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 229 | # Can't do the upgrade, thus skip trying to do so. |
| 230 | CHROOT_TRUNK_DIR="${_CHROOT_TRUNK_DIRS[1]}" |
| 231 | DEPOT_TOOLS_DIR="${_DEPOT_TOOLS_DIRS[1]}" |
| 232 | return |
| 233 | fi |
Alex Deymo | 76277de | 2015-01-22 11:02:13 -0800 | [diff] [blame] | 234 | _process_mount_pt "${1:-}" CHROOT_TRUNK_DIR "${_CHROOT_TRUNK_DIRS[@]}" \ |
| 235 | ${2:+true} |
| 236 | _process_mount_pt "${1:-}" DEPOT_TOOLS_DIR "${_DEPOT_TOOLS_DIRS[@]}" \ |
| 237 | ${2:+true} |
Brian Harring | 9086a3f | 2013-01-14 01:43:16 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | set_chroot_trunk_dir |
| 241 | |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 242 | # Construct a list of possible locations for the source tree. This list is |
| 243 | # based on various environment variables and globals that may have been set |
| 244 | # by the calling script. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 245 | get_gclient_root_list() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 246 | if [[ ${INSIDE_CHROOT} -eq 1 ]]; then |
Brian Harring | 2499bfb | 2012-12-18 16:23:31 -0800 | [diff] [blame] | 247 | echo "${CHROOT_TRUNK_DIR}" |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 248 | fi |
| 249 | |
Alex Deymo | 135faa1 | 2015-01-21 11:20:13 -0800 | [diff] [blame] | 250 | if [[ -n ${COMMON_SH:-} ]]; then echo "$(dirname "${COMMON_SH}")/../.."; fi |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 251 | if [[ -n ${BASH_SOURCE} ]]; then echo "$(dirname "${BASH_SOURCE}")/../.."; fi |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | # Based on the list of possible source locations we set GCLIENT_ROOT if it is |
| 255 | # not already defined by looking for a src directory in each seach path |
| 256 | # 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] | 257 | get_gclient_root() { |
Alex Deymo | 135faa1 | 2015-01-21 11:20:13 -0800 | [diff] [blame] | 258 | if [[ -n ${GCLIENT_ROOT:-} ]]; then |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 259 | return |
| 260 | fi |
| 261 | |
| 262 | for path in $(get_gclient_root_list); do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 263 | if [[ -d ${path}/src ]]; then |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 264 | GCLIENT_ROOT=${path} |
| 265 | break |
| 266 | fi |
| 267 | done |
| 268 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 269 | if [[ -z ${GCLIENT_ROOT} ]]; then |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 270 | # Using dash or sh, we don't know where we are. $0 refers to the calling |
| 271 | # script, not ourselves, so that doesn't help us. |
| 272 | echo "Unable to determine location for common.sh. If you are sourcing" |
| 273 | echo "common.sh from a script run via dash or sh, you must do it in the" |
| 274 | echo "following way:" |
| 275 | echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"' |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 276 | echo ' . "${COMMON_SH}"' |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 277 | echo "where the first line is the relative path from your script to" |
| 278 | echo "common.sh." |
| 279 | exit 1 |
| 280 | fi |
| 281 | } |
| 282 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 283 | # Populate the ENVIRONMENT_WHITELIST array. |
| 284 | load_environment_whitelist() { |
| 285 | set -f |
| 286 | ENVIRONMENT_WHITELIST=( |
| 287 | $("${GCLIENT_ROOT}/chromite/scripts/cros_env_whitelist") |
| 288 | ) |
| 289 | set +f |
| 290 | } |
| 291 | |
Anton Staaf | 30acb0b | 2011-01-26 16:00:20 -0800 | [diff] [blame] | 292 | # Find root of source tree |
| 293 | get_gclient_root |
| 294 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 295 | # Canonicalize the directories for the root dir and the calling script. |
| 296 | # 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] | 297 | # This is better than just using |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 298 | # FOO="$(cd ${FOO} ; pwd)" |
tedbo | 4f44d9e | 2010-01-08 17:26:11 -0800 | [diff] [blame] | 299 | # since that leaves symbolic links intact. |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 300 | # Note that 'realpath' is equivalent to 'readlink -f'. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 301 | SCRIPT_LOCATION=$(readlink -f "${SCRIPT_LOCATION}") |
| 302 | GCLIENT_ROOT=$(readlink -f "${GCLIENT_ROOT}") |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 303 | |
| 304 | # Other directories should always be pathed down from GCLIENT_ROOT. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 305 | SRC_ROOT="${GCLIENT_ROOT}/src" |
| 306 | SRC_INTERNAL="${GCLIENT_ROOT}/src-internal" |
| 307 | SCRIPTS_DIR="${SRC_ROOT}/scripts" |
| 308 | BUILD_LIBRARY_DIR="${SCRIPTS_DIR}/build_library" |
Mike Frysinger | 66accd2 | 2017-09-13 03:50:30 -0400 | [diff] [blame] | 309 | CHROMITE_BIN="${GCLIENT_ROOT}/chromite/bin" |
Alex Klein | 999443c | 2018-10-17 12:02:05 -0600 | [diff] [blame] | 310 | IMAGES_DIR="${GCLIENT_ROOT}/src/build/images" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 311 | |
| 312 | # Load developer's custom settings. Default location is in scripts dir, |
| 313 | # since that's available both inside and outside the chroot. By convention, |
| 314 | # settings from this file are variables starting with 'CHROMEOS_' |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 315 | : ${CHROMEOS_DEV_SETTINGS:=${SCRIPTS_DIR}/.chromeos_dev} |
| 316 | if [[ -f ${CHROMEOS_DEV_SETTINGS} ]]; then |
Mike Frysinger | 57ffdd5 | 2019-08-21 23:44:16 -0400 | [diff] [blame] | 317 | warn "Support for .chromeos_dev is being removed. If you have a need for" |
| 318 | warn "this still, please contact chromium-os-dev@chromium.org." |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 319 | # Turn on exit-on-error during custom settings processing |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 320 | SAVE_OPTS=$(set +o) |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 321 | switch_to_strict_mode |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 322 | |
| 323 | # Read settings |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 324 | . "${CHROMEOS_DEV_SETTINGS}" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 325 | |
| 326 | # Restore previous state of exit-on-error |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 327 | eval "${SAVE_OPTS}" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 328 | fi |
| 329 | |
| 330 | # Load shflags |
Zdenek Behan | 07d2422 | 2011-11-02 00:46:25 +0000 | [diff] [blame] | 331 | # NOTE: This code snippet is in particular used by the au-generator (which |
| 332 | # stores shflags in ./lib/shflags/) and should not be touched. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 333 | if [[ -f ${SCRIPTS_DIR}/lib/shflags/shflags ]]; then |
Mike Frysinger | 77c674b | 2012-02-07 18:05:07 -0500 | [diff] [blame] | 334 | . "${SCRIPTS_DIR}/lib/shflags/shflags" || die "Couldn't find shflags" |
Zdenek Behan | 07d2422 | 2011-11-02 00:46:25 +0000 | [diff] [blame] | 335 | else |
| 336 | . ./lib/shflags/shflags || die "Couldn't find shflags" |
| 337 | fi |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 338 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 339 | # Our local mirror |
| 340 | DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 341 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 342 | # Upstream mirrors and build suites come in 2 flavors |
| 343 | # DEV - development chroot, used to build the chromeos image |
| 344 | # IMG - bootable image, to run on actual hardware |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 345 | |
Bill Richardson | 10d27c2 | 2010-01-20 13:38:50 -0800 | [diff] [blame] | 346 | DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 347 | DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"} |
| 348 | |
| 349 | DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"} |
| 350 | DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 351 | |
| 352 | # Default location for chroot |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 353 | DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"${GCLIENT_ROOT}/chroot"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 354 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 355 | # 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] | 356 | # they don't pollute the source directory. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 357 | DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"${SRC_ROOT}/build"} |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 358 | |
Chris Ching | 4bc95a1 | 2016-11-22 13:44:13 -0700 | [diff] [blame] | 359 | # Default location for event files |
| 360 | DEFAULT_EVENT_DIR=${DEFAULT_EVENT_DIR:-"${DEFAULT_BUILD_ROOT}/events"} |
| 361 | |
| 362 | # Default event file. Format is YYYYDD.HHMM.json |
| 363 | DEFAULT_EVENT_FILE=${DEFAULT_EVENT_FILE:-"${DEFAULT_EVENT_DIR}/$(date +%Y%m%d.%H%M.).json"} |
| 364 | |
Mike Frysinger | c17a493 | 2012-03-12 17:07:40 -0400 | [diff] [blame] | 365 | # Sets the default board variable for calling script. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 366 | if [[ -f ${GCLIENT_ROOT}/src/scripts/.default_board ]]; then |
| 367 | DEFAULT_BOARD=$(<"${GCLIENT_ROOT}/src/scripts/.default_board") |
Mike Frysinger | c17a493 | 2012-03-12 17:07:40 -0400 | [diff] [blame] | 368 | # Check for user typos like whitespace. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 369 | if [[ -n ${DEFAULT_BOARD//[a-zA-Z0-9-_]} ]]; then |
Mike Frysinger | c17a493 | 2012-03-12 17:07:40 -0400 | [diff] [blame] | 370 | die ".default_board: invalid name detected; please fix:" \ |
| 371 | "'${DEFAULT_BOARD}'" |
| 372 | fi |
| 373 | fi |
David McMahon | 4930294 | 2010-02-18 16:55:35 -0800 | [diff] [blame] | 374 | |
David James | ff07201 | 2010-11-30 13:22:05 -0800 | [diff] [blame] | 375 | # Enable --fast by default. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 376 | DEFAULT_FAST=${FLAGS_TRUE} |
David James | 0366864 | 2010-07-28 17:08:29 -0700 | [diff] [blame] | 377 | |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 378 | # Directory to store built images. Should be set by sourcing script when used. |
| 379 | BUILD_DIR= |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 380 | |
Mike Frysinger | 34808e5 | 2017-01-11 21:14:08 -0500 | [diff] [blame] | 381 | # Path to the verified boot directory where we get signing related keys/scripts. |
| 382 | VBOOT_DIR="${CHROOT_TRUNK_DIR}/src/platform/vboot_reference" |
| 383 | VBOOT_TESTKEYS_DIR="${VBOOT_DIR}/tests/testkeys" |
Mike Frysinger | 40cea03 | 2017-03-12 19:48:20 -0400 | [diff] [blame] | 384 | # We load these from the chroot rather than directly from the vboot source repo |
| 385 | # so we work correctly even in a minilayout. |
| 386 | VBOOT_DEVKEYS_DIR="/usr/share/vboot/devkeys" |
| 387 | VBOOT_SIGNING_DIR="/usr/share/vboot/bin" |
Mike Frysinger | 34808e5 | 2017-01-11 21:14:08 -0500 | [diff] [blame] | 388 | |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 389 | # Standard filenames |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 390 | CHROMEOS_BASE_IMAGE_NAME="chromiumos_base_image.bin" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 391 | CHROMEOS_IMAGE_NAME="chromiumos_image.bin" |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 392 | CHROMEOS_DEVELOPER_IMAGE_NAME="chromiumos_image.bin" |
Gilad Arnold | 0836627 | 2012-02-08 10:46:26 -0800 | [diff] [blame] | 393 | CHROMEOS_RECOVERY_IMAGE_NAME="recovery_image.bin" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 394 | CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin" |
Chris Sosa | b0f5732 | 2011-10-25 03:07:23 +0000 | [diff] [blame] | 395 | CHROMEOS_FACTORY_INSTALL_SHIM_NAME="factory_install_shim.bin" |
Bertrand SIMONNET | b17cd57 | 2015-04-01 10:27:37 -0700 | [diff] [blame] | 396 | SYSROOT_SETTINGS_FILE="/var/cache/edb/chromeos" |
Simon Glass | 142ca06 | 2011-02-09 13:39:43 -0800 | [diff] [blame] | 397 | |
Rahul Chaudhry | 306f688 | 2015-05-13 15:14:13 -0700 | [diff] [blame] | 398 | # Install mask for portage ebuilds. Used by build_image and gmergefs. |
Chris Masone | d11ce17 | 2010-11-09 14:22:08 -0800 | [diff] [blame] | 399 | # TODO: Is /usr/local/autotest-chrome still used by anyone? |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 400 | COMMON_INSTALL_MASK=" |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 401 | *.a |
Alex Deymo | 1856c4f | 2014-09-24 08:15:34 -0700 | [diff] [blame] | 402 | *.c |
| 403 | *.cc |
Rahul Chaudhry | 306f688 | 2015-05-13 15:14:13 -0700 | [diff] [blame] | 404 | *.go |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 405 | *.la |
Rene Bolldorf | 3222822 | 2012-07-21 14:18:22 -0400 | [diff] [blame] | 406 | *.h |
Brian Norris | ef0880d | 2016-05-19 12:39:56 -0700 | [diff] [blame] | 407 | *.hh |
Rene Bolldorf | 3222822 | 2012-07-21 14:18:22 -0400 | [diff] [blame] | 408 | *.hpp |
Kevin Cernekee | 6ab57a9 | 2015-06-25 15:43:06 -0700 | [diff] [blame] | 409 | *.h++ |
Nam T. Nguyen | aa15f14 | 2015-05-15 14:27:50 -0700 | [diff] [blame] | 410 | *.hxx |
Mike Frysinger | 97c6ac9 | 2014-06-16 00:06:37 -0400 | [diff] [blame] | 411 | */.keep* |
Shuhei Takahashi | c62d307 | 2019-01-29 18:04:35 +0900 | [diff] [blame] | 412 | /build/libexec/tast |
| 413 | /build/share/tast |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 414 | /etc/init.d |
| 415 | /etc/runlevels |
Qijiang Fan | 6834cba | 2018-04-04 16:41:56 +0900 | [diff] [blame] | 416 | /etc/selinux/intermediates |
Hung-Te Lin | 76272be | 2012-08-07 19:10:09 +0800 | [diff] [blame] | 417 | /firmware |
Mike Frysinger | 785f09d | 2013-09-25 22:11:21 -0400 | [diff] [blame] | 418 | /lib/modules/*/vdso |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 419 | /lib/rc |
Tomasz Figa | 435f0e5 | 2017-01-06 14:28:06 +0900 | [diff] [blame] | 420 | /opt/google/containers/android/vendor/lib*/pkgconfig |
Benjamin Gordon | ffcb2b1 | 2017-10-24 13:02:46 -0600 | [diff] [blame] | 421 | /opt/google/containers/android/build |
Nam T. Nguyen | f1de1a7 | 2015-05-27 08:37:46 -0700 | [diff] [blame] | 422 | /usr/bin/*-config |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 423 | /usr/bin/Xnest |
| 424 | /usr/bin/Xvfb |
Manoj Gupta | 019843b | 2017-08-17 13:28:18 -0700 | [diff] [blame] | 425 | /usr/include/c++ |
Mike Frysinger | 10c5647 | 2013-03-01 00:58:10 -0500 | [diff] [blame] | 426 | /usr/include/nspr/* |
Yunlian Jiang | ed7cd2d | 2018-10-27 13:07:02 -0700 | [diff] [blame] | 427 | /usr/include/rpcsvc/*.x |
Bing Xue | 18e3758 | 2019-01-17 17:04:54 -0800 | [diff] [blame] | 428 | /usr/include/tensorflow |
Yufeng Wang | 6dea68d | 2019-05-13 11:52:51 -0700 | [diff] [blame] | 429 | /usr/include/boost |
Mike Frysinger | 10c5647 | 2013-03-01 00:58:10 -0500 | [diff] [blame] | 430 | /usr/include/X11/* |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 431 | /usr/lib/debug |
Rahul Chaudhry | 306f688 | 2015-05-13 15:14:13 -0700 | [diff] [blame] | 432 | /usr/lib/gopath |
Mike Frysinger | 27b5edb | 2012-08-08 18:52:06 -0400 | [diff] [blame] | 433 | /usr/lib*/pkgconfig |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 434 | /usr/local/autotest-chrome |
| 435 | /usr/man |
| 436 | /usr/share/aclocal |
Brian Norris | 0f3c4e2 | 2016-06-21 12:23:18 -0700 | [diff] [blame] | 437 | /usr/share/cups/drv |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 438 | /usr/share/doc |
| 439 | /usr/share/gettext |
| 440 | /usr/share/gtk-2.0 |
| 441 | /usr/share/gtk-doc |
| 442 | /usr/share/info |
| 443 | /usr/share/man |
Brian Norris | 0f3c4e2 | 2016-06-21 12:23:18 -0700 | [diff] [blame] | 444 | /usr/share/ppd |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 445 | /usr/share/openrc |
| 446 | /usr/share/pkgconfig |
Ryan Harrison | b8bd594 | 2013-02-02 13:39:43 -0500 | [diff] [blame] | 447 | /usr/share/profiling |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 448 | /usr/share/readline |
Chris Wolfe | d13775f | 2011-07-26 16:34:38 -0400 | [diff] [blame] | 449 | /usr/src |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 450 | " |
Chris Sosa | aa1a7fd | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 451 | |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 452 | # Mask for base, dev, and test images (build_image, build_image --test) |
| 453 | DEFAULT_INSTALL_MASK=" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 454 | ${COMMON_INSTALL_MASK} |
Don Garrett | ac1cd0d | 2013-07-18 18:04:06 -0700 | [diff] [blame] | 455 | /boot/config-* |
| 456 | /boot/System.map-* |
Aviv Keshet | a1838ba | 2013-07-23 10:58:23 -0700 | [diff] [blame] | 457 | /usr/local/build/autotest |
Olof Johansson | 1222b2e | 2012-08-08 15:27:35 -0700 | [diff] [blame] | 458 | /lib/modules/*/build |
| 459 | /lib/modules/*/source |
Kees Cook | 74f163b | 2012-12-18 15:46:51 -0800 | [diff] [blame] | 460 | test_*.ko |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 461 | " |
| 462 | |
Chris Sosa | c9422fa | 2012-03-05 15:58:07 -0800 | [diff] [blame] | 463 | # Mask for factory install shim (build_image factory_install) |
Hung-Te Lin | d32c59f | 2012-01-19 19:54:01 +0800 | [diff] [blame] | 464 | FACTORY_SHIM_INSTALL_MASK=" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 465 | ${DEFAULT_INSTALL_MASK} |
Cheng-Han Yang | c924c2c | 2018-11-02 17:02:29 +0800 | [diff] [blame] | 466 | /opt/google/chrome |
| 467 | /opt/google/containers |
Vic Yang | 982556a | 2012-12-11 11:41:54 +0800 | [diff] [blame] | 468 | /usr/lib64/dri |
Hung-Te Lin | 7f721ec | 2017-07-18 15:14:48 +0800 | [diff] [blame] | 469 | /usr/lib/dri |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 470 | /usr/share/X11 |
Hung-Te Lin | 7f721ec | 2017-07-18 15:14:48 +0800 | [diff] [blame] | 471 | /usr/share/chromeos-assets/[^i]* |
| 472 | /usr/share/chromeos-assets/i[^m]* |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 473 | /usr/share/fonts |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 474 | /usr/share/locale |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 475 | /usr/share/mime |
Vic Yang | 982556a | 2012-12-11 11:41:54 +0800 | [diff] [blame] | 476 | /usr/share/oem |
Daniel Erat | e82f07c | 2010-12-21 13:39:22 -0800 | [diff] [blame] | 477 | /usr/share/sounds |
| 478 | /usr/share/tts |
| 479 | /usr/share/zoneinfo |
| 480 | " |
Tom Wai-Hong Tam | f87a367 | 2010-05-17 16:06:33 +0800 | [diff] [blame] | 481 | |
Sabin Floares | e790ea7 | 2016-11-21 14:56:41 +0200 | [diff] [blame] | 482 | # Mask for images without systemd. |
| 483 | SYSTEMD_INSTALL_MASK=" |
Chris Morin | 83226ab | 2018-12-07 19:53:48 -0800 | [diff] [blame] | 484 | /lib/systemd/network |
| 485 | /usr/lib/systemd/system |
Sabin Floares | e790ea7 | 2016-11-21 14:56:41 +0200 | [diff] [blame] | 486 | " |
| 487 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 488 | # ----------------------------------------------------------------------------- |
| 489 | # Functions |
| 490 | |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 491 | # Enter a chroot and restart the current script if needed |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 492 | restart_in_chroot_if_needed() { |
David Rochberg | 3b91070 | 2010-12-02 10:45:21 -0500 | [diff] [blame] | 493 | # NB: Pass in ARGV: restart_in_chroot_if_needed "$@" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 494 | if [[ ${INSIDE_CHROOT} -ne 1 ]]; then |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 495 | # Get inside_chroot path for script. |
| 496 | local chroot_path="$(reinterpret_path_for_chroot "$0")" |
Mike Frysinger | 66accd2 | 2017-09-13 03:50:30 -0400 | [diff] [blame] | 497 | exec "${CHROMITE_BIN}/cros_sdk" -- "${chroot_path}" "$@" |
Don Garrett | 640a058 | 2010-05-04 16:54:28 -0700 | [diff] [blame] | 498 | fi |
| 499 | } |
| 500 | |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 501 | # Fail unless we're inside the chroot. This guards against messing up your |
| 502 | # workstation. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 503 | assert_inside_chroot() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 504 | if [[ ${INSIDE_CHROOT} -ne 1 ]]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 505 | echo "This script must be run inside the chroot. Run this first:" |
Zdenek Behan | 2811c16 | 2011-08-13 00:47:38 +0200 | [diff] [blame] | 506 | echo " cros_sdk" |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 507 | exit 1 |
| 508 | fi |
| 509 | } |
| 510 | |
| 511 | # Fail if we're inside the chroot. This guards against creating or entering |
| 512 | # nested chroots, among other potential problems. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 513 | assert_outside_chroot() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 514 | if [[ ${INSIDE_CHROOT} -ne 0 ]]; then |
rspangler@google.com | d74220d | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 515 | echo "This script must be run outside the chroot." |
| 516 | exit 1 |
| 517 | fi |
| 518 | } |
| 519 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 520 | assert_not_root_user() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 521 | if [[ ${UID:-$(id -u)} == 0 ]]; then |
derat@google.com | 86dcc8e | 2009-11-21 19:49:49 +0000 | [diff] [blame] | 522 | echo "This script must be run as a non-root user." |
| 523 | exit 1 |
| 524 | fi |
| 525 | } |
| 526 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 527 | assert_root_user() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 528 | if [[ ${UID:-$(id -u)} != 0 ]] || [[ ${SUDO_USER:-root} == "root" ]]; then |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 529 | die_notrace "This script must be run using sudo from a non-root user." |
| 530 | fi |
| 531 | } |
| 532 | |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 533 | # Writes stdin to the given file name as root using sudo in overwrite mode. |
| 534 | # |
| 535 | # $1 - The output file name. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 536 | sudo_clobber() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 537 | sudo tee "$1" >/dev/null |
tedbo | 373c390 | 2010-04-12 10:52:40 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 540 | # Execute multiple commands in a single sudo. Generally will speed things |
| 541 | # up by avoiding multiple calls to `sudo`. If any commands fail, we will |
| 542 | # call die with the failing command. We can handle a max of ~100 commands, |
| 543 | # but hopefully no one will ever try that many at once. |
| 544 | # |
| 545 | # $@ - The commands to execute, one per arg. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 546 | sudo_multi() { |
Mike Frysinger | 286b592 | 2011-09-28 11:59:53 -0400 | [diff] [blame] | 547 | local i cmds |
| 548 | |
| 549 | # Construct the shell code to execute. It'll be of the form: |
| 550 | # ... && ( ( command ) || exit <command index> ) && ... |
| 551 | # This way we know which command exited. The exit status of |
| 552 | # the underlying command is lost, but we never cared about it |
| 553 | # in the first place (other than it is non zero), so oh well. |
| 554 | for (( i = 1; i <= $#; ++i )); do |
| 555 | cmds+=" && ( ( ${!i} ) || exit $(( i + 10 )) )" |
| 556 | done |
| 557 | |
| 558 | # Execute our constructed shell code. |
| 559 | sudo -- sh -c ":${cmds[*]}" && i=0 || i=$? |
| 560 | |
| 561 | # See if this failed, and if so, print out the failing command. |
| 562 | if [[ $i -gt 10 ]]; then |
| 563 | : $(( i -= 10 )) |
| 564 | die "sudo_multi failed: ${!i}" |
| 565 | elif [[ $i -ne 0 ]]; then |
| 566 | die "sudo_multi failed for unknown reason $i" |
| 567 | fi |
| 568 | } |
| 569 | |
Chris Masone | bbccc24 | 2014-02-08 16:23:53 -0800 | [diff] [blame] | 570 | # Clears out stale shadow-utils locks in the given target root. |
| 571 | sudo_clear_shadow_locks() { |
| 572 | info "Clearing shadow utils lockfiles under $1" |
| 573 | sudo rm -f "$1/etc/"{passwd,group,shadow,gshadow}.lock* |
| 574 | } |
| 575 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 576 | # Writes stdin to the given file name as the sudo user in overwrite mode. |
| 577 | # |
| 578 | # $@ - The output file names. |
| 579 | user_clobber() { |
| 580 | install -m644 -o ${SUDO_UID} -g ${SUDO_GID} /dev/stdin "$@" |
| 581 | } |
| 582 | |
David James | 22dc2ba | 2012-11-29 15:46:58 -0800 | [diff] [blame] | 583 | # Copies the specified file owned by the user to the specified location. |
| 584 | # If the copy fails as root (e.g. due to root_squash and NFS), retry the copy |
| 585 | # with the user's account before failing. |
| 586 | user_cp() { |
| 587 | cp -p "$@" 2>/dev/null || sudo -u ${SUDO_USER} -- cp -p "$@" |
| 588 | } |
| 589 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 590 | # Appends stdin to the given file name as the sudo user. |
| 591 | # |
| 592 | # $1 - The output file name. |
| 593 | user_append() { |
| 594 | cat >> "$1" |
| 595 | chown ${SUDO_UID}:${SUDO_GID} "$1" |
| 596 | } |
| 597 | |
| 598 | # Create the specified directory, along with parents, as the sudo user. |
| 599 | # |
| 600 | # $@ - The directories to create. |
| 601 | user_mkdir() { |
| 602 | install -o ${SUDO_UID} -g ${SUDO_GID} -d "$@" |
| 603 | } |
| 604 | |
| 605 | # Create the specified symlink as the sudo user. |
| 606 | # |
| 607 | # $1 - Link target |
| 608 | # $2 - Link name |
| 609 | user_symlink() { |
| 610 | ln -sfT "$1" "$2" |
| 611 | chown -h ${SUDO_UID}:${SUDO_GID} "$2" |
| 612 | } |
| 613 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 614 | # Locate all mounts below a specified directory. |
| 615 | # |
| 616 | # $1 - The root tree. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 617 | sub_mounts() { |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 618 | # Assume that `mount` outputs a list of mount points in the order |
| 619 | # that things were mounted (since it always has and hopefully always |
| 620 | # will). As such, we have to unmount in reverse order to cleanly |
| 621 | # unmount submounts (think /dev/pts and /dev). |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 622 | awk -v path="$1" -v len="${#1}" \ |
Benjamin Gordon | 1d5afed | 2017-06-14 16:35:32 -0600 | [diff] [blame] | 623 | '(substr($2, 1, len+1) == path || |
| 624 | substr($2, 1, len+1) == (path "/")) { print $2 }' /proc/mounts | \ |
Zdenek Behan | 52970f4 | 2012-08-30 22:36:40 +0200 | [diff] [blame] | 625 | tac | \ |
| 626 | sed -e 's/\\040(deleted)$//' |
| 627 | # Hack(zbehan): If a bind mount's source is mysteriously removed, |
| 628 | # we'd end up with an orphaned mount with the above string in its name. |
| 629 | # It can only be seen through /proc/mounts and will stick around even |
| 630 | # when it should be gone already. crosbug.com/31250 |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 631 | } |
| 632 | |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 633 | # Unmounts a directory, if the unmount fails, warn, and then lazily unmount. |
| 634 | # |
| 635 | # $1 - The path to unmount. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 636 | safe_umount_tree() { |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 637 | local mount_point="$1" |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 638 | |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 639 | local mounts=( $(sub_mounts "${mount_point}") ) |
| 640 | |
| 641 | # Silently return if the mount_point was already unmounted. |
| 642 | if [[ ${#mounts[@]} -eq 0 ]]; then |
Mike Frysinger | e8aec37 | 2011-09-21 00:03:22 -0400 | [diff] [blame] | 643 | return 0 |
| 644 | fi |
| 645 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 646 | # First try to unmount in one shot to speed things up. |
Chirantan Ekbote | 30823e9 | 2016-06-29 11:39:03 -0700 | [diff] [blame] | 647 | if LC_ALL=C safe_umount -d "${mounts[@]}"; then |
| 648 | return 0 |
David James | d9b6798 | 2012-10-26 09:46:50 -0700 | [diff] [blame] | 649 | fi |
| 650 | |
| 651 | # Well that didn't work, so lazy unmount remaining ones. |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 652 | warn "Failed to unmount ${mounts[@]}, these are the processes using the" \ |
| 653 | "mount points." |
| 654 | sudo fuser -vm "${mount_point}" || true |
| 655 | |
Mike Frysinger | 1aa6124 | 2011-09-15 17:46:44 -0400 | [diff] [blame] | 656 | warn "Doing a lazy unmount" |
Alex Deymo | 7ba56be | 2015-02-12 14:28:06 -0800 | [diff] [blame] | 657 | if ! safe_umount -d -l "${mounts[@]}"; then |
| 658 | mounts=( $(sub_mounts "${mount_point}") ) |
| 659 | die "Failed to lazily unmount ${mounts[@]}" |
robotboy | 9891221 | 2010-04-12 14:08:14 -0700 | [diff] [blame] | 660 | fi |
| 661 | } |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 662 | |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 663 | |
David James | 7676488 | 2012-10-24 19:46:29 -0700 | [diff] [blame] | 664 | # Run umount as root. |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 665 | safe_umount() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 666 | $([[ ${UID:-$(id -u)} != 0 ]] && echo sudo) umount "$@" |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 669 | # Setup a loopback device for a file and scan for partitions, with retries. |
| 670 | # |
| 671 | # $1 - The file to back the new loopback device. |
| 672 | # $2-$N - Additional arguments to pass to losetup. |
| 673 | loopback_partscan() { |
| 674 | local lb_dev image="$1" |
| 675 | shift |
Mike Frysinger | ad3b200 | 2019-08-21 23:35:59 -0400 | [diff] [blame^] | 676 | lb_dev=$(sudo losetup --show -f "$@" "${image}") |
Gabe Black | 01ba4de | 2015-03-11 16:34:56 -0700 | [diff] [blame] | 677 | # Ignore problems deleting existing partitions. There shouldn't be any |
| 678 | # which will upset partx, but that's actually ok. |
Mike Frysinger | ad3b200 | 2019-08-21 23:35:59 -0400 | [diff] [blame^] | 679 | sudo partx -d "${lb_dev}" 2>/dev/null || true |
| 680 | sudo partx -a "${lb_dev}" |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 681 | |
Gabe Black | 7bb6c5a | 2015-03-16 20:23:06 -0700 | [diff] [blame] | 682 | echo "${lb_dev}" |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | # Detach a loopback device set up earlier. |
| 686 | # |
Gabe Black | 01ba4de | 2015-03-11 16:34:56 -0700 | [diff] [blame] | 687 | # $1 - The loop device to detach. |
| 688 | # $2-$N - Additional arguments to pass to losetup. |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 689 | loopback_detach() { |
Mike Frysinger | af64ed5 | 2015-04-06 13:20:39 -0400 | [diff] [blame] | 690 | # Retry the deletes before we detach. crbug.com/469259 |
| 691 | local i |
| 692 | for (( i = 0; i < 10; i++ )); do |
Mike Frysinger | ad3b200 | 2019-08-21 23:35:59 -0400 | [diff] [blame^] | 693 | if sudo partx -d "$1"; then |
Mike Frysinger | af64ed5 | 2015-04-06 13:20:39 -0400 | [diff] [blame] | 694 | break |
| 695 | fi |
| 696 | warn "Sleeping & retrying ..." |
| 697 | sync |
| 698 | sleep 1 |
| 699 | done |
Mike Frysinger | ad3b200 | 2019-08-21 23:35:59 -0400 | [diff] [blame^] | 700 | sudo losetup --detach "$@" |
Gabe Black | fbdb1d5 | 2014-09-22 23:43:35 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 703 | # Sets up symlinks for the developer root. It is necessary to symlink |
| 704 | # usr and local since the developer root is mounted at /usr/local and |
| 705 | # applications expect to be installed under /usr/local/bin, etc. |
| 706 | # This avoids packages installing into /usr/local/usr/local/bin. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 707 | # $1 specifies the symlink target for the developer root. |
| 708 | # $2 specifies the symlink target for the var directory. |
| 709 | # $3 specifies the location of the stateful partition. |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 710 | setup_symlinks_on_root() { |
| 711 | # Give args better names. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 712 | local dev_image_target=$1 |
| 713 | local var_target=$2 |
| 714 | local dev_image_root="$3/dev_image" |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 715 | |
Mike Frysinger | 9e15538 | 2019-07-12 21:11:00 -0400 | [diff] [blame] | 716 | # Make sure the dev_image dir itself exists. |
| 717 | if [[ ! -d "${dev_image_root}" ]]; then |
| 718 | sudo mkdir "${dev_image_root}" |
| 719 | fi |
| 720 | |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 721 | # If our var target is actually the standard var, we are cleaning up the |
| 722 | # symlinks (could also check for /usr/local for the dev_image_target). |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 723 | if [[ ${var_target} == "/var" ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 724 | echo "Cleaning up /usr/local symlinks for ${dev_image_root}" |
| 725 | else |
| 726 | echo "Setting up symlinks for /usr/local for ${dev_image_root}" |
| 727 | fi |
| 728 | |
| 729 | # Set up symlinks that should point to ${dev_image_target}. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 730 | local path |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 731 | for path in usr local; do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 732 | if [[ -h ${dev_image_root}/${path} ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 733 | sudo unlink "${dev_image_root}/${path}" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 734 | elif [[ -e ${dev_image_root}/${path} ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 735 | die "${dev_image_root}/${path} should be a symlink if exists" |
| 736 | fi |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 737 | sudo ln -s "${dev_image_target}" "${dev_image_root}/${path}" |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 738 | done |
| 739 | |
| 740 | # Setup var symlink. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 741 | if [[ -h ${dev_image_root}/var ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 742 | sudo unlink "${dev_image_root}/var" |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 743 | elif [[ -e ${dev_image_root}/var ]]; then |
Chris Sosa | 702618f | 2010-05-14 12:52:32 -0700 | [diff] [blame] | 744 | die "${dev_image_root}/var should be a symlink if it exists" |
| 745 | fi |
| 746 | |
| 747 | sudo ln -s "${var_target}" "${dev_image_root}/var" |
| 748 | } |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 749 | |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 750 | # These two helpers clobber the ro compat value in our root filesystem. |
| 751 | # |
| 752 | # When the system is built with --enable_rootfs_verification, bit-precise |
| 753 | # integrity checking is performed. That precision poses a usability issue on |
| 754 | # systems that automount partitions with recognizable filesystems, such as |
| 755 | # ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be |
| 756 | # automatically updated even if no other writes are performed to the |
| 757 | # filesystem. In addition, ext2+ does not support a "read-only" flag for a |
| 758 | # given filesystem. That said, forward and backward compatibility of |
| 759 | # filesystem features are supported by tracking if a new feature breaks r/w or |
| 760 | # just write compatibility. We abuse the read-only compatibility flag[1] in |
| 761 | # the filesystem header by setting the high order byte (le) to FF. This tells |
| 762 | # the kernel that features R24-R31 are all enabled. Since those features are |
| 763 | # undefined on all ext-based filesystem, all standard kernels will refuse to |
| 764 | # mount the filesystem as read-write -- only read-only[2]. |
| 765 | # |
| 766 | # [1] 32-bit flag we are modifying: |
Aaron Gable | 5dde848 | 2013-09-27 18:35:57 -0700 | [diff] [blame] | 767 | # https://chromium.googlesource.com/chromiumos/third_party/kernel.git/+/master/include/linux/ext2_fs.h#l417 |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 768 | # [2] Mount behavior is enforced here: |
Aaron Gable | 5dde848 | 2013-09-27 18:35:57 -0700 | [diff] [blame] | 769 | # https://chromium.googlesource.com/chromiumos/third_party/kernel.git/+/master/ext2/super.c#l857 |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 770 | # |
| 771 | # N.B., if the high order feature bits are used in the future, we will need to |
| 772 | # revisit this technique. |
| 773 | disable_rw_mount() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 774 | local rootfs=$1 |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 775 | local offset="${2-0}" # in bytes |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 776 | local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
Alex Deymo | f6a527d | 2015-01-12 18:30:58 -0800 | [diff] [blame] | 777 | is_ext_filesystem "${rootfs}" "${offset}" || return 0 |
Alex Deymo | 4122631 | 2015-03-09 15:35:24 -0700 | [diff] [blame] | 778 | is_ext2_rw_mount_enabled "${rootfs}" "${offset}" || return 0 |
| 779 | |
| 780 | make_block_device_rw "${rootfs}" |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 781 | printf '\377' | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 782 | sudo dd of="${rootfs}" seek=$((offset + ro_compat_offset)) \ |
Alex Deymo | 4122631 | 2015-03-09 15:35:24 -0700 | [diff] [blame] | 783 | conv=notrunc count=1 bs=1 status=none |
LaMont Jones | 7074374 | 2019-04-26 10:14:33 -0600 | [diff] [blame] | 784 | # Force all of the file writes to complete, in case it's necessary for |
| 785 | # crbug.com/954188 |
| 786 | sync |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | enable_rw_mount() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 790 | local rootfs=$1 |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 791 | local offset="${2-0}" |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 792 | local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
Alex Deymo | f6a527d | 2015-01-12 18:30:58 -0800 | [diff] [blame] | 793 | is_ext_filesystem "${rootfs}" "${offset}" || return 0 |
Alex Deymo | 4122631 | 2015-03-09 15:35:24 -0700 | [diff] [blame] | 794 | is_ext2_rw_mount_enabled "${rootfs}" "${offset}" && return 0 |
| 795 | |
| 796 | make_block_device_rw "${rootfs}" |
Will Drewry | 9b7cb51 | 2010-10-20 18:11:24 -0500 | [diff] [blame] | 797 | printf '\000' | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 798 | sudo dd of="${rootfs}" seek=$((offset + ro_compat_offset)) \ |
Alex Deymo | 4122631 | 2015-03-09 15:35:24 -0700 | [diff] [blame] | 799 | conv=notrunc count=1 bs=1 status=none |
LaMont Jones | 7074374 | 2019-04-26 10:14:33 -0600 | [diff] [blame] | 800 | # Force all of the file writes to complete, in case it's necessary for |
| 801 | # crbug.com/954188 |
| 802 | sync |
Alex Deymo | 4122631 | 2015-03-09 15:35:24 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | is_ext2_rw_mount_enabled() { |
| 806 | local rootfs=$1 |
| 807 | local offset="${2-0}" |
| 808 | local ro_compat_offset=$((0x464 + 3)) # Get 'highest' byte |
| 809 | local ro_compat_flag=$(sudo dd if="${rootfs}" \ |
| 810 | skip=$((offset + ro_compat_offset)) bs=1 count=1 status=none \ |
| 811 | 2>/dev/null | hexdump -e '1 "%.2x"') |
| 812 | test "${ro_compat_flag}" = "00" |
Will Drewry | 55b42c9 | 2010-10-20 15:44:11 -0500 | [diff] [blame] | 813 | } |
| 814 | |
Alex Deymo | f923595 | 2015-01-15 11:15:09 -0800 | [diff] [blame] | 815 | # Returns whether the passed rootfs is an extended filesystem by checking the |
Alex Deymo | f6a527d | 2015-01-12 18:30:58 -0800 | [diff] [blame] | 816 | # ext2 s_magic field in the superblock. |
| 817 | is_ext_filesystem() { |
| 818 | local rootfs=$1 |
| 819 | local offset="${2-0}" |
| 820 | local ext_magic_offset=$((0x400 + 56)) |
Alex Deymo | 4122631 | 2015-03-09 15:35:24 -0700 | [diff] [blame] | 821 | local ext_magic=$(sudo dd if="${rootfs}" \ |
| 822 | skip=$((offset + ext_magic_offset)) bs=1 count=2 2>/dev/null | |
| 823 | hexdump -e '1/2 "%.4x"') |
Alex Deymo | f6a527d | 2015-01-12 18:30:58 -0800 | [diff] [blame] | 824 | test "${ext_magic}" = "ef53" |
| 825 | } |
| 826 | |
Alex Deymo | 4122631 | 2015-03-09 15:35:24 -0700 | [diff] [blame] | 827 | # If the passed argument is a block device, ensure it is writtable and make it |
| 828 | # writtable if not. |
| 829 | make_block_device_rw() { |
| 830 | local block_dev="$1" |
| 831 | [[ -b "${block_dev}" ]] || return 0 |
| 832 | if [[ $(sudo blockdev --getro "${block_dev}") == "1" ]]; then |
| 833 | sudo blockdev --setrw "${block_dev}" |
| 834 | fi |
| 835 | } |
| 836 | |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 837 | # Get current timestamp. Assumes common.sh runs at startup. |
| 838 | start_time=$(date +%s) |
| 839 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 840 | # Get time elapsed since start_time in seconds. |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 841 | _get_elapsed_seconds() { |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 842 | local end_time=$(date +%s) |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 843 | local elapsed_seconds=$(( end_time - start_time )) |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 844 | echo ${elapsed_seconds} |
| 845 | } |
| 846 | |
| 847 | # Print time elapsed since start_time. |
| 848 | print_time_elapsed() { |
| 849 | # Optional first arg to specify elapsed_seconds. If not given, will |
| 850 | # recalculate elapsed time to now. Optional second arg to specify |
| 851 | # command name associated with elapsed time. |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 852 | local elapsed_seconds=${1:-$(_get_elapsed_seconds)} |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 853 | local cmd_base=${2:-} |
| 854 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 855 | local minutes=$(( elapsed_seconds / 60 )) |
| 856 | local seconds=$(( elapsed_seconds % 60 )) |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 857 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 858 | if [[ -n ${cmd_base} ]]; then |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 859 | info "Elapsed time (${cmd_base}): ${minutes}m${seconds}s" |
| 860 | else |
| 861 | info "Elapsed time: ${minutes}m${seconds}s" |
| 862 | fi |
| 863 | } |
| 864 | |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 865 | command_completed() { |
| 866 | # Call print_elapsed_time regardless. |
Mike Frysinger | bbec711 | 2019-08-22 00:44:29 -0400 | [diff] [blame] | 867 | local run_time=$(_get_elapsed_seconds) |
Mike Frysinger | 3131d41 | 2019-03-02 14:09:37 -0500 | [diff] [blame] | 868 | local cmd_base=$(basename "$0") |
Matt Tennant | 298f61a | 2012-06-25 21:54:33 -0700 | [diff] [blame] | 869 | print_time_elapsed ${run_time} ${cmd_base} |
Nick Sanders | d250927 | 2010-06-16 03:50:04 -0700 | [diff] [blame] | 870 | } |
Doug Anderson | 0c9e88d | 2010-10-19 14:49:39 -0700 | [diff] [blame] | 871 | |
Albert Chaulk | 5d190f7 | 2013-07-12 11:42:18 -0700 | [diff] [blame] | 872 | # Load configuration files that allow board-specific overrides of default |
| 873 | # functionality to be specified in overlays. |
Steve Fung | d3b1f5f | 2015-03-29 21:47:24 -0700 | [diff] [blame] | 874 | # $1 - File to load. |
Albert Chaulk | 5d190f7 | 2013-07-12 11:42:18 -0700 | [diff] [blame] | 875 | load_board_specific_script() { |
Steve Fung | 88897cc | 2015-03-25 13:38:38 -0700 | [diff] [blame] | 876 | local file=$1 overlay |
| 877 | [[ $# -ne 1 ]] && die "load_board_specific_script requires exactly 1 param" |
| 878 | for overlay in ${BOARD_OVERLAY}; do |
Albert Chaulk | 5d190f7 | 2013-07-12 11:42:18 -0700 | [diff] [blame] | 879 | local setup_sh="${overlay}/scripts/${file}" |
| 880 | if [[ -e ${setup_sh} ]]; then |
| 881 | source "${setup_sh}" |
| 882 | fi |
| 883 | done |
| 884 | } |
| 885 | |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 886 | # Reinterprets path from outside the chroot for use inside. |
| 887 | # Returns "" if "" given. |
| 888 | # $1 - The path to reinterpret. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 889 | reinterpret_path_for_chroot() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 890 | if [[ ${INSIDE_CHROOT} -ne 1 ]]; then |
| 891 | if [[ -z $1 ]]; then |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 892 | echo "" |
| 893 | else |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 894 | local path_abs_path=$(readlink -f "$1") |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 895 | local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}") |
| 896 | |
| 897 | # Strip the repository root from the path. |
| 898 | local relative_path=$(echo ${path_abs_path} \ |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 899 | | sed "s:${gclient_root_abs_path}/::") |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 900 | |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 901 | if [[ ${relative_path} == "${path_abs_path}" ]]; then |
| 902 | die "Error reinterpreting path. Path $1 is not within source tree." |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 903 | fi |
| 904 | |
| 905 | # Prepend the chroot repository path. |
Chris Sosa | ed2ff4b | 2013-08-30 12:58:41 -0700 | [diff] [blame] | 906 | echo "/mnt/host/source/${relative_path}" |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 907 | fi |
| 908 | else |
| 909 | # Path is already inside the chroot :). |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 910 | echo "$1" |
Chris Sosa | fd2cdec | 2011-03-24 16:06:59 -0700 | [diff] [blame] | 911 | fi |
| 912 | } |
Gabe Black | 83d8b82 | 2011-08-01 17:50:09 -0700 | [diff] [blame] | 913 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 914 | emerge_custom_kernel() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 915 | local install_root=$1 |
David James | dee866c | 2012-03-15 14:53:19 -0700 | [diff] [blame] | 916 | local root=/build/${FLAGS_board} |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 917 | local tmp_pkgdir=${root}/custom-packages |
| 918 | |
| 919 | # Clean up any leftover state in custom directories. |
Mike Frysinger | a1a06ab | 2011-08-10 11:40:30 -0400 | [diff] [blame] | 920 | sudo rm -rf "${tmp_pkgdir}" |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 921 | |
Aviv Keshet | 1437391 | 2014-06-27 14:34:06 +0000 | [diff] [blame] | 922 | # Update chromeos-initramfs to contain the latest binaries from the build |
| 923 | # tree. This is basically just packaging up already-built binaries from |
| 924 | # ${root}. We are careful not to muck with the existing prebuilts so that |
| 925 | # prebuilts can be uploaded in parallel. |
| 926 | # TODO(davidjames): Implement ABI deps so that chromeos-initramfs will be |
| 927 | # rebuilt automatically when its dependencies change. |
| 928 | sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} -1 \ |
| 929 | chromeos-base/chromeos-initramfs || die "Cannot emerge chromeos-initramfs" |
| 930 | |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 931 | # Verify all dependencies of the kernel are installed. This should be a |
| 932 | # no-op, but it's good to check in case a developer didn't run |
Mike Frysinger | 0957a18 | 2012-03-21 23:17:14 -0400 | [diff] [blame] | 933 | # build_packages. We need the expand_virtual call to workaround a bug |
| 934 | # in portage where it only installs the virtual pkg. |
| 935 | local kernel=$(portageq-${FLAGS_board} expand_virtual ${root} \ |
| 936 | virtual/linux-sources) |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 937 | sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} --onlydeps \ |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 938 | ${kernel} || die "Cannot emerge kernel dependencies" |
| 939 | |
| 940 | # Build the kernel. This uses the standard root so that we can pick up the |
| 941 | # initramfs from there. But we don't actually install the kernel to the |
| 942 | # standard root, because that'll muck up the kernel debug symbols there, |
| 943 | # which we want to upload in parallel. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 944 | sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} --buildpkgonly \ |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 945 | ${kernel} || die "Cannot emerge kernel" |
| 946 | |
| 947 | # Install the custom kernel to the provided install root. |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 948 | sudo -E PKGDIR="${tmp_pkgdir}" ${EMERGE_BOARD_CMD} --usepkgonly \ |
David James | 0ea96e4 | 2011-08-03 11:53:50 -0700 | [diff] [blame] | 949 | --root=${install_root} ${kernel} || die "Cannot emerge kernel to root" |
| 950 | } |
Brian Harring | feb04f7 | 2012-02-03 21:22:50 -0800 | [diff] [blame] | 951 | |
David James | 855afb7 | 2012-03-14 20:04:59 -0700 | [diff] [blame] | 952 | # Display --help if requested. This is used to hide options from help |
| 953 | # that are not intended for developer use. |
| 954 | # |
| 955 | # How to use: |
| 956 | # 1) Declare the options that you want to appear in help. |
| 957 | # 2) Call this function. |
| 958 | # 3) Declare the options that you don't want to appear in help. |
| 959 | # |
| 960 | # See build_packages for example usage. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 961 | show_help_if_requested() { |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 962 | local opt |
David James | 855afb7 | 2012-03-14 20:04:59 -0700 | [diff] [blame] | 963 | for opt in "$@"; do |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 964 | if [[ ${opt} == "-h" || ${opt} == "--help" ]]; then |
David James | 855afb7 | 2012-03-14 20:04:59 -0700 | [diff] [blame] | 965 | flags_help |
| 966 | exit 0 |
| 967 | fi |
| 968 | done |
| 969 | } |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 970 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 971 | switch_to_strict_mode() { |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 972 | # Set up strict execution mode; note that the trap |
| 973 | # must follow switch_to_strict_mode, else it will have no effect. |
| 974 | set -e |
Mike Frysinger | 9ebc8ce | 2015-04-06 13:15:01 -0400 | [diff] [blame] | 975 | trap 'die_err_trap' ERR |
Mike Frysinger | baae8eb | 2013-01-15 01:34:26 -0500 | [diff] [blame] | 976 | if [[ $# -ne 0 ]]; then |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 977 | set "$@" |
| 978 | fi |
| 979 | } |
| 980 | |
| 981 | # TODO: Re-enable this once shflags is set -e safe. |
| 982 | #switch_to_strict_mode |