I use btrfs with impermanence.
It uses a volume pool located in /dev/pool/root.
I am unable to access this device in a oneshot service I use to revert the root to clean state for impermanence. What can I do?
boot.initrd.systemd.services.rollback = {
description = "Rollback BTRFS root subvolume to a pristine state";
wantedBy = [
"initrd.target"
];
before = [
"sysroot.mount"
];
unitConfig.DefaultDependencies = "no";
serviceConfig.Type = "oneshot";
script = ''
mkdir /btrfs_tmp
mount /dev/pool/root /btrfs_tmp
if [[ -e /btrfs_tmp/root ]]; then
mkdir -p /btrfs_tmp/old_roots
timestamp=$(date --date="@$(stat -c %Y /btrfs_tmp/root)" "+%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 +${builtins.toString cfg.removeTmpFilesOlderThan}); do
delete_subvolume_recursively "$i"
done
btrfs subvolume create /btrfs_tmp/root
umount /btrfs_tmp
'';
};
No, I wouldn’t expect that to be an improvement. In fact ordering after = ["sysroot.mount"]; should certainly break it. We’ll have to see some logs to know what happened.
It fails to mount /sysroot/var/lib, according to the logs.
Mounting /sysroot/nix...
Mounting /sysroot/persist...
Mounting /sysroot/var/lib...
Starting Mountpoints configured in the Real Root...
Mounted /sysroot/nix...
Failed to mount /sysroot/var/lib.
When it fails in initrd, it should give you access to the emergency shell if you have boot.initrd.systemd.emergencyAccess set. If not, you can add rd.systemd.debug_shell to the kernel params if you’re using a very recent nixos unstable, and then press ctrl-alt-f9 to go to the debug shell. From there you can use commands like systemctl status and journalctl to get a more detailed log about the mount units that failed
Alright, after some troubleshooting it seems that i was using the guest module for microvms, which messes with the systemd initrd. Now that i am no longer using it, your original solution works, thank you!