phoebus_os/installer/lib/btrfs.sh
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

109 lines
3.3 KiB
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
#
# Btrfs layout backend.
#
# Default layout (see config/defaults.conf):
# @ / root
# @home /home
# @var /var
# @snapshots /.snapshots
# @swap /swap holds the swapfile
#
# Top-level (subvolid=5) is only mounted temporarily to create subvolumes.
btrfs_require_tools() {
require_cmd btrfs mkfs.btrfs || return 1
}
btrfs_mount_opts() {
local opts
opts="noatime,compress=$(config_get COMPRESS),space_cache=v2"
if [[ "$(disk_transport "$(config_get TARGET_DISK)")" == "nvme" || \
"$(disk_transport "$(config_get TARGET_DISK)")" == "sata" ]]; then
opts+=",ssd"
fi
printf '%s' "$opts"
}
# btrfs_mkfs DEVICE
btrfs_mkfs() {
local dev="$1" label
label="$(config_get BTRFS_LABEL)"
run_destructive "create Btrfs filesystem on $dev" \
mkfs.btrfs -f -L "$label" "$dev"
}
# btrfs_mount_top DEVICE MOUNTPOINT
btrfs_mount_top() {
local dev="$1" mp="$2"
mkdir -p "$mp"
run_cmd "mount Btrfs top-level" mount -o "subvolid=5" "$dev" "$mp"
}
# btrfs_create_subvolumes MOUNTPOINT
btrfs_create_subvolumes() {
local mp="$1" sv
for sv in $(config_get BTRFS_SUBVOLS); do
run_destructive "create subvolume $sv" btrfs subvolume create "$mp/$sv" || return 1
done
}
# btrfs_set_default MOUNTPOINT - make @ the default subvolume
btrfs_set_default() {
local mp="$1" id
id="$(btrfs subvolume list -o "$mp" 2>/dev/null | awk '$NF=="@"{print $2; exit}')"
[[ -n "$id" ]] || return 0
run_cmd "set @ as default subvolume" btrfs subvolume set-default "$id" "$mp"
}
# btrfs_mount_layout DEVICE ROOT
btrfs_mount_layout() {
local dev="$1" root="$2" opts
opts="$(btrfs_mount_opts)"
mkdir -p "$root"
run_cmd "mount @ at $root" mount -o "${opts},subvol=@" "$dev" "$root" || return 1
local pair sv target
for pair in "@home:home" "@var:var" "@snapshots:.snapshots" "@swap:swap"; do
sv="${pair%%:*}"; target="${pair##*:}"
mkdir -p "$root/$target"
run_cmd "mount $sv at /$target" mount -o "${opts},subvol=$sv" "$dev" "$root/$target" || return 1
done
}
# btrfs_create_layout DEVICE ROOT
# Create the subvolume layout on a freshly created filesystem, then mount it.
# The top-level (subvolid=5) must be mounted to create @, @home, ...; only
# afterwards can the individual subvolumes be mounted.
btrfs_create_layout() {
local dev="$1" root="$2"
btrfs_mount_top "$dev" "$root" || return 1
if ! btrfs_create_subvolumes "$root"; then
run_cmd "unmount top-level" umount "$root" || true
return 1
fi
btrfs_set_default "$root" || true
run_cmd "unmount top-level" umount "$root" || return 1
btrfs_mount_layout "$dev" "$root"
}
# btrfs_create_swapfile ROOT [MIB]
btrfs_create_swapfile() {
local root="$1" mib="${2:-$(plan_swap_mib)}"
local swapdir="$root/swap"
mkdir -p "$swapdir"
if btrfs filesystem mkswapfile --help >/dev/null 2>&1; then
run_cmd "create Btrfs swapfile" \
btrfs filesystem mkswapfile --size "${mib}m" "$swapdir/swapfile"
else
run_cmd "create swapfile container" \
truncate -s "${mib}M" "$swapdir/swapfile" || return 1
run_cmd "disable CoW on swapfile" chattr +C "$swapdir/swapfile"
run_cmd "make swap" mkswap "$swapdir/swapfile"
fi
}
# btrfs_scrub_device DEVICE - verify checksums (non-destructive)
btrfs_scrub_device() {
run_cmd "scrub $1" btrfs scrub start -B "$1"
}