From 635796045f75bf3bd238704a2fc23ac58d73c971 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Thu, 23 Nov 2023 12:08:27 +0100 Subject: [PATCH] spin-down-hdd --- hosts/yodaHedgehog/configuration.nix | 2 +- hosts/yodaNas/configuration.nix | 10 ++- hosts/yodaYoga/configuration.nix | 8 --- modules/btrfs-mount-options.nix | 4 +- modules/{spin-down.nix => spin-down-hdd.nix} | 65 ++++++++++++-------- 5 files changed, 50 insertions(+), 39 deletions(-) rename modules/{spin-down.nix => spin-down-hdd.nix} (71%) diff --git a/hosts/yodaHedgehog/configuration.nix b/hosts/yodaHedgehog/configuration.nix index de40048..6c2b7cf 100644 --- a/hosts/yodaHedgehog/configuration.nix +++ b/hosts/yodaHedgehog/configuration.nix @@ -22,7 +22,7 @@ ../../modules/btrbk ./btrbk-config.nix ../../modules/de-p1st-monitor.nix - #../../modules/spin-down.nix + #../../modules/spin-down-hdd.nix ../../modules/btrfs-scrub.nix ../../modules/btrfs-mount-options.nix diff --git a/hosts/yodaNas/configuration.nix b/hosts/yodaNas/configuration.nix index db71ad7..9d5221c 100644 --- a/hosts/yodaNas/configuration.nix +++ b/hosts/yodaNas/configuration.nix @@ -26,7 +26,7 @@ ./btrbk-config.nix ../../modules/netcup-dns.nix ../../modules/de-p1st-monitor.nix - ../../modules/spin-down.nix + ../../modules/spin-down-hdd.nix ../../modules/btrfs-scrub.nix ../../modules/btrfs-mount-options.nix @@ -36,6 +36,14 @@ boot.initrd.luks.devices."luks-3d974bd0-f373-469b-8e9c-2d5516e9f0f5".allowDiscards = true; yoda.btrfsFileSystems = ["/" "/mnt/data" "/mnt/backup"]; #yoda.btrfsMounts = yoda.btrfsFileSystems; + yoda.spin-down-hdd = [ + # 4tb1 + "ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0CPF0N1" + # 3tb1 + "ata-WDC_WD30EFRX-68EUZN0_WD-WCC4N1173157" + # 3tb2 + "ata-WDC_WD30EFRX-68EUZN0_WD-WMC4N0564095" + ]; boot.kernelParams = [ # Microarchitectural Data Sampling (MDS), see https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/mds.html#mitigation-control-on-the-kernel-command-line diff --git a/hosts/yodaYoga/configuration.nix b/hosts/yodaYoga/configuration.nix index 21dcc42..69725cc 100644 --- a/hosts/yodaYoga/configuration.nix +++ b/hosts/yodaYoga/configuration.nix @@ -10,22 +10,14 @@ ../../modules/headless.nix ../../modules/ssh-fde-unlock.nix - #../../modules/git.nix ../../modules/zsh.nix - #../../modules/ssh-client.nix ../../modules/ssh-server.nix - - #../../modules/podman.nix ../../modules/docker.nix - #../../modules/docker-pushrm.nix ../../modules/lid-switch-handling.nix ../../modules/sendmail-mta.nix ../../modules/journalwatch.nix - #../../modules/btrbk - #../../modules/spin-down.nix - ../../modules/btrfs-scrub.nix ../../modules/btrfs-mount-options.nix ]; diff --git a/modules/btrfs-mount-options.nix b/modules/btrfs-mount-options.nix index f360f12..b7837e0 100644 --- a/modules/btrfs-mount-options.nix +++ b/modules/btrfs-mount-options.nix @@ -35,8 +35,8 @@ in config = mkIf (length cfg > 0) { # For each element (e.g. "/") in `cfg` create this config: # fileSystems."/".options = [ "compress=zstd" "noatime" "commit=120" ]; - fileSystems = builtins.listToAttrs (builtins.map (fs: { - name = fs; + fileSystems = builtins.listToAttrs (builtins.map (btrfs_mount_path: { + name = btrfs_mount_path; value = { options = [ "compress=zstd" "noatime" "commit=120" ]; }; }) cfg); }; diff --git a/modules/spin-down.nix b/modules/spin-down-hdd.nix similarity index 71% rename from modules/spin-down.nix rename to modules/spin-down-hdd.nix index c2d2764..9bc0b45 100644 --- a/modules/spin-down.nix +++ b/modules/spin-down-hdd.nix @@ -1,5 +1,3 @@ -{ config, pkgs, ... }: - # hdparm - get/set SATA/IDE device parameters # -q Handle the next option quietly, suppressing normal output (but not error messages). # -I Print device information. @@ -61,32 +59,45 @@ # - The standby timeout has been set before already, otherwise it would be spinning. # - Don't set the standby timeout again, as this will spin up the disk. +{ lib, config, options, pkgs, modulesPath, ... }: +with lib; +let + cfg = config.yoda.spin-down-hdd; +in { - environment.systemPackages = with pkgs; [ - hdparm - ]; - systemd.services."hdparm-4tb1" = { - description = "Spin down inactive HDD"; - wantedBy = [ "multi-user.target" ]; - serviceConfig.Type = "oneshot"; - script = '' - ${pkgs.hdparm}/bin/hdparm -C /dev/disk/by-id/ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0CPF0N1 | ${pkgs.busybox}/bin/grep 'standby' || ${pkgs.hdparm}/bin/hdparm -S 24 /dev/disk/by-id/ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0CPF0N1 - ''; + options = { + yoda.spin-down-hdd = mkOption { + type = types.listOf types.str; + default = [ ]; + example = ["ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0CPF0N1"]; + description = '' + List with IDs (/dev/disk/by-id/) of HDDs to spin down after 10 minutes. + ''; + }; }; - systemd.services."hdparm-3tb1" = { - description = "Spin down inactive HDD"; - wantedBy = [ "multi-user.target" ]; - serviceConfig.Type = "oneshot"; - script = '' - ${pkgs.hdparm}/bin/hdparm -C /dev/disk/by-id/ata-WDC_WD30EFRX-68EUZN0_WD-WCC4N1173157 | ${pkgs.busybox}/bin/grep 'standby' || ${pkgs.hdparm}/bin/hdparm -S 24 /dev/disk/by-id/ata-WDC_WD30EFRX-68EUZN0_WD-WCC4N1173157 - ''; - }; - systemd.services."hdparm-3tb2" = { - description = "Spin down inactive HDD"; - wantedBy = [ "multi-user.target" ]; - serviceConfig.Type = "oneshot"; - script = '' - ${pkgs.hdparm}/bin/hdparm -C /dev/disk/by-id/ata-WDC_WD30EFRX-68EUZN0_WD-WMC4N0564095 | ${pkgs.busybox}/bin/grep 'standby' || ${pkgs.hdparm}/bin/hdparm -S 24 /dev/disk/by-id/ata-WDC_WD30EFRX-68EUZN0_WD-WMC4N0564095 - ''; + + config = mkIf (length cfg > 0) { + # TODO: Is this required or is it enough to use `${pkgs.hdparm}` in the systemd service script? + environment.systemPackages = with pkgs; [ + hdparm + ]; + + # For each element (e.g. "ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0CPF0N1") in `cfg` create this config: + # systemd.services."hdparm-ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0CPF0N1" = { + # description = ...; + # wantedBy = ...; + # ... + # }; + systemd.services = builtins.listToAttrs (builtins.map (id: { + name = "hdparm-${id}"; + value = { + description = "Spin down inactive HDD"; + wantedBy = [ "multi-user.target" ]; + serviceConfig.Type = "oneshot"; + script = '' + ${pkgs.hdparm}/bin/hdparm -C /dev/disk/by-id/${id} | ${pkgs.busybox}/bin/grep 'standby' || ${pkgs.hdparm}/bin/hdparm -S 24 /dev/disk/by-id/${id} + ''; + }; + }) cfg); }; }