From 3439bd5ac5999ff57e0c176922251ebb4256c20e Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sun, 8 Oct 2023 22:02:21 +0200 Subject: [PATCH] refactor: btrfs-scrub module --- hosts/yodaNas/configuration.nix | 3 +++ hosts/yodaTab/configuration.nix | 3 +++ hosts/yodaTux/configuration.nix | 3 +++ hosts/yodaYoga/configuration.nix | 3 +++ modules/base.nix | 21 ---------------- modules/btrfs-scrub.nix | 41 ++++++++++++++++++++++++++++++++ 6 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 modules/btrfs-scrub.nix diff --git a/hosts/yodaNas/configuration.nix b/hosts/yodaNas/configuration.nix index 75385a0..05fd98f 100644 --- a/hosts/yodaNas/configuration.nix +++ b/hosts/yodaNas/configuration.nix @@ -59,10 +59,13 @@ in #../../modules/veracrypt.nix ../../modules/btrbk ../../modules/spin-down.nix + + ../../modules/btrfs-scrub.nix ]; networking.hostName = "yodaNas"; boot.initrd.luks.devices."luks-3d974bd0-f373-469b-8e9c-2d5516e9f0f5".allowDiscards = true; + yoda.btrfs-scrub = ["/" "/mnt/data" "/mnt/backup"]; boot.kernelParams = []; diff --git a/hosts/yodaTab/configuration.nix b/hosts/yodaTab/configuration.nix index 287d40b..289c2ff 100644 --- a/hosts/yodaTab/configuration.nix +++ b/hosts/yodaTab/configuration.nix @@ -58,11 +58,14 @@ in #../../modules/veracrypt.nix #../../modules/btrbk #../../modules/spin-down.nix + + ../../modules/btrfs-scrub.nix ]; networking.hostName = "yodaTab"; boot.initrd.luks.devices."luks-ba8b94d0-7f70-496a-ad87-eadc5e852aad".allowDiscards = true; boot.initrd.luks.devices."512gb".allowDiscards = true; + yoda.btrfs-scrub = ["/"]; boot.kernelParams = []; diff --git a/hosts/yodaTux/configuration.nix b/hosts/yodaTux/configuration.nix index f3d9cc1..c0aadf5 100644 --- a/hosts/yodaTux/configuration.nix +++ b/hosts/yodaTux/configuration.nix @@ -58,10 +58,13 @@ in #../../modules/veracrypt.nix #../../modules/btrbk #../../modules/spin-down.nix + + ../../modules/btrfs-scrub.nix ]; networking.hostName = "yodaTux"; boot.initrd.luks.devices."luks-ea7099e3-320d-4eb3-a4c3-9910a9af817b".allowDiscards = true; + yoda.btrfs-scrub = ["/"]; # Systemd Journal entry: # S Sat Sep 23 16:11:52 2023 p4 kernel: TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'. diff --git a/hosts/yodaYoga/configuration.nix b/hosts/yodaYoga/configuration.nix index b579a53..0854447 100644 --- a/hosts/yodaYoga/configuration.nix +++ b/hosts/yodaYoga/configuration.nix @@ -59,10 +59,13 @@ in #../../modules/veracrypt.nix #../../modules/btrbk #../../modules/spin-down.nix + + ../../modules/btrfs-scrub.nix ]; networking.hostName = "yodaYoga"; boot.initrd.luks.devices."luks-a8521407-e25b-4f26-8e7a-a56fcbfd2e35".allowDiscards = true; + yoda.btrfs-scrub = ["/"]; boot.kernelParams = []; diff --git a/modules/base.nix b/modules/base.nix index 11b5aac..3de06c6 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -98,27 +98,6 @@ "/".options = [ "compress=zstd" "noatime" "commit=120" ]; }; - # BTRFS scrub. - # - # Scrubbing is the process of checking file consistency. - # Scrubbing may be done "online", meaning you don't need to unmount a subvolume to scrub it. - # https://nixos.wiki/wiki/Btrfs#Scrubbing - # Btrfs scrub is "[a]n online filesystem checking tool. Reads all the data and metadata on the filesystem and uses checksums and the duplicate copies from RAID storage to identify and repair any corrupt data." - # https://wiki.archlinux.org/title/btrfs#Scrub - # The scrub command operates on a whole filesystem, not just individual subvolumes. - # https://unix.stackexchange.com/a/724412 - # - # As this command reads all data, it wears down the disk. One should not run it too often. For large, slow disks once per month should be fine. - # - # To run it manually: - # sudo btrfs scrub start / - # sudo btrfs scrub status / - services.btrfs.autoScrub = { - enable = true; - interval = "monthly"; - fileSystems = [ "/" ]; - }; - nix.settings.auto-optimise-store = true; # https://nixos.wiki/wiki/Storage_optimization#Automation diff --git a/modules/btrfs-scrub.nix b/modules/btrfs-scrub.nix new file mode 100644 index 0000000..f8313e4 --- /dev/null +++ b/modules/btrfs-scrub.nix @@ -0,0 +1,41 @@ +# BTRFS scrub. +# +# Scrubbing is the process of checking file consistency. +# Scrubbing may be done "online", meaning you don't need to unmount a subvolume to scrub it. +# https://nixos.wiki/wiki/Btrfs#Scrubbing +# Btrfs scrub is "[a]n online filesystem checking tool. Reads all the data and metadata on the filesystem and uses checksums and the duplicate copies from RAID storage to identify and repair any corrupt data." +# https://wiki.archlinux.org/title/btrfs#Scrub +# The scrub command operates on a whole filesystem, not just individual subvolumes. +# https://unix.stackexchange.com/a/724412 +# +# As this command reads all data, it wears down the disk. One should not run it too often. For large, slow disks once per month should be fine. +# +# To run it manually: +# sudo btrfs scrub start / +# sudo btrfs scrub status / + +{ lib, config, options, pkgs, modulesPath, ... }: +with lib; +let + cfg = config.yoda.btrfs-scrub; +in +{ + options = { + yoda.btrfs-scrub = mkOption { + type = types.listOf types.path; + default = ["/"]; + example = ["/" "/mnt/data"]; + description = '' + List containing each mounted BTRFS filesystem once. + ''; + }; + }; + + config = mkIf (length cfg > 0) { + services.btrfs.autoScrub = { + enable = true; + interval = "monthly"; + fileSystems = cfg; + }; + }; +}