From b2ea9b360de1fdd456fe9eeecd62641be5362e6d Mon Sep 17 00:00:00 2001 From: Carlos Prado Date: Sat, 7 Jan 2023 14:02:15 +0100 Subject: [PATCH] feat: hooks (#70) * Hooks Hook system to allow further customization at specific steps of the installation process. - before/after_prepare_environment - before/after_disk_configuration - before/after_download_stage3 - before/after_extract_stage3 - before/after_install - before/after_configure_base_system - before/after_configure_portage - before/after_install_kernel * add hook examples * fix: remove eval --- gentoo.conf.example | 73 ++++++++++++++++++++++++++++++++++++++++++++ scripts/functions.sh | 16 ++++++++++ scripts/main.sh | 10 ++++++ scripts/utils.sh | 6 ++++ 4 files changed, 105 insertions(+) diff --git a/gentoo.conf.example b/gentoo.conf.example index 7942667..bc5fc0b 100644 --- a/gentoo.conf.example +++ b/gentoo.conf.example @@ -286,3 +286,76 @@ ROOT_SSH_AUTHORIZED_KEYS="" # To prove that you have read and edited the config # properly, set the following value to true. I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY=false + +################################################ +# Hooks + +# before_prepare_environment() { +# einfo 'before prepare environment' +# } + +# after_prepare_environment() { +# einfo 'after prepare environment' +# } + +# before_disk_configuration() { +# einfo 'before disk configuration' +# } + +# after_disk_configuration() { +# einfo 'after disk configuration' +# } + +# before_download_stage3() { +# einfo "stage3 basename: $1" +# einfo 'before download stage3' +# } + +# after_download_stage3() { +# einfo "stage3 downloaded file name: $1" +# einfo 'after download stage3' +# } + +# before_extract_stage3() { +# einfo "stage3 downloaded file path: $1" +# einfo "root mountpoint: $1" +# einfo 'before extract stage3' +# } + +# after_extract_stage3() { +# einfo "stage3 downloaded file path: $1" +# einfo "root mountpoint: $1" +# einfo 'after extract stage3' +# } + +# before_install() { +# einfo 'before install' +# } + +# after_install() { +# einfo 'after install' +# } + +# before_configure_base_system() { +# einfo 'before configure base system' +# } + +# after_configure_base_system() { +# einfo 'after configure base system' +# } + +# before_configure_portage() { +# einfo 'before configure portage' +# } + +# after_configure_portage() { +# einfo 'after configure portage' +# } + +# before_install_kernel() { +# einfo 'before install kernel' +# } + +# after_install_kernel() { +# einfo 'after install kernel' +# } diff --git a/scripts/functions.sh b/scripts/functions.sh index c5bb3db..28898e1 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -64,6 +64,8 @@ function preprocess_config() { } function prepare_installation_environment() { + maybe_exec 'before_prepare_environment' + einfo "Preparing installation environment" local wanted_programs=( @@ -94,6 +96,8 @@ function prepare_installation_environment() { # Sync time now to prevent issues later sync_time + + maybe_exec 'after_prepare_environment' } function check_encryption_key() { @@ -735,6 +739,8 @@ function apply_disk_configuration() { || die "Aborted" countdown "Applying in " 5 + maybe_exec 'before_disk_configuration' + einfo "Applying disk configuration" apply_disk_actions @@ -742,6 +748,8 @@ function apply_disk_configuration() { elog "New lsblk output:" for_line_in <(lsblk \ || die "Error in lsblk") elog + + maybe_exec 'after_disk_configuration' } function mount_efivars() { @@ -819,6 +827,8 @@ function download_stage3() { # File to indiciate successful verification CURRENT_STAGE3_VERIFIED="${CURRENT_STAGE3}.verified" + maybe_exec 'before_download_stage3' "$STAGE3_BASENAME" + # Download file if not already downloaded if [[ -e $CURRENT_STAGE3_VERIFIED ]]; then einfo "$STAGE3_BASENAME tarball already downloaded and verified" @@ -855,6 +865,8 @@ function download_stage3() { # Create verification file in case the script is restarted touch_or_die 0644 "$CURRENT_STAGE3_VERIFIED" fi + + maybe_exec 'after_download_stage3' "${CURRENT_STAGE3}" } function extract_stage3() { @@ -865,6 +877,8 @@ function extract_stage3() { [[ -e "$TMP_DIR/$CURRENT_STAGE3" ]] \ || die "stage3 file does not exist" + maybe_exec 'before_extract_stage3' "$TMP_DIR/$CURRENT_STAGE3" "$ROOT_MOUNTPOINT" + # Go to root directory cd "$ROOT_MOUNTPOINT" \ || die "Could not move to '$ROOT_MOUNTPOINT'" @@ -879,6 +893,8 @@ function extract_stage3() { || die "Error while extracting tarball" cd "$TMP_DIR" \ || die "Could not cd into '$TMP_DIR'" + + maybe_exec 'after_extract_stage3' "$TMP_DIR/$CURRENT_STAGE3" "$ROOT_MOUNTPOINT" } function gentoo_umount() { diff --git a/scripts/main.sh b/scripts/main.sh index abdf490..7fd4e9c 100644 --- a/scripts/main.sh +++ b/scripts/main.sh @@ -320,6 +320,8 @@ function generate_fstab() { function main_install_gentoo_in_chroot() { [[ $# == 0 ]] || die "Too many arguments" + maybe_exec 'before_install' + # Remove the root password, making the account accessible for automated # tasks during the period of installation. einfo "Clearing root password" @@ -342,9 +344,12 @@ function main_install_gentoo_in_chroot() { try emerge-webrsync # Configure basic system things like timezone, locale, ... + maybe_exec 'before_configure_base_system' configure_base_system + maybe_exec 'after_configure_base_system' # Prepare portage environment + maybe_exec 'before_configure_portage' configure_portage # Install git (for git portage overlays) @@ -372,6 +377,7 @@ EOF || die "Could not delete obsolete rsync gentoo repository" try emerge --sync fi + maybe_exec 'after_configure_portage' einfo "Generating ssh host keys" try ssh-keygen -A @@ -421,7 +427,9 @@ EOF fi # Install kernel and initramfs + maybe_exec 'before_install_kernel' install_kernel + maybe_exec 'after_install_kernel' # Generate a valid fstab file generate_fstab @@ -487,6 +495,8 @@ EOF || die "Could not modify /etc/portage/make.conf" fi + maybe_exec 'after_install' + einfo "Gentoo installation complete." [[ $USED_LUKS == "true" ]] \ && einfo "A backup of your luks headers can be found at '$LUKS_HEADER_BACKUP_DIR', in case you want to have a backup." diff --git a/scripts/utils.sh b/scripts/utils.sh index bda549b..3b7a38f 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -445,3 +445,9 @@ function check_wanted_programs() { ask "Continue without recommended programs?" fi } + +# exec function if defined +# $@ function name and arguments +function maybe_exec() { + type "$1" &>/dev/null && "$@" +}