diff --git a/devices/nas/default.nix b/devices/nas/default.nix index a9f4b4e8..1291c1c7 100644 --- a/devices/nas/default.nix +++ b/devices/nas/default.nix @@ -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"; diff --git a/devices/pc/default.nix b/devices/pc/default.nix index 327d102c..e0ee882d 100644 --- a/devices/pc/default.nix +++ b/devices/pc/default.nix @@ -25,7 +25,7 @@ inputs: }; swap = [ "/dev/mapper/swap" ]; resume = "/dev/mapper/swap"; - rollingRootfs = { device = "/dev/mapper/root"; path = "/nix/rootfs"; }; + rollingRootfs = {}; }; grub = { diff --git a/devices/surface/default.nix b/devices/surface/default.nix index f3498d2c..cfcbb4f6 100644 --- a/devices/surface/default.nix +++ b/devices/surface/default.nix @@ -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"; diff --git a/devices/vps6/default.nix b/devices/vps6/default.nix index 687a2a16..91857371 100644 --- a/devices/vps6/default.nix +++ b/devices/vps6/default.nix @@ -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"; diff --git a/devices/vps7/default.nix b/devices/vps7/default.nix index 15432c1d..41e0411c 100644 --- a/devices/vps7/default.nix +++ b/devices/vps7/default.nix @@ -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"; diff --git a/devices/xmupc1/default.nix b/devices/xmupc1/default.nix index f5a8328e..4f800054 100644 --- a/devices/xmupc1/default.nix +++ b/devices/xmupc1/default.nix @@ -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 = diff --git a/modules/system/fileSystems/default.nix b/modules/system/fileSystems/default.nix index 0d19e20a..9ca007f6 100644 --- a/modules/system/fileSystems/default.nix +++ b/modules/system/fileSystems/default.nix @@ -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 + ''; }; }; }