diff --git a/scripts/functions.sh b/scripts/functions.sh index 5342c88..888aec3 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -233,7 +233,7 @@ download_stage3() { || die "Checksum mismatch!" # Create verification file in case the script is restarted - touch "$CURRENT_STAGE3_VERIFIED" + touch_or_die "$CURRENT_STAGE3_VERIFIED" fi } @@ -267,7 +267,7 @@ disable_logging() { exec 1>&3 # Close fd 3 exec 3<&- -} +}; export -f disable_logging gentoo_umount() { if mountpoint -q -- "$ROOT_MOUNTPOINT"; then @@ -285,6 +285,16 @@ env_update() { export PS1="(chroot) \$PS1" } +mkdir_or_die() { + mkdir -p "$1" \ + || die "Could not create directory '$1'" +} + +touch_or_die() { + touch "$1" \ + || die "Could not touch '$1'" +} + gentoo_chroot() { if [[ $# -eq 0 ]]; then cat > "$TMP_DIR/.bashrc" < /etc/timezone \ || die "Could not write /etc/timezone" - emerge -v --config sys-libs/timezone-data + try emerge -v --config sys-libs/timezone-data # Set locale einfo "Selecting locale" @@ -80,12 +80,22 @@ main_install_gentoo_in_chroot() { || die "Could not write /etc/locale.gen" locale-gen \ || die "Could not generate locales" - eselect locale set "$LOCALE" \ - || die "Could not select locale" + try eselect locale set "$LOCALE" # Update environment env_update + # Prepare /etc/portage for autounmask + mkdir_or_die "/etc/portage/package.use" + touch_or_die "/etc/portage/package.use/zz-autounmask" + mkdir_or_die "/etc/portage/package.keywords" + touch_or_die "/etc/portage/package.keywords/zz-autounmask" + + # Install git (for git portage overlays) + einfo "Installing git" + try emerge --verbose dev-vcs/git \ + || die "Error while installing git" + #get kernel #compile minimal kernel to boot system @@ -125,7 +135,7 @@ main_umount() { # Main dispatch # Instantly kill when pressing ctrl-c -trap "kill $GENTOO_BOOTSTRAP_SCRIPT_PID" INT +trap 'kill "$GENTOO_BOOTSTRAP_SCRIPT_PID"' INT einfo "Verbose script output will be logged to: '$GENTOO_BOOTSTRAP_DIR/log-$LOGDATE.out'" # Save old stdout diff --git a/scripts/utils.sh b/scripts/utils.sh index e96d3f2..c45a6b5 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -2,9 +2,17 @@ source "$GENTOO_BOOTSTRAP_DIR/scripts/protection.sh" || exit 1 +echo_console() { + if { true >&3; } 2<> /dev/null; then + echo "$@" >&3 + else + echo "$@" + fi +} + log_stdout() { echo "$*" - if { >&3; } 2<> /dev/null; then + if { true >&3; } 2<> /dev/null; then echo "$*" >&3 fi } @@ -42,13 +50,15 @@ for_line_in() { done <"$1" } -ask() { - # Empty stdin +flush_stdin() { local empty_stdin while read -r -t 0.01 empty_stdin; do true; done - unset empty_stdin +} +ask() { + local response while true; do + flush_stdin read -r -p "$* (Y/n) " response case "${response,,}" in '') return 0 ;; @@ -59,6 +69,41 @@ ask() { done } +try() { + local response + local cmd_status + local prompt_parens="(Shell/retry/cancel/print)" + + # 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 + read -r response + 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 ;; + c|cancel) die "Installation cancelled" ;; + p|print) echo_console "\$ $*" ;; + *) echo_console -n "Response not understood $prompt_parens " ;; + esac + done + fi + done +} + countdown() { echo -n "$1" >&3