WIP: Adding framework for more sophisticated disk setups

This commit is contained in:
oddlama 2020-04-19 21:34:36 +02:00
parent 00fe9bc553
commit 7effe7f76b
No known key found for this signature in database
GPG Key ID: 88EA325D51D53908
5 changed files with 244 additions and 31 deletions

View File

@ -1,5 +1,3 @@
#!/bin/bash
source "$GENTOO_INSTALL_REPO_DIR/scripts/protection.sh" || exit 1
source "$GENTOO_INSTALL_REPO_DIR/scripts/internal_config.sh" || exit 1
@ -7,27 +5,49 @@ source "$GENTOO_INSTALL_REPO_DIR/scripts/internal_config.sh" || exit 1
################################################
# Disk configuration
# Enable swap?
ENABLE_SWAP=true
# Enable partitioning (will still ask before doing anything critical)
ENABLE_PARTITIONING=true
# Format the partitions with the correct filesystems,
# if you didn't chose automatic partitioning, you will be asked
# before any formatting is done.
ENABLE_FORMATTING=true
# Example 1: Single disk, 3 partitions (efi, swap, root)
create_default_disk_layout() {
local device="$1"
# The device to partition
PARTITION_DEVICE="/dev/sda"
# Size of swap partition (if enabled)
PARTITION_SWAP_SIZE="8GiB"
# The size of the EFI partition
PARTITION_EFI_SIZE="128MiB"
create_partition new_id=part_efi device="$device" size=128MiB type=efi
create_partition new_id=part_swap device="$device" size=8GiB type=raid
create_partition new_id=part_root device="$device" size=auto type=raid
# Partition UUIDs.
# You must insert these by hand, if you do not use automatic partitioning
PARTITION_UUID_EFI="$(load_or_generate_uuid 'efi')"
PARTITION_UUID_SWAP="$(load_or_generate_uuid 'swap')"
PARTITION_UUID_LINUX="$(load_or_generate_uuid 'linux')"
format id=part_efi type=efi label=efi
format id=part_swap type=swap label=swap
format id=part_root type=ext4 label=ext4
set_efi id=part_efi
set_swap id=part_swap
set_root id=part_root
}
create_default_disk_layout
# Example 2: Multiple disks, with raid 0 and luks
# - efi: partition on all disks, but only first disk used
# - swap: raid 0 → fs
# - root: raid 0 → luks → fs
devices=(/dev/sd{X,Y})
for i in "${!devices[@]}"; do
device="${devices[$i]}"
create_partition new_id="part_efi_dev${i}" device="$device" size=128MiB type=efi
create_partition new_id="part_swap_dev${i}" device="$device" size=8GiB type=raid
create_partition new_id="part_root_dev${i}" device="$device" size=auto type=raid
done
create_raid new_id=part_raid_swap level=0 ids="${part_swap_dev*}"
create_raid new_id=part_raid_root level=0 ids="${part_root_dev*}"
create_luks new_id=part_luks_root id=part_raid_root
format id=part_efi_dev0 type=efi label=efi
format id=part_raid_swap type=swap label=swap
format id=part_luks_root type=ext4 label=ext4
set_efi id=part_efi_dev0
set_swap id=part_raid_swap
set_root id=part_luks_root
################################################
@ -47,8 +67,8 @@ KEYMAP="de-latin1-nodeadkeys"
# add locales here if you really need them and want to localize
# your system. Otherwise, leave this list empty, and use C.utf8.
LOCALES=""
# The locale to set for the system. Be careful, this setting differs
# from the LOCALES list entries. (e.g. .UTF-8 vs .utf8)
# The locale to set for the system. Be careful, this setting differs from the LOCALES
# list entries (e.g. .UTF-8 vs .utf8). Use the name as shown in `eselect locale`
LOCALE="C.utf8"
# For a german system you could use:
# LOCALES="

View File

@ -1,5 +1,3 @@
#!/bin/bash
source "$GENTOO_INSTALL_REPO_DIR/scripts/protection.sh" || exit 1

View File

