Merge pull request #23396 from mayflower/feature/zfs-auto-scrub

zfs.autoScrub service: init

authored by

Jörg Thalheim and committed by
GitHub
4487a993 3686e1bb

+61 -2
+61 -2
nixos/modules/tasks/filesystems/zfs.nix
··· 13 cfgZfs = config.boot.zfs; 14 cfgSnapshots = config.services.zfs.autoSnapshot; 15 cfgSnapFlags = cfgSnapshots.flags; 16 17 inInitrd = any (fs: fs == "zfs") config.boot.initrd.supportedFilesystems; 18 inSystem = any (fs: fs == "zfs") config.boot.supportedFilesystems; 19 20 enableAutoSnapshots = cfgSnapshots.enable; 21 - enableZfs = inInitrd || inSystem || enableAutoSnapshots; 22 23 kernel = config.boot.kernelPackages; 24 ··· 217 ''; 218 }; 219 }; 220 }; 221 222 ###### implementation ··· 282 zfsSupport = true; 283 }; 284 285 - environment.etc."zfs/zed.d".source = "${packages.zfsUser}/etc/zfs/zed.d/*"; 286 287 system.fsPackages = [ packages.zfsUser ]; # XXX: needed? zfs doesn't have (need) a fsck 288 environment.systemPackages = [ packages.zfsUser ] ··· 390 }; 391 }; 392 }) snapshotNames); 393 }) 394 ]; 395 }
··· 13 cfgZfs = config.boot.zfs; 14 cfgSnapshots = config.services.zfs.autoSnapshot; 15 cfgSnapFlags = cfgSnapshots.flags; 16 + cfgScrub = config.services.zfs.autoScrub; 17 18 inInitrd = any (fs: fs == "zfs") config.boot.initrd.supportedFilesystems; 19 inSystem = any (fs: fs == "zfs") config.boot.supportedFilesystems; 20 21 enableAutoSnapshots = cfgSnapshots.enable; 22 + enableAutoScrub = cfgScrub.enable; 23 + enableZfs = inInitrd || inSystem || enableAutoSnapshots || enableAutoScrub; 24 25 kernel = config.boot.kernelPackages; 26 ··· 219 ''; 220 }; 221 }; 222 + 223 + services.zfs.autoScrub = { 224 + enable = mkOption { 225 + default = false; 226 + type = types.bool; 227 + description = '' 228 + Enables periodic scrubbing of ZFS pools. 229 + ''; 230 + }; 231 + 232 + interval = mkOption { 233 + default = "Sun, 02:00"; 234 + type = types.str; 235 + example = "daily"; 236 + description = '' 237 + Systemd calendar expression when to scrub ZFS pools. See 238 + <citerefentry><refentrytitle>systemd.time</refentrytitle> 239 + <manvolnum>5</manvolnum></citerefentry>. 240 + ''; 241 + }; 242 + 243 + pools = mkOption { 244 + default = []; 245 + type = types.listOf types.str; 246 + example = [ "tank" ]; 247 + description = '' 248 + List of ZFS pools to periodically scrub. If empty, all pools 249 + will be scrubbed. 250 + ''; 251 + }; 252 + }; 253 }; 254 255 ###### implementation ··· 315 zfsSupport = true; 316 }; 317 318 + environment.etc."zfs/zed.d".source = "${packages.zfsUser}/etc/zfs/zed.d/"; 319 320 system.fsPackages = [ packages.zfsUser ]; # XXX: needed? zfs doesn't have (need) a fsck 321 environment.systemPackages = [ packages.zfsUser ] ··· 423 }; 424 }; 425 }) snapshotNames); 426 + }) 427 + 428 + (mkIf enableAutoScrub { 429 + systemd.services.zfs-scrub = { 430 + description = "ZFS pools scrubbing"; 431 + after = [ "zfs-import.target" ]; 432 + serviceConfig = { 433 + Type = "oneshot"; 434 + }; 435 + script = '' 436 + ${packages.zfsUser}/bin/zpool scrub ${ 437 + if cfgScrub.pools != [] then 438 + (concatStringsSep " " cfgScrub.pools) 439 + else 440 + "$(${packages.zfsUser}/bin/zpool list -H -o name)" 441 + } 442 + ''; 443 + }; 444 + 445 + systemd.timers.zfs-scrub = { 446 + wantedBy = [ "timers.target" ]; 447 + timerConfig = { 448 + OnCalendar = cfgScrub.interval; 449 + Persistent = "yes"; 450 + }; 451 + }; 452 }) 453 ]; 454 }