From a36922f81daefbc05dae8a7f152f6003e210dd5a Mon Sep 17 00:00:00 2001 From: chn Date: Fri, 14 Jul 2023 16:57:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E5=AE=8C=E6=88=90=20filesyst?= =?UTF-8?q?em?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flake.nix | 3 ++- modules/boot/chn-PC.nix | 30 +------------------------- modules/boot/fileSystems.nix | 42 +++++++++++++++++++++++++----------- 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/flake.nix b/flake.nix index a207352e..0bc4d274 100644 --- a/flake.nix +++ b/flake.nix @@ -136,7 +136,7 @@ btrfs = { "/dev/disk/by-uuid/02e426ec-cfa2-4a18-b3a5-57ef04d66614"."/" = "/boot"; - "/dev/mapper/root"."/nix" = "/nix"; + "/dev/mapper/root" = { "/nix" = "/nix"; "/nix/rootfs/current" = "/"; }; }; }; decrypt.auto = @@ -148,6 +148,7 @@ "ARRAY /dev/md/swap metadata=1.2 name=chn-PC:swap UUID=2b546b8d:e38007c8:02990dd1:df9e23a4"; swap = [ "/dev/mapper/swap" ]; resume = "/dev/mapper/swap"; + rollingRootfs = { device = "/dev/mapper/root"; path = "/nix/rootfs"; }; }; };} ) diff --git a/modules/boot/chn-PC.nix b/modules/boot/chn-PC.nix index ef6a34cb..33d153c1 100644 --- a/modules/boot/chn-PC.nix +++ b/modules/boot/chn-PC.nix @@ -3,12 +3,6 @@ inputs: config = { # filesystem mount - fileSystems."/" = - { - device = "/dev/mapper/root"; - fsType = "btrfs"; - options = [ "subvol=nix/rootfs/current" "compress-force=zstd" ]; - }; # sudo btrfs fi mkswapfile --size 64g --uuid clear swap # sudo btrfs inspect-internal map-swapfile -r swap # sudo mdadm --create /dev/md/swap --level 0 --raid-devices 2 /dev/nvme1n1p5 /dev/nvme0n1p5 @@ -99,29 +93,7 @@ inputs: # initrd, luks boot.initrd = { - systemd = - { - enable = true; - services.create-current-rootfs = - { - wantedBy = [ "local-fs-pre.target" ]; - after = [ "cryptsetup.target" ]; - before = [ "local-fs-pre.target" ]; - unitConfig.DefaultDependencies = false; - serviceConfig.Type = "oneshot"; - script = - '' - mount /dev/mapper/root /mnt -m - if [ -f /mnt/nix/rootfs/current/.timestamp ] - then - mv /mnt/nix/rootfs/current /mnt/nix/rootfs/$(cat /mnt/nix/rootfs/current/.timestamp) - fi - btrfs subvolume create /mnt/nix/rootfs/current - echo $(date '+%Y%m%d%H%M%S') > /mnt/nix/rootfs/current/.timestamp - umount /mnt - ''; - }; - }; + systemd.enable = true; # modules in initrd # modprobe --show-depends availableKernelModules = diff --git a/modules/boot/fileSystems.nix b/modules/boot/fileSystems.nix index fc3941f8..8223b103 100644 --- a/modules/boot/fileSystems.nix +++ b/modules/boot/fileSystems.nix @@ -18,18 +18,11 @@ inputs: swap = mkOption { type = types.listOf types.nonEmptyStr; }; resume = mkOption { type = types.nullOr (types.str or (types.submodule { options = { device = mkOption { type = types.nonEmptyStr; }; offset = mkOption { type = types.ints.unsigned; }; }; })); }; - # cleanRootfs = mkOption { type = types.nullOr - - # swap and resume - # swap != resume.device if swap is a file - # swap = mkOption { type = types.nullOr types.str; }; - # resume = - # { - # device = mkOption { type = types.nullOr types.str; }; - # # sudo btrfs fi mkswapfile --size 64g --uuid clear swap - # # sudo btrfs inspect-internal map-swapfile -r swap - # offset = mkOption { type = types.nullOr types.ints.unsigned; }; - # }; + rollingRootfs = mkOption { type = types.nullOr (types.submodule { options = + { + device = mkOption { type = types.nonEmptyStr; }; + path = mkOption { type = types.nonEmptyStr; }; + }; }); }; }; config = { @@ -91,6 +84,31 @@ inputs: if inputs.config.nixos.fileSystems.mdadm != null then { services.swraid = { enable = true; mdadmConf = inputs.config.nixos.fileSystems.mdadm; }; } else {} + ) + // ( + if inputs.config.nixos.fileSystems.rollingRootfs != null then + { + systemd.services.roll-rootfs = + { + wantedBy = [ "local-fs-pre.target" ]; + after = [ "cryptsetup.target" ]; + before = [ "local-fs-pre.target" ]; + unitConfig.DefaultDependencies = false; + serviceConfig.Type = "oneshot"; + script = let inherit (inputs.config.nixos.fileSystems.rollingRootfs) device path; in + '' + mount ${device} /mnt + if [ -f /mnt${path}/current/.timestamp ] + then + mv /mnt${path}/current /mnt${path}/$(cat /mnt${path}/current/.timestamp) + fi + btrfs subvolume create /mnt${path}/current + echo $(date '+%Y%m%d%H%M%S') > /mnt${path}/current/.timestamp + umount /mnt + ''; + }; + } + else {} ); } // (