forked from coreos/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
1066 lines (943 loc) · 31.4 KB
/
common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# All scripts should die on error unless commands are specifically excepted
# by prefixing with '!' or surrounded by 'set +e' / 'set -e'.
# The number of jobs to pass to tools that can run in parallel (such as make
# and dpkg-buildpackage
if [[ -z ${NUM_JOBS} ]]; then
NUM_JOBS=$(grep -c "^processor" /proc/cpuinfo)
fi
# Ensure that any sub scripts we invoke get the max proc count.
export NUM_JOBS
# Make sure we have the location and name of the calling script, using
# the current value if it is already set.
: ${SCRIPT_LOCATION:=$(dirname "$(readlink -f "$0")")}
: ${SCRIPT_NAME:=$(basename "$0")}
# Detect whether we're inside a chroot or not
if [[ -e /etc/debian_chroot ]]; then
INSIDE_CHROOT=1
else
INSIDE_CHROOT=0
fi
# Determine and set up variables needed for fancy color output (if supported).
V_BOLD_RED=
V_BOLD_GREEN=
V_BOLD_YELLOW=
V_REVERSE=
V_VIDOFF=
if tput colors >&/dev/null; then
# order matters: we want VIDOFF last so that when we trace with `set -x`,
# our terminal doesn't bleed colors as bash dumps the values of vars.
V_BOLD_RED=$(tput bold; tput setaf 1)
V_BOLD_GREEN=$(tput bold; tput setaf 2)
V_BOLD_YELLOW=$(tput bold; tput setaf 3)
V_REVERSE=$(tput rev)
V_VIDOFF=$(tput sgr0)
fi
# Turn on bash debug support if available for backtraces.
shopt -s extdebug 2>/dev/null
# Output a backtrace all the way back to the raw invocation, suppressing
# only the _dump_trace frame itself.
_dump_trace() {
local j n p func src line args
p=${#BASH_ARGV[@]}
for (( n = ${#FUNCNAME[@]}; n > 1; --n )); do
func=${FUNCNAME[${n} - 1]}
src=${BASH_SOURCE[${n}]##*/}
line=${BASH_LINENO[${n} - 1]}
args=
if [[ -z ${BASH_ARGC[${n} -1]} ]]; then
args='(args unknown, no debug available)'
else
for (( j = 0; j < ${BASH_ARGC[${n} -1]}; ++j )); do
args="${args:+${args} }'${BASH_ARGV[$(( p - j - 1 ))]}'"
done
! (( p -= ${BASH_ARGC[${n} - 1]} ))
fi
if [[ ${n} == ${#FUNCNAME[@]} ]]; then
error "script called: ${0##*/} ${args}"
error "Backtrace: (most recent call is last)"
else
error "$(printf ' file %s, line %s, called: %s %s' \
"${src}" "${line}" "${func}" "${args}")"
fi
done
}
# Declare these asap so that code below can safely assume they exist.
_message() {
local prefix="$1${CROS_LOG_PREFIX:-${SCRIPT_NAME}}"
shift
if [[ $# -eq 0 ]]; then
echo -e "${prefix}:${V_VIDOFF}" >&2
return
fi
(
# Handle newlines in the message, prefixing each chunk correctly.
# Do this in a subshell to avoid having to track IFS/set -f state.
IFS="
"
set +f
set -- $*
IFS=' '
if [[ $# -eq 0 ]]; then
# Empty line was requested.
set -- ''
fi
for line in "$@"; do
echo -e "${prefix}: ${line}${V_VIDOFF}" >&2
done
)
}
info() {
_message "${V_BOLD_GREEN}INFO " "$*"
}
warn() {
_message "${V_BOLD_YELLOW}WARNING " "$*"
}
error() {
_message "${V_BOLD_RED}ERROR " "$*"
}
# For all die functions, they must explicitly force set +eu;
# no reason to have them cause their own crash if we're inthe middle
# of reporting an error condition then exiting.
die_err_trap() {
local command=$1 result=$2
set +e +u
# Per the message, bash misreports 127 as 1 during err trap sometimes.
# Note this fact to ensure users don't place too much faith in the
# exit code in that case.
set -- "Command '${command}' exited with nonzero code: ${result}"
if [[ ${result} -eq 1 ]] && [[ -z $(type -t ${command}) ]]; then
set -- "$@" \
'(Note bash sometimes misreports "command not found" as exit code 1 '\
'instead of 127)'
fi
_dump_trace
error
error "Command failed:"
DIE_PREFIX=' '
die_notrace "$@"
}
# Exit this script due to a failure, outputting a backtrace in the process.
die() {
set +e +u
_dump_trace
error
error "Error was:"
DIE_PREFIX=' '
die_notrace "$@"
}
# Exit this script w/out a backtrace.
die_notrace() {
set +e +u
if [[ $# -eq 0 ]]; then
set -- '(no error message given)'
fi
local line
for line in "$@"; do
error "${DIE_PREFIX}${line}"
done
exit 1
}
# Simple version comparison routine
# Note: not a true semver comparison and build revisions are ignored
cmp_ver() {
local rev a="${2%%+*}" b="${3%%+*}"
case "$1" in
le) rev="" ;;
ge) rev="--reverse" ;;
*) die "Invalid operator $1" ;;
esac
printf '%s\n%s\n' "$a" "$b" | sort --version-sort --check=quiet $rev
return $?
}
# Split a semver into a 3 item array (major minor patch)
# Usage: split_ver 1.2.3 NAME
split_ver() {
local v="$1" n="$2"
v="${v%%-*}" # strip off pre-release suffix
v="${v%%+*}" # strip off build id suffix
v="${v//./ }"
local -a a="(${v})"
if [[ ${#a[@]} -ne 3 ]]; then
die "Invalid version string '$1'"
fi
declare -g -a ${n}="(${v})"
}
# repo source root inside the chroot, usually mounted from the outside.
CHROOT_TRUNK_DIR="/mnt/host/source"
# Construct a list of possible locations for the source tree. This list is
# based on various environment variables and globals that may have been set
# by the calling script.
get_gclient_root_list() {
if [[ ${INSIDE_CHROOT} -eq 1 ]]; then
echo "${CHROOT_TRUNK_DIR}"
fi
if [[ -n ${COMMON_SH} ]]; then echo "$(dirname "${COMMON_SH}")/../.."; fi
if [[ -n ${BASH_SOURCE} ]]; then echo "$(dirname "${BASH_SOURCE}")/../.."; fi
}
# Based on the list of possible source locations we set GCLIENT_ROOT if it is
# not already defined by looking for a src directory in each seach path
# location. If we do not find a valid looking root we error out.
get_gclient_root() {
if [[ -n ${GCLIENT_ROOT} ]]; then
return
fi
for path in $(get_gclient_root_list); do
if [[ -d ${path}/src ]]; then
GCLIENT_ROOT=${path}
break
fi
done
if [[ -z ${GCLIENT_ROOT} ]]; then
# Using dash or sh, we don't know where we are. $0 refers to the calling
# script, not ourselves, so that doesn't help us.
echo "Unable to determine location for common.sh. If you are sourcing"
echo "common.sh from a script run via dash or sh, you must do it in the"
echo "following way:"
echo ' COMMON_SH="$(dirname "$0")/../../scripts/common.sh"'
echo ' . "${COMMON_SH}"'
echo "where the first line is the relative path from your script to"
echo "common.sh."
exit 1
fi
}
# Populate the ENVIRONMENT_WHITELIST array.
load_environment_whitelist() {
ENVIRONMENT_WHITELIST=(
COREOS_BUILD_ID
COREOS_OFFICIAL
GIT_AUTHOR_EMAIL
GIT_AUTHOR_NAME
GIT_COMMITTER_EMAIL
GIT_COMMITTER_NAME
GIT_PROXY_COMMAND
GIT_SSH
RSYNC_PROXY
GNUPGHOME
GPG_AGENT_INFO
SSH_AGENT_PID
SSH_AUTH_SOCK
USE
all_proxy
ftp_proxy
http_proxy
https_proxy
no_proxy
)
}
load_environment_var() {
local file="$1" name value
shift
for name in "$@"; do
value=$(grep "^${name}=" "${file}")
export "${value}"
done
}
# Find root of source tree
get_gclient_root
# Canonicalize the directories for the root dir and the calling script.
# readlink is part of coreutils and should be present even in a bare chroot.
# This is better than just using
# FOO="$(cd ${FOO} ; pwd)"
# since that leaves symbolic links intact.
# Note that 'realpath' is equivalent to 'readlink -f'.
SCRIPT_LOCATION=$(readlink -f "${SCRIPT_LOCATION}")
GCLIENT_ROOT=$(readlink -f "${GCLIENT_ROOT}")
# TODO(marineam): I'm tempted to deprecate GCLIENT_ROOT, this isn't Google
# and even if it was the source is managed by 'repo', not 'gclient'
REPO_ROOT="${GCLIENT_ROOT}"
# Other directories should always be pathed down from GCLIENT_ROOT.
SRC_ROOT="${GCLIENT_ROOT}/src"
SRC_INTERNAL="${GCLIENT_ROOT}/src-internal"
SCRIPTS_DIR="${SRC_ROOT}/scripts"
BUILD_LIBRARY_DIR="${SCRIPTS_DIR}/build_library"
REPO_CACHE_DIR="${REPO_ROOT}/.cache"
REPO_MANIFESTS_DIR="${REPO_ROOT}/.repo/manifests"
# Source COREOS_VERSION_ID from manifest.
if [[ -f "${REPO_MANIFESTS_DIR}/version.txt" ]]; then
load_environment_var "${REPO_MANIFESTS_DIR}/version.txt" \
COREOS_VERSION_ID COREOS_SDK_VERSION
# The build id may be provided externally by the build system.
: ${COREOS_BUILD_ID:=$(date +%Y-%m-%d-%H%M)}
elif [[ -f "${SCRIPT_LOCATION}/version.txt" ]]; then
# This only happens in update.zip where we must use the current build id.
load_environment_var "${SCRIPT_LOCATION}/version.txt" \
COREOS_VERSION_ID COREOS_BUILD_ID COREOS_SDK_VERSION
else
die "Unable to locate version.txt"
fi
# Official builds must set COREOS_OFFICIAL=1 to use an official version.
# Unofficial builds always appended the build identifier.
if [[ ${COREOS_OFFICIAL:-0} -ne 1 && -n "${COREOS_BUILD_ID}" ]]; then
COREOS_VERSION="${COREOS_VERSION_ID}+${COREOS_BUILD_ID}"
else
COREOS_VERSION="${COREOS_VERSION_ID}"
fi
# Compatibility alias
COREOS_VERSION_STRING="${COREOS_VERSION}"
# Calculate what today's build version should be, used by release
# scripts to provide a reasonable default value. The value is the number
# of days since COREOS_EPOCH, Mon Jul 1 00:00:00 UTC 2013
readonly COREOS_EPOCH=1372636800
TODAYS_VERSION=$(( (`date +%s` - ${COREOS_EPOCH}) / 86400 ))
# Download URL prefix for SDK and board binary packages
: ${COREOS_DEV_BUILDS:=http://builds.developer.core-os.net}
# Load developer's custom settings. Default location is in scripts dir,
# since that's available both inside and outside the chroot. By convention,
# settings from this file are variables starting with 'CHROMEOS_'
: ${CHROMEOS_DEV_SETTINGS:=${SCRIPTS_DIR}/.chromeos_dev}
if [[ -f ${CHROMEOS_DEV_SETTINGS} ]]; then
# Turn on exit-on-error during custom settings processing
SAVE_OPTS=$(set +o)
switch_to_strict_mode
# Read settings
. "${CHROMEOS_DEV_SETTINGS}"
# Restore previous state of exit-on-error
eval "${SAVE_OPTS}"
fi
# Load shflags
# NOTE: This code snippet is in particular used by the au-generator (which
# stores shflags in ./lib/shflags/) and should not be touched.
if [[ -f ${SCRIPTS_DIR}/lib/shflags/shflags ]]; then
. "${SCRIPTS_DIR}/lib/shflags/shflags" || die "Couldn't find shflags"
else
. ./lib/shflags/shflags || die "Couldn't find shflags"
fi
# Our local mirror
DEFAULT_CHROMEOS_SERVER=${CHROMEOS_SERVER:-"http://build.chromium.org/mirror"}
# Upstream mirrors and build suites come in 2 flavors
# DEV - development chroot, used to build the chromeos image
# IMG - bootable image, to run on actual hardware
DEFAULT_DEV_MIRROR=${CHROMEOS_DEV_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
DEFAULT_DEV_SUITE=${CHROMEOS_DEV_SUITE:-"karmic"}
DEFAULT_IMG_MIRROR=${CHROMEOS_IMG_MIRROR:-"${DEFAULT_CHROMEOS_SERVER}/ubuntu"}
DEFAULT_IMG_SUITE=${CHROMEOS_IMG_SUITE:-"karmic"}
# Default location for chroot
DEFAULT_CHROOT_DIR=${CHROMEOS_CHROOT_DIR:-"${GCLIENT_ROOT}/chroot"}
# All output files from build should go under ${DEFAULT_BUILD_ROOT}, so that
# they don't pollute the source directory.
DEFAULT_BUILD_ROOT=${CHROMEOS_BUILD_ROOT:-"${SRC_ROOT}/build"}
# Sets the default board variable for calling script.
if [[ -f ${GCLIENT_ROOT}/src/scripts/.default_board ]]; then
DEFAULT_BOARD=$(<"${GCLIENT_ROOT}/src/scripts/.default_board")
# Check for user typos like whitespace.
if [[ -n ${DEFAULT_BOARD//[a-zA-Z0-9-_]} ]]; then
die ".default_board: invalid name detected; please fix:" \
"'${DEFAULT_BOARD}'"
fi
fi
# Directory to store built images. Should be set by sourcing script when used.
BUILD_DIR=
# Standard filenames
COREOS_DEVELOPER_IMAGE_NAME="coreos_developer_image.bin"
COREOS_DEVELOPER_CONTAINER_NAME="coreos_developer_container.bin"
COREOS_PRODUCTION_IMAGE_NAME="coreos_production_image.bin"
# -----------------------------------------------------------------------------
# Functions
setup_board_warning() {
echo
echo "${V_REVERSE}================ WARNING =====================${V_VIDOFF}"
echo
echo "*** No default board detected in " \
"${GCLIENT_ROOT}/src/scripts/.default_board"
echo "*** Either run setup_board with default flag set"
echo "*** or echo |board_name| > ${GCLIENT_ROOT}/src/scripts/.default_board"
echo
}
is_nfs() {
[[ $(stat -f -L -c %T "$1") == "nfs" ]]
}
warn_if_nfs() {
if is_nfs "$1"; then
warn "$1 is on NFS. This is untested. You can send patches if it's broken."
fi
}
# Enter a chroot and restart the current script if needed
restart_in_chroot_if_needed() {
# NB: Pass in ARGV: restart_in_chroot_if_needed "$@"
if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
# Get inside_chroot path for script.
local chroot_path="$(reinterpret_path_for_chroot "$0")"
exec ${GCLIENT_ROOT}/chromite/bin/cros_sdk -- "${chroot_path}" "$@"
fi
}
# Fail unless we're inside the chroot. This guards against messing up your
# workstation.
assert_inside_chroot() {
if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
echo "This script must be run inside the chroot. Run this first:"
echo " cros_sdk"
exit 1
fi
}
# Fail if we're inside the chroot. This guards against creating or entering
# nested chroots, among other potential problems.
assert_outside_chroot() {
if [[ ${INSIDE_CHROOT} -ne 0 ]]; then
echo "This script must be run outside the chroot."
exit 1
fi
}
assert_not_root_user() {
if [[ ${UID:-$(id -u)} == 0 ]]; then
echo "This script must be run as a non-root user."
exit 1
fi
}
assert_root_user() {
if [[ ${UID:-$(id -u)} != 0 ]] || [[ ${SUDO_USER:-root} == "root" ]]; then
die_notrace "This script must be run using sudo from a non-root user."
fi
}
# We depend on some relatively modern kernel features, in particular a
# reasonably recent btrfs version is required to generate images.
# Current requirement: 3.7 added btrfs' extref incompat feature
assert_kernel_version() {
local req_kv="3.7"
local cur_kv=$(uname -r)
if ! cmp_ver ge "${cur_kv}" "${req_kv}"; then
die_notrace "Detected kernel ${cur_kv}, ${req_kv} or later is required"
fi
}
# Check that all arguments are flags; that is, there are no remaining arguments
# after parsing from shflags. Allow (with a warning) a single empty-string
# argument.
#
# TODO: fix buildbot so that it doesn't pass the empty-string parameter,
# then change this function.
#
# Usage: check_flags_only_and_allow_null_arg "$@" && set --
check_flags_only_and_allow_null_arg() {
local do_shift=1
if [[ $# -eq 1 ]] && [[ -z $1 ]]; then
echo "$0: warning: ignoring null argument" >&2
shift
do_shift=0
fi
if [[ $# -gt 0 ]]; then
echo "error: invalid arguments: \"$*\"" >&2
flags_help
exit 1
fi
return ${do_shift}
}
# Removes single quotes around parameter
# Arguments:
# $1 - string which optionally has surrounding quotes
# Returns:
# None, but prints the string without quotes.
remove_quotes() {
echo "$1" | sed -e "s/^'//; s/'$//"
}
# Writes stdin to the given file name as root using sudo in overwrite mode.
#
# $1 - The output file name.
sudo_clobber() {
sudo tee "$1" >/dev/null
}
# Writes stdin to the given file name as root using sudo in append mode.
#
# $1 - The output file name.
sudo_append() {
sudo tee -a "$1" >/dev/null
}
# Execute multiple commands in a single sudo. Generally will speed things
# up by avoiding multiple calls to `sudo`. If any commands fail, we will
# call die with the failing command. We can handle a max of ~100 commands,
# but hopefully no one will ever try that many at once.
#
# $@ - The commands to execute, one per arg.
sudo_multi() {
local i cmds
# Construct the shell code to execute. It'll be of the form:
# ... && ( ( command ) || exit <command index> ) && ...
# This way we know which command exited. The exit status of
# the underlying command is lost, but we never cared about it
# in the first place (other than it is non zero), so oh well.
for (( i = 1; i <= $#; ++i )); do
cmds+=" && ( ( ${!i} ) || exit $(( i + 10 )) )"
done
# Execute our constructed shell code.
sudo -- sh -c ":${cmds[*]}" && i=0 || i=$?
# See if this failed, and if so, print out the failing command.
if [[ $i -gt 10 ]]; then
: $(( i -= 10 ))
die "sudo_multi failed: ${!i}"
elif [[ $i -ne 0 ]]; then
die "sudo_multi failed for unknown reason $i"
fi
}
# Writes stdin to the given file name as the sudo user in overwrite mode.
#
# $@ - The output file names.
user_clobber() {
install -m644 -o ${SUDO_UID} -g ${SUDO_GID} /dev/stdin "$@"
}
# Copies the specified file owned by the user to the specified location.
# If the copy fails as root (e.g. due to root_squash and NFS), retry the copy
# with the user's account before failing.
user_cp() {
cp -p "$@" 2>/dev/null || sudo -u ${SUDO_USER} -- cp -p "$@"
}
# Appends stdin to the given file name as the sudo user.
#
# $1 - The output file name.
user_append() {
cat >> "$1"
chown ${SUDO_UID}:${SUDO_GID} "$1"
}
# Create the specified directory, along with parents, as the sudo user.
#
# $@ - The directories to create.
user_mkdir() {
install -o ${SUDO_UID} -g ${SUDO_GID} -d "$@"
}
# Create the specified symlink as the sudo user.
#
# $1 - Link target
# $2 - Link name
user_symlink() {
ln -sfT "$1" "$2"
chown -h ${SUDO_UID}:${SUDO_GID} "$2"
}
# Locate all mounts below a specified directory.
#
# $1 - The root tree.
sub_mounts() {
# Assume that `mount` outputs a list of mount points in the order
# that things were mounted (since it always has and hopefully always
# will). As such, we have to unmount in reverse order to cleanly
# unmount submounts (think /dev/pts and /dev).
awk -v path=$1 -v len="${#1}" \
'(substr($2, 1, len) == path) { print $2 }' /proc/self/mounts | \
tac | \
sed -e 's/\\040(deleted)$//'
# Hack(zbehan): If a bind mount's source is mysteriously removed,
# we'd end up with an orphaned mount with the above string in its name.
# It can only be seen through /proc/mounts and will stick around even
# when it should be gone already. crosbug.com/31250
}
# Unmounts a directory, if the unmount fails, warn, and then lazily unmount.
#
# $1 - The path to unmount.
safe_umount_tree() {
local mounts=$(sub_mounts "$1")
# Hmm, this shouldn't normally happen, but anything is possible.
if [[ -z ${mounts} ]]; then
return 0
fi
# First try to unmount, this might fail because of nested binds.
if sudo umount -d ${mounts}; then
return 0;
fi
# Check whether our mounts were successfully unmounted.
mounts=$(sub_mounts "$1")
if [[ -z ${mounts} ]]; then
warn "umount failed, but devices were unmounted anyway"
return 0
fi
# Try one more time, this one will die hard if it fails.
warn "Failed to unmount ${mounts}"
safe_umount -d ${mounts}
}
# Run umount as root.
safe_umount() {
if sudo umount "$@"; then
return 0;
else
failboat safe_umount
fi
}
# Check if a single path is mounted.
is_mounted() {
if grep -q "$(readlink -f "$1")" /proc/self/mounts; then
return 0
else
return 1
fi
}
fix_mtab() {
local root="$1" mounts="../proc/self/mounts"
if [[ "$(readlink "${root}/etc/mtab")" != "${mounts}" ]]; then
sudo ln -sf "${mounts}" "${root}/etc/mtab"
fi
}
get_git_id() {
git var GIT_COMMITTER_IDENT | sed -e 's/^.*<\(\S\+\)>.*$/\1/'
}
# These two helpers clobber the ro compat value in our root filesystem.
#
# When the system is built with --enable_rootfs_verification, bit-precise
# integrity checking is performed. That precision poses a usability issue on
# systems that automount partitions with recognizable filesystems, such as
# ext2/3/4. When the filesystem is mounted 'rw', ext2 metadata will be
# automatically updated even if no other writes are performed to the
# filesystem. In addition, ext2+ does not support a "read-only" flag for a
# given filesystem. That said, forward and backward compatibility of
# filesystem features are supported by tracking if a new feature breaks r/w or
# just write compatibility. We abuse the read-only compatibility flag[1] in
# the filesystem header by setting the high order byte (le) to FF. This tells
# the kernel that features R24-R31 are all enabled. Since those features are
# undefined on all ext-based filesystem, all standard kernels will refuse to
# mount the filesystem as read-write -- only read-only[2].
#
# [1] 32-bit flag we are modifying:
# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=include/linux/ext2_fs.h#l417
# [2] Mount behavior is enforced here:
# http://git.chromium.org/cgi-bin/gitweb.cgi?p=kernel.git;a=blob;f=fs/ext2/super.c#l857
#
# N.B., if the high order feature bits are used in the future, we will need to
# revisit this technique.
disable_rw_mount() {
local rootfs=$1
local offset="${2-0}" # in bytes
local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
printf '\377' |
sudo dd of="${rootfs}" seek=$((offset + ro_compat_offset)) \
conv=notrunc count=1 bs=1
}
enable_rw_mount() {
local rootfs=$1
local offset="${2-0}"
local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte
printf '\000' |
sudo dd of="${rootfs}" seek=$((offset + ro_compat_offset)) \
conv=notrunc count=1 bs=1
}
# Generate a DIGESTS file, as normally used by Gentoo.
# This is an alternative to shash which doesn't know how to report errors.
# Usage: make_digests -d file.DIGESTS file1 [file2...]
_digest_types="md5 sha1 sha512"
make_digests() {
[[ "$1" == "-d" ]] || die
local digests="$(readlink -f "$2")"
shift 2
pushd "$(dirname "$1")" >/dev/null
echo -n > "${digests}"
for filename in "$@"; do
filename=$(basename "$filename")
info "Computing DIGESTS for ${filename}"
for hash_type in $_digest_types; do
echo "# $hash_type HASH" | tr "a-z" "A-Z" >> "${digests}"
${hash_type}sum "${filename}" >> "${digests}"
done
done
popd >/dev/null
}
# Validate a DIGESTS file. Essentially the inverse of make_digests.
# Usage: verify_digests [-d file.DIGESTS] file1 [file2...]
# If -d is not specified file1.DIGESTS will be used
verify_digests() {
local digests
if [[ "$1" == "-d" ]]; then
[[ -n "$2" ]] || die "-d requires an argument"
digests="$(readlink -f "$2")"
shift 2
else
digests=$(basename "${1}.DIGESTS")
fi
pushd "$(dirname "$1")" >/dev/null
for filename in "$@"; do
filename=$(basename "$filename")
info "Validating DIGESTS for ${filename}"
for hash_type in $_digest_types; do
grep -A1 -i "^# ${hash_type} HASH$" "${digests}" | \
grep "$filename$" | ${hash_type}sum -c - --strict || return 1
# Also check that none of the greps failed in the above pipeline
[[ -z ${PIPESTATUS[*]#0} ]] || return 1
done
done
popd >/dev/null
}
# Get current timestamp. Assumes common.sh runs at startup.
start_time=$(date +%s)
# Get time elapsed since start_time in seconds.
get_elapsed_seconds() {
local end_time=$(date +%s)
local elapsed_seconds=$(( end_time - start_time ))
echo ${elapsed_seconds}
}
# Print time elapsed since start_time.
print_time_elapsed() {
# Optional first arg to specify elapsed_seconds. If not given, will
# recalculate elapsed time to now. Optional second arg to specify
# command name associated with elapsed time.
local elapsed_seconds=${1:-$(get_elapsed_seconds)}
local cmd_base=${2:-}
local minutes=$(( elapsed_seconds / 60 ))
local seconds=$(( elapsed_seconds % 60 ))
if [[ -n ${cmd_base} ]]; then
info "Elapsed time (${cmd_base}): ${minutes}m${seconds}s"
else
info "Elapsed time: ${minutes}m${seconds}s"
fi
}
# Save original command line.
command_line_arr=( "$0" "$@" )
command_completed() {
# Call print_elapsed_time regardless.
local run_time=$(get_elapsed_seconds)
local cmd_base=$(basename "${command_line_arr[0]}")
print_time_elapsed ${run_time} ${cmd_base}
}
# The board and variant command line options can be used in a number of ways
# to specify the board and variant. The board can encode both pieces of
# information separated by underscores. Or the variant can be passed using
# the separate variant option. This function extracts the canonical board and
# variant information and provides it in the BOARD, VARIANT and BOARD_VARIANT
# variables.
get_board_and_variant() {
local flags_board=$1
local flags_variant=$2
BOARD=$(echo "${flags_board}" | cut -d '_' -f 1)
VARIANT=${flags_variant:-$(echo "${flags_board}" | cut -s -d '_' -f 2)}
BOARD_VARIANT=${BOARD}
if [[ -n ${VARIANT} ]]; then
BOARD_VARIANT+="_${VARIANT}"
fi
}
# Check that the specified file exists. If the file path is empty or the file
# doesn't exist on the filesystem generate useful error messages. Otherwise
# show the user the name and path of the file that will be used. The padding
# parameter can be used to tabulate multiple name:path pairs. For example:
#
# check_for_file "really long name" "...:" "file.foo"
# check_for_file "short name" ".........:" "another.bar"
#
# Results in the following output:
#
# Using really long name...: file.foo
# Using short name.........: another.bar
#
# If tabulation is not required then passing "" for padding generates the
# output "Using <name> <path>"
check_for_file() {
local name=$1
local padding=$2
local path=$3
if [[ -z ${path} ]]; then
die "No ${name} file specified."
fi
if [[ ! -e ${path} ]]; then
die "No ${name} file found at: ${path}"
else
info "Using ${name}${padding} ${path}"
fi
}
# Check that the specified tool exists. If it does not exist in the PATH
# generate a useful error message indicating how to install the ebuild
# that contains the required tool.
check_for_tool() {
local tool=$1
local ebuild=$2
if ! which "${tool}" >/dev/null; then
error "The ${tool} utility was not found in your path. Run the following"
error "command in your chroot to install it: sudo -E emerge ${ebuild}"
exit 1
fi
}
# Reinterprets path from outside the chroot for use inside.
# Returns "" if "" given.
# $1 - The path to reinterpret.
reinterpret_path_for_chroot() {
if [[ ${INSIDE_CHROOT} -ne 1 ]]; then
if [[ -z $1 ]]; then
echo ""
else
local path_abs_path=$(readlink -f "$1")
local gclient_root_abs_path=$(readlink -f "${GCLIENT_ROOT}")
# Strip the repository root from the path.
local relative_path=$(echo ${path_abs_path} \
| sed "s:${gclient_root_abs_path}/::")
if [[ ${relative_path} == "${path_abs_path}" ]]; then
die "Error reinterpreting path. Path $1 is not within source tree."
fi
# Prepend the chroot repository path.
echo "/home/${USER}/trunk/${relative_path}"
fi
else
# Path is already inside the chroot :).
echo "$1"
fi
}
# Get the relative path between two locations. Handy for printing paths to
# the user that will usually make sense both inside and outside the chroot.
relpath() {
local py='import sys, os; print os.path.relpath(sys.argv[1], sys.argv[2])'
python2 -c "${py}" "${1}" "${2:-.}"
}
enable_strict_sudo() {
if [[ -z ${CROS_SUDO_KEEP_ALIVE} ]]; then
echo "$0 was somehow invoked in a way that the sudo keep alive could"
echo "not be found. Failing due to this. See crosbug.com/18393."
exit 126
fi
sudo() {
$(type -P sudo) -n "$@"
}
}
# Checks that stdin and stderr are both terminals.
# If so, we assume that there is a live user we can interact with.
# This check can be overridden by setting the CROS_NO_PROMPT environment
# variable to a non-empty value.
is_interactive() {
[[ -z ${CROS_NO_PROMPT} && -t 0 && -t 2 ]]
}
assert_interactive() {
if ! is_interactive; then
die "Script ${0##*/} tried to get user input on a non-interactive terminal."
fi
}
# Selection menu with a default option: this is similar to bash's select
# built-in, only that in case of an empty selection it'll return the default
# choice. Like select, it uses PS3 as the prompt.
#
# $1: name of variable to be assigned the selected value; it better not be of
# the form choose_foo to avoid conflict with local variables.
# $2: default value to return in case of an empty user entry.
# $3: value to return in case of an invalid choice.
# $...: options for selection.
#
# Usage example:
#
# PS3="Select one [1]: "
# choose reply "foo" "ERROR" "foo" "bar" "foobar"
#
# This will present the following menu and prompt:
#
# 1) foo
# 2) bar
# 3) foobar
# Select one [1]:
#
# The return value will be stored in a variable named 'reply'. If the input is
# 1, 2 or 3, the return value will be "foo", "bar" or "foobar", respectively.
# If it is empty (i.e. the user clicked Enter) it will be "foo". Anything else
# will return "ERROR".
choose() {
typeset -i choose_i=1
# Retrieve output variable name and default return value.
local choose_reply=$1
local choose_default=$2
local choose_invalid=$3
shift 3
# Select a return value
unset REPLY
if [[ $# -gt 0 ]]; then
assert_interactive
# Actual options provided, present a menu and prompt for a choice.
local choose_opt
for choose_opt in "$@"; do
echo "${choose_i}) ${choose_opt}" >&2
: $(( ++choose_i ))
done
read -p "$PS3"
fi
# Filter out strings containing non-digits.
if [[ ${REPLY} != "${REPLY%%[!0-9]*}" ]]; then
REPLY=0
fi
choose_i="${REPLY}"
if [[ ${choose_i} -ge 1 && ${choose_i} -le $# ]]; then
# Valid choice, return the corresponding value.
eval ${choose_reply}=\""${!choose_i}"\"
elif [[ -z ${REPLY} ]]; then
# Empty choice, return default value.
eval ${choose_reply}=\""${choose_default}"\"
else
# Invalid choice, return corresponding value.
eval ${choose_reply}=\""${choose_invalid}\""
fi
}
# Display --help if requested. This is used to hide options from help
# that are not intended for developer use.
#
# How to use:
# 1) Declare the options that you want to appear in help.
# 2) Call this function.
# 3) Declare the options that you don't want to appear in help.
#
# See build_packages for example usage.
show_help_if_requested() {
local opt
for opt in "$@"; do
if [[ ${opt} == "-h" || ${opt} == "--help" ]]; then
flags_help
exit 0
fi
done
}
switch_to_strict_mode() {
# Set up strict execution mode; note that the trap
# must follow switch_to_strict_mode, else it will have no effect.
set -e
trap 'die_err_trap "${BASH_COMMAND:-command unknown}" "$?"' ERR
if [[ $# -ne 0 ]]; then
set "$@"
fi
}