2017-12-08 22:46:10 +01:00
|
|
|
{ config, lib, pkgs, utils, ... }:
|
2012-03-09 15:37:58 +01:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2012-03-09 15:37:58 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
inInitrd = any (fs: fs == "btrfs") config.boot.initrd.supportedFilesystems;
|
2017-12-08 22:46:10 +01:00
|
|
|
inSystem = any (fs: fs == "btrfs") config.boot.supportedFilesystems;
|
|
|
|
|
|
|
|
cfgScrub = config.services.btrfs.autoScrub;
|
|
|
|
|
|
|
|
enableAutoScrub = cfgScrub.enable;
|
|
|
|
enableBtrfs = inInitrd || inSystem || enableAutoScrub;
|
2012-03-09 15:37:58 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2017-12-08 22:46:10 +01:00
|
|
|
options = {
|
|
|
|
# One could also do regular btrfs balances, but that shouldn't be necessary
|
|
|
|
# during normal usage and as long as the filesystems aren't filled near capacity
|
|
|
|
services.btrfs.autoScrub = {
|
2019-04-20 03:41:48 +02:00
|
|
|
enable = mkEnableOption "regular btrfs scrub";
|
2012-03-09 15:37:58 +01:00
|
|
|
|
2017-12-08 22:46:10 +01:00
|
|
|
fileSystems = mkOption {
|
|
|
|
type = types.listOf types.path;
|
|
|
|
example = [ "/" ];
|
|
|
|
description = ''
|
|
|
|
List of paths to btrfs filesystems to regularily call <command>btrfs scrub</command> on.
|
|
|
|
Defaults to all mount points with btrfs filesystems.
|
|
|
|
If you mount a filesystem multiple times or additionally mount subvolumes,
|
|
|
|
you need to manually specify this list to avoid scrubbing multiple times.
|
|
|
|
'';
|
|
|
|
};
|
2012-03-09 15:37:58 +01:00
|
|
|
|
2017-12-08 22:46:10 +01:00
|
|
|
interval = mkOption {
|
|
|
|
default = "monthly";
|
|
|
|
type = types.str;
|
|
|
|
example = "weekly";
|
|
|
|
description = ''
|
|
|
|
Systemd calendar expression for when to scrub btrfs filesystems.
|
|
|
|
The recommended period is a month but could be less
|
|
|
|
(<citerefentry><refentrytitle>btrfs-scrub</refentrytitle>
|
|
|
|
<manvolnum>8</manvolnum></citerefentry>).
|
|
|
|
See
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum></citerefentry>
|
|
|
|
for more information on the syntax.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
2012-03-09 15:37:58 +01:00
|
|
|
|
2017-12-08 22:46:10 +01:00
|
|
|
config = mkMerge [
|
|
|
|
(mkIf enableBtrfs {
|
|
|
|
system.fsPackages = [ pkgs.btrfs-progs ];
|
|
|
|
|
2021-07-06 15:57:10 +02:00
|
|
|
boot.initrd.kernelModules = mkIf inInitrd [ "btrfs" ];
|
|
|
|
boot.initrd.availableKernelModules = mkIf inInitrd (
|
|
|
|
[ "crc32c" ]
|
|
|
|
++ optionals (config.boot.kernelPackages.kernel.kernelAtLeast "5.5") [
|
|
|
|
# Needed for mounting filesystems with new checksums
|
|
|
|
"xxhash_generic"
|
|
|
|
"blake2b_generic"
|
|
|
|
"sha256_generic" # Should be baked into our kernel, just to be sure
|
|
|
|
]
|
|
|
|
);
|
2017-12-08 22:46:10 +01:00
|
|
|
|
2022-04-16 21:46:32 +02:00
|
|
|
boot.initrd.extraUtilsCommands = mkIf (inInitrd && !config.boot.initrd.systemd.enable)
|
2012-03-09 15:37:58 +01:00
|
|
|
''
|
2016-01-03 19:21:27 +01:00
|
|
|
copy_bin_and_libs ${pkgs.btrfs-progs}/bin/btrfs
|
2013-05-13 23:07:58 +02:00
|
|
|
ln -sv btrfs $out/bin/btrfsck
|
|
|
|
ln -sv btrfsck $out/bin/fsck.btrfs
|
2012-03-09 15:37:58 +01:00
|
|
|
'';
|
|
|
|
|
2022-04-16 21:46:32 +02:00
|
|
|
boot.initrd.extraUtilsCommandsTest = mkIf (inInitrd && !config.boot.initrd.systemd.enable)
|
2013-05-13 20:07:38 +02:00
|
|
|
''
|
|
|
|
$out/bin/btrfs --version
|
|
|
|
'';
|
|
|
|
|
2022-04-17 14:55:48 +02:00
|
|
|
boot.initrd.postDeviceCommands = mkIf (inInitrd && !config.boot.initrd.systemd.enable)
|
2012-03-09 15:37:58 +01:00
|
|
|
''
|
|
|
|
btrfs device scan
|
|
|
|
'';
|
2017-12-08 22:46:10 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf enableAutoScrub {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfgScrub.enable -> (cfgScrub.fileSystems != []);
|
|
|
|
message = ''
|
|
|
|
If 'services.btrfs.autoScrub' is enabled, you need to have at least one
|
|
|
|
btrfs file system mounted via 'fileSystems' or specify a list manually
|
|
|
|
in 'services.btrfs.autoScrub.fileSystems'.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
# This will yield duplicated units if the user mounts a filesystem multiple times
|
|
|
|
# or additionally mounts subvolumes, but going the other way around via devices would
|
|
|
|
# yield duplicated units when a filesystem spans multiple devices.
|
|
|
|
# This way around seems like the more sensible default.
|
|
|
|
services.btrfs.autoScrub.fileSystems = mkDefault (mapAttrsToList (name: fs: fs.mountPoint)
|
|
|
|
(filterAttrs (name: fs: fs.fsType == "btrfs") config.fileSystems));
|
|
|
|
|
|
|
|
# TODO: Did not manage to do it via the usual btrfs-scrub@.timer/.service
|
|
|
|
# template units due to problems enabling the parameterized units,
|
|
|
|
# so settled with many units and templating via nix for now.
|
|
|
|
# https://github.com/NixOS/nixpkgs/pull/32496#discussion_r156527544
|
|
|
|
systemd.timers = let
|
|
|
|
scrubTimer = fs: let
|
|
|
|
fs' = utils.escapeSystemdPath fs;
|
|
|
|
in nameValuePair "btrfs-scrub-${fs'}" {
|
|
|
|
description = "regular btrfs scrub timer on ${fs}";
|
|
|
|
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfgScrub.interval;
|
|
|
|
AccuracySec = "1d";
|
|
|
|
Persistent = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in listToAttrs (map scrubTimer cfgScrub.fileSystems);
|
|
|
|
|
|
|
|
systemd.services = let
|
|
|
|
scrubService = fs: let
|
|
|
|
fs' = utils.escapeSystemdPath fs;
|
|
|
|
in nameValuePair "btrfs-scrub-${fs'}" {
|
|
|
|
description = "btrfs scrub on ${fs}";
|
2020-02-14 13:00:00 +01:00
|
|
|
# scrub prevents suspend2ram or proper shutdown
|
|
|
|
conflicts = [ "shutdown.target" "sleep.target" ];
|
|
|
|
before = [ "shutdown.target" "sleep.target" ];
|
2017-12-08 22:46:10 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
2020-02-14 13:00:00 +01:00
|
|
|
# simple and not oneshot, otherwise ExecStop is not used
|
|
|
|
Type = "simple";
|
2017-12-08 22:46:10 +01:00
|
|
|
Nice = 19;
|
|
|
|
IOSchedulingClass = "idle";
|
|
|
|
ExecStart = "${pkgs.btrfs-progs}/bin/btrfs scrub start -B ${fs}";
|
2020-04-02 14:00:00 +02:00
|
|
|
# if the service is stopped before scrub end, cancel it
|
|
|
|
ExecStop = pkgs.writeShellScript "btrfs-scrub-maybe-cancel" ''
|
|
|
|
(${pkgs.btrfs-progs}/bin/btrfs scrub status ${fs} | ${pkgs.gnugrep}/bin/grep finished) || ${pkgs.btrfs-progs}/bin/btrfs scrub cancel ${fs}
|
|
|
|
'';
|
2017-12-08 22:46:10 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
in listToAttrs (map scrubService cfgScrub.fileSystems);
|
|
|
|
})
|
|
|
|
];
|
2012-03-09 15:37:58 +01:00
|
|
|
}
|