This commit is contained in:
2026-07-01 21:19:17 +01:00
parent e3a2de3bde
commit 1ad416c69e
5 changed files with 59 additions and 39 deletions
+34 -24
View File
@@ -6,35 +6,45 @@ bootDisk = "/dev/disk/by-partlabel/disk-${hostname}-ESP";
in
{
boot.initrd.postDeviceCommands = lib.mkAfter ''
mkdir /btrfs_tmp
mount ${rootDisk} /btrfs_tmp
boot.initrd.supportedFilesystems = [ "btrfs" ];
# Ensure the old_roots directory exists
mkdir -p /btrfs_tmp/old_roots
boot.initrd.systemd.services.rollback-root = {
description = "Rollback btrfs root subvolume";
wantedBy = [ "initrd.target" ];
after = [ "systemd-cryptsetup@.service" ];
before = [ "sysroot.mount" ];
unitConfig.DefaultDependencies = "no";
serviceConfig.Type = "oneshot";
script = ''
mkdir -p /btrfs_tmp
mount ${rootDisk} /btrfs_tmp
if [[ -e /btrfs_tmp/root ]]; then
timestamp=$(date +%Y-%m-%d_%H-%M-%S)
mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp"
fi
# Ensure the old_roots directory exists
mkdir -p /btrfs_tmp/old_roots
delete_subvolume_recursively() {
IFS=$'\n'
for i in $(btrfs subvolume list -o "$1" | cut -f 9 -d ' '); do
delete_subvolume_recursively "/btrfs_tmp/$i"
if [[ -e /btrfs_tmp/root ]]; then
timestamp=$(date +%Y-%m-%d_%H-%M-%S)
mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp"
fi
delete_subvolume_recursively() {
IFS=$'\n'
for i in $(btrfs subvolume list -o "$1" | cut -f 9 -d ' '); do
delete_subvolume_recursively "/btrfs_tmp/$i"
done
btrfs subvolume delete "$1"
}
for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -mtime +7); do
# Don't delete the old_roots directory itself
if [[ "$i" == "/btrfs_tmp/old_roots/" ]]; then continue; fi
delete_subvolume_recursively "$i"
done
btrfs subvolume delete "$1"
}
for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -mtime +7); do
# Don't delete the old_roots directory itself
if [[ "$i" == "/btrfs_tmp/old_roots/" ]]; then continue; fi
delete_subvolume_recursively "$i"
done
btrfs subvolume create /btrfs_tmp/root
umount /btrfs_tmp
'';
btrfs subvolume create /btrfs_tmp/root
umount /btrfs_tmp
'';
};
# fileSystems."/" = {
# device = rootDisk;