gentoo-install/configure

324 lines
8.2 KiB
Bash
Executable File

#!/bin/bash
set -o pipefail
################################################
# Initialize script environment
# Find the directory this script is stored in. (from: http://stackoverflow.com/questions/59895)
function 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="$(get_source_dir)"
export GENTOO_INSTALL_REPO_SCRIPT_ACTIVE=true
source "$GENTOO_INSTALL_REPO_DIR/scripts/utils.sh"
# Remember actual config file path
CONFIG_FILE="$(realpath "${1-"gentoo.conf"}" 2>/dev/null)"
RELA_CONFIG_FILE="$(realpath --relative-to="$(pwd)" "$CONFIG_FILE" 2>/dev/null)"
# Check if help is requested
while [[ $# -gt 0 ]]; do
case "$1" in
""|"help"|"--help"|"-help"|"-h")
echo "Usage: $0 [config]"
echo "Starts the gentoo configurator. If no file is given, it defaults to 'gentoo.conf'."
echo "If the file doesn't exist, the configurator loads default values, otherwise, the"
echo "the configuration is loaded into the configurator."
exit 0
;;
esac
shift
done
# TODO check install dialog
echo "Please install dialog on your system to use the configurator"
################################################
# Configuration storage
SAVE_AS_FILENAME="$RELA_CONFIG_FILE"
if [[ -e "$CONFIG_FILE" ]]; then
# Load settings
UNSAVED_CHANGES=false
source "$CONFIG_FILE" || die "Could not load given configuration."
else
# Default settings
UNSAVED_CHANGES=true
HOSTNAME="gentoo"
# TODO get from current system
TIMEZONE="Europe/London"
KEYMAP="us"
KEYMAP_INITRAMFS="$KEYMAP"
LOCALES=""
LOCALE="C.utf8"
GENTOO_MIRROR="https://mirror.eu.oneandone.net/linux/distributions/gentoo/gentoo"
GENTOO_ARCH="amd64"
STAGE3_BASENAME="stage3-$GENTOO_ARCH-systemd"
SELECT_MIRRORS=true
SELECT_MIRRORS_LARGE_FILE=false
INIT_SYSTEM=systemd
ADDITIONAL_PACKAGES=("app-editors/neovim")
INSTALL_SSHD=true
ROOT_SSH_AUTHORIZED_KEYS=""
fi
################################################
# Menu definition
MENU_ITEMS=(
"HOSTNAME"
"TIMEZONE"
"KEYMAP"
"LOCALE"
"INIT_SYSTEM"
"KEYFILE"
)
function HOSTNAME_tag() { echo "Hostname"; }
function HOSTNAME_label() { echo "($HOSTNAME)"; }
function HOSTNAME_help() { echo "Enter the desired system hostname here, be aware that when creating mdadm raid arrays, this value will be recorded in metadata block. If you change it later, you should also update the metadata."; }
function HOSTNAME_menu() {
local sel
sel="$(dialog --clear \
--help-button --help-label "Menu" \
--ok-label "Next" --cancel-label "Exit" \
--extra-button --extra-label "Back" \
--title "Select hostname" \
--inputbox "Enter the hostname for your new system." \
8 72 "$HOSTNAME" 3>&2 2>&1 1>&3 3>&-)"
UNSAVED_CHANGES=true
}
function TIMEZONE_tag() { echo "Timezone"; }
function TIMEZONE_label() { echo "($TIMEZONE)"; }
function TIMEZONE_help() { echo "The timezone for the new system."; }
function TIMEZONE_menu() {
true
}
function KEYMAP_tag() { echo "Keymap"; }
function KEYMAP_label() { echo "($KEYMAP)"; }
function KEYMAP_help() { echo "The default vconsole keymap for the system."; }
function KEYMAP_menu() {
local items=()
local map
for map in $(find /usr/share/keymaps/ /usr/share/kbd/keymaps/ -type f -iname '*.map.gz' -printf "%f\n" 2>/dev/null | sort -u); do
map="${map%%.map.gz}"
if [[ $map == $KEYMAP ]]; then
items+=("${map}" "off")
else
items+=("${map}" "off")
fi
done
local sel
sel="$(dialog --clear \
--help-button --help-label "Menu" \
--ok-label "Next" --cancel-label "Exit" \
--extra-button --extra-label "Back" \
--noitem \
--title "Select keymap" \
--radiolist "Select which keymap to use in the vconsole." \
16 72 8 "${items[@]}" 3>&2 2>&1 1>&3 3>&-)"
}
function LOCALE_tag() { echo "Locale"; }
function LOCALE_label() { echo "($LOCALE)"; }
function LOCALE_help() { echo "The locale to set for the new system. Be careful, the available options deviate from the corresponding names in the list of locales which is used by locale-gen. For example the locale 'en_US.utf8' is called 'en_US.UTF-8' in /etc/locale.gen). Use the name as shown in `eselect locale` here."; }
function LOCALE_menu() {
true
}
function INIT_SYSTEM_tag() { echo "Init system"; }
function INIT_SYSTEM_label() { echo "($INIT_SYSTEM)"; }
function INIT_SYSTEM_help() { echo ""; }
function INIT_SYSTEM_menu() {
true
}
function KEYFILE_tag() { echo "Key file"; }
function KEYFILE_label() { echo "($KEYFILE)"; }
function KEYFILE_help() { echo ""; }
function KEYFILE_menu() {
true
}
################################################
# Menu functions
# $1: filename
function save() {
echo save to "$1"
cat > "$1" <<EOF
# vim: set ft=sh ts=4 sw=4 sts=-1 noet:
# This file will be interpreted by /bin/bash.
################################################
# Disk configuration
create_btrfs_raid_layout swap=8GiB luks=true /dev/sdX
luks_getkeyfile() {
case "\$1" in
#'my_luks_partition') echo -n '/path/to/my_luks_partition_keyfile' ;;
*) echo -n "/path/to/luks-keyfile" ;;
esac
}
################################################
# System configuration
HOSTNAME=${HOSTNAME@Q}
TIMEZONE=${TIMEZONE@Q}
KEYMAP=${KEYMAP@Q}
KEYMAP_INITRAMFS=${KEYMAP_INITRAMFS@Q}
LOCALES=${LOCALES@Q}
LOCALE=${LOCALE@Q}
################################################
# Gentoo configuration
GENTOO_MIRROR="https://mirror.eu.oneandone.net/linux/distributions/gentoo/gentoo"
GENTOO_ARCH="amd64"
STAGE3_BASENAME="stage3-$GENTOO_ARCH-systemd"
SELECT_MIRRORS=true
SELECT_MIRRORS_LARGE_FILE=false
SYSTEMD=true
################################################
# Additional (optional) configuration
ADDITIONAL_PACKAGES=("app-editors/neovim")
INSTALL_SSHD=true
ROOT_SSH_AUTHORIZED_KEYS=""
################################################
# Prove that you have read the config
I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY=true
EOF
}
function msgbox_help() {
dialog --clear \
--msgbox "$1" \
8 66 3>&2 2>&1 1>&3 3>&-
}
function menu_exit() {
if [[ $UNSAVED_CHANGES == "true" ]]; then
local sel
sel="$(dialog --clear \
--help-button --help-label "Back" \
--yes-label "Save" --no-label "Discard" \
--yesno "Do you want to save your configuration?\n(Press <ESC><ESC>, or choose <Back> to continue gentoo configuration)." \
8 66 3>&2 2>&1 1>&3 3>&-)"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Save>
save "$CONFIG_FILE"
exit 0
elif [[ $diag_exit == 1 ]]; then
# <Discard>
exit 0
else
# Back to menu (<ESC><ESC>, <Back>)
true
fi
else
# Nothing was changed. Exit immediately.
exit 0
fi
}
function menu_save_as() {
local sel
sel="$(dialog --clear \
--ok-label "Save" \
--inputbox "Enter a filename to which this configuration should be saved.\n(Press <ESC><ESC>, or choose <Cancel> to abort)." \
8 66 "$SAVE_AS_FILENAME" 3>&2 2>&1 1>&3 3>&-)"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Save>
SAVE_AS_FILENAME="$sel"
save "$SAVE_AS_FILENAME"
UNSAVED_CHANGES=false
else
# Back to menu (<ESC><ESC>, <Cancel>)
true
fi
}
function menu() {
local item
local item_tag
local tag_item_list=()
declare -A reverse_lookup
# Create menu list
for item in "${MENU_ITEMS[@]}"; do
item_tag="$("${item}_tag")"
tag_item_list+=("$item_tag" "$("${item}_label")")
reverse_lookup["$item_tag"]="$item"
done
local sel
sel="$(dialog --clear \
--title "Gentoo configuration ($RELA_CONFIG_FILE)" \
--extra-button --extra-label "Exit" \
--help-button \
--ok-label "Select" --cancel-label "Save" \
--menu "This is the gentoo configuration menu. Read and adjust all options below carefully. Save your desired configuration and run ./install afterwards. Use the help if you want further information for any item." \
20 72 12 "${tag_item_list[@]}" 3>&2 2>&1 1>&3 3>&-)"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Select>
"${reverse_lookup[$sel]}_menu"
elif [[ $diag_exit == 1 ]]; then
# <Save>
menu_save_as
elif [[ $diag_exit == 2 ]]; then
# <Help>
msgbox_help "$("${reverse_lookup[${sel#HELP }]}_help")"
else
# Exit (<ESC><ESC>, <Exit>)
menu_exit
true
fi
}
# Begin menu loop. Exit will be called to end this loop where it is appropriate.
while true; do
menu
done