From a412d90e101b94d797f6aca4b96fdac2fd85e599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 7 Oct 2019 10:43:59 +0100 Subject: [PATCH 1/4] nixos/zfs: only enable trim if zfs is enabled Also don't fail the service if there are no pools yet. This might happen on installation ISOs. --- nixos/modules/tasks/filesystems/zfs.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nixos/modules/tasks/filesystems/zfs.nix b/nixos/modules/tasks/filesystems/zfs.nix index cfdc0a31020b..6cfa510a78a4 100644 --- a/nixos/modules/tasks/filesystems/zfs.nix +++ b/nixos/modules/tasks/filesystems/zfs.nix @@ -552,14 +552,14 @@ in }; }) - (mkIf cfgTrim.enable { + (mkIf (enableZfs && cfgTrim.enable) { systemd.services.zpool-trim = { description = "ZFS pools trim"; after = [ "zfs-import.target" ]; path = [ packages.zfsUser ]; startAt = cfgTrim.interval; script = '' - zpool list -H -o name | xargs -n1 zpool trim + zpool list -H -o name | xargs --no-run-if-empty -n1 zpool trim ''; }; }) From 692656daf85d17ceb8b9f0ad2a96aea208233857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Mon, 7 Oct 2019 10:50:30 +0100 Subject: [PATCH 2/4] nixos/zfs: avoid script derivation for trim service Since we only have a single pipe we can save the overhead of building a derivation when creating the zfs trim service file when building the system. --- nixos/modules/tasks/filesystems/zfs.nix | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nixos/modules/tasks/filesystems/zfs.nix b/nixos/modules/tasks/filesystems/zfs.nix index 6cfa510a78a4..c8ba2cac0c10 100644 --- a/nixos/modules/tasks/filesystems/zfs.nix +++ b/nixos/modules/tasks/filesystems/zfs.nix @@ -558,9 +558,7 @@ in after = [ "zfs-import.target" ]; path = [ packages.zfsUser ]; startAt = cfgTrim.interval; - script = '' - zpool list -H -o name | xargs --no-run-if-empty -n1 zpool trim - ''; + serviceConfig.ExecStart = "${pkgs.runtimeShell} -c 'zpool list -H -o name | xargs --no-run-if-empty -n1 zpool trim'"; }; }) ]; From 9a894676068434ad953fc70091885ac12093fd31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 8 Oct 2019 12:10:23 +0100 Subject: [PATCH 3/4] nixos/zfs: simplify logic for scrub/autosnapshot service This makes them consistent with the way zfs.trim is enabled and allow to enable them by default in future. --- nixos/doc/manual/release-notes/rl-2003.xml | 9 +++++++++ nixos/modules/tasks/filesystems/zfs.nix | 10 ++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/nixos/doc/manual/release-notes/rl-2003.xml b/nixos/doc/manual/release-notes/rl-2003.xml index bdf56acd5451..7c234d004602 100644 --- a/nixos/doc/manual/release-notes/rl-2003.xml +++ b/nixos/doc/manual/release-notes/rl-2003.xml @@ -36,6 +36,15 @@ quirk in the boot menu. + + + The zfs scrub service (services.zfs.autoScrub.enable) + and the zfs autosnapshot service (services.zfs.autoSnapshot.enable) + are now only enabled if zfs is set in config.boot.initrd.supportedFilesystems or + config.boot.supportedFilesystems. These lists will automatically contain + zfs as soon as any zfs mountpoint is configured in fileSystems. + + diff --git a/nixos/modules/tasks/filesystems/zfs.nix b/nixos/modules/tasks/filesystems/zfs.nix index c8ba2cac0c10..baf6da8b6f7f 100644 --- a/nixos/modules/tasks/filesystems/zfs.nix +++ b/nixos/modules/tasks/filesystems/zfs.nix @@ -16,9 +16,7 @@ let inInitrd = any (fs: fs == "zfs") config.boot.initrd.supportedFilesystems; inSystem = any (fs: fs == "zfs") config.boot.supportedFilesystems; - enableAutoSnapshots = cfgSnapshots.enable; - enableAutoScrub = cfgScrub.enable; - enableZfs = inInitrd || inSystem || enableAutoSnapshots || enableAutoScrub; + enableZfs = inInitrd || inSystem; kernel = config.boot.kernelPackages; @@ -395,7 +393,7 @@ in system.fsPackages = [ packages.zfsUser ]; # XXX: needed? zfs doesn't have (need) a fsck environment.systemPackages = [ packages.zfsUser ] - ++ optional enableAutoSnapshots autosnapPkg; # so the user can run the command to see flags + ++ optional cfgSnapshots.enable autosnapPkg; # so the user can run the command to see flags services.udev.packages = [ packages.zfsUser ]; # to hook zvol naming, etc. systemd.packages = [ packages.zfsUser ]; @@ -487,7 +485,7 @@ in systemd.targets.zfs.wantedBy = [ "multi-user.target" ]; }) - (mkIf enableAutoSnapshots { + (mkIf (enableZfs && cfgSnapshots.enable) { systemd.services = let descr = name: if name == "frequent" then "15 mins" else if name == "hourly" then "hour" @@ -525,7 +523,7 @@ in }) snapshotNames); }) - (mkIf enableAutoScrub { + (mkIf (enableZfs && cfgScrub.enable) { systemd.services.zfs-scrub = { description = "ZFS pools scrubbing"; after = [ "zfs-import.target" ]; From 12880e57e173379ba094d0dd9815f262248729d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 8 Oct 2019 12:15:15 +0100 Subject: [PATCH 4/4] nixos/zfs: mention trim support in the release notes --- nixos/doc/manual/release-notes/rl-2003.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nixos/doc/manual/release-notes/rl-2003.xml b/nixos/doc/manual/release-notes/rl-2003.xml index 7c234d004602..ab0951e831ce 100644 --- a/nixos/doc/manual/release-notes/rl-2003.xml +++ b/nixos/doc/manual/release-notes/rl-2003.xml @@ -38,6 +38,10 @@ + By default zfs pools will now be trimmed on a weekly basis. + Trimming is only done on supported devices (i.e. NVME or SSDs) + and should improve throughput and lifetime of these devices. + It is controlled by the services.zfs.trim.enable varname. The zfs scrub service (services.zfs.autoScrub.enable) and the zfs autosnapshot service (services.zfs.autoSnapshot.enable) are now only enabled if zfs is set in config.boot.initrd.supportedFilesystems or