system.fileSystems.rollingRootfs: fix

This commit is contained in:
2024-03-08 18:43:42 +08:00
parent f9f0d5137a
commit 097010113d
7 changed files with 37 additions and 24 deletions

View File

@@ -37,7 +37,7 @@ inputs:
delayedMount = [ "/" "/nix" ];
};
swap = [ "/nix/swap/swap" ];
rollingRootfs = { device = "/dev/mapper/root1"; path = "/nix/rootfs"; };
rollingRootfs.device = "/dev/mapper/root1";
};
initrd.sshd.enable = true;
grub.installDevice = "efi";

View File

@@ -25,7 +25,7 @@ inputs:
};
swap = [ "/dev/mapper/swap" ];
resume = "/dev/mapper/swap";
rollingRootfs = { device = "/dev/mapper/root"; path = "/nix/rootfs"; };
rollingRootfs = {};
};
grub =
{

View File

@@ -26,7 +26,7 @@ inputs:
};
swap = [ "/dev/mapper/swap" ];
resume = "/dev/mapper/swap";
rollingRootfs = { device = "/dev/mapper/root"; path = "/nix/rootfs"; };
rollingRootfs = {};
};
nixpkgs.march = "skylake";
grub.installDevice = "efi";

View File

@@ -23,7 +23,7 @@ inputs:
delayedMount = [ "/" ];
};
swap = [ "/nix/swap/swap" ];
rollingRootfs = { device = "/dev/mapper/root"; path = "/nix/rootfs"; };
rollingRootfs = {};
};
grub.installDevice = "/dev/disk/by-path/pci-0000:00:05.0-scsi-0:0:0:0";
nixpkgs.march = "sandybridge";

View File

@@ -23,7 +23,7 @@ inputs:
delayedMount = [ "/" ];
};
swap = [ "/nix/swap/swap" ];
rollingRootfs = { device = "/dev/mapper/root"; path = "/nix/rootfs"; };
rollingRootfs = {};
};
grub.installDevice = "/dev/disk/by-path/pci-0000:00:05.0-scsi-0:0:0:0";
nixpkgs.march = "broadwell";

View File

@@ -24,7 +24,11 @@ inputs:
};
};
swap = [ "/nix/swap/swap" ];
rollingRootfs = { device = "/dev/disk/by-uuid/a04a1fb0-e4ed-4c91-9846-2f9e716f6e12"; path = "/nix/rootfs"; };
rollingRootfs =
{
device = "/dev/disk/by-uuid/a04a1fb0-e4ed-4c91-9846-2f9e716f6e12";
waitDevices = [ "/dev/disk/by-partuuid/cdbfc7d4-965e-42f2-89a3-eb2202849429" ];
};
};
grub.installDevice = "efi";
nixpkgs =

View File

@@ -60,7 +60,11 @@ inputs:
rollingRootfs = mkOption
{
type = types.nullOr (types.submodule { options =
{ device = mkOption { type = types.nonEmptyStr; }; path = mkOption { type = types.nonEmptyStr; }; }; });
{
device = mkOption { type = types.nonEmptyStr; default = "/dev/mapper/root"; };
path = mkOption { type = types.nonEmptyStr; default = "/nix/rootfs"; };
waitDevices = mkOption { type = types.listOf types.nonEmptyStr; default = []; };
};});
default = null;
};
};
@@ -236,23 +240,28 @@ inputs:
before = [ "local-fs-pre.target" "sysroot.mount" ];
unitConfig.DefaultDependencies = false;
serviceConfig.Type = "oneshot";
script = let inherit (fileSystems.rollingRootfs) device path; in
''
while ! lsmod | grep -q btrfs; do sleep 1; done
while ! [ -e ${device} ]; do sleep 1; done
mount ${device} /mnt -m
if [ -f /mnt${path}/current/.timestamp ]
then
timestamp=$(cat /mnt${path}/current/.timestamp)
subvolid=$(btrfs subvolume show /mnt${path}/current | grep 'Subvolume ID:' | awk '{print $NF}')
mv /mnt${path}/current /mnt${path}/$timestamp-$subvolid
btrfs property set -ts /mnt${path}/$timestamp-$subvolid ro true
fi
btrfs subvolume create /mnt${path}/current
chattr +C /mnt${path}/current
echo $(date '+%Y%m%d%H%M%S') > /mnt${path}/current/.timestamp
umount /mnt
'';
script =
let
inherit (fileSystems.rollingRootfs) device path waitDevices;
waitDevice = concatStringsSep "\n" (builtins.map
(device: "while ! [ -e ${device} ]; do sleep 1; done") (waitDevices ++ [ device ]));
in
''
while ! lsmod | grep -q btrfs; do sleep 1; done
${waitDevice}
mount ${device} /mnt -m
if [ -f /mnt${path}/current/.timestamp ]
then
timestamp=$(cat /mnt${path}/current/.timestamp)
subvolid=$(btrfs subvolume show /mnt${path}/current | grep 'Subvolume ID:' | awk '{print $NF}')
mv /mnt${path}/current /mnt${path}/$timestamp-$subvolid
btrfs property set -ts /mnt${path}/$timestamp-$subvolid ro true
fi
btrfs subvolume create /mnt${path}/current
chattr +C /mnt${path}/current
echo $(date '+%Y%m%d%H%M%S') > /mnt${path}/current/.timestamp
umount /mnt
'';
};
};
}