summaryrefslogtreecommitdiff
path: root/raveos-calamares-theme/etc/calamares/modules/calamares-create-snapshots-subvol.sh
blob: 092b7b923aeb325bc5f654ab8f1d88ca4e1d82dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
set -e

echo ">>> [snapshots-subvol] Running INSIDE target system"

# Calamares' partition module ignores our btrfsSubvolumes config here and
# always creates its own hardcoded set (/home /root /srv /var/cache
# /var/log /var/tmp as separate subvolumes) -- there's no ".snapshots"
# subvolume among them, which grub-btrfs/Snapper need. Create it by hand
# as a proper top-level (subvolid=5) sibling of the other subvolumes,
# so snapshots of "/" don't recursively include the snapshot store itself.

ROOT_DEV="$(findmnt -no SOURCE / | sed 's/\[.*\]//')"
ROOT_FS="$(findmnt -no FSTYPE /)"

if [ "$ROOT_FS" != "btrfs" ]; then
  echo ">>> [snapshots-subvol] Root is not btrfs (${ROOT_FS}), skipping."
  exit 0
fi

if [ -d /.snapshots ] && findmnt -no SOURCE /.snapshots >/dev/null 2>&1; then
  echo ">>> [snapshots-subvol] /.snapshots already mounted, skipping."
  exit 0
fi

mkdir -p /mnt/topvol
mount -o subvolid=5 "${ROOT_DEV}" /mnt/topvol

if [ ! -d /mnt/topvol/@snapshots ]; then
  btrfs subvolume create /mnt/topvol/@snapshots
  echo ">>> [snapshots-subvol] Created @snapshots subvolume"
else
  echo ">>> [snapshots-subvol] @snapshots subvolume already exists"
fi

umount /mnt/topvol
rmdir /mnt/topvol

mkdir -p /.snapshots

ROOT_UUID="$(blkid -s UUID -o value "${ROOT_DEV}")"
if ! grep -q '/.snapshots' /etc/fstab; then
  echo "UUID=${ROOT_UUID} /.snapshots btrfs rw,noatime,compress=zstd:3,discard=async,space_cache=v2,subvol=/@snapshots 0 0" >> /etc/fstab
  echo ">>> [snapshots-subvol] Added /.snapshots to fstab"
fi

mount /.snapshots

echo ">>> [snapshots-subvol] Done."