fix(filesystems): add cleanup trap for temp btrfs mount

Add trap to ensure temp mount is cleaned up if btrfs subvolume
creation fails. Without this, failures leave mounted filesystems
and orphaned directories, blocking retry attempts.

Use mountpoint -q for robust mount detection before cleanup.

Decision: Use local trap within format_filesystems to avoid
interfering with main error handler. Reset trap after successful
completion to prevent double cleanup.

Trade-off: Slightly more complex code vs guaranteed cleanup on
error paths.
This commit is contained in:
Stefan Strobl 2025-12-24 15:24:29 +01:00
parent c9fbc5486c
commit c303a75192

View File

@ -64,6 +64,18 @@ format_filesystems() {
local temp_mount local temp_mount
temp_mount="/tmp/void-wrapper-btrfs" temp_mount="/tmp/void-wrapper-btrfs"
mkdir -p "$temp_mount" mkdir -p "$temp_mount"
# Ensure cleanup on error
cleanup_temp_mount() {
if mountpoint -q "$temp_mount" 2>/dev/null; then
umount "$temp_mount" || log_warn "Failed to unmount temp mount: $temp_mount"
fi
if [[ -d "$temp_mount" ]]; then
rmdir "$temp_mount" 2>/dev/null || true
fi
}
trap cleanup_temp_mount EXIT
mount "$root_device" "$temp_mount" mount "$root_device" "$temp_mount"
btrfs subvolume create "$temp_mount/@" btrfs subvolume create "$temp_mount/@"
btrfs subvolume create "$temp_mount/@home" btrfs subvolume create "$temp_mount/@home"
@ -73,6 +85,7 @@ format_filesystems() {
btrfs subvolume create "$temp_mount/@swap" btrfs subvolume create "$temp_mount/@swap"
umount "$temp_mount" umount "$temp_mount"
rmdir "$temp_mount" rmdir "$temp_mount"
trap - EXIT
else else
log_info "Formatting root as ext4 on $root_device" log_info "Formatting root as ext4 on $root_device"
mkfs.ext4 -L "$ROOT_LABEL" "$root_device" mkfs.ext4 -L "$ROOT_LABEL" "$root_device"