#!/usr/bin/env bash # shellcheck shell=bash # # Filesystem creation, mounting and fstab/crypttab generation. # # fstab is generated deterministically from the plan rather than scraping the # live system, so no host state leaks into the installed system. declare -g FS_ROOT_SOURCE="" declare -g FS_EFI_PART="" declare -g FS_ROOT_PART="" filesystem_require_tools() { require_cmd mkfs.fat mount umount blkid || return 1 } # filesystem_mkfs_efi PARTITION filesystem_mkfs_efi() { local part="$1" run_destructive "create FAT32 EFI filesystem on $part" \ mkfs.fat -F32 -n EFI "$part" } # filesystem_mount_efi PARTITION ROOT filesystem_mount_efi() { local part="$1" root="$2" mp mp="$(config_get EFI_MOUNT)" mkdir -p "$root$mp" run_cmd "mount EFI" mount "$part" "$root$mp" } # filesystem_prepare_root DISK # Create the root filesystem, applying encryption when configured. # Sets FS_ROOT_SOURCE to the device that carries the Btrfs filesystem. filesystem_prepare_root() { local disk="$1" part="$2" FS_ROOT_PART="$part" if [[ "$(config_get ENCRYPTION)" == "none" ]]; then btrfs_mkfs "$part" || return 1 FS_ROOT_SOURCE="$part" else encryption_format "$part" || return 1 encryption_open "$part" "$(config_get LUKS_NAME)" || return 1 local mapper mapper="$(encryption_mapper_path)" btrfs_mkfs "$mapper" || return 1 FS_ROOT_SOURCE="$mapper" fi log_info "root filesystem source: $FS_ROOT_SOURCE" } # filesystem_mount_root ROOT filesystem_mount_root() { local root="$1" btrfs_mount_layout "$FS_ROOT_SOURCE" "$root" } # filesystem_source_ref - stable reference for fstab (UUID when unencrypted) filesystem_source_ref() { if [[ "$FS_ROOT_SOURCE" == /dev/mapper/* ]]; then printf '%s' "$FS_ROOT_SOURCE" else local uuid uuid="$(blkid -s UUID -o value "$FS_ROOT_SOURCE" 2>/dev/null)" if [[ -n "$uuid" ]]; then printf 'UUID=%s' "$uuid"; else printf '%s' "$FS_ROOT_SOURCE"; fi fi } # filesystem_generate_fstab ROOT filesystem_generate_fstab() { local root="$1" local fstab="$root/etc/fstab" local opts src src="$(filesystem_source_ref)" opts="$(btrfs_mount_opts)" mkdir -p "$root/etc" { printf '# /etc/fstab: %s\n' "$(config_get DISTRO_NAME)" printf '# Generated by the installer. Use `findmnt --verify` to check.\n' printf '#\n' printf '# \t\t\t\t\t\n' local efi_uuid efi_uuid="$(blkid -s UUID -o value "$FS_EFI_PART" 2>/dev/null)" if [[ -n "$efi_uuid" ]]; then printf 'UUID=%s\t%s\tvfat\tumask=0077,shortname=winnt\t0\t2\n' "$efi_uuid" "$(config_get EFI_MOUNT)" fi printf '%s\t/\tbtrfs\t%s,subvol=@\t0\t1\n' "$src" "$opts" printf '%s\t/home\tbtrfs\t%s,subvol=@home\t0\t2\n' "$src" "$opts" printf '%s\t/var\tbtrfs\t%s,subvol=@var\t0\t2\n' "$src" "$opts" printf '%s\t/.snapshots\tbtrfs\t%s,subvol=@snapshots\t0\t2\n' "$src" "$opts" printf '%s\t/swap\tbtrfs\t%s,subvol=@swap\t0\t2\n' "$src" "$opts" printf '/swap/swapfile\tnone\tswap\tdefaults\t0\t0\n' } >"$fstab" log_info "wrote $fstab" } # filesystem_generate_crypttab ROOT filesystem_generate_crypttab() { local root="$1" local crypttab="$root/etc/crypttab" local line [[ "$(config_get ENCRYPTION)" == "none" ]] && return 0 line="$(encryption_crypttab_line "$FS_ROOT_PART" "$(config_get LUKS_NAME)")" || { log_error "could not determine LUKS UUID for crypttab" return 1 } mkdir -p "$root/etc" { printf '# /etc/crypttab: unlock the root volume at boot.\n' printf '# \t\t\t\n' printf '%s\n' "$line" } >"$crypttab" log_info "wrote $crypttab" } # filesystem_verify ROOT - basic post-mount checks filesystem_verify() { local root="$1" [[ -d "$root/etc" ]] || { log_error "$root/etc missing"; return 1; } findmnt -n "$root" >/dev/null 2>&1 || { log_error "$root is not a mountpoint"; return 1; } return 0 } # filesystem_unmount_all ROOT - best effort teardown (used on rollback) filesystem_unmount_all() { local root="$1" # Unmount children first, the root last. local -a mounts=("$(config_get EFI_MOUNT)" "/.snapshots" "/var" "/home" "/swap" "") local m for m in "${mounts[@]}"; do run_cmd "unmount $root$m" umount -R "$root$m" 2>/dev/null || true done }