nullbox: refactor volatile root

This commit is contained in:
NullBite 2024-07-04 03:23:52 -04:00
parent 933916abdf
commit 485d6464f4
Signed by: nullbite
GPG Key ID: 6C4D545385D4925A

View File

@ -1,39 +1,30 @@
{ pkgs, config, lib, ... }: { pkgs, config, lib, ... }:
let let
mkBtrfsInit = { prefix ? "", inherit (lib) escapeShellArg;
volume }: # (wip) more configurable than old one, will be used by volatile btrfs module
mkBtrfsInit = { volatileRoot ? "/volatile",
oldRoots ? "/old_roots",
volume }:
'' ''
mkdir /btrfs_tmp mkdir -p /btrfs_tmp
mount ${volume} /btrfs_tmp -o subvol=/ mount ${escapeShellArg volume} /btrfs_tmp -o subvol=/
# unix is fine with multiple consecutive slashes if prefix is empty or # ensure subvol parent directory exists
# contains a leading or trailing slash mkdir -p $(dirname /btrfs_tmp/${escapeShellArg volatileRoot})
mkdir -p "/btrfs_tmp/${prefix}/"
if [[ -e "/btrfs_tmp/${prefix}/volatile" ]] ; then if [[ -e /btrfs_tmp/${escapeShellArg volatileRoot} ]] ; then
mkdir -p "/btrfs_tmp/${prefix}/old_roots" mkdir -p /btrfs_tmp/${escapeShellArg oldRoots}
timestamp=$(date --date="@$(stat -c %Y "/btrfs_tmp/${prefix}/volatile")" "+%Y-%m-%-d_%H:%M:%S") timestamp=$(date --date="@$(stat -c %Y /btrfs_tmp/${escapeShellArg volatileRoot})" "+%Y-%m-%-d_%H:%M:%S")
mv "/btrfs_tmp/${prefix}/volatile" "/btrfs_tmp/${prefix}/old_roots/$timestamp" mv /btrfs_tmp/${escapeShellArg volatileRoot} /btrfs_tmp/${escapeShellArg oldRoots}/"$timestamp"
fi fi
delete_subvolume_recursively() { btrfs subvolume create /btrfs_tmp/${escapeShellArg volatileRoot}
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"
echo would run: btrfs subvolume delete "$1"
echo remove this echo once you see this message
}
for i in $(find /btrfs_tmp/${prefix}/old_roots/ -maxdepth 1 -mtime +30); do
delete_subvolume_recursively "$i"
done
btrfs subvolume create /btrfs_tmp/${prefix}/volatile
umount /btrfs_tmp umount /btrfs_tmp
# TODO implement deletion once system is booted. the old implementation did
# it here, which is not safe until system time is at least monotonic.
# systemd tmpfiles is good enough, just mount it to somewhere in /run
''; '';
root_vol = "/dev/archdesktop/root"; root_vol = "/dev/archdesktop/root";
@ -46,7 +37,13 @@ in {
options = [ "subvol=/nixos/@persist" ]; options = [ "subvol=/nixos/@persist" ];
}; };
boot.initrd.postDeviceCommands = lib.mkAfter (mkBtrfsInit { prefix = "nixos"; volume = root_vol; }); # TODO volatile btrfs module
boot.initrd.postDeviceCommands = lib.mkAfter (mkBtrfsInit {
volume = root_vol;
volatileRoot = "/nixos/volatile";
oldRoots = "/nixos/old_roots";
});
fileSystems."/" = lib.mkForce { fileSystems."/" = lib.mkForce {
device = root_vol; device = root_vol;
fsType = "btrfs"; fsType = "btrfs";