From a531945dbb6614a7d4ac9df13cc5b549cb6cfdf0 Mon Sep 17 00:00:00 2001 From: oddlama Date: Tue, 20 Apr 2021 00:51:48 +0200 Subject: [PATCH] Use sane script locations and add help menu --- TODO | 6 ++ chroot | 1 - scripts/config.sh => config.sh | 6 +- install | 111 ++++++++++++++++++++++++++++++- scripts/install_gentoo_in_chroot | 1 - scripts/main.sh | 63 +----------------- scripts/utils.sh | 7 +- umount | 1 - 8 files changed, 124 insertions(+), 72 deletions(-) create mode 100644 TODO delete mode 120000 chroot rename scripts/config.sh => config.sh (97%) mode change 120000 => 100755 install delete mode 120000 scripts/install_gentoo_in_chroot mode change 100755 => 100644 scripts/main.sh delete mode 120000 umount diff --git a/TODO b/TODO new file mode 100644 index 0000000..afb3c4d --- /dev/null +++ b/TODO @@ -0,0 +1,6 @@ +- root authorized_keys support +- generalize ansible -> any infrastructure management by allowing only root ssh login. +- zfs support +- dracut -> genkernel +- save meta information to /var/db/gentoo-install +- systemd settings pls diff --git a/chroot b/chroot deleted file mode 120000 index e8e1bb5..0000000 --- a/chroot +++ /dev/null @@ -1 +0,0 @@ -scripts/main.sh \ No newline at end of file diff --git a/scripts/config.sh b/config.sh similarity index 97% rename from scripts/config.sh rename to config.sh index 288a1bf..b0a06e8 100644 --- a/scripts/config.sh +++ b/config.sh @@ -1,7 +1,3 @@ -source "$GENTOO_INSTALL_REPO_DIR/scripts/protection.sh" || exit 1 -source "$GENTOO_INSTALL_REPO_DIR/scripts/internal_config.sh" || exit 1 - - ################################################ # Disk configuration @@ -10,7 +6,7 @@ source "$GENTOO_INSTALL_REPO_DIR/scripts/internal_config.sh" || exit 1 # # You can also create your own scheme using the functions provided in internal_config.sh, # if you need something tailored to your specific system. Generally supported is -# any combination of RAID0/1, luks, btrfs and the usual filesystems (ext4, fat) +# any combination of RAID0/1, luks, zfs, btrfs and the usual filesystems (ext4, fat) # Have a look at the implementation of the default schemes, but be aware that you # most likely don't want to implement your own scheme. # diff --git a/install b/install deleted file mode 120000 index e8e1bb5..0000000 --- a/install +++ /dev/null @@ -1 +0,0 @@ -scripts/main.sh \ No newline at end of file diff --git a/install b/install new file mode 100755 index 0000000..c2659b9 --- /dev/null +++ b/install @@ -0,0 +1,110 @@ +#!/bin/bash +set -o pipefail + +################################################ +# Initialize script environment + +# Find the directory this script is stored in. (from: http://stackoverflow.com/questions/59895) +get_source_dir() { + local source="${BASH_SOURCE[0]}" + while [[ -h $source ]] + do + local tmp="$(cd -P "$(dirname "${source}")" && pwd)" + source="$(readlink "${source}")" + [[ $source != /* ]] && source="${tmp}/${source}" + done + + echo -n "$(realpath "$(dirname "${source}")")" +} + +export GENTOO_INSTALL_REPO_DIR_ORIGINAL="$(get_source_dir)" +export GENTOO_INSTALL_REPO_DIR="$GENTOO_INSTALL_REPO_DIR_ORIGINAL" +export GENTOO_INSTALL_REPO_SCRIPT_ACTIVE=true +export GENTOO_INSTALL_REPO_SCRIPT_PID=$$ + +umask 0077 + +source "$GENTOO_INSTALL_REPO_DIR/scripts/utils.sh" +source "$GENTOO_INSTALL_REPO_DIR/scripts/internal_config.sh" +source "$GENTOO_INSTALL_REPO_DIR/scripts/functions.sh" +source "$GENTOO_INSTALL_REPO_DIR/scripts/main.sh" + + +################################################ +# Main dispatch + +# Instantly kill when pressing ctrl-c +trap 'kill "$GENTOO_INSTALL_REPO_SCRIPT_PID"' INT + +ACTION="" +CONFIG="$GENTOO_INSTALL_REPO_DIR/config.sh" + +while [[ $# -gt 0 ]]; do + case "$1" in + ""|"help"|"--help"|"-help"|"-h") + echo "Usage: $0 [opts]... " + echo "Performs a minimal gentoo installation. See https://github.com/oddlama/gentoo-install" + echo "for more information." + echo " location (config.sh)" + echo "Options:" + echo " -c, --config Use the given configuration file instead of the default" + echo " location (config.sh)" + echo "" + echo "Actions:" + echo " -i, --install Installs gentoo using the configuration" + echo " provided in config.sh" + echo " -R, --chroot Chroot into an existing system. The root filesystem" + echo " is mounted automatically based on the partition" + echo " UUIDs (generated when installing for the first time)," + echo " and unmounted when the chroot exits" + echo " -u, --umount Try to unmount all associated mountpoints. (Only needed" + echo " if anything gets forcefully interrupted)" + exit 0 + ;; + "-c"|"--config") + [[ -f "$2" ]] \ + || die "Config file not found: '$2'" + CONFIG="$(realpath "$2" 2>/dev/null)" + shift + ;; + "-R"|"--chroot") + [[ -z $ACTION ]] || die "Multiple actions given" + ACTION="chroot" + ;; + "-i"|"--install") + [[ -z $ACTION ]] || die "Multiple actions given" + ACTION="install" + ;; + "-u"|"--umount"|"--unmount") + [[ -z $ACTION ]] || die "Multiple actions given" + ACTION="umount" + ;; + "__install_gentoo_in_chroot") + ACTION="__install_gentoo_in_chroot" + ;; + *) die "Invalid option '$1'" ;; + esac + shift +done + +# Load config +[[ -z "${CONFIG%%$GENTOO_INSTALL_REPO_DIR*}" ]] \ + || die "Configuration file must be inside the installation directory. This is needed so it is accessible from within the chroot environment." + +source "$CONFIG" +[[ $I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY == "true" ]] \ + || die "You have not properly read the config. Edit the config file and set I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY=true to continue." + +preprocess_config +[[ $EUID == 0 ]] \ + || die "Must be root" + +mkdir_or_die 0755 "$TMP_DIR" + +case "$ACTION" in + "chroot") main_chroot "$@" ;; + "install") main_install "$@" ;; + "umount") main_umount "$@" ;; + "__install_gentoo_in_chroot") main_install_gentoo_in_chroot "$@" ;; + *) die "Invalid action '$ACTION'" ;; +esac diff --git a/scripts/install_gentoo_in_chroot b/scripts/install_gentoo_in_chroot deleted file mode 120000 index fe75b4e..0000000 --- a/scripts/install_gentoo_in_chroot +++ /dev/null @@ -1 +0,0 @@ -main.sh \ No newline at end of file diff --git a/scripts/main.sh b/scripts/main.sh old mode 100755 new mode 100644 index 1d1e8a3..700636e --- a/scripts/main.sh +++ b/scripts/main.sh @@ -1,41 +1,4 @@ -#!/bin/bash -set -o pipefail - -################################################ -# Initialize script environment - -# Find the directory this script is stored in. (from: http://stackoverflow.com/questions/59895) -get_source_dir() { - local source="${BASH_SOURCE[0]}" - while [[ -h $source ]] - do - local tmp="$(cd -P "$(dirname "${source}")" && pwd)" - source="$(readlink "${source}")" - [[ $source != /* ]] && source="${tmp}/${source}" - done - - echo -n "$(realpath "$(dirname "${source}")")" -} - -export GENTOO_INSTALL_REPO_DIR_ORIGINAL="$(dirname "$(get_source_dir)")" -export GENTOO_INSTALL_REPO_DIR="$GENTOO_INSTALL_REPO_DIR_ORIGINAL" -export GENTOO_INSTALL_REPO_SCRIPT_ACTIVE=true -export GENTOO_INSTALL_REPO_SCRIPT_PID=$$ - -umask 0077 - -source "$GENTOO_INSTALL_REPO_DIR/scripts/utils.sh" -source "$GENTOO_INSTALL_REPO_DIR/scripts/config.sh" -source "$GENTOO_INSTALL_REPO_DIR/scripts/functions.sh" - -[[ $I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY == "true" ]] \ - || die "You have not properly read the config. Set I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY=true to continue." - -preprocess_config - -mkdir_or_die 0755 "$TMP_DIR" -[[ $EUID == 0 ]] \ - || die "Must be root" +source "$GENTOO_INSTALL_REPO_DIR/scripts/protection.sh" || exit 1 ################################################ @@ -433,35 +396,15 @@ main_install() { [[ $IS_EFI == "true" ]] \ && mount_efivars - gentoo_chroot "$GENTOO_INSTALL_REPO_BIND/scripts/main.sh" install_gentoo_in_chroot + gentoo_chroot "$GENTOO_INSTALL_REPO_BIND/scripts/main.sh" __install_gentoo_in_chroot gentoo_umount } main_chroot() { gentoo_chroot "$@" + gentoo_umount } main_umount() { gentoo_umount } - - -################################################ -# Main dispatch - -# Instantly kill when pressing ctrl-c -trap 'kill "$GENTOO_INSTALL_REPO_SCRIPT_PID"' INT - -SCRIPT_ALIAS="$(basename "$0")" -if [[ $SCRIPT_ALIAS == main.sh ]]; then - SCRIPT_ALIAS="$1" - shift -fi - -case "$SCRIPT_ALIAS" in - "chroot") main_chroot "$@" ;; - "install") main_install "$@" ;; - "install_gentoo_in_chroot") main_install_gentoo_in_chroot "$@" ;; - "umount") main_umount "$@" ;; - *) die "Invalid alias '$SCRIPT_ALIAS' was used to execute this script" ;; -esac diff --git a/scripts/utils.sh b/scripts/utils.sh index 946c79f..d0cc0a9 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -5,7 +5,7 @@ elog() { } einfo() { - echo "[+] $*" + echo "[+] $*" } ewarn() { @@ -13,12 +13,13 @@ ewarn() { } eerror() { - echo " * ERROR: $*" >&2 + echo "error: $*" >&2 } die() { eerror "$*" - kill "$GENTOO_INSTALL_REPO_SCRIPT_PID" + [[ $$ == $GENTOO_INSTALL_REPO_SCRIPT_PID ]] \ + || kill "$GENTOO_INSTALL_REPO_SCRIPT_PID" exit 1 } diff --git a/umount b/umount deleted file mode 120000 index e8e1bb5..0000000 --- a/umount +++ /dev/null @@ -1 +0,0 @@ -scripts/main.sh \ No newline at end of file