Compare commits

..

No commits in common. "75b37cc9ec2014dd8dea941bb3faf9c0b665c6d0" and "a483dd37659bb730ff981b5947fc92b23f1b34e4" have entirely different histories.

View file

@ -34,6 +34,8 @@ lib.mkMerge [
fsType = "bcachefs"; fsType = "bcachefs";
options = [ options = [
"X-mount.subdir=@root" "X-mount.subdir=@root"
# "x-systemd.requires=unlock-bcachefs-custom.service"
# "x-systemd.after=unlock-bcachefs-custom.service"
]; ];
}; };
fileSystems."/nix" = { fileSystems."/nix" = {
@ -62,6 +64,8 @@ lib.mkMerge [
"X-mount.mkdir" "X-mount.mkdir"
"X-mount.subdir=@persist" "X-mount.subdir=@persist"
]; ];
# NOTE for impermanence
# neededForBoot = true;
}; };
} }
# SWAP (optional) # SWAP (optional)
@ -226,19 +230,23 @@ lib.mkMerge [
boot.initrd.systemd.services.bcachefs-reset-root = { boot.initrd.systemd.services.bcachefs-reset-root = {
description = "Reset bcachefs root subvolume before pivot"; description = "Reset bcachefs root subvolume before pivot";
# We want this to run after we've ATTEMPTED to unlock,
# but strictly BEFORE the real root is mounted at /sysroot
after = [ after = [
"initrd-root-device.target" "initrd-root-device.target"
"cryptsetup.target" "cryptsetup.target"
"unlock-bcachefs-custom.service" "unlock-bcachefs-custom.service"
]; ];
# This is the most important part: prevent sysroot from mounting until we are done resetting it
before = [
"sysroot.mount"
];
requires = [ requires = [
primaryDeviceUnit primaryDeviceUnit
"unlock-bcachefs-custom.service" "unlock-bcachefs-custom.service"
]; ];
before = [
"sysroot.mount"
];
wantedBy = [ wantedBy = [
"initrd-root-fs.target" "initrd-root-fs.target"
"sysroot.mount" "sysroot.mount"
@ -247,7 +255,6 @@ lib.mkMerge [
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";
RemainAfterExit = true;
KeyringMode = "shared"; KeyringMode = "shared";
# Environment = "PATH=${ # Environment = "PATH=${
# lib.makeBinPath [ # lib.makeBinPath [
@ -258,10 +265,10 @@ lib.mkMerge [
script = '' script = ''
cleanup() { cleanup() {
if [[ ! -e /primary_tmp/@root ]]; then # if [[ ! -e /primary_tmp/@root ]]; then
echo "Cleanup: Creating new @root" # echo "Cleanup: Creating new @root"
bcachefs subvolume create /primary_tmp/@root # bcachefs subvolume create /primary_tmp/@root
fi # fi
echo "Cleanup: Unmounting /primary_tmp" echo "Cleanup: Unmounting /primary_tmp"
umount /primary_tmp || true umount /primary_tmp || true
} }
@ -273,20 +280,21 @@ lib.mkMerge [
echo "Mounting ${PRIMARY}..." echo "Mounting ${PRIMARY}..."
if ! mount "${PRIMARY}" /primary_tmp; then if ! mount "${PRIMARY}" /primary_tmp; then
echo "Mount failed. Cannot reset root." echo "Mount failed. Cannot reset root."
exit 1 # TODO change to exit 1
exit 0
fi fi
if [[ -e /primary_tmp/@root ]]; then if [[ -e /primary_tmp/@root ]]; then
mkdir -p /primary_tmp/@snapshots/old_roots mkdir -p /primary_tmp/@snapshots/old_roots
# Use safe timestamp format (dashes instead of colons) # Use safe timestamp format (dashes instead of colons)
timestamp=$(date "+%Y-%m-%d_%H-%M-%S") timestamp=$(date --date="@$(stat -c %Y /primary_tmp/@root)" "+%Y-%m-%d_%H-%M-%S")
snap="/primary_tmp/@snapshots/old_roots/$timestamp"
echo "Snapshotting @root to $snap"
bcachefs subvolume snapshot /primary_tmp/@root "$snap"
echo "Deleting current @root" echo "Snapshotting @root to .../$timestamp"
bcachefs subvolume delete /primary_tmp/@root bcachefs subvolume snapshot /primary_tmp/@root "/primary_tmp/@snapshots/old_roots/$timestamp"
# echo "Deleting current @root"
# bcachefs subvolume delete /primary_tmp/@root
fi fi
# Trap handles creating new root and unmount # Trap handles creating new root and unmount