Merge pull request #296 from ryan4yin/fix/usb-hdd-mitigations

fix(youko): mitigate the flaky USB-SATA HDD bridge
This commit is contained in:
ryan4yin | 二花 authored and GitHub committed 2026-09-21 17:56:08 +08:00
commit 12966d326c
1 file changed
+60 -6
+60 -6
View File
@@ -9,6 +9,11 @@
let
hostName = "youko"; # Define your hostname.
# The two HDDs behind the flaky USB-SATA bridge, addressed by-id like the
# disko config does (the /dev/sdX names are not stable).
hddPublic = "/dev/disk/by-id/ata-WDC_WD40EJRX-89T1XY0_WD-WCC7K0XDCZE6";
hddEncrypted = "/dev/disk/by-id/ata-WDC_WD40EZRZ-22GXCB0_WD-WCC7K7VV9613";
coreModule = mylib.genVmHostModule {
inherit pkgs hostName;
inherit (myvars) networking;
@@ -31,11 +36,60 @@ in
# scheduling). The VMs now use ordinary memory.
"transparent_hugepage=madvise"
# https://kubevirt.io/user-guide/compute/host-devices/
#
# PCI passthrough
# "amd_iommu=on" # enable IOMMU
# "iommu=pt" # use passthrough mode
# "pcie_acs_override=downstream" # enable ACS override
# --- mitigations for the flaky USB-SATA bridge the two 4TB HDDs sit behind.
# It is a JMicron JMS567 that resets and throws link (UDMA-CRC) + I/O errors.
"usbcore.autosuspend=-1" # no USB autosuspend
"usb-storage.delay_use=10" # give the bridge time to settle after probing
];
# Keep the bridge powered: a suspended device is what most often triggers a
# reset on resume.
services.udev.extraRules = ''
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="152d", ATTR{idProduct}=="0567", TEST=="power/control", ATTR{power/control}="on"
'';
# A spindown/spinup is a common trigger for the bridge resetting, so disable
# APM/standby on the HDDs. sdb in particular has ~1M load cycles (WD
# Intellipark), so stopping the idle head-parking also saves its mechanism.
# Read the settings back into the journal, and re-assert them hourly: the
# bridge or the disk's own firmware can drop them.
systemd.services.hdd-no-spindown = {
description = "Disable APM/standby on the USB HDDs";
after = [ "local-fs.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script = ''
for d in ${hddPublic} ${hddEncrypted}; do
echo "== $d =="
# -B 255: APM off; -S 0: no standby timer; -J 0: WD idle3 (Intellipark) off
${pkgs.hdparm}/bin/hdparm -B 255 -S 0 -J 0 "$d" || true
${pkgs.hdparm}/bin/hdparm -B "$d" || true
${pkgs.hdparm}/bin/hdparm -J "$d" || true
${pkgs.hdparm}/bin/hdparm -C "$d" || true
done
'';
};
systemd.timers.hdd-no-spindown = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnBootSec = "10min";
OnUnitActiveSec = "1h";
Persistent = true;
};
};
# Watch the disks with SMART. UDMA_CRC_Error_Count growth is the early warning
# for the bridge/link problem.
services.smartd = {
enable = true;
notifications.wall.enable = true;
devices = [
{ device = hddPublic; }
{ device = hddEncrypted; }
];
};
}