From e49a95254d439e5e83c05756a3bc92e4575360b0 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 19 May 2026 12:24:43 -0500 Subject: refactor: lift FILES= keyfile sed to ensure_initramfs_files helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit btrfs.sh's configure_btrfs_initramfs had a six-line inline that ensured mkinitcpio.conf's FILES= line listed the LUKS keyfile: sed-replace the existing FILES= line, then grep + append as a fallback when no FILES= line existed. The pattern is mkinitcpio-specific and self-healing rather than error-on-miss (FILES= is optional in mkinitcpio.conf, so missing means "no extra files," not a broken config). I lifted the block into ensure_initramfs_files in lib/common.sh next to prepend_grub_cmdline_linux, then collapsed the btrfs.sh call site to a single ensure_initramfs_files line. Added three bats tests for the three cases (FILES= present and empty, FILES= present with a different value, FILES= absent entirely). Bats: 174 → 177. No behavior change. The helper's logic matches the inline byte-for-byte: same sed pattern, same grep fallback, same final state. --- installer/lib/btrfs.sh | 6 +----- installer/lib/common.sh | 17 ++++++++++++++++ tests/unit/test_common.bats | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/installer/lib/btrfs.sh b/installer/lib/btrfs.sh index 0715ba7..0a34be0 100644 --- a/installer/lib/btrfs.sh +++ b/installer/lib/btrfs.sh @@ -212,11 +212,7 @@ configure_luks_initramfs() { # Include keyfile in initramfs for testing mode (unattended boot) if [[ "${TESTING:-}" == "yes" ]]; then info "Testing mode: embedding keyfile in initramfs" - sed -i "s|^FILES=.*|FILES=($LUKS_KEYFILE)|" $MNTPOINT/etc/mkinitcpio.conf - # If FILES line doesn't exist, add it - if ! grep -q "^FILES=" $MNTPOINT/etc/mkinitcpio.conf; then - echo "FILES=($LUKS_KEYFILE)" >> $MNTPOINT/etc/mkinitcpio.conf - fi + ensure_initramfs_files "$LUKS_KEYFILE" "$MNTPOINT/etc/mkinitcpio.conf" fi # Create crypttab.initramfs for sd-encrypt (used by multi-disk LUKS) diff --git a/installer/lib/common.sh b/installer/lib/common.sh index e5f7246..2cd4798 100644 --- a/installer/lib/common.sh +++ b/installer/lib/common.sh @@ -328,3 +328,20 @@ prepend_grub_cmdline_linux() { grep -qF "GRUB_CMDLINE_LINUX=\"${addition}" "$config_file" \ || error "GRUB_CMDLINE_LINUX not modified in $config_file (line missing or pattern unmatched)" } + +############################# +# Initramfs Configuration +############################# + +# Ensure mkinitcpio.conf's FILES= line lists the given value. Replaces +# an existing FILES= line, or appends one if absent. Self-healing +# rather than error-on-miss: FILES= is optional in mkinitcpio.conf, so +# a missing line means "no extra files," not a broken config. +ensure_initramfs_files() { + local value="$1" + local config_file="$2" + sed -i "s|^FILES=.*|FILES=(${value})|" "$config_file" + if ! grep -q "^FILES=" "$config_file"; then + echo "FILES=(${value})" >> "$config_file" + fi +} diff --git a/tests/unit/test_common.bats b/tests/unit/test_common.bats index abe3938..8ce7280 100644 --- a/tests/unit/test_common.bats +++ b/tests/unit/test_common.bats @@ -473,3 +473,50 @@ Boot0001* ZFSBootMenu" ! grep -q 'cryptdevice' "$f" rm -f "$f" } + +############################# +# ensure_initramfs_files +############################# +# ensure_initramfs_files sets mkinitcpio.conf's FILES= line to the +# given value, replacing an existing line or appending one if absent. +# Self-healing rather than error-on-miss: FILES= is optional, so a +# missing line means "no extra files," not a broken config. + +@test "ensure_initramfs_files replaces an empty FILES= line" { + local f + f=$(mktemp) + printf '%s\n' 'FILES=()' > "$f" + + ensure_initramfs_files "/etc/luks.key" "$f" + + grep -qF 'FILES=(/etc/luks.key)' "$f" + rm -f "$f" +} + +@test "ensure_initramfs_files replaces a FILES= line that lists a different value" { + local f + f=$(mktemp) + printf '%s\n' 'FILES=(/etc/old-key)' > "$f" + + ensure_initramfs_files "/etc/luks.key" "$f" + + grep -qF 'FILES=(/etc/luks.key)' "$f" + ! grep -qF '/etc/old-key' "$f" + rm -f "$f" +} + +@test "ensure_initramfs_files appends FILES= when the line is absent" { + local f + f=$(mktemp) + printf '%s\n' \ + 'MODULES=()' \ + 'BINARIES=()' \ + 'HOOKS=(base udev)' > "$f" + + ensure_initramfs_files "/etc/luks.key" "$f" + + grep -qF 'FILES=(/etc/luks.key)' "$f" + grep -qF 'MODULES=()' "$f" + grep -qF 'HOOKS=(base udev)' "$f" + rm -f "$f" +} -- cgit v1.2.3