void-bootstrapp/src/filesystems.sh
2025-12-24 14:36:53 +01:00

81 lines
3.2 KiB
Bash

#!/usr/bin/env bash
# === Motivation ===
# Provide a stable filesystem base aligned with user intent.
# === Problem Statement ===
# The wrapper must format the root and boot partitions using the selected filesystem.
# === Scope ===
# In scope: filesystem options, defaults, and naming.
# Out of scope: exact formatting command flags.
# === Concepts ===
# Root filesystem: primary data store inside the encrypted container.
# Boot filesystem: unencrypted partition for firmware and bootloader data.
# === Decisions ===
# Default to btrfs with opt-in alternatives for users who want ext4.
# Ensure filesystem choices are explicit and confirmed before formatting.
# Use swap file instead of swap partition to keep layout simple and flexible.
# Swap file lives inside encrypted root for automatic encryption protection.
# Swap file size is chosen interactively based on system needs.
# For btrfs, swap file must reside on a No-COW directory to avoid corruption.
# For btrfs, create subvolumes for root, home, var, log, snapshots, and swap.
# === Alternatives Considered ===
# Automatic detection of best filesystem rejected to keep behavior predictable.
# Swap partition rejected because it requires either:
# - Separate LUKS container (second passphrase at boot)
# - Unencrypted swap (security risk: sensitive data in swap)
# - LVM setup (out of scope for this wrapper)
# Swap partition also has fixed size, while swap file can be resized post-install.
# === Constraints ===
# Filesystem choice must be compatible with the initramfs and bootloader.
# Hibernation with swap file requires kernel >= 5.0 and resume_offset parameter.
# For btrfs swap files, COW must be disabled on the swap directory.
# === Open Questions ===
# How should we handle swap file creation - in this phase or defer to post-install?
# Should we support configurable filesystem options (compression, mount flags)?
# === Success Criteria ===
# Filesystems are prepared in the right places and match the chosen layout.
# Swap file is ready for activation and resides within encrypted root.
format_filesystems() {
: "${ESP_PART:?ESP partition is required}"
: "${CRYPT_NAME:?Crypt mapping name is required}"
: "${FS_TYPE:?Filesystem type is required}"
: "${ROOT_LABEL:?Root filesystem label is required}"
: "${EFI_LABEL:?EFI filesystem label is required}"
local root_device
root_device="/dev/mapper/${CRYPT_NAME}"
log_info "Formatting ESP on $ESP_PART"
mkfs.vfat -F32 -n "$EFI_LABEL" "$ESP_PART"
if [[ "$FS_TYPE" == "btrfs" ]]; then
log_info "Formatting root as btrfs on $root_device"
mkfs.btrfs -L "$ROOT_LABEL" "$root_device"
log_info "Creating btrfs subvolumes"
local temp_mount
temp_mount="/tmp/void-wrapper-btrfs"
mkdir -p "$temp_mount"
mount "$root_device" "$temp_mount"
btrfs subvolume create "$temp_mount/@"
btrfs subvolume create "$temp_mount/@home"
btrfs subvolume create "$temp_mount/@var"
btrfs subvolume create "$temp_mount/@log"
btrfs subvolume create "$temp_mount/@snapshots"
btrfs subvolume create "$temp_mount/@swap"
umount "$temp_mount"
rmdir "$temp_mount"
else
log_info "Formatting root as ext4 on $root_device"
mkfs.ext4 -L "$ROOT_LABEL" "$root_device"
fi
}