Repository navigation
Expand file tree
/
Copy pathdisks.nix
More file actions
384 lines (330 loc) · 12.1 KB
/
Copy pathdisks.nix
File metadata and controls
384 lines (330 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
{ config, lib, pkgs, ... }:
let
disks = config.disks;
dataDisks = lib.imap (n: v: ''/dev/disk/by-id/${v}'' ) disks.data;
dataPartitions = lib.imap (n: v: ''/dev/disk/by-id/${v}-part1'' ) disks.data;
#dataPartitions = [ "" ];
rootDisk = ''/dev/disk/by-id/${disks.root}'';
rootDiskSmartdOptions = disks.smartd.rootDiskOptions;
allDisks = [ rootDisk ] ++ dataDisks;
smartdDataDevices = lib.imap (n: v: { device = v; }) dataDisks;
smartdDevices = smartdDataDevices ++ [{ device = rootDisk; options = rootDiskSmartdOptions; }];
hdparmEnabled = disks.hdparm.enable;
# -S: idle spin-down; units of 5s for 1–240. 120 = 10 minutes (was 12 = 1 min).
# -B: APM level; 127 mid (was 16 = very aggressive, thrashy on SMR under load).
hdparmStandby = disks.hdparm.standby;
hdparmApm = disks.hdparm.apm;
dataDisksLength = lib.length dataDisks;
btrfsEnabled = if dataDisksLength == 0 then false else true;
dataVolumes = disks.btrfs.volumes;
dataVolumesTail = lib.tail dataVolumes;
monthlySnapshotVolumes = disks.btrfs.snapshots.monthly.volumes;
scrubLimitFlag = lib.optionalString (disks.btrfs.scrubLimit != "") "--limit ${disks.btrfs.scrubLimit}";
removeBackup = volume: ''
SNAPSHOTS_TO_KEEP=40
COUNT_SNAPSHOTS=0
COUNT_SNAPSHOTS=`${pkgs.btrfs-progs}/bin/btrfs subvolume \
list \
-r \
--sort=path,gen \
-o $BACKUP_PATH \
| grep ${volume} \
| grep nightly \
| wc -l`
let "TO_DELETE=$COUNT_SNAPSHOTS - $SNAPSHOTS_TO_KEEP"
if [ $TO_DELETE -gt 0 ]
then
for i in `${pkgs.btrfs-progs}/bin/btrfs subvolume \
list \
-r \
--sort=path,gen \
-o $BACKUP_PATH \
| grep ${volume} \
| grep nightly \
| ${pkgs.gawk}/bin/gawk '{ print $9 }' \
| head -n $TO_DELETE`;
do
${pkgs.btrfs-progs}/bin/btrfs subvolume delete -c /media/disks/$i;
done
fi
'';
in
with lib;
{
options = {
disks = {
data = mkOption {
default = [];
example = [
"ata-ST4000LM016-1N2170_W801E6ES"
];
};
root = mkOption {
example = "ata-KingSpec_KDM-44HS.2-008GMS_984071620123";
};
smartd = {
rootDiskOptions = mkOption {
example = "";
default = "";
};
};
hdparm = {
enable = mkOption {
default = true;
type = types.bool;
};
# hdparm -S value (1–240 → ×5 seconds idle before standby).
standby = mkOption {
default = 120; # 10 minutes
type = types.int;
};
# hdparm -B APM level (1–254; lower = more aggressive power saving).
apm = mkOption {
default = 127;
type = types.int;
};
};
btrfs = {
volumes = mkOption {
default = [];
};
# Passed as `btrfs scrub start --limit`; per-device throughput cap
# (bytes/s, KMGT suffixes accepted). Tune down on weaker/busier
# hosts so a scrub doesn't starve other traffic. Empty = unlimited.
scrubLimit = mkOption {
default = "";
type = types.str;
};
snapshots = {
monthly = {
volumes = mkOption {
default = [];
};
};
};
};
};
};
config = {
services.smartd = {
enable = true;
autodetect = false;
notifications = {
mail = {
enable = true;
mailer = "/run/current-system/sw/bin/sendmail";
recipient = config.secrets.email;
sender = "${config.networking.hostName}@nathan.gs";
};
test = true;
wall.enable = false;
};
defaults.monitored = "-a -f -p -t -o on -S on -n standby,q -s (S/../.././02|L/../../6/03) -W 4,40,50";
devices = smartdDevices;
};
systemd.services.smartd = {
environment.HOSTNAME=config.networking.hostName;
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
};
# When multi-device btrfs has a MISSING member, udev keeps SYSTEMD_READY=0
# and fstab mounts (nofail) never come up. Do not force READY (avoids
# racing SMR spin-up). Instead mail if expected volumes are still unmounted
# after the device-timeout window. Only disks.btrfs.volumes (documents/media/
# archive/…); /media/disks is noauto snapshot helper — never required.
systemd.services.btrfs-mount-alert = mkIf btrfsEnabled {
description = "Email if expected btrfs volumes are not mounted";
after = [ "network-online.target" "local-fs.target" ];
wants = [ "network-online.target" ];
path = [ pkgs.btrfs-progs pkgs.gnugrep pkgs.coreutils pkgs.util-linux ];
script = ''
set -euo pipefail
missing=""
# dataVolumes only — not /media/disks
${concatStringsSep "\n" (map (v: ''
if ! ${pkgs.util-linux}/bin/mountpoint -q /media/${v}; then
missing="$missing /media/${v}"
fi
'') dataVolumes)}
if [[ -z "$missing" ]]; then
rm -f /var/lib/btrfs-mount-alert/last-mail-day
exit 0
fi
mkdir -p /var/lib/btrfs-mount-alert
stamp=/var/lib/btrfs-mount-alert/last-mail-day
today="$(date -u +%F)"
# At most one mail per day while mounts stay down (e.g. multi-day replace).
if [[ -f "$stamp" && "$(cat "$stamp")" == "$today" ]]; then
exit 0
fi
report="$(${pkgs.btrfs-progs}/bin/btrfs filesystem show 2>&1 || true)"
{
echo "Subject: [${config.networking.hostName}] btrfs volume(s) not mounted:$missing"
echo "From: ${config.networking.hostName}@nathan.gs"
echo "To: ${config.secrets.email}"
echo
echo "Expected btrfs volumes not mounted after boot:$missing"
echo
echo "Boot leaves them unmounted when systemd never sees the array as"
echo "ready (often a MISSING member). Mount manually if needed; finish"
echo "device replace/remove so auto-mount works again."
echo
echo "=== btrfs filesystem show ==="
echo "$report"
echo
echo "=== findmnt -t btrfs ==="
${pkgs.util-linux}/bin/findmnt -t btrfs || true
} | /run/wrappers/bin/sendmail -t
echo "$today" > "$stamp"
'';
serviceConfig.Type = "oneshot";
};
systemd.timers.btrfs-mount-alert = mkIf btrfsEnabled {
description = "Check expected btrfs volumes are mounted";
wantedBy = [ "timers.target" ];
timerConfig = {
# After x-systemd.device-timeout (2m) so we do not false-alarm mid-wait.
OnBootSec = "3min";
OnUnitActiveSec = "1d";
Persistent = true;
};
};
systemd.services.disks-smr = {
description = "Disks: Set timeouts for SMR disks";
wantedBy = [ "multi-user.target" ];
script = concatStringsSep "\n" (lib.imap (n: v: ''
DEVICE="$(basename $(readlink -f ${v}))"
echo 600 > /sys/block/$DEVICE/device/timeout
echo 600 > /sys/block/$DEVICE/device/eh_timeout
'') dataDisks);
};
systemd.services.btrfs-scrub = mkIf btrfsEnabled {
description = "btrfs monthly scrub";
after = [ "local-fs.target" ];
script = ''
# `btrfs scrub` runs its checksum/endio work in kernel kworker
# threads, not the calling process, so systemd's CPUQuota/Nice/
# IOWeight below don't reliably reach it - confirmed a scrub run
# still made the box unreachable over ssh despite those. The `-c`/
# `-n` ioprio flags (previously used here) are deprecated upstream
# and explicitly documented as not working with mq-deadline (the
# default scheduler), only bfq/kyber.
#
# --limit is the actual reliable throttle: a kernel-enforced
# per-device throughput cap, independent of scheduler/cgroups.
# Tune disks.btrfs.scrubLimit per-host if scrubs start taking too
# long or the box still struggles under load.
${pkgs.btrfs-progs}/bin/btrfs scrub start ${scrubLimitFlag} -B -d /media/${lib.head dataVolumes}
'';
startAt = "*-*-08 23:00:00";
serviceConfig = {
CPUQuota = "20%";
Nice = 19;
IOWeight = 1;
};
};
systemd.services.btrfs-backup-nightly = mkIf btrfsEnabled {
description = "btrfs nightly snapshot";
after = [ "local-fs.target" ];
script = ''
DT=`date '+%Y%m%d-%H%M'`
BACKUP_PATH="/media/disks/backup"
IS_DISKS_MOUNTED=`${pkgs.gnugrep}/bin/grep -c /media/disks /proc/mounts || :`
if [ $IS_DISKS_MOUNTED -eq 0 ]; then
${pkgs.util-linux}/bin/mount /media/disks
else
echo "/media/disks is mounted, not remounting."
fi
${concatStringsSep "\n" (lib.imap (n: v: "mkdir -p /media/disks/backup/${v}") dataVolumes)}
${concatStringsSep "\n" (lib.imap (n: v: "${pkgs.btrfs-progs}/bin/btrfs subvolume snapshot -r /media/${v} /media/disks/backup/${v}/${v}_nightly_\$DT") dataVolumes)}
${concatStringsSep "\n" (lib.imap (n: v: removeBackup v) dataVolumes)}
if [ $IS_DISKS_MOUNTED -eq 0 ]; then
${pkgs.util-linux}/bin/umount /media/disks
fi
'';
startAt = "*-*-* 03:00:00";
};
systemd.services.btrfs-backup-monthly = mkIf btrfsEnabled {
description = "btrfs monthly snapshot";
after = [ "local-fs.target" "mount-btrfs-volumes.service" ];
script = ''
DT=`date '+%Y%m%d-%H%M'`
BACKUP_PATH="/media/disks/backup"
IS_DISKS_MOUNTED=`${pkgs.gnugrep}/bin/grep -c /media/disks /proc/mounts || :`
if [ $IS_DISKS_MOUNTED -eq 0 ]; then
${pkgs.util-linux}/bin/mount /media/disks
else
echo "/media/disks is mounted, not remounting."
fi
${concatStringsSep "\n" (lib.imap (n: v: "mkdir -p /media/disks/backup/${v}") monthlySnapshotVolumes)}
${concatStringsSep "\n" (lib.imap (n: v: "${pkgs.btrfs-progs}/bin/btrfs subvolume snapshot -r /media/${v} /media/disks/backup/${v}/${v}_monthly_\$DT") monthlySnapshotVolumes)}
if [ $IS_DISKS_MOUNTED -eq 0 ]; then
${pkgs.util-linux}/bin/umount /media/disks
fi
'';
startAt = "*-*-01 04:00:00";
};
systemd.services.mount-btrfs-volumes = mkIf false {
after = [ "local-fs.target" ];
serviceConfig = {
Restart = "on-failure";
RestartSec = 5;
};
script = concatStringsSep "\n" (lib.imap (n: v: ''
if [ `${pkgs.gnugrep}/bin/grep -c /media/${v} /proc/mounts || :` -eq 0 ]; then
mkdir -p /media/${v}
${pkgs.util-linux}/bin/mount /media/${v}
fi
'') dataVolumes);
};
systemd.services.hdparm-setup = mkIf hdparmEnabled {
after = [ "local-fs.target"];
wantedBy = ["multi-user.target"];
script = concatStringsSep "\n" (lib.imap (n: v: ''
${pkgs.hdparm}/bin/hdparm -S${toString hdparmStandby} ${v}
${pkgs.hdparm}/bin/hdparm -B${toString hdparmApm} ${v}
'') dataDisks);
serviceConfig.Type = "oneshot";
};
fileSystems =
listToAttrs (lib.imap (i: v:
{
name = ''/media/${v}'';
value = {
device = lib.head dataPartitions;
fsType = "btrfs";
noCheck = true;
# archive only: zstd for compressible backups/text.
# media (video) + documents (mostly JPEG): skip — already compressed.
options = [
"subvol=${v}"
"noatime"
"autodefrag"
"space_cache=v2"
"x-systemd.mount-timeout=2min"
"x-systemd.device-timeout=2min"
"nofail"
# "noauto"
"degraded"
] ++ lib.optional (v == "archive") "compress=zstd:1";
};
}) dataVolumes)
// {
# Top-level FS for snapshot mgmt; no default compress (archive subvol has zstd:1).
"/media/disks" = {
device = lib.head dataPartitions;
fsType = "btrfs";
noCheck = true;
options = [
"subvolid=0"
"noauto"
"noatime"
"autodefrag"
"space_cache=v2"
"x-systemd.mount-timeout=2min"
"degraded"
];
};
};
};
}