@ -1,5 +1,3 @@
#!/bin/bash
source "$GENTOO_INSTALL_REPO_DIR/scripts/protection.sh" || exit 1
@ -18,3 +16,144 @@ UUID_STORAGE_DIR="$TMP_DIR/uuids"
# The desired efi partition mountpoint for the actual system
EFI_MOUNTPOINT="/boot/efi"
# An array of disk related actions to perform
DISK_ACTIONS=()
# An associative set to check for existing ids
declare -A DISK_KNOWN_IDS
only_one_of() {
local previous=""
local a
for a in "$@"; do
if [[ -v "arguments[$a]" ]]; then
if [[ -z "$previous" ]]; then
previous="$a"
else
die_trace 2 "Only one of the arguments ($*) can be given"
fi
fi
done
}
create_new_id() {
local id="${arguments[$1]}"
[[ ! -v "DISK_KNOWN_IDS[$id]" ]] \
|| die_trace 2 "Identifier '$id' already exists"
DISK_KNOWN_IDS[$id]=true
}
verify_existing_id() {
local id="${arguments[$1]}"
[[ -v "DISK_KNOWN_IDS[$id]" ]] \
|| die_trace 2 "Identifier $1='$id' not found"
}
verify_existing_unique_ids() {
local ids="${arguments[$1]}"
count_orig="$(tr ' ' '\n' <<< "$ids" | wc -l)"
count_uniq="$(tr ' ' '\n' <<< "$ids" | sort -u | wc -l)"
[[ "$count_orig" -eq "$count_uniq" ]] \
|| die_trace 2 "$1=... contains duplicate identifiers"
local i
# Splitting is intentional here
for i in $ids; do # shellcheck disable=SC2068
[[ -v "DISK_KNOWN_IDS[$i]" ]] \
|| die_trace 2 "$1=... contains unknown identifier '$i'"
done
}
verify_option() {
local opt="$1"
shift
local arg="${arguments[$opt]}"
local i
for i in "$@"; do
[[ "$i" == "$arg" ]] \
&& return 0
done
die_trace 2 "Invalid option $opt='$arg', must be one of ($*)"
}
#create_luks() {
# gpg --decrypt /tmp/efiboot/luks-key.gpg | \
# cryptsetup --cipher serpent-xts-plain64 --key-size 512 --hash whirlpool --key-file - luksFormat /dev/sdZn
# local dev
# cryptsetup luksFormat \
# --type=luks2 \
# --cipher aes-xts-plain64 \
# --key-size 512 \
# --pbkdf argon2id \
# --iter-time=4000 "$dev"
#}
# Named arguments:
# new_id: Id for the new partition
# size: Size for the new partition, or auto to allocate the rest
# type: The parition type, either (efi, swap, raid, luks, linux) (or a 4 digit hex-code for gdisk).
# [one of]
# device: The operand block device
# id: The operand device id
create_partition() {
local known_arguments=('+new_id' '+device|id' '+size' '+type')
unset arguments; declare -A arguments; parse_arguments "$@"
only_one_of device id
create_new_id new_id
verify_option type efi swap raid luks linux
[[ -v "arguments[id]" ]] \
&& verify_existing_id id
DISK_ACTIONS+=("action=create_partition" "$@")
}
# Named arguments:
# new_id: Id for the new raid
# level: Raid level
# ids: Ids of all member devices
create_raid() {
local known_arguments=('+new_id' '+level' '+ids')
unset arguments; declare -A arguments; parse_arguments "$@"
create_new_id new_id
verify_option level 0 1 5 6
verify_existing_unique_ids ids
DISK_ACTIONS+=("action=create_raid" "$@")
}
# Named arguments:
# new_id: Id for the new luks
# [one of]
# device: The operand block device
# id: The operand device id
create_luks() {
local known_arguments=('+new_id' '+device|id')
unset arguments; declare -A arguments; parse_arguments "$@"
create_new_id new_id
only_one_of device id
[[ -v "arguments[id]" ]] \
&& verify_existing_id id
DISK_ACTIONS+=("action=create_luks" "$@")
}
# Named arguments:
# id: Id of the device / partition created earlier
# type: One of (efi, swap, ext4)
# label: The label for the formatted disk
format() {
local known_arguments=('+id' '+type')
unset arguments; declare -A arguments; parse_arguments "$@"
verify_existing_id id
verify_option type efi swap ext4
DISK_ACTIONS+=("action=format" "$@")
}

View File

@ -1,5 +1,3 @@
#!/bin/bash
if [[ "$GENTOO_INSTALL_REPO_SCRIPT_ACTIVE" != true ]]; then
echo " * ERROR: This script must not be executed directly!" >&2
exit 1

View File

@ -1,5 +1,3 @@
#!/bin/bash
source "$GENTOO_INSTALL_REPO_DIR/scripts/protection.sh" || exit 1
elog() {
@ -24,6 +22,15 @@ die() {
exit 1
}
# Prints an error with file:line info of the nth "stack frame".
# 0 is this function, 1 the calling function, 2 its parent, and so on.
die_trace() {
local idx="${1:-0}"
shift
echo "${BASH_SOURCE[$((idx + 1))]}:${BASH_LINENO[$idx]}: error: ${FUNCNAME[$idx]}: $*" >&2
exit 1
}
for_line_in() {
while IFS="" read -r line || [[ -n "$line" ]]; do
"$2" "$line"
@ -135,3 +142,54 @@ load_or_generate_uuid() {
echo -n "$uuid"
}
# Parses named arguments and stores them in the associative array `arguments`.
# The associative array `known_arguments` must contain a list of arguments
# prefixed with + (mandatory) or ? (optional).
# "at least one of" can be expressed by +a|b|c.
# all mandatory arguments are given.
parse_arguments() {
local key
local value
local a
for a in "$@"; do
key="${a%%=*}"
value="${a#*=}"
arguments["$key"]="$value"
done
declare -A allowed_keys
if [[ -v "known_arguments" ]]; then
local m
for m in "${known_arguments[@]}"; do
case "${m:0:1}" in
'+')
m="${m:1}"
local has_opt=false
local m_opt
# Splitting is intentional here
for m_opt in ${m//|/ }; do # shellcheck disable=SC2068
allowed_keys["$m_opt"]=true
if [[ -v "arguments[$m_opt]" ]]; then
has_opt=true
fi
done
[[ "$has_opt" == true ]] \
|| die_trace 2 "Missing mandatory argument $m=..."
;;
'?')
allowed_keys["${m:1}"]=true
;;
*) die_trace 2 "Invalid start character in known_arguments, in argument '$m'" ;;
esac
done
fi
for a in "${!arguments[@]}"; do
[[ -v "allowed_keys[$a]" ]] \
|| die_trace 2 "Unkown argument '$a'"
done
}