Monitor disk temperature of disks passed through to a VM, so that you can contol fan speed according to that temp from the host (Proxmox, etc.)
I use it to control fan speed of my Proxmox server based on the temperature of the HDD disk on a SATA Controller that is passed through into a VM (it is Unraid in my case).
This project uses a DKMS module to fake a virtual temperature sensor on the host, which can then be read by lm-sensors and used to control fan speed.
Inside the VM, it uses a script to read the disk temperature calculate the disk's max temperature and can be collected by the host via qemu-guest-agent. The host system then collects this information and updates the virtual sensor using a systemd timer.
Important Note: this script won't wake up the disk, so if all the disks are idle, the temperature will be reported as 0°C.
- A VM with a disk controller passed through (e.g., SATA controller).
- A disk attached to that controller.
- A Linux-based VM (e.g., Unraid, Ubuntu, etc.) with the necessary tools installed.
- A host system that supports DKMS (e.g., Proxmox, Ubuntu, etc.).
-
Clone the repository:
git clone https://github.com/wxxsfxyzm/hdd-temp-monitor.git cd hdd-temp-monitor -
Install the required packages:
sudo apt update sudo apt install -y \ build-essential \ linux-headers-$(uname -r) \ dkms \ lm-sensors \ fancontrol \ jq -
Copy the DKMS module to the appropriate directory:
sudo cp -r virt-temp/virt-temp-1.0 /usr/src/
-
Register the DKMS module:
sudo dkms add -m virt-temp -v 1.0
-
Build and install the DKMS module:
sudo dkms build -m virt-temp -v 1.0 sudo dkms install -m virt-temp -v 1.0
-
Load the module:
sudo modprobe virt-temp
-
Verify that the module is loaded through
dmesg:dmesg | grep virt-tempif you see output indicating that the module is loaded
[ 3.123456] virt-temp: device registered
it means the installation was successful.
-
Configure sensors to read the temperature:
- Run the script below to create a configuration file for the sensors, so that
sensorscan read the temperature from the virtual disk:
echo -e 'chip "virt_temp-*"\n label temp1 "HDD Max"' | sudo tee /etc/sensors.d/virt-temp.conf
after that let sensors reload the configuration:
sensors -s
if you see no error, it means the configuration is successful.
- Run the script below to create a configuration file for the sensors, so that
-
Verify that the module is loaded and the temperature is being read:
sensors
You should see an output similar to:
virt_temp-virtual-0 Adapter: Virtual device HDD Max: +0.0°CThis indicates that the temperature sensor is working correctly.
-
Configure the module to load at boot:
echo "virt-temp" | sudo tee /etc/modules-load.d/virt-temp.conf
-
Ensure that the VM has necessary permissions to access the disk controller and temperature sensors.
- For Unraid, it works out of the box as long as the disk controller is passed through correctly.
-
Unraid runs it's system in memory, so you'd better put scripts you need into
/boot/configfolder to avoid losing them after reboot.- In my case, I put them in
/boot/config/extrasfolder, you can create this folder if it does not exist:
mkdir -p /boot/config/extras
- In my case, I put them in
-
Upload two scripts in
vm/unraidfolder to your Unraid VM's/boot/config/extrasfolder (you can put them elsewhere, but remember to change the paths in the scripts accordingly):hdd-temp-monitor.sh: This script will read the disk temperature and output it, it also have some other options:
Usage: bash hdd_temp_monitor.sh [OPTIONS] Monitor disk temperatures and output maximum value Options: -t, --type [default|unraid] Disk type context (default: default) default: include sda in monitoring unraid: skip monitoring sda -i, --info [short|full] Output information level (default: short) short: output only maximum temperature full: output detailed disk information -h, --help Show this help message
report_temp.sh: boot partition is FAT32, which does not support Linux ACLs, andqemu-guest-agentdoes not support executing script with options inside a VM, so you need a wrapper script to runhdd-temp-monitor.shwith options, this script will be executed byqemu-guest-agentto report the temperature to the host.
-
remember to enable
qemu-guest-agentin your/boot/config/gofile, add the following line before the last line/usr/local/sbin/emhttp:# Start qemu-ga /usr/bin/qemu-ga -l /tmp/qemu-ga.log -da reboot is required for Unraid to apply the changes.
Other Linux-based VMs (like Ubuntu) can use the hdd-temp-monitor.sh script directly without the need for a wrapper script, just make sure to set the executable permission:
chmod +x hdd-temp-monitor.shRemember to install qemu-guest-agent and smartmontools if they are not already installed in your VM.
-
Create a script on the host to fetch the disk temperature from the VM and update it in the host's sensors:
sudo cp host/proxmox/virt-sensor-collector.sh /usr/local/bin/ sudo chmod +x /usr/local/bin/virt-sensor-collector.sh
-
Create a systemd timer to run the script periodically:
# /etc/systemd/system/hdd-temp.service [Unit] Description=Update Virtual HDD Temperature [Service] ExecStart=/usr/local/bin/virt-sensor-collector.sh# /etc/systemd/system/hdd-temp.timer [Unit] Description=Update HDD temp every 5 seconds [Timer] OnBootSec=5s OnUnitActiveSec=5s AccuracySec=1ms [Install] WantedBy=timers.target -
Enable and start the timer:
sudo systemctl daemon-reload sudo systemctl enable hdd-temp.timer sudo systemctl start hdd-temp.timer -
Verify that the timer is running:
systemctl status hdd-temp.timer
-
Check the disk temperature on the host:
sensors
After following these steps, you should see the disk temperature being reported correctly on the host system.
-
Configure fancontrol:
-
Edit the fancontrol configuration file:
sudo nano /etc/fancontrol
-
Set the appropriate parameters for your system. You can use the
pwmconfigutility to help generate a basic configuration. -
Start the fancontrol service:
sudo systemctl start fancontrol sudo systemctl enable fancontrol
-
-
Monitor the disk temperature and fan speed:
- You can use the
sensorscommand to check the disk temperature. - The fan speed will be controlled automatically based on the temperature readings.
- You can use the