2021-07-14 22:03:04 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
let cfg = config.services.snapraid;
|
2021-07-14 22:03:04 +00:00
|
|
|
in
|
|
|
|
{
|
2024-01-02 11:29:13 +00:00
|
|
|
imports = [
|
|
|
|
# Should have never been on the top-level.
|
|
|
|
(mkRenamedOptionModule [ "snapraid" ] [ "services" "snapraid" ])
|
|
|
|
];
|
|
|
|
|
|
|
|
options.services.snapraid = with types; {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "SnapRAID";
|
2021-07-14 22:03:04 +00:00
|
|
|
dataDisks = mkOption {
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
d1 = "/mnt/disk1/";
|
|
|
|
d2 = "/mnt/disk2/";
|
|
|
|
d3 = "/mnt/disk3/";
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "SnapRAID data disks.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = attrsOf str;
|
|
|
|
};
|
|
|
|
parityFiles = mkOption {
|
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
"/mnt/diskp/snapraid.parity"
|
|
|
|
"/mnt/diskq/snapraid.2-parity"
|
|
|
|
"/mnt/diskr/snapraid.3-parity"
|
|
|
|
"/mnt/disks/snapraid.4-parity"
|
|
|
|
"/mnt/diskt/snapraid.5-parity"
|
|
|
|
"/mnt/disku/snapraid.6-parity"
|
|
|
|
];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "SnapRAID parity files.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = listOf str;
|
|
|
|
};
|
|
|
|
contentFiles = mkOption {
|
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
"/var/snapraid.content"
|
|
|
|
"/mnt/disk1/snapraid.content"
|
|
|
|
"/mnt/disk2/snapraid.content"
|
|
|
|
];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "SnapRAID content list files.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = listOf str;
|
|
|
|
};
|
|
|
|
exclude = mkOption {
|
|
|
|
default = [ ];
|
|
|
|
example = [ "*.unrecoverable" "/tmp/" "/lost+found/" ];
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "SnapRAID exclude directives.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = listOf str;
|
|
|
|
};
|
|
|
|
touchBeforeSync = mkOption {
|
|
|
|
default = true;
|
|
|
|
example = false;
|
2024-04-21 15:54:59 +00:00
|
|
|
description =
|
2022-08-12 12:06:08 +00:00
|
|
|
"Whether {command}`snapraid touch` should be run before {command}`snapraid sync`.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = bool;
|
|
|
|
};
|
|
|
|
sync.interval = mkOption {
|
|
|
|
default = "01:00";
|
|
|
|
example = "daily";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "How often to run {command}`snapraid sync`.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
scrub = {
|
|
|
|
interval = mkOption {
|
|
|
|
default = "Mon *-*-* 02:00:00";
|
|
|
|
example = "weekly";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "How often to run {command}`snapraid scrub`.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
plan = mkOption {
|
|
|
|
default = 8;
|
|
|
|
example = 5;
|
2024-04-21 15:54:59 +00:00
|
|
|
description =
|
2022-08-12 12:06:08 +00:00
|
|
|
"Percent of the array that should be checked by {command}`snapraid scrub`.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = int;
|
|
|
|
};
|
|
|
|
olderThan = mkOption {
|
|
|
|
default = 10;
|
|
|
|
example = 20;
|
2024-04-21 15:54:59 +00:00
|
|
|
description =
|
2021-07-14 22:03:04 +00:00
|
|
|
"Number of days since data was last scrubbed before it can be scrubbed again.";
|
|
|
|
type = int;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
extraConfig = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
nohidden
|
|
|
|
blocksize 256
|
|
|
|
hashsize 16
|
|
|
|
autosave 500
|
|
|
|
pool /pool
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Extra config options for SnapRAID.";
|
2021-07-14 22:03:04 +00:00
|
|
|
type = lines;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config =
|
|
|
|
let
|
|
|
|
nParity = builtins.length cfg.parityFiles;
|
|
|
|
mkPrepend = pre: s: pre + s;
|
|
|
|
in
|
|
|
|
mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = nParity <= 6;
|
|
|
|
message = "You can have no more than six SnapRAID parity files.";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = builtins.length cfg.contentFiles >= nParity + 1;
|
|
|
|
message =
|
|
|
|
"There must be at least one SnapRAID content file for each SnapRAID parity file plus one.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
environment = {
|
|
|
|
systemPackages = with pkgs; [ snapraid ];
|
|
|
|
|
|
|
|
etc."snapraid.conf" = {
|
|
|
|
text = with cfg;
|
|
|
|
let
|
|
|
|
prependData = mkPrepend "data ";
|
|
|
|
prependContent = mkPrepend "content ";
|
|
|
|
prependExclude = mkPrepend "exclude ";
|
|
|
|
in
|
|
|
|
concatStringsSep "\n"
|
|
|
|
(map prependData
|
|
|
|
((mapAttrsToList (name: value: name + " " + value)) dataDisks)
|
|
|
|
++ zipListsWith (a: b: a + b)
|
|
|
|
([ "parity " ] ++ map (i: toString i + "-parity ") (range 2 6))
|
|
|
|
parityFiles ++ map prependContent contentFiles
|
|
|
|
++ map prependExclude exclude) + "\n" + extraConfig;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services = with cfg; {
|
|
|
|
snapraid-scrub = {
|
|
|
|
description = "Scrub the SnapRAID array";
|
|
|
|
startAt = scrub.interval;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${pkgs.snapraid}/bin/snapraid scrub -p ${
|
|
|
|
toString scrub.plan
|
|
|
|
} -o ${toString scrub.olderThan}";
|
|
|
|
Nice = 19;
|
|
|
|
IOSchedulingPriority = 7;
|
|
|
|
CPUSchedulingPolicy = "batch";
|
|
|
|
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
RestrictAddressFamilies = "none";
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = "@system-service";
|
|
|
|
SystemCallErrorNumber = "EPERM";
|
|
|
|
CapabilityBoundingSet = "CAP_DAC_OVERRIDE";
|
|
|
|
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = "read-only";
|
|
|
|
ReadWritePaths =
|
|
|
|
# scrub requires access to directories containing content files
|
|
|
|
# to remove them if they are stale
|
|
|
|
let
|
|
|
|
contentDirs = map dirOf contentFiles;
|
|
|
|
in
|
|
|
|
unique (
|
|
|
|
attrValues dataDisks ++ contentDirs
|
|
|
|
);
|
|
|
|
};
|
|
|
|
unitConfig.After = "snapraid-sync.service";
|
|
|
|
};
|
|
|
|
snapraid-sync = {
|
|
|
|
description = "Synchronize the state of the SnapRAID array";
|
|
|
|
startAt = sync.interval;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${pkgs.snapraid}/bin/snapraid sync";
|
|
|
|
Nice = 19;
|
|
|
|
IOSchedulingPriority = 7;
|
|
|
|
CPUSchedulingPolicy = "batch";
|
|
|
|
|
|
|
|
LockPersonality = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
RestrictAddressFamilies = "none";
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
SystemCallFilter = "@system-service";
|
|
|
|
SystemCallErrorNumber = "EPERM";
|
2021-12-19 01:06:50 +00:00
|
|
|
CapabilityBoundingSet = "CAP_DAC_OVERRIDE" +
|
2021-12-06 16:07:01 +00:00
|
|
|
lib.optionalString cfg.touchBeforeSync " CAP_FOWNER";
|
2021-07-14 22:03:04 +00:00
|
|
|
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = "read-only";
|
|
|
|
ReadWritePaths =
|
|
|
|
# sync requires access to directories containing content files
|
|
|
|
# to remove them if they are stale
|
|
|
|
let
|
|
|
|
contentDirs = map dirOf contentFiles;
|
2024-01-02 11:29:13 +00:00
|
|
|
# Multiple "split" parity files can be specified in a single
|
|
|
|
# "parityFile", separated by a comma.
|
|
|
|
# https://www.snapraid.it/manual#7.1
|
|
|
|
splitParityFiles = map (s: splitString "," s) parityFiles;
|
2021-07-14 22:03:04 +00:00
|
|
|
in
|
|
|
|
unique (
|
2024-01-02 11:29:13 +00:00
|
|
|
attrValues dataDisks ++ splitParityFiles ++ contentDirs
|
2021-07-14 22:03:04 +00:00
|
|
|
);
|
|
|
|
} // optionalAttrs touchBeforeSync {
|
|
|
|
ExecStartPre = "${pkgs.snapraid}/bin/snapraid touch";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|