From c9fbc5486c54df81e06d10ac3bc1bc54a627a437 Mon Sep 17 00:00:00 2001 From: Stefan Strobl Date: Wed, 24 Dec 2025 15:22:50 +0100 Subject: [PATCH] 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. --- src/config.sh | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/config.sh b/src/config.sh index 45bff24..22c5065 100644 --- a/src/config.sh +++ b/src/config.sh @@ -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() {