Files
os-build/btrfs/init.sh
Blake Ridgway 36d0c5b9d1 feat: add btrfs subvolume, snapshot, and rollback tooling
- init.sh: creates the @ / @home / @log / @snapshots subvolume layout
  on a target device (the installer step).
- snapshot.sh: scheduled read-only snapshots with pruning, installed as
  /usr/local/sbin/arcline-snapshot.
- rollback.sh: safe boot-to-snapshot rollback that refuses to touch the
  live @ and promotes a snapshot atomically.
2026-08-21 13:15:43 -05:00

67 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────────────────────
# Arcline OS — btrfs subvolume layout initialiser
#
# btrfs/init.sh <device> (e.g. /dev/sda2)
# btrfs/init.sh --list <device>
#
# Creates the Arcline subvolume layout on a fresh (or existing) btrfs device:
#
# @ → / system root (rolled back on failure)
# @home → /home user data
# @log → /var/log logs (not rolled back, survives)
# @snapshots → /.snapshots where arcline-snapshot keeps snapshots
#
# This is the step the installer runs; it is safe to re-run (existing
# subvolumes are kept, missing ones are created). Requires root.
# ─────────────────────────────────────────────────────────────────────────────
set -euo pipefail
DEV="${1:?usage: btrfs/init.sh <device>}"
TOP="/run/arcline-btrfs-init"
require_root() {
if [[ $EUID -ne 0 ]] && command -v sudo >/dev/null; then
exec sudo -E "$0" "$@"
elif [[ $EUID -ne 0 ]]; then
echo "error: needs root" >&2; exit 1
fi
}
require_root "$0" "$@"
command -v btrfs >/dev/null || { echo "error: btrfs-progs not installed" >&2; exit 1; }
if ! blkid -p -O "$DEV" | grep -q btrfs 2>/dev/null; then
echo "error: $DEV is not a btrfs filesystem" >&2
exit 1
fi
mkdir -p "$TOP"
mount -o subvolid=5 "$DEV" "$TOP"
trap 'umount "$TOP" 2>/dev/null || true; rmdir "$TOP" 2>/dev/null || true' EXIT
create_subvol() {
local name="$1"
if [[ -d "$TOP/$name" ]]; then
echo " keeping existing subvolume: $name"
else
echo " creating subvolume: $name"
btrfs subvolume create "$TOP/$name" >/dev/null
fi
}
echo "Arcline btrfs layout on $DEV:"
create_subvol "@"
create_subvol "@home"
create_subvol "@log"
create_subvol "@snapshots"
# mark @snapshots read-only friendly (snapper/arcline-snapshot manage it)
btrfs property set -ts "$TOP/@snapshots" ro false 2>/dev/null || true
echo "done. The installer mounts:"
echo " @ → /"
echo " @home → /home"
echo " @log → /var/log"
echo " @snapshots → /.snapshots"