{"id":3352,"date":"2017-05-24T17:02:46","date_gmt":"2017-05-25T00:02:46","guid":{"rendered":"http:\/\/kmwoley.com\/blog\/?p=3352"},"modified":"2017-05-24T17:08:12","modified_gmt":"2017-05-25T00:08:12","slug":"controlling-case-fans-based-on-hard-drive-temperature","status":"publish","type":"post","link":"https:\/\/kmwoley.com\/blog\/controlling-case-fans-based-on-hard-drive-temperature\/","title":{"rendered":"Controlling Case Fans Based on Hard Drive Temperature"},"content":{"rendered":"<p>I&#8217;ve always been annoyed that the fans in my server spin all the time (wasting power, making noise) when they&#8217;re not needed. The only reason they&#8217;re there is to cool my hard drives &#8211; which aren&#8217;t in use half the time.<\/p>\n<p>When switching to unRAID, I wanted to fix this. There is a plugin (<a href=\"https:\/\/forums.lime-technology.com\/topic\/34889-dynamix-v6-plugins\/\">Dynamix System AutoFan<\/a>) that is supposed to do this, but it didn&#8217;t work for me. In reading through several forum posts,<a href=\"https:\/\/forums.lime-technology.com\/topic\/5375-temperature-based-fan-speed-control\/?page=5\"> I found a shell script that many people use to control their case fans based on their hard drive temperatures<\/a>. But it wasn&#8217;t straight forward to get it operating, so I thought I&#8217;d post the steps I followed to get it running.<\/p>\n<p>These instructions are specific to unRAID v6, but may be useful for other Linux installations.<\/p>\n<h2>Discover your fan sensor kernel modules<\/h2>\n<p>By default, it&#8217;s likely that you don&#8217;t have the kernel modules installed to give the system access to your fan speed sensors. First we need to get that setup.<\/p>\n<ol>\n<li><a href=\"https:\/\/forums.lime-technology.com\/topic\/35866-unraid-6-nerdpack-cli-tools-iftop-iotop-screen-kbd-etc\/\">Install the NerdPack plugin<\/a>. It&#8217;s the easiest way to install Perl which is needed to discover your sensors.<\/li>\n<li>SSH into your unRAID machine as root.<\/li>\n<li>From the prompt, run\u00a0<em>sensors-detect.\u00a0<\/em>Answer &#8220;Yes&#8221; (by pressing Enter) to every question except the last prompt- you do not need to automatically generate the config file.\n<pre>sensors-detect<\/pre>\n<p>The output will be pretty long, and result in the something that looks like the following. Copy this block of text &#8211; you&#8217;ll need it in the next step.<\/p>\n<pre>#----cut here----\r\n# Chip drivers\r\nmodprobe coretemp\r\nmodprobe nct6775\r\n\/usr\/bin\/sensors -s\r\n#----cut here----<\/pre>\n<h2>Load the kernel modules at each boot<\/h2>\n<\/li>\n<\/ol>\n<p>The\u00a0<em>modprobe\u00a0<\/em>commands will load the specific kernel modules for your hardware enabling your computer to detect, read, and control these fans. But we want to make sure they run each time you boot your machine &#8211; to do that unRAID, we have to modify the &#8216;go&#8217; file that runs each time your machine starts.<\/p>\n<ol>\n<li>Open up your unRAID &#8216;go&#8217; file in an editor\n<pre>nano \/boot\/config\/go<\/pre>\n<\/li>\n<li>Take the lines that start with\u00a0<em>modprobe<\/em>\u00a0from the\u00a0<em>sensors-detect<\/em>\u00a0output and add them to your &#8216;go&#8217;<em>\u00a0<\/em>file. Do not add in the &#8216;\/user\/bin\/sensors -s&#8217; line.<br \/>\n<a href=\"https:\/\/kmwoley.com\/blog\/wp-content\/uploads\/2017\/05\/go_modprobe.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3353\" src=\"https:\/\/kmwoley.com\/blog\/wp-content\/uploads\/2017\/05\/go_modprobe.png\" alt=\"\" width=\"1024\" height=\"217\" srcset=\"https:\/\/kmwoley.com\/blog\/wp-content\/uploads\/2017\/05\/go_modprobe.png 1024w, https:\/\/kmwoley.com\/blog\/wp-content\/uploads\/2017\/05\/go_modprobe-300x64.png 300w, https:\/\/kmwoley.com\/blog\/wp-content\/uploads\/2017\/05\/go_modprobe-768x163.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/li>\n<li>Save and quit nano <em>(Ctrl-X, Yes)<\/em><\/li>\n<li>Reboot your unRAID server. (Or, alternatively, you can run the\u00a0<em>modprobe\u00a0<\/em>commands from the command line to load the kernel modules without rebooting).<\/li>\n<\/ol>\n<p>After you reboot (or run the <em>modprobe<\/em> commands), you can run the <em>sensors<\/em> command from a command prompt which should show you the status of all the temperature sensors your machine knows about.<\/p>\n<h2>Figure out which controls what<\/h2>\n<p>Next up, you&#8217;ll need to figure out which device controls what fan, as well as the minimum power that needs to be applied for a given fan to keep spinning.<\/p>\n<p>Fans are controlled in Linux by assigning a value between 0 and 255 to the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pulse-width_modulation\">Pulse Width Modulated (PWM)<\/a>\u00a0fan header (0 is off, 255 is max power).<\/p>\n<p>The\u00a0<em>pwmconfig\u00a0<\/em>tool runs through all of your fan headers and vary the power output to find out which fans are controlled by each header.<\/p>\n<p>To run it, SSH into your machine and run:<\/p>\n<pre>pwmconfig<\/pre>\n<p>When prompted, answer &#8220;yes&#8221; to generating a detailed correlation.<\/p>\n<p>When prompted to set up the configuration file, say &#8220;no&#8221;.<\/p>\n<p>The output will be long.\u00a0What you&#8217;re looking for are the following things.<\/p>\n<pre>Testing pwm control hwmon2\/pwm1 ...\r\nhwmon2\/fan1_input ... speed was 1931 now 0\r\n<strong>It appears that fan hwmon2\/fan1_input<\/strong>\r\n<strong>is controlled by pwm hwmon2\/pwm1<\/strong>\r\nWould you like to generate a detailed correlation (y)? y\r\nPWM 255 FAN 1925\r\nPWM 240 FAN 1859\r\nPWM 225 FAN 1748\r\nPWM 210 FAN 1648\r\nPWM 195 FAN 1535\r\nPWM 180 FAN 1415\r\nPWM 165 FAN 1306\r\nPWM 150 FAN 1184\r\nPWM 135 FAN 1063\r\nPWM 120 FAN 946\r\nPWM 105 FAN 826\r\nPWM 90 FAN 698\r\nPWM 75 FAN 0\r\n<strong>Fan Stopped at PWM = 75<\/strong><\/pre>\n<ul>\n<li>The path to the fan controls. Above the fan I want is controlled by <strong>hwmon2\/pwm1<\/strong><\/li>\n<li>The path to the fan monitor. Above that I can see that <strong>hwmon2\/pwm1<\/strong> is correlated to <strong>hwmon2\/fan1_input.<\/strong><\/li>\n<li>The setting (PWM) at which the fan stopped spinning. In this case, it was &#8220;75&#8221;.<\/li>\n<\/ul>\n<h3>Verify the correlation<\/h3>\n<p>You likely have two sets of fans (at least) that show up in the output. One of them is probably your CPU fan, which I&#8217;d recommend leaving under the control of your BIOS (it&#8217;s safer that way).<\/p>\n<p>In order to make sure that you pick the right fan, I recommend doing a quick check. From the command line, you can assign a value to a fan to directly control the speed.<\/p>\n<p>Replace the bold text below with the fan you want to control:<\/p>\n<pre>root@tower:~# echo 0 &gt; \/sys\/class\/hwmon\/<strong>hwmon2\/pwm1<\/strong>\r\nroot@tower:~# cat \/sys\/class\/hwmon\/h<strong>wmon2\/fan1_input<\/strong>\r\n0<\/pre>\n<p>You should also physically check that this shut off the fan you expect it to (i.e. your case fan stopped spinning, not your CPU fan).<\/p>\n<h3>Find the minimum fan start speed<\/h3>\n<p>Fans have momentum &#8211; because\u00a0<em>pwmconfig<\/em> was working down from the highest-to-lowest speed, it found the lowest speed it would keep spinning after it was already moving. To find the starting speed, you have to work upwards from 0.<\/p>\n<p>In my case, the &#8220;75&#8221; stopping PWM was not enough to get the fan to start from 0:<\/p>\n<pre>root@tower:~# echo 0 &gt; \/sys\/class\/hwmon\/hwmon2\/pwm1\r\nroot@tower:~# cat \/sys\/class\/hwmon\/hwmon2\/fan1_input\r\n0\r\nroot@tower:~# echo 75 &gt; \/sys\/class\/hwmon\/hwmon2\/pwm1\r\nroot@tower:~# cat \/sys\/class\/hwmon\/hwmon2\/fan1_input\r\n0\r\nroot@tower:~# echo 85 &gt; \/sys\/class\/hwmon\/hwmon2\/pwm1\r\nroot@tower:~# cat \/sys\/class\/hwmon\/hwmon2\/fan1_input\r\n0\r\nroot@tower:~# echo 95 &gt; \/sys\/class\/hwmon\/hwmon2\/pwm1\r\nroot@tower:~# cat \/sys\/class\/hwmon\/hwmon2\/fan1_input\r\n402<\/pre>\n<h2>Install a fan monitoring script<\/h2>\n<p>Now that we know how the fans are controlled, and the minimum speed to start them, we can plug that into the fan script along with some other inputs.<\/p>\n<h3>Get &amp; edit the script<\/h3>\n<p>I started with <a href=\"https:\/\/gist.github.com\/kidpixo\/04d61b42221f6bc8489b\">this version<\/a> the the script and modified it to add a few features. You can download my version or the original. I&#8217;ll assume you downloaded mine.<\/p>\n<ol>\n<li>Make a directory to hold your scripts (if you haven&#8217;t done so already). From the command line:\n<pre>mkdir \/boot\/config\/scripts<\/pre>\n<\/li>\n<li>Download <a href=\"https:\/\/github.com\/kmwoley\/unRAID-Tools\/blob\/master\/unraid_array_fan.sh\">unraid_array_fan.sh<\/a><\/li>\n<li>Move it to <em>\/boot\/config\/scripts<\/em><\/li>\n<li>Edit unraid_array_fan.sh with your specific settings discovered above.\n<pre>nano \/boot\/config\/scripts\/unraid_array_fan.sh<\/pre>\n<ol>\n<li>Set NUM_OF_DRIVES to match your number of hard disks. Be sure to edit \/ comment out the specific device identifiers you want to key off of (i.e. sdb, sdc, etc.) I&#8217;d recommend eliminating any SSDs.<\/li>\n<li>Set the FAN_LOW_PWM to the &#8216;Fan stopped at PWM&#8217; reported by pwmconfig<\/li>\n<li>Set FAN_START_PWM to the speed discovered through manual testing.<\/li>\n<li>Set the ARRAY_FAN to match the path of the device you want to control.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>To test out the script, you can run it from the command line directly.<\/p>\n<pre>\/boot\/config\/scripts\/unraid_array_fan.sh<\/pre>\n<h3>Set the script to runs at boot<\/h3>\n<p>Last thing that needs to happen is to make sure that the script runs at boot, and runs periodically to adjust the speed.<\/p>\n<p>To do that, we&#8217;ll add the script to crontab. unRAID is a lil strange in that you have to do this via some go-file hackery plus an additional script<\/p>\n<ol>\n<li>Download<a href=\"https:\/\/github.com\/kmwoley\/unRAID-Tools\/blob\/master\/mycrontab.txt\"> mycrontab.txt<\/a>\u00a0and move it to\u00a0<em>\/boot\/config\/scripts\/\u00a0&#8211; i<\/em>t contains the following cron entry which runs the script every 5 min and puts the output into the unRAID system log.\n<pre># fan control - every 5 min\r\n*\/5 * * * * \/boot\/config\/scripts\/fan_control.sh 2&gt;&amp;1 | \/usr\/bin\/logger -t fan_control<\/pre>\n<\/li>\n<li>Open your <em>go<\/em> file in an editor\n<pre>nano \/boot\/config\/go<\/pre>\n<\/li>\n<li>And add the following lines at the bottom\n<pre># setup crontab\r\ncrontab -l &gt; \/tmp\/file\r\necho '#' &gt;&gt; \/tmp\/file\r\necho '# Start of Custom crontab entries' &gt;&gt; \/tmp\/file\r\ncat \/boot\/config\/scripts\/mycrontab.txt &gt;&gt; \/tmp\/file\r\necho '# End of Custom crontab entries' &gt;&gt; \/tmp\/file\r\ncrontab \/tmp\/file\r\nrm -f \/tmp\/file<\/pre>\n<\/li>\n<li>Save (Ctrl-X, Yes) and then Reboot your machine<\/li>\n<\/ol>\n<p>You can check to see if the script is running automatically by looking in your unRAID log file (and observing your fan speeds).<\/p>\n<h2>Final notes<\/h2>\n<p>You may want to adjust the FAN_OFF_TEMP and FAN_HIGH_TEMP to tune when the fans come on and off for your particular application.<\/p>\n<p>Finally, I have to thank all of the various folks who created the original script, who&#8217;ve posted on the <a href=\"https:\/\/forums.lime-technology.com\/topic\/5375-temperature-based-fan-speed-control\/?page=5\">unRAID forums<\/a>, <a href=\"https:\/\/wiki.lime-technology.com\/Setting_up_CPU_and_board_temperature_sensing\">the unRAID wiki<\/a>, etc. I pieced this together from a lot of good information from the folks that have done this before me.<\/p>\n<p>If there&#8217;s a better way to do this, I&#8217;d love to hear it. And I welcome <a href=\"https:\/\/github.com\/kmwoley\/unRAID-Tools\/blob\/master\/unraid_array_fan.sh\">contributions to the script on GitHub<\/a>, continuing the work of those that have come before me.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve always been annoyed that the fans in my server spin all the time (wasting power, making noise) when they&#8217;re not needed. The only reason they&#8217;re there is to cool my hard drives &#8211; which aren&#8217;t in use half the time. When switching to unRAID, I wanted to fix this. There is a plugin (Dynamix &hellip; <a href=\"https:\/\/kmwoley.com\/blog\/controlling-case-fans-based-on-hard-drive-temperature\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Controlling Case Fans Based on Hard Drive Temperature<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3352","post","type-post","status-publish","format-standard","hentry","category-geek"],"_links":{"self":[{"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/posts\/3352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/comments?post=3352"}],"version-history":[{"count":2,"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/posts\/3352\/revisions"}],"predecessor-version":[{"id":3355,"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/posts\/3352\/revisions\/3355"}],"wp:attachment":[{"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/media?parent=3352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/categories?post=3352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kmwoley.com\/blog\/wp-json\/wp\/v2\/tags?post=3352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}