installer script: work in progress (2)

This commit is contained in:
Daniel Langbein 2021-04-30 21:42:12 +02:00
parent 195c1835fb
commit 30dfe15a6e
4 changed files with 169 additions and 54 deletions

View File

@ -3,6 +3,9 @@
# load config # load config
source installer.cfg || { exit 1; } source installer.cfg || { exit 1; }
# load functions
source helper.sh || { exit 1; }
function check_network() { function check_network() {
echo "Sending ping to wikipedia.de ..." echo "Sending ping to wikipedia.de ..."
@ -26,31 +29,6 @@ function increase_cow_space() {
mount -o remount,size=2G /run/archiso/cowspace || return $? mount -o remount,size=2G /run/archiso/cowspace || return $?
} }
function get_block_devices(){
# return: the following variables:
# BLOCK_DEVICES (array with each block device as one entry)
local tmp
# # get list of devices , one per line
# #
# # -> remove everything after first whitespace
# # -> with sed; alternative: awk '{print $1}'
# # -> add "/dev/" at start of each line
# tmp=$(lsblk --noheadings --nodeps | sed 's|\s.*$||; s|^|&/dev/|') || return $?
tmp=$(lsblk -dplnx size -o name | grep -Ev "boot|rpmb|loop")
readarray -t BLOCK_DEVICES <<<"$tmp"
}
function get_block_devices_with_size() {
# return: the following variables:
# BLOCK_DEVICE_SIZES (space separated list of block devices and their sizes)
# one could use sed to remove multiple white spaces
# sed 's|\s\s*| |'
BLOCK_DEVICE_SIZES="$(lsblk -dplnx size -o name,size | grep -Ev "boot|rpmb|loop" | tac)"
}
function get_text_input { function get_text_input {
# If variable with name $1 is zero, then ask user for input. # If variable with name $1 is zero, then ask user for input.
# Only one line user input is allowed. # Only one line user input is allowed.
@ -64,49 +42,49 @@ function get_text_input {
fi fi
for i in "$@"; do for i in "$@"; do
if [ -z "$i" ]; then if [ -z "$i" ]; then
echo "get_menu_selections: all given args must not be empty"; echo "get_text_input: all given args must not be empty";
return 1; return 1;
fi fi
done done
local -n VAR=$1 local -n ptr=$1
if [ -z "$VAR" ]; then if [ -z "$ptr" ]; then
# if VAR has no value yet, ask user for input! # if ptr has no value yet, ask user for input!
echo "$2" echo "$2"
read -r VAR || return $? read -r ptr || return $?
fi fi
# check string length to be greater than zero! # check string length to be greater than zero!
if [ "${#VAR}" -lt 1 ]; then if [ "${#ptr}" -lt 1 ]; then
echo "get_text_input must not be empty!"; echo "get_text_input must not be empty!";
return 1; return 1;
fi fi
} }
function get_menu_selection { function get_single_choice {
# If variable with name $1 is zero, then ask user to select one option. # If variable with name $1 is zero, then ask user to select one option.
# #
# $1: name of variable to store user selection # $1: name of variable to store the selected option
# $2: text to display # $2: text to display
# $3: name of variable with space-separated menu options to display (each menu option consists of an item and a description) # $3: name of variable with space-separated menu options to display (each menu option consists of an item and a description)
if [ "$#" -ne 3 ]; then if [ "$#" -ne 3 ]; then
echo "get_menu_selection requires three args!"; echo "get_single_choice requires three args!";
return 1 return 1
fi fi
for i in "$@"; do for i in "$@"; do
if [ -z "$i" ]; then if [ -z "$i" ]; then
echo "get_menu_selections: all given args must not be empty"; echo "get_single_choice: all given args must not be empty";
return 1; return 1;
fi fi
done done
local -n VAR=$1 local -n ptr=$1
if [ -z "$VAR" ]; then if [ -z "$ptr" ]; then
# if VAR has no value yet, ask user for input! # if ptr has no value yet, ask user for input!
local -n MENU_OPTIONS=$3 local -n MENU_OPTIONS=$3
VAR=$(dialog --stdout --menu "$2" 0 0 0 ${MENU_OPTIONS}) || { ptr=$(dialog --stdout --menu "$2" 0 0 0 ${MENU_OPTIONS}) || {
echo "Error during menu selection!" echo "Error during menu selection!"
exit 1 exit 1
} }
@ -114,6 +92,41 @@ function get_menu_selection {
fi fi
} }
function get_multi_choice {
# If variable with name $1 is zero, then ask user to select one ore more options.
#
# $1: name of variable to store array of selected options
# $2: text to display
# $3: name of variable with space-separated menu options to display (each menu option consists of an item, a description and on/off if preselected or not)
if [ "$#" -ne 3 ]; then
echo "get_multi_choice requires three args!";
return 1
fi
for i in "$@"; do
if [ -z "$i" ]; then
echo "get_multi_choice: all given args must not be empty";
return 1;
fi
done
local -n ptr=$1
if [ -z "$ptr" ]; then
# if ptr has no value yet, ask user for input!
local -n MENU_OPTIONS=$3
local tmp=$(dialog --stdout --checklist "$2" 0 0 0 ${MENU_OPTIONS}) || {
echo "Error during menu selection!"
exit 1
}
clear
# Result of dialog is space separated list
# Store this as an array
readarray -d " " -t ptr <<<"$tmp"
fi
}
function get_user_input() { function get_user_input() {
# return: the following variables: # return: the following variables:
# BIOS_TYPE (uefi or bios) # BIOS_TYPE (uefi or bios)
@ -123,18 +136,17 @@ function get_user_input() {
# USERNAME, USER_PWD # USERNAME, USER_PWD
# LUKS_PWD # LUKS_PWD
# get_block_devices || return $?
get_block_devices_with_size || return $? get_block_devices_with_size || return $?
get_menu_selection TARGET_BLOCK_DEVICE "Select target device for installation" BLOCK_DEVICE_SIZES || return $? get_single_choice TARGET_BLOCK_DEVICE "Select target device for installation" BLOCK_DEVICE_SIZES || return $?
TMP1="uefi Newer-mainboards bios Legacy-BIOS-on-older-mainboards" TMP1="uefi Newer-mainboards bios Legacy-BIOS-on-older-mainboards"
get_menu_selection BIOS_TYPE "Select your bios type" TMP1 || return $? get_single_choice BIOS_TYPE "Select your bios type" TMP1 || return $?
TMP1="linux-lts Long-Time-Stable-Linux-kernel linux Latest-Linux-kernel" TMP1="linux-lts Long-Time-Stable-Linux-kernel linux Latest-Linux-kernel"
get_menu_selection KERNEL "Select kernel version" TMP1 || return $? get_single_choice KERNEL "Select kernel version" TMP1 || return $?
TMP1="BTRFS allows-snapshots-and-dynamic-extension-of-the-fs EXT4 default-fs-of-many-distributions F2FS Flash-Friendly-fs-for-SSD-or-NVMe" TMP1="BTRFS allows-snapshots-and-dynamic-extension-of-the-fs EXT4 default-fs-of-many-distributions F2FS Flash-Friendly-fs-for-SSD-or-NVMe"
get_menu_selection FS "Select filesystem to use" TMP1 || return $? get_single_choice FS "Select filesystem to use" TMP1 || return $?
get_text_input HOSTNAME "Enter hostname:" || return $? get_text_input HOSTNAME "Enter hostname:" || return $?
get_text_input USERNAME "Enter username:" || return $? get_text_input USERNAME "Enter username:" || return $?
@ -178,10 +190,77 @@ function get_cpu_vendor() {
fi fi
} }
function get_default_mount_options() {
# return: the following variables:
# FS_DEFAULT_MOUNT_OPTIONS (array)
FS_DEFAULT_MOUNT_OPTIONS=()
case "${FS}" in
BTRFS)
# compress=lzo: archwiki -> Btrfs#Compression
# "Enable compression (better performance, longer flash lifespan)"
FS_DEFAULT_MOUNT_OPTIONS+=('compress=lzo')
;;
EXT4)
# https://wiki.archlinux.org/index.php/Ext4#Enabling_metadata_checksums
# If the CPU supports SSE 4.2, make sure the crc32c_intel kernel module is loaded (TODO)
FS_DEFAULT_MOUNT_OPTIONS+=('metadata_csum')
;;
F2FS)
# When mounting the filesystem, specify compress_algorithm=(lzo|lz4|zstd|lzo-rle).
# Using compress_extension=txt will cause all txt files to be compressed by default.
FS_DEFAULT_MOUNT_OPTIONS+=('compress_algorithm=lz4')
;;
*)
echo "Filesystem $FS not yet supported!"
return 1
;;
esac
}
function choose_mount_options() {
# return: the following variables:
# FS_MOUNT_OPTIONS (array)
case "${FS}" in
BTRFS)
# noatime, nodiratime:
# - The atime options do impact drive performance;
# - noatime implies nodiratime, one does not need to specify both;
# - The noatime option fully disables writing file access times to the drive every time you read a file.
# This works well for almost all applications, except for those that need to know if a file has been
# read since the last time it was modified.
TMP1="noatime Don't-write-file/folder-access-times off ssd Only-enable-when-on-ssd-or-nvme off"
;;
EXT4)
TMP1="noatime Don't-write-file/folder-access-times off"
;;
F2FS)
TMP1="noatime Don't-write-file/folder-access-times off"
;;
*)
echo "Filesystem $FS not yet supported!"
return 1
;;
esac
get_multi_choice FS_MOUNT_OPTIONS "Select mount options" TMP1 || return $?
# TODO: test FS_MOUNT_OPTIONS
echo "${FS_MOUNT_OPTIONS[@]}"
echo "${#FS_MOUNT_OPTIONS[@]}"
for i in "${FS_MOUNT_OPTIONS[@]}"; do
echo "<<$i>>"
done
}
function main() { function main() {
check_network || return $? check_network || return $?
get_user_input || return $? get_user_input || return $?
get_cpu_vendor || return $? get_cpu_vendor || return $?
get_default_mount_options || return $?
choose_mount_options || return $?
} }
main "$@" main "$@"

