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 13 cfgZfs = config.boot.zfs; 14 14 cfgSnapshots = config.services.zfs.autoSnapshot; 15 15 cfgSnapFlags = cfgSnapshots.flags; 16 + cfgScrub = config.services.zfs.autoScrub; 16 17 17 18 inInitrd = any (fs: fs == "zfs") config.boot.initrd.supportedFilesystems; 18 19 inSystem = any (fs: fs == "zfs") config.boot.supportedFilesystems; 19 20 20 21 enableAutoSnapshots = cfgSnapshots.enable; 21 - enableZfs = inInitrd || inSystem || enableAutoSnapshots; 22 + enableAutoScrub = cfgScrub.enable; 23 + enableZfs = inInitrd || inSystem || enableAutoSnapshots || enableAutoScrub; 22 24 23 25 kernel = config.boot.kernelPackages; 24 26 ··· 217 219 ''; 218 220 }; 219 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 + }; 220 253 }; 221 254 222 255 ###### implementation ··· 282 315 zfsSupport = true; 283 316 }; 284 317 285 - environment.etc."zfs/zed.d".source = "${packages.zfsUser}/etc/zfs/zed.d/*"; 318 + environment.etc."zfs/zed.d".source = "${packages.zfsUser}/etc/zfs/zed.d/"; 286 319 287 320 system.fsPackages = [ packages.zfsUser ]; # XXX: needed? zfs doesn't have (need) a fsck 288 321 environment.systemPackages = [ packages.zfsUser ] ··· 390 423 }; 391 424 }; 392 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 + }; 393 452 }) 394 453 ]; 395 454 }