From 333d4bcf563d8bc341460a5ff5e47c7fa36ddc4a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 29 May 2021 15:18:04 +0200 Subject: surtr: zfs snapshots --- hosts/surtr/default.nix | 34 +++---------------- hosts/surtr/zfs.nix | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 hosts/surtr/zfs.nix (limited to 'hosts') diff --git a/hosts/surtr/default.nix b/hosts/surtr/default.nix index ba304e22..8cbb51ef 100644 --- a/hosts/surtr/default.nix +++ b/hosts/surtr/default.nix @@ -1,7 +1,7 @@ { flake, pkgs, ... }: { imports = with flake.nixosModules.systemProfiles; [ - qemu-guest openssh rebuild-machines + qemu-guest openssh rebuild-machines ./zfs.nix ]; config = { @@ -24,7 +24,10 @@ tmpOnTmpfs = true; supportedFilesystems = [ "zfs" ]; - zfs.devNodes = "/dev"; # /dev/vda2 does not show up in /dev/disk/by-id + zfs = { + enableUnstable = true; + devNodes = "/dev"; # /dev/vda2 does not show up in /dev/disk/by-id + }; }; fileSystems = { @@ -37,33 +40,6 @@ { device = "/dev/disk/by-label/boot"; fsType = "vfat"; }; - - "/nix" = - { device = "surtr/local/nix"; - fsType = "zfs"; - }; - - "/root" = - { device = "surtr/safe/home-root"; - fsType = "zfs"; - neededForBoot = true; - }; - - "/var/lib/systemd" = - { device = "surtr/local/var-lib-systemd"; - fsType = "zfs"; - neededForBoot = true; - }; - - "/var/log" = - { device = "surtr/local/var-log"; - fsType = "zfs"; - }; - - "/home" = - { device = "surtr/safe/home"; - fsType = "zfs"; - }; }; networking = { diff --git a/hosts/surtr/zfs.nix b/hosts/surtr/zfs.nix new file mode 100644 index 00000000..72cc79e3 --- /dev/null +++ b/hosts/surtr/zfs.nix @@ -0,0 +1,89 @@ +{ pkgs, config, ... }: +let + snapshotNames = ["frequent" "hourly" "daily" "monthly" "yearly"]; + snapshotCount = { + frequent = 24; + hourly = 24; + daily = 30; + monthly = 12; + yearly = 5; + }; + snapshotTimerConfig = { + frequent = { OnCalendar = "*:0/5"; Persistent = true; }; + hourly = { OnCalendar = "hourly"; Persistent = true; }; + daily = { OnCalendar = "daily"; Persistent = true; }; + monthly = { OnCalendar = "monthly"; Persistent = true; }; + yearly = { OnCalendar = "yearly"; Persistent = true; }; + }; + snapshotDescr = { + frequent = "few minutes"; + hourly = "hour"; + daily = "day"; + monthly = "month"; + yearly = "year"; + }; + + zfs = config.boot.zfs.package; + + autosnapPackage = pkgs.zfstools.override { inherit zfs; }; +in { + config = { + fileSystems = { + "/nix" = + { device = "surtr/local/nix"; + fsType = "zfs"; + }; + + "/root" = + { device = "surtr/safe/home-root"; + fsType = "zfs"; + neededForBoot = true; + }; + + "/var/lib/systemd" = + { device = "surtr/local/var-lib-systemd"; + fsType = "zfs"; + neededForBoot = true; + }; + + "/var/log" = + { device = "surtr/local/var-log"; + fsType = "zfs"; + }; + + "/home" = + { device = "surtr/safe/home"; + fsType = "zfs"; + }; + }; + + systemd.services = + let mkSnapService = snapName: { + name = "zfs-snapshot-${snapName}"; + value = { + description = "ZFS auto-snapshot every ${snapshotDescr.${snapName}}"; + after = [ "zfs-import.target" ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${autosnapPackage}/bin/zfs-auto-snapshot -k -p -u ${snapName} ${toString snapshotCount.${snapName}}"; + }; + restartIfChanged = false; + + preStart = '' + ${zfs}/bin/zfs set com.sun:auto-snapshot=true surtr/safe + ''; + }; + }; + in builtins.listToAttrs (map mkSnapService snapshotNames); + + systemd.timers = + let mkSnapTimer = snapName: { + name = "zfs-snapshot-${snapName}"; + value = { + wantedBy = [ "timers.target" ]; + timerConfig = snapshotTimerConfig.${snapName}; + }; + }; + in builtins.listToAttrs (map mkSnapTimer snapshotNames); + }; +} -- cgit v1.2.3