2020-01-08 16:21:01 -01:00
|
|
|
|
source "$GENTOO_INSTALL_REPO_DIR/scripts/protection.sh" || exit 1
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
|
|
|
|
elog() {
|
2020-01-04 12:10:40 -01:00
|
|
|
|
echo "[1m *[m $*"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
einfo() {
|
2020-01-04 12:10:40 -01:00
|
|
|
|
echo "[1;32m *[m $*"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ewarn() {
|
2020-01-04 12:10:40 -01:00
|
|
|
|
echo "[1;33m *[m $*" >&2
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eerror() {
|
2020-01-04 12:10:40 -01:00
|
|
|
|
echo "[1;31m * ERROR:[m $*" >&2
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
die() {
|
|
|
|
|
eerror "$*"
|
2020-04-21 18:48:23 +00:00
|
|
|
|
kill "$GENTOO_INSTALL_REPO_SCRIPT_PID"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 19:34:36 +00:00
|
|
|
|
# Prints an error with file:line info of the nth "stack frame".
|
|
|
|
|
# 0 is this function, 1 the calling function, 2 its parent, and so on.
|
|
|
|
|
die_trace() {
|
|
|
|
|
local idx="${1:-0}"
|
|
|
|
|
shift
|
|
|
|
|
echo "[1m${BASH_SOURCE[$((idx + 1))]}:${BASH_LINENO[$idx]}: [1;31merror:[m ${FUNCNAME[$idx]}: $*" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-02 22:29:17 -01:00
|
|
|
|
for_line_in() {
|
2020-04-20 22:40:54 +00:00
|
|
|
|
while IFS="" read -r line || [[ -n $line ]]; do
|
2020-01-02 22:29:17 -01:00
|
|
|
|
"$2" "$line"
|
|
|
|
|
done <"$1"
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 10:55:31 -01:00
|
|
|
|
flush_stdin() {
|
2020-01-02 22:29:17 -01:00
|
|
|
|
local empty_stdin
|
|
|
|
|
while read -r -t 0.01 empty_stdin; do true; done
|
2020-01-04 10:55:31 -01:00
|
|
|
|
}
|
2020-01-02 22:29:17 -01:00
|
|
|
|
|
2020-01-04 10:55:31 -01:00
|
|
|
|
ask() {
|
|
|
|
|
local response
|
2019-12-31 14:28:42 -01:00
|
|
|
|
while true; do
|
2020-01-04 10:55:31 -01:00
|
|
|
|
flush_stdin
|
2020-01-04 11:09:29 -01:00
|
|
|
|
read -r -p "$* (Y/n) " response \
|
|
|
|
|
|| die "Error in read"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
case "${response,,}" in
|
|
|
|
|
'') return 0 ;;
|
|
|
|
|
y|yes) return 0 ;;
|
|
|
|
|
n|no) return 1 ;;
|
|
|
|
|
*) continue ;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 10:55:31 -01:00
|
|
|
|
try() {
|
|
|
|
|
local response
|
|
|
|
|
local cmd_status
|
2020-01-04 11:09:29 -01:00
|
|
|
|
local prompt_parens="([1mS[mhell/[1mr[metry/[1ma[mbort/[1mc[montinue/[1mp[mrint)"
|
2020-01-04 10:55:31 -01:00
|
|
|
|
|
|
|
|
|
# Outer loop, allows us to retry the command
|
|
|
|
|
while true; do
|
|
|
|
|
# Try command
|
|
|
|
|
"$@"
|
|
|
|
|
cmd_status="$?"
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ $cmd_status != 0 ]]; then
|
2020-01-04 12:10:40 -01:00
|
|
|
|
echo "[1;31m * Command failed: [1;33m\$[m $*"
|
|
|
|
|
echo "Last command failed with exit code $cmd_status"
|
2020-01-04 10:55:31 -01:00
|
|
|
|
|
|
|
|
|
# Prompt until input is valid
|
|
|
|
|
while true; do
|
2020-01-04 12:10:40 -01:00
|
|
|
|
echo -n "Specify next action $prompt_parens "
|
2020-01-04 10:55:31 -01:00
|
|
|
|
flush_stdin
|
2020-01-04 11:09:29 -01:00
|
|
|
|
read -r response \
|
|
|
|
|
|| die "Error in read"
|
2020-01-04 10:55:31 -01:00
|
|
|
|
case "${response,,}" in
|
|
|
|
|
''|s|shell)
|
2020-01-04 12:10:40 -01:00
|
|
|
|
echo "You will be prompted for action again after exiting this shell."
|
|
|
|
|
/bin/bash --init-file <(echo "init_bash")
|
2020-01-04 10:55:31 -01:00
|
|
|
|
;;
|
|
|
|
|
r|retry) continue 2 ;;
|
2020-01-04 11:09:29 -01:00
|
|
|
|
a|abort) die "Installation aborted" ;;
|
|
|
|
|
c|continue) return 0 ;;
|
2020-01-04 12:10:40 -01:00
|
|
|
|
p|print) echo "[1;33m\$[m $*" ;;
|
2020-01-04 11:31:39 -01:00
|
|
|
|
*) ;;
|
2020-01-04 10:55:31 -01:00
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
fi
|
2020-01-04 11:27:12 -01:00
|
|
|
|
|
|
|
|
|
return
|
2020-01-04 10:55:31 -01:00
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
|
countdown() {
|
2020-01-04 12:19:21 -01:00
|
|
|
|
echo -n "$1" >&2
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
|
|
|
|
local i="$2"
|
|
|
|
|
while [[ $i -gt 0 ]]; do
|
2020-01-04 12:19:21 -01:00
|
|
|
|
echo -n "[1;31m$i[m " >&2
|
2019-12-31 14:28:42 -01:00
|
|
|
|
i=$((i - 1))
|
|
|
|
|
sleep 1
|
|
|
|
|
done
|
2020-01-04 12:19:21 -01:00
|
|
|
|
echo >&2
|
2019-12-31 14:28:42 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
download_stdout() {
|
|
|
|
|
wget --quiet --https-only --secure-protocol=PFS -O - -- "$1"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
download() {
|
|
|
|
|
wget --quiet --https-only --secure-protocol=PFS --show-progress -O "$2" -- "$1"
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 18:58:21 +00:00
|
|
|
|
get_device_by_blkid_field() {
|
|
|
|
|
local blkid_field="$1"
|
|
|
|
|
local field_value="$2"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
blkid -g \
|
|
|
|
|
|| die "Error while executing blkid"
|
|
|
|
|
local dev
|
2020-04-21 18:58:21 +00:00
|
|
|
|
dev="$(blkid -o export -t "$blkid_field=$field_value")" \
|
|
|
|
|
|| die "Error while executing blkid to find $blkid_field=$field_value"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
dev="$(grep DEVNAME <<< "$dev")" \
|
|
|
|
|
|| die "Could not find DEVNAME=... in blkid output"
|
|
|
|
|
dev="${dev:8}"
|
|
|
|
|
echo -n "$dev"
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 18:58:21 +00:00
|
|
|
|
get_device_by_partuuid() {
|
|
|
|
|
get_device_by_blkid_field 'PARTUUID' "$1"
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 21:41:27 +00:00
|
|
|
|
get_device_by_uuid() {
|
|
|
|
|
get_device_by_blkid_field 'UUID' "$1"
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 18:58:21 +00:00
|
|
|
|
get_device_by_ptuuid() {
|
2020-04-21 21:15:29 +00:00
|
|
|
|
echo -n "${DISK_PTUUID_TO_DEVICE[${1,,}]}"
|
2020-04-21 18:58:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 21:41:27 +00:00
|
|
|
|
get_device_by_mdadm_uuid() {
|
|
|
|
|
echo -n "${DISK_MDADM_UUID_TO_DEVICE[${1,,}]}"
|
2020-04-21 12:33:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
|
load_or_generate_uuid() {
|
|
|
|
|
local uuid
|
|
|
|
|
local uuid_file="$UUID_STORAGE_DIR/$1"
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ -e $uuid_file ]]; then
|
2019-12-31 14:28:42 -01:00
|
|
|
|
uuid="$(cat "$uuid_file")"
|
|
|
|
|
else
|
|
|
|
|
uuid="$(uuidgen -r)"
|
|
|
|
|
mkdir -p "$UUID_STORAGE_DIR"
|
|
|
|
|
echo -n "$uuid" > "$uuid_file"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo -n "$uuid"
|
|
|
|
|
}
|
2020-04-19 19:34:36 +00:00
|
|
|
|
|
|
|
|
|
# Parses named arguments and stores them in the associative array `arguments`.
|
2020-04-20 22:40:54 +00:00
|
|
|
|
# If given, the associative array `known_arguments` must contain a list of arguments
|
|
|
|
|
# prefixed with + (mandatory) or ? (optional). "at least one of" can be expressed by +a|b|c.
|
2020-04-19 19:34:36 +00:00
|
|
|
|
parse_arguments() {
|
|
|
|
|
local key
|
|
|
|
|
local value
|
|
|
|
|
local a
|
|
|
|
|
for a in "$@"; do
|
|
|
|
|
key="${a%%=*}"
|
|
|
|
|
value="${a#*=}"
|
2020-04-21 19:52:46 +00:00
|
|
|
|
|
2020-04-21 20:04:39 +00:00
|
|
|
|
if [[ $key == "$a" ]]; then
|
2020-04-21 19:52:46 +00:00
|
|
|
|
extra_arguments+=("$a")
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
arguments[$key]="$value"
|
2020-04-19 19:34:36 +00:00
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
declare -A allowed_keys
|
2020-04-20 22:40:54 +00:00
|
|
|
|
if [[ -v known_arguments ]]; then
|
2020-04-19 19:34:36 +00:00
|
|
|
|
local m
|
|
|
|
|
for m in "${known_arguments[@]}"; do
|
|
|
|
|
case "${m:0:1}" in
|
|
|
|
|
'+')
|
|
|
|
|
m="${m:1}"
|
|
|
|
|
local has_opt=false
|
|
|
|
|
local m_opt
|
|
|
|
|
# Splitting is intentional here
|
2020-04-20 22:40:54 +00:00
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
for m_opt in ${m//|/ }; do
|
|
|
|
|
allowed_keys[$m_opt]=true
|
|
|
|
|
if [[ -v arguments[$m_opt] ]]; then
|
2020-04-19 19:34:36 +00:00
|
|
|
|
has_opt=true
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
[[ $has_opt == true ]] \
|
2020-04-19 19:34:36 +00:00
|
|
|
|
|| die_trace 2 "Missing mandatory argument $m=..."
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
'?')
|
2020-04-20 22:40:54 +00:00
|
|
|
|
allowed_keys[${m:1}]=true
|
2020-04-19 19:34:36 +00:00
|
|
|
|
;;
|
|
|
|
|
|
|
|
|
|
*) die_trace 2 "Invalid start character in known_arguments, in argument '$m'" ;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
|
for a in "${!arguments[@]}"; do
|
|
|
|
|
[[ -v allowed_keys[$a] ]] \
|
|
|
|
|
|| die_trace 2 "Unkown argument '$a'"
|
|
|
|
|
done
|
|
|
|
|
fi
|
2020-04-19 19:34:36 +00:00
|
|
|
|
}
|