gentoo-install/scripts/utils.sh

156 lines
2.9 KiB
Bash
Raw Normal View History

#!/bin/bash
source "$GENTOO_BOOTSTRAP_DIR/scripts/protection.sh" || exit 1
2020-01-04 10:55:31 -01:00
echo_console() {
if { true >&3; } 2<> /dev/null; then
echo "$@" >&3
else
echo "$@"
fi
}
log_stdout() {
echo "$*"
2020-01-04 10:55:31 -01:00
if { true >&3; } 2<> /dev/null; then
echo "$*" >&3
fi
}
log_stderr() {
echo "$*" >&2
echo "$*"
}
elog() {
log_stdout " * $*"
}
einfo() {
log_stdout " * $*"
}
ewarn() {
log_stderr " * $*"
}
eerror() {
log_stderr " * ERROR: $*"
}
die() {
eerror "$*"
kill "$GENTOO_BOOTSTRAP_SCRIPT_PID"
exit 1
}
for_line_in() {
while IFS="" read -r line || [[ -n "$line" ]]; do
"$2" "$line"
done <"$1"
}
2020-01-04 10:55:31 -01:00
flush_stdin() {
local empty_stdin
while read -r -t 0.01 empty_stdin; do true; done
2020-01-04 10:55:31 -01:00
}
2020-01-04 10:55:31 -01:00
ask() {
local response
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"
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="(Shell/retry/abort/continue/print)"
2020-01-04 10:55:31 -01:00
# Outer loop, allows us to retry the command
while true; do
# Try command
"$@"
cmd_status="$?"
if [[ "$cmd_status" != 0 ]]; then
echo_console " * Command failed: \$ $*"
echo_console -n "Last command failed (code $cmd_status), specify next action $prompt_parens "
# Prompt until input is valid
while true; do
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)
echo_console "Hint: The script log is at '$GENTOO_BOOTSTRAP_DIR/log.out'"
echo_console "You will be prompted for action again after exiting this shell."
/bin/bash --init-file <(echo "disable_logging; source $TMP_DIR/.bashrc")
;;
r|retry) continue 2 ;;
2020-01-04 11:09:29 -01:00
a|abort) die "Installation aborted" ;;
c|continue) return 0 ;;
2020-01-04 10:55:31 -01:00
p|print) echo_console "\$ $*" ;;
*) echo_console -n "Response not understood $prompt_parens " ;;
esac
done
fi
done
}
countdown() {
echo -n "$1" >&3
local i="$2"
while [[ $i -gt 0 ]]; do
echo -n "$i " >&3
i=$((i - 1))
sleep 1
done
echo >&3
}
download_stdout() {
wget --quiet --https-only --secure-protocol=PFS -O - -- "$1"
}
download() {
wget --quiet --https-only --secure-protocol=PFS --show-progress -O "$2" -- "$1"
}
get_device_by_partuuid() {
blkid -g \
|| die "Error while executing blkid"
local dev
dev="$(blkid -o export -t PARTUUID="$1")" \
|| die "Error while executing blkid to find PARTUUID=$1"
dev="$(grep DEVNAME <<< "$dev")" \
|| die "Could not find DEVNAME=... in blkid output"
dev="${dev:8}"
echo -n "$dev"
}
load_or_generate_uuid() {
local uuid
local uuid_file="$UUID_STORAGE_DIR/$1"
if [[ -e "$uuid_file" ]]; then
uuid="$(cat "$uuid_file")"
else
uuid="$(uuidgen -r)"
mkdir -p "$UUID_STORAGE_DIR"
echo -n "$uuid" > "$uuid_file"
fi
echo -n "$uuid"
}