fix(config): add size validation and improve swap normalization

Add validate_size() function to check format of user-provided
size inputs (ESP_SIZE, ROOT_END, SWAP_SIZE) before they reach
partitioning tools. Prevents cryptic parted errors from invalid
formats like "abc" or "1X".

Improve SWAP_SIZE normalization to handle all zero variants
(0, 0G, 0GB, 0GiB, 0M, 0MB, 0MiB) consistently.

Decision: Validate at config phase rather than partition phase
to provide early, clear feedback. Use regex pattern that matches
parted's expected format (number + optional unit or percentage).

Alternative considered: Let parted handle validation, rejected
because error messages would be less user-friendly.
This commit is contained in:
Stefan Strobl 2025-12-24 15:22:50 +01:00
parent 4e0d9573e6
commit c9fbc5486c

View File

@ -53,6 +53,15 @@ export ESP_MOUNT="/mnt/boot/efi"
export ROOT_LABEL="void-root"
export EFI_LABEL="EFI"
validate_size() {
local size="$1"
# Allow sizes like: 1GiB, 100MB, 50%, 100%
if [[ "$size" =~ ^[0-9]+(\.[0-9]+)?(GiB|GB|MiB|MB|%)?$ ]]; then
return 0
fi
return 1
}
prompt_with_default() {
local var_name="$1"
local prompt_text="$2"
@ -145,9 +154,21 @@ config_validate() {
*) die "Unsupported LUKS version: $LUKS_VERSION" ;;
esac
if [[ "$SWAP_SIZE" == "0" || "$SWAP_SIZE" == "0G" || "$SWAP_SIZE" == "0GiB" ]]; then
# Normalize swap size first
if [[ "$SWAP_SIZE" =~ ^0(G|GB|GiB|M|MB|MiB)?$ ]]; then
SWAP_SIZE="0"
fi
# Validate size formats
if ! validate_size "$ESP_SIZE"; then
die "Invalid ESP size format: $ESP_SIZE (expected: 1GiB, 100MB, etc.)"
fi
if ! validate_size "$ROOT_END"; then
die "Invalid root partition end format: $ROOT_END (expected: 100%, 200GiB, etc.)"
fi
if [[ "$SWAP_SIZE" != "0" ]] && ! validate_size "$SWAP_SIZE"; then
die "Invalid swap size format: $SWAP_SIZE (expected: 4GiB, 8GB, 0 to disable)"
fi
}
config_print_summary() {