Added disk resolvable saving and loading

This commit is contained in:
oddlama 2020-04-23 17:32:36 +02:00
parent b80d27e467
commit 2274c3f7c2
No known key found for this signature in database
GPG Key ID: 88EA325D51D53908
3 changed files with 66 additions and 33 deletions

View File

@ -59,6 +59,7 @@ check_config() {
preprocess_config() {
check_config
load_resolvable_entries
}
prepare_installation_environment() {
@ -115,24 +116,6 @@ summary_color_args() {
done
}
resolve_device_by_id() {
local id="$1"
[[ -v DISK_ID_TO_RESOLVABLE[$id] ]] \
|| die "Cannot resolve id='$id' to a block device (no table entry)"
local type="${DISK_ID_TO_RESOLVABLE[$id]%%:*}"
local arg="${DISK_ID_TO_RESOLVABLE[$id]#*:}"
case "$type" in
'partuuid') get_device_by_partuuid "$arg" ;;
'ptuuid') get_device_by_ptuuid "$arg" ;;
'uuid') get_device_by_uuid "$arg" ;;
'mdadm') get_device_by_mdadm_uuid "$arg" ;;
'luks') get_device_by_luks_uuid "$arg" ;;
*) die "Cannot resolve '$type:$arg' to device (unkown type)"
esac
}
disk_create_gpt() {
local new_id="${arguments[new_id]}"
if [[ $disk_action_summarize_only == true ]]; then
@ -155,8 +138,7 @@ disk_create_gpt() {
fi
local ptuuid="${DISK_ID_TO_UUID[$new_id]}"
DISK_PTUUID_TO_DEVICE[${ptuuid,,}]="$device"
DISK_ID_TO_RESOLVABLE[$new_id]="ptuuid:$ptuuid"
create_resolve_entry "$new_id" ptuuid "$ptuuid" "$device"
einfo "Creating new gpt partition table ($new_id) on $device_desc"
sgdisk -Z -U "$ptuuid" "$device" >/dev/null \
@ -193,7 +175,7 @@ disk_create_partition() {
*) ;;
esac
DISK_ID_TO_RESOLVABLE[$new_id]="partuuid:$partuuid"
create_resolve_entry "$new_id" partuuid "$partuuid"
einfo "Creating partition ($new_id) with type=$type, size=$size on $device"
# shellcheck disable=SC2086
@ -233,8 +215,7 @@ disk_create_raid() {
local mddevice="/dev/md/$name"
local uuid="${DISK_ID_TO_UUID[$new_id]}"
DISK_MDADM_UUID_TO_DEVICE[${uuid,,}]="$mddevice"
DISK_ID_TO_RESOLVABLE[$new_id]="mdadm:$uuid"
create_resolve_entry "$new_id" mdadm "$uuid" "$mddevice"
einfo "Creating raid$level ($new_id) on $devices_desc"
mdadm \
@ -259,7 +240,7 @@ disk_create_luks() {
local device="$(resolve_device_by_id "$id")"
local uuid="${DISK_ID_TO_UUID[$new_id]}"
DISK_ID_TO_RESOLVABLE[$new_id]="luks:$uuid"
create_resolve_entry "$new_id" luks "$uuid"
einfo "Creating luks ($new_id) on $device ($id)"
local keyfile

View File

@ -15,6 +15,8 @@ GENTOO_INSTALL_REPO_BIND="$TMP_DIR/bind"
UUID_STORAGE_DIR="$TMP_DIR/uuids"
# Backup dir for luks headers
LUKS_HEADER_BACKUP_DIR="$TMP_DIR/luks-headers"
# Backup dir for luks headers
RESOLVABLE_MAP_DIR="$TMP_DIR/resolve-uuids"
# Flag to track usage of raid (needed to check for mdadm existence)
USED_RAID=false
@ -24,17 +26,15 @@ USED_LUKS=false
# An array of disk related actions to perform
DISK_ACTIONS=()
# An associative array from disk id to a resolvable string
declare -Ax DISK_ID_TO_RESOLVABLE
declare -A DISK_ID_TO_RESOLVABLE
# An associative array from disk id to parent gpt disk id (only for partitions)
declare -Ax DISK_ID_PART_TO_GPT_ID
declare -A DISK_ID_PART_TO_GPT_ID
# An associative array to check for existing ids (maps to uuids)
declare -A DISK_ID_TO_UUID
# An associative set to check for correct usage of size=remaining in gpt tables
declare -A DISK_GPT_HAD_SIZE_REMAINING
# An associative from PTUUID to device
declare -Ax DISK_PTUUID_TO_DEVICE
# An associative from MDADM uuid to device
declare -Ax DISK_MDADM_UUID_TO_DEVICE
# An associative from uuid to device
declare -A DISK_UUID_TO_DEVICE
only_one_of() {
local previous=""

View File

@ -139,17 +139,69 @@ get_device_by_uuid() {
}
get_device_by_ptuuid() {
echo -n "${DISK_PTUUID_TO_DEVICE[${1,,}]}"
echo -n "${DISK_UUID_TO_DEVICE[${1,,}]}"
}
get_device_by_mdadm_uuid() {
echo -n "${DISK_MDADM_UUID_TO_DEVICE[${1,,}]}"
echo -n "${DISK_UUID_TO_DEVICE[${1,,}]}"
}
get_device_by_luks_uuid() {
echo -n "/dev/mapper/${1,,}"
}
create_resolve_entry() {
local id="$1"
local type="$2"
local arg="${3,,}"
local device="$4" # optional
case "$type" in
'partuuid') ;;
'ptuuid') DISK_UUID_TO_DEVICE[$arg]="$device" ;;
'uuid') ;;
'mdadm') DISK_UUID_TO_DEVICE[$arg]="$device" ;;
'luks') ;;
*) die "Cannot add resolvable entry for '$type:$arg' (unknown type)"
esac
DISK_ID_TO_RESOLVABLE[$id]="$type:$arg"
mkdir -p "$RESOLVABLE_MAP_DIR" \
|| die "Could not create resolveable map dir '$RESOLVABLE_MAP_DIR'"
echo -n "$type:$arg" > "$RESOLVABLE_MAP_DIR/$(base64 -w 0 <<< "$id")"
}
load_resolvable_entries() {
[[ -d $RESOLVABLE_MAP_DIR ]] \
|| return 0
local base_id
local id
local saved
for base_id in "$RESOLVABLE_MAP_DIR/"*; do
id="$(base64 -d <<< "$(basename "$base_id")")"
saved="$(cat "$base_id")"
DISK_ID_TO_RESOLVABLE[$id]="$saved"
done
}
resolve_device_by_id() {
local id="$1"
[[ -v DISK_ID_TO_RESOLVABLE[$id] ]] \
|| die "Cannot resolve id='$id' to a block device (no table entry)"
local type="${DISK_ID_TO_RESOLVABLE[$id]%%:*}"
local arg="${DISK_ID_TO_RESOLVABLE[$id]#*:}"
case "$type" in
'partuuid') get_device_by_partuuid "$arg" ;;
'ptuuid') get_device_by_ptuuid "$arg" ;;
'uuid') get_device_by_uuid "$arg" ;;
'mdadm') get_device_by_mdadm_uuid "$arg" ;;
'luks') get_device_by_luks_uuid "$arg" ;;
*) die "Cannot resolve '$type:$arg' to device (unknown type)"
esac
}
load_or_generate_uuid() {
local uuid
local uuid_file="$UUID_STORAGE_DIR/$1"
@ -216,7 +268,7 @@ parse_arguments() {
for a in "${!arguments[@]}"; do
[[ -v allowed_keys[$a] ]] \
|| die_trace 2 "Unkown argument '$a'"
|| die_trace 2 "Unknown argument '$a'"
done
fi
}