View File

@ -0,0 +1,35 @@
function newline_to_space(){
# Replaces all newlines with spaces
# $1: name of variable
local -n ptr=$1
# Replace newlines with spaces
# See bash string substitution: https://gist.github.com/JPvRiel/b279524d3e56229a896477cb8082a72b
echo "removing newlines in str: -->$ptr<--"
ptr="${ptr//$'\n'/' '}"
echo "new str is: -->$ptr<--"
}
function get_block_devices(){
# return: the following variables:
# BLOCK_DEVICES (array with each block device as one entry)
# get list of devices, one per line
local tmp
tmp=$(lsblk -dplnx size -o name | grep -Ev "boot|rpmb|loop")
readarray -t BLOCK_DEVICES <<<"$tmp"
}
function get_block_devices_with_size() {
# return: the following variables:
# BLOCK_DEVICE_SIZES (space separated list of block devices and their sizes)
# one could use sed to remove multiple white spaces
# sed 's|\s\s*| |'
BLOCK_DEVICE_SIZES="$(lsblk -dplnx size -o name,size | grep -Ev "boot|rpmb|loop" | tac)"
# replace newlines with spaces
newline_to_space BLOCK_DEVICE_SIZES
}

View File

@ -8,8 +8,9 @@ LUKS_PWD=test
KERNEL=linux KERNEL=linux
TARGET_BLOCK_DEVICE=/dev/sda # TARGET_BLOCK_DEVICE=/dev/sda
FS=BTRFS FS=BTRFS
CPU_VENDOR=none CPU_VENDOR=none
BIOS_TYPE=uefi BIOS_TYPE=uefi
# FS_MOUNT_OPTIONS=()

View File

@ -18,14 +18,14 @@ function text_input {
return 1 return 1
fi fi
local -n VAR=$1 local -n ptr=$1
if [ -z "$VAR" ]; then if [ -z "$ptr" ]; then
echo "$2" echo "$2"
read -r VAR || return $? read -r ptr || return $?
fi fi
# check string length to be greater than zero! # check string length to be greater than zero!
if [ "${#VAR}" -lt 1 ]; then if [ "${#ptr}" -lt 1 ]; then
echo "text_input must not be empty!"; echo "text_input must not be empty!";
return 1; return 1;
fi fi