Advertisement
VolleMelk

Fan control for LS-WVL on Debian with custom kernel

Apr 13th, 2013
873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.51 KB | None | 0 0
  1. # ---------------------------------
  2. # Fan control script for the LS-WVL
  3. # ---------------------------------
  4.  
  5. # Part of a bigger guide here: http://pastebin.com/H6rEZ5ge
  6. # ---
  7. # To recover automatic fan control. If the HDD temperature goes up, so goes the fan.
  8. # ---
  9. # Sources: * http://forum.buffalo.nas-central.org/viewtopic.php?f=73&t=25947 (Do not use scripts there, read on)
  10. #          * Email from hato (http://forum.buffalo.nas-central.org/memberlist.php?mode=viewprofile&u=5411)
  11.  
  12. # First we install hddtemp
  13. apt-get update
  14. apt-get install hddtemp
  15.  
  16. nano /usr/local/sbin/temperature-monitor
  17.  
  18. # Put this in /usr/local/sbin/temperature-monitor
  19.  
  20. #!/bin/sh
  21. # An alternative temperature monitoring and fan control script for
  22. # WD MyBook World Edition external hard drives
  23. #
  24. # Copyright (C) 2007 by kyyhkynen at gmail.com
  25. #
  26. # You may do with this file (and parts thereof) whatever you want, as long
  27. # as my copyright notice is retained.
  28. # last updated 2008/04/23
  29. #
  30. #120411:    Adapted for vanilla kernel patched for LS-WVL/E-AP
  31. #
  32.  
  33. ########################################################
  34. #
  35. # Configuration settings
  36. #
  37. ########################################################
  38.  
  39. # Disk(s) to monitor.
  40. DISK1=sda
  41. DISK2=sdb
  42.  
  43. # Program location. we'll use HDDTEMP to get the temperature if available, otherwise
  44. # smartctl will be used
  45. HDPARM=$(which hdparm)
  46. HDDTEMP=$(which hddtemp)
  47. SMARTCTL=$(which smartctl)
  48.  
  49. # Control interfaces
  50. ALARM_LED=/sys/devices/platform/leds-gpio/leds/lswvl:alarm:red
  51. ALARM_LED_TRIGGER=${ALARM_LED}/trigger
  52. ALARM_LED_BRIGHTNESS=${ALARM_LED}/brightness
  53. ALARM_LED_DELAY_ON=${ALARM_LED}/delay_on
  54. ALARM_LED_DELAY_OFF=${ALARM_LED}/delay_off
  55.  
  56. FAN_CTRL=/sys/devices/platform/gpio-fan
  57. FAN_TARGET=${FAN_CTRL}/fan1_target
  58. FAN_INPUT=${FAN_CTRL}/fan1_input
  59.  
  60. # (default) temperature limits for speeds.
  61. # When the temperature of the disk(s) is below TEMP_LOW, the fan speed is set to 0
  62. # When the temperature of the disk(s) is above TEMP_LOW, the fan speed is set to 1500
  63. # When the temperature of the disk(s) is above TEMP_HIGH, the fan speed is set to 3250
  64. # When the temperature of the disk(s) goes beyond TEMP_FULL, the fan speed is set to 5000
  65. TEMP_LOW=36
  66. TEMP_HIGH=45
  67. TEMP_FULL=50
  68.  
  69. # Temperature hysteresis for actually lower-down/stop the fan, i.e. the gap between
  70. # lowering down and speeding up the fan to avoid frequently change of the speed
  71. TEMP_HYS=3
  72.  
  73. [ -s /etc/default/temp-monitor ] && . /etc/default/temp-monitor
  74.  
  75. #fan speeds
  76. SPEED_STOP=0
  77. SPEED_LOW=1500
  78. SPEED_HIGH=3250
  79. SPEED_FULL=5000
  80.  
  81. # Logging stuff
  82. LOG_ENABLED=false
  83. LOG_INTERVAL=1 #in half minute
  84. #LOGGER="echo Temp. monitor:"
  85. LOGGER="/usr/bin/logger -t temp-monitor"
  86.  
  87. #######################################################################
  88. #
  89. # End of configuration. You shouldn't need to touch lines below this :)
  90. #
  91. #######################################################################
  92.  
  93. # Device name(s) for the disk(s).
  94. DEV1=/dev/${DISK1}
  95. DEV2=/dev/${DISK2}
  96.  
  97. # temperature threshold for lower-down/stop the fan
  98. TEMP_STOP=$((TEMP_LOW - TEMP_HYS))
  99. TEMP_DN_LOW=$((TEMP_HIGH - TEMP_HYS))
  100. TEMP_DN_HIGH=$((TEMP_FULL - TEMP_HYS))
  101.  
  102. hdd_temp()
  103. {
  104.     temp=
  105.     hdtemp=-274
  106.     if [ -n "$HDDTEMP" ]; then
  107.         [ -b $DEV1 ] && hdtemp=$($HDDTEMP -n $DEV1)
  108.         [ -b $DEV2 ] && {
  109.             temp=$($HDDTEMP -n $DEV2)
  110.             [ $temp -gt $hdtemp ] && hdtemp=$temp
  111.         }
  112.     elif [ -n "$SMARTCTL" ]; then
  113.         [ -b $DEV1 ] && hdtemp=$($SMARTCTL -d ata -A $DEV1 | grep Tempera | cut -c 88-90)
  114.         [ -b $DEV2 ] && {
  115.             temp=$($SMARTCTL -d ata -A $DEV2 | grep Tempera | cut -c 88-90)
  116.             [ $temp -gt $hdtemp ] && hdtemp=$temp
  117.         }
  118.     fi
  119.     echo "$hdtemp"
  120. }
  121.  
  122. alarm_blink()
  123. {
  124.     if [ $1 = "on" ]; then
  125.         echo 1 > $ALARM_LED_BRIGHTNESS  #light the alarm led
  126.         echo timer > $ALARM_LED_TRIGGER #blink it
  127.         echo 333 > $ALARM_LED_DELAY_ON
  128.         echo 333 > $ALARM_LED_DELAY_OFF
  129.     elif [ $1 = "off" ]; then
  130.         echo 0 > $ALARM_LED_BRIGHTNESS  #turn off the alarm led
  131.         echo none > $ALARM_LED_TRIGGER  #restore trigger
  132.     fi
  133. }
  134.  
  135. fan_speed()
  136. {
  137.     if [ $1 = "get" ]; then
  138.         cat $FAN_INPUT
  139.     elif [ $1 = "set" ]; then
  140.         echo $2 > $FAN_TARGET
  141.     fi
  142. }
  143.  
  144. HDD_TEMP=$(hdd_temp)
  145. if [ $HDD_TEMP -eq -274 -o ! -d "$FAN_CTRL"  ]; then
  146.     $LOG_ENABLED && ${LOGGER} "Required program (hddtemp or smartctl), harddisk or fan control interface not found, exit now."
  147.     exit 1
  148. fi
  149.  
  150. # Get current fan speed
  151. fspeed=$(fan_speed get)
  152. log_counter=0
  153.  
  154. while true; do
  155.  
  156.     if [ $HDD_TEMP -ge $TEMP_FULL ]; then
  157.         [ $fspeed -lt $SPEED_FULL ] && fan_speed set $SPEED_FULL
  158.         fspeed=$SPEED_FULL
  159.         alarm_blink on
  160.     else
  161.         alarm_blink off
  162.         if [ $HDD_TEMP -gt $TEMP_DN_HIGH -a $fspeed -lt $SPEED_FULL ]; then
  163.             [ $fspeed -ne $SPEED_HIGH ] && fan_speed set $SPEED_HIGH
  164.             fspeed=$SPEED_HIGH
  165.         elif [ $HDD_TEMP -ge $TEMP_HIGH ]; then
  166.             [ $fspeed -ne $SPEED_HIGH ] && fan_speed set $SPEED_HIGH
  167.             fspeed=$SPEED_HIGH
  168.         elif [ $HDD_TEMP -gt $TEMP_DN_LOW -a $fspeed -lt $SPEED_HIGH ]; then
  169.             [ $fspeed -ne $SPEED_LOW ] && fan_speed set $SPEED_LOW
  170.             fspeed=$SPEED_LOW
  171.         elif [ $HDD_TEMP -ge $TEMP_LOW ]; then
  172.             [ $fspeed -ne $SPEED_LOW ] && fan_speed set $SPEED_LOW
  173.             fspeed=$SPEED_LOW
  174.         elif [ $HDD_TEMP -gt $TEMP_STOP -a $fspeed -lt $SPEED_LOW ]; then
  175.             [ $fspeed -ne $SPEED_STOP ] && fan_speed set $SPEED_STOP
  176.             fspeed=$SPEED_STOP
  177.         else
  178.             [ $fspeed -ne $SPEED_STOP ] && fan_speed set $SPEED_STOP
  179.             fspeed=$SPEED_STOP
  180.         fi
  181.     fi
  182.  
  183.     log_counter=$(( log_counter + 1 ))
  184.     if [ $log_counter -ge $((LOG_INTERVAL * 2)) ]; then
  185.         $LOG_ENABLED && ${LOGGER} "${HDD_TEMP} ${fspeed}"
  186.         log_counter=0
  187.     fi
  188.  
  189.     sleep 30
  190.     HDD_TEMP=$(hdd_temp)
  191. done
  192.  
  193. # Save with CTRL+O and exit with CTRL+X
  194.  
  195. nano /etc/init.d/temperature-monitor
  196.  
  197. # Put this in /etc/init.d/temperature-monitor
  198.  
  199. #! /bin/sh
  200. #
  201. # temperature_monitor startup/stop script
  202. # made for WD MyBook World II by kyyhkynen at gmail.com
  203.  
  204. ### BEGIN INIT INFO
  205. # Provides:        temperature_mon
  206. # Required-Start:  $local_fs $syslog
  207. # Required-Stop:   $local_fs $syslog
  208. # Default-Start:   1 2 3 4 5
  209. # Default-Stop:    0 6
  210. # Short-Description: Start temperature monitor daemon
  211. ### END INIT INFO
  212.  
  213. SVC_NAME="Temperature Monitor"
  214. SVC_SCRIPT=/usr/local/sbin/temperature-monitor
  215. SVC_PID=/var/run/$(basename ${SVC_SCRIPT}).pid
  216.  
  217. checkconfig() {
  218.    if [ ! -r $SVC_SCRIPT ] ; then
  219.       echo "$SVC_NAME not found at $SVC_SCRIPT"
  220.       return 1
  221.    fi
  222. }
  223.  
  224.  
  225. start() {
  226.    checkconfig || return 1
  227.    echo "Starting ${SVC_NAME}"
  228.    start-stop-daemon --start --background --quiet \
  229.       --pidfile ${SVC_PID} --make-pidfile \
  230.       --exec ${SVC_SCRIPT}
  231. }
  232.  
  233. stop() {
  234.    echo "Stopping ${SVC_NAME}"
  235.    start-stop-daemon --stop --quiet --pidfile ${SVC_PID}
  236. }
  237.  
  238. case "$1" in
  239.    start)
  240.       start
  241.       ;;
  242.    stop)
  243.       stop
  244.       ;;
  245.    restart)
  246.       stop
  247.       start
  248.       ;;
  249.    *)
  250.       echo $"Usage: $0 {start|stop|restart}"
  251.       exit 1
  252.       ;;
  253. esac
  254.  
  255. # Update rc.d:
  256.  
  257. /etc/init.d/update-rc.d temperature-monitor defaults
  258.  
  259. # Reboot the device to check if the monitor is started upon boot
  260.  
  261. reboot
  262. ps -A | grep temperature-mon
  263.  
  264. # Output should be something like
  265. #  1348 ?        00:00:00 temperature-mon
  266.  
  267. # To check if the temperature monitor is actually doing something, you can turn
  268. # on the red LED. Within 30 seconds it should turn off again
  269.  
  270. echo 1 > /sys/devices/platform/leds-gpio/leds/lswvl\:alarm\:red/brightness
  271.  
  272. # You can check the hdd temperature (celcius degree) and current fan speed (rpm):
  273. hddtemp /dev/sda                    # Output drive info + temperature
  274. hddtemp /dev/sdb -n                 # Only give temperature
  275. cat /sys/devices/platform/gpio-fan/fan1_input       # Fan speed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement