2021-04-19 22:51:48 +00:00
#!/bin/bash
2021-04-26 21:48:30 +00:00
set -uo pipefail
2021-04-19 22:51:48 +00:00
2021-04-21 13:04:56 +00:00
2021-04-19 22:51:48 +00:00
################################################
# Initialize script environment
# Find the directory this script is stored in. (from: http://stackoverflow.com/questions/59895)
2021-04-21 13:04:56 +00:00
function get_source_dir() {
2021-04-19 22:51:48 +00:00
local source="${BASH_SOURCE[0]}"
while [[ -h $source ]]
do
2021-04-28 14:06:52 +00:00
local tmp
tmp="$(cd -P "$(dirname "${source}")" && pwd)"
2021-04-19 22:51:48 +00:00
source="$(readlink "${source}")"
[[ $source != /* ]] && source="${tmp}/${source}"
done
echo -n "$(realpath "$(dirname "${source}")")"
}
2021-04-28 14:06:52 +00:00
ACTUAL_WORKING_DIRECTORY="$(realpath "$(pwd)")" || exit 1
export ACTUAL_WORKING_DIRECTORY
GENTOO_INSTALL_REPO_DIR_ORIGINAL="$(get_source_dir)"
export GENTOO_INSTALL_REPO_DIR_ORIGINAL
2021-04-19 22:51:48 +00:00
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
2021-04-28 14:06:52 +00:00
# shellcheck source=./scripts/utils.sh
2021-04-19 22:51:48 +00:00
source "$GENTOO_INSTALL_REPO_DIR/scripts/utils.sh"
2021-04-28 14:06:52 +00:00
# shellcheck source=./scripts/config.sh
2021-04-20 14:55:38 +00:00
source "$GENTOO_INSTALL_REPO_DIR/scripts/config.sh"
2021-04-28 14:06:52 +00:00
# shellcheck source=./scripts/functions.sh
2021-04-19 22:51:48 +00:00
source "$GENTOO_INSTALL_REPO_DIR/scripts/functions.sh"
2021-04-28 14:06:52 +00:00
# shellcheck source=./scripts/main.sh
2021-04-19 22:51:48 +00:00
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=""
2021-04-19 22:53:38 +00:00
CONFIG="$GENTOO_INSTALL_REPO_DIR/gentoo.conf"
2021-04-19 22:51:48 +00:00
while [[ $# -gt 0 ]]; do
case "$1" in
""|"help"|"--help"|"-help"|"-h")
echo "Usage: $0 [opts]... <action>"
echo "Performs a minimal gentoo installation. See https://github.com/oddlama/gentoo-install"
2021-04-21 13:04:56 +00:00
echo "for more information. If the configuration file does not exist, the configurator will"
echo "be started instead."
2021-04-19 22:53:38 +00:00
echo ""
2021-04-19 22:51:48 +00:00
echo "Options:"
2021-05-27 18:35:28 +00:00
echo " -c, --config <CONFIG> Use the given configuration file instead of the default"
echo " location (gentoo.conf). Applies to installation as well"
echo " as initial configuration in case it doesn't exist."
2021-04-19 22:51:48 +00:00
echo ""
echo "Actions:"
2021-05-27 18:35:28 +00:00
echo " -i, --install Installs gentoo as configured. This is the default mode,"
echo " if the given configuration file exists."
echo " -R, --chroot <DIR> [CMD...] Chroot into an existing system. The root filesystem"
echo " must already be mounted under DIR. All required special"
echo " filesystems will be mounted inside, and unmounted when"
echo " the chroot exits."
2021-04-19 22:51:48 +00:00
exit 0
;;
"-c"|"--config")
[[ -f "$2" ]] \
|| die "Config file not found: '$2'"
2021-04-28 14:06:52 +00:00
CONFIG="$(cd "$ACTUAL_WORKING_DIRECTORY" && realpath --relative-to="$GENTOO_INSTALL_REPO_DIR" "$2" 2>/dev/null)" || die "Could not determine realpath to config"
2021-04-19 22:51:48 +00:00
shift
;;
"-R"|"--chroot")
[[ -z $ACTION ]] || die "Multiple actions given"
ACTION="chroot"
2021-05-27 18:35:28 +00:00
CHROOT_DIR="$2"
[[ -e "$CHROOT_DIR" ]] || die "Chroot directory not found: '$CHROOT_DIR'"
shift
2021-04-19 22:51:48 +00:00
;;
"-i"|"--install")
[[ -z $ACTION ]] || die "Multiple actions given"
ACTION="install"
;;
"__install_gentoo_in_chroot")
ACTION="__install_gentoo_in_chroot"
;;
*) die "Invalid option '$1'" ;;
esac
shift
done
2021-04-21 13:04:56 +00:00
# Check configuration location
2022-05-13 20:43:58 +00:00
[[ -z "${CONFIG%%"$GENTOO_INSTALL_REPO_DIR"*}" ]] \
2021-04-19 22:51:48 +00:00
|| die "Configuration file must be inside the installation directory. This is needed so it is accessible from within the chroot environment."
2021-04-21 13:04:56 +00:00
if [[ -z "$ACTION" ]]; then
if [[ -e "$CONFIG" ]]; then
# Default if configuration exists: Run installer
ACTION="install"
else
2023-10-15 02:01:05 +00:00
# Default if configuration does not exist: Run configurator, and exit afterwards.
2022-06-28 15:29:38 +00:00
echo "You have not created a gentoo.conf. Starting configurator instead of installing."
2021-04-21 13:04:56 +00:00
exec "$GENTOO_INSTALL_REPO_DIR/configure" "$CONFIG"
fi
fi
2021-05-31 15:20:06 +00:00
if [[ "$ACTION" != "chroot" ]]; then
2021-05-31 15:17:40 +00:00
# Load config if we aren't just chrooting
2021-05-31 15:20:06 +00:00
[[ -e "$CONFIG" ]] \
|| die "Configuration file '$CONFIG' does not exist. To run the configurator, omit '-i' flag or run ./configure"
2021-04-21 13:04:56 +00:00
2021-05-31 15:11:39 +00:00
# shellcheck disable=SC1090
source "$CONFIG" || die "Could not 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
fi
2021-04-19 22:51:48 +00:00
[[ $EUID == 0 ]] \
|| die "Must be root"
mkdir_or_die 0755 "$TMP_DIR"
case "$ACTION" in
2021-05-27 18:35:28 +00:00
"chroot") main_chroot "$CHROOT_DIR" "$@" ;;
2021-04-19 22:51:48 +00:00
"install") main_install "$@" ;;
"__install_gentoo_in_chroot") main_install_gentoo_in_chroot "$@" ;;
*) die "Invalid action '$ACTION'" ;;
esac