Skip to content

Instantly share code, notes, and snippets.

@x-magic
Last active July 2, 2024 10:07
Show Gist options
  • Save x-magic/6a4b37aec01a397f473596aa4f4a012e to your computer and use it in GitHub Desktop.
Save x-magic/6a4b37aec01a397f473596aa4f4a012e to your computer and use it in GitHub Desktop.
UDM-SE: Adjust fan speed based on harddrive temperature
#!/bin/bash
# Create the HDD temperature script
cat > /tmp/zz-hddtemp.sh <<- "EOF"
#!/bin/bash
# PWM1 (HDD) related Variables
PWM1TEMP=$(/usr/sbin/hddtemp /dev/sdb | awk -F ": " '{print $3}' | grep -Po '\d+')
PWM1TEMPMAX=55
PWM1TEMPMIN=40
PWM1DEFAULT=89 # 35% fan speed
PWM1MAX=255
PWM1PATH='/sys/class/hwmon/hwmon0/device/pwm1'
# PWM2 (Board/SSD) related Variables
PWM2TEMP=$(/usr/sbin/hddtemp /dev/sda | awk -F ": " '{print $3}' | grep -Po '\d+')
PWM2TEMPMAX=65
PWM2TEMPMIN=50
PWM2DEFAULT=140 # 55% fan speed
PWM2MAX=255
PWM2PATH='/sys/class/hwmon/hwmon0/device/pwm2'
# Calculate and apply PWM1 value
PWM1TARGET=`echo $PWM1TEMP $PWM1TEMPMAX $PWM1TEMPMIN $PWM1DEFAULT $PWM1MAX | awk '{print int(($1-$3)/($2-$3)*($5-$4)+$4)}'`
echo $PWM1TARGET
if (( $PWM1TARGET > PWM1MAX )); then
echo $PWM1MAX > $PWM1PATH
elif (( $PWM1TARGET < PWM1DEFAULT )); then
echo $PWM1DEFAULT > $PWM1PATH
else
echo $PWM1TARGET > $PWM1PATH
fi
# Calculate and apply PWM2 value
PWM2TARGET=`echo $PWM2TEMP $PWM2TEMPMAX $PWM2TEMPMIN $PWM2DEFAULT $PWM2MAX | awk '{print int(($1-$3)/($2-$3)*($5-$4)+$4)}'`
echo $PWM2TARGET
if (( $PWM2TARGET > PWM2MAX )); then
echo $PWM2MAX > $PWM2PATH
elif (( $PWM2TARGET < PWM2DEFAULT )); then
echo $PWM2DEFAULT > $PWM2PATH
else
echo $PWM2TARGET > $PWM2PATH
fi
EOF
# And set the script to executable
chmod +x /tmp/zz-hddtemp.sh
# Always re-add the required cronjobs
HDDTEMPCRON='* * * * * /bin/bash /tmp/zz-hddtemp.sh >/dev/null 2>&1'
crontab -u root -l | grep -v "zz-hddtemp.sh" | { cat; echo "$HDDTEMPCRON"; } | crontab -u root -
@x-magic
Copy link
Author

x-magic commented Mar 10, 2024

This is mainly to resolve issue that UDM-SE's temperature control is way too mild and doesn't do much to prevent HDD from overheating. UDM might be able to endure 80-90°C temperature but HDD are certainly having a hard time mostly over 60°C. Before Ubiquiti finally decides do something about this, here's my solution.

Needs this to be bootable. Note unifios-utilities/on-boot-script will install CNI stuff and you may not need them. This can be resolved by creating dummy files:

mkdir -p /data/on_boot.d
cat > /data/on_boot.d/05-install-cni-plugins.sh <<- 'EOF'
	#!/bin/sh
	echo "Skip executing CNI plugins installer..."
EOF
cat > /data/on_boot.d/06-cni-bridge.sh <<- 'EOF'
	#!/bin/sh
	echo "Skip executing CNI bridge installer..."
EOF
chmod +x /data/on_boot.d/05-install-cni-plugins.sh /data/on_boot.d/06-cni-bridge.sh

or simply install the script manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment