| 1 | #!/bin/bash |
|---|
| 2 | # |
|---|
| 3 | # pwmconfig v0.10 |
|---|
| 4 | # Tests the pwm outputs of sensors and configures fancontrol |
|---|
| 5 | # |
|---|
| 6 | # Warning!!! This program will stop your fans, one at a time, |
|---|
| 7 | # for approximately 5 seconds each!!! |
|---|
| 8 | # This may cause your processor temperature to rise!!! |
|---|
| 9 | # Verify that all fans are running at normal speed after this |
|---|
| 10 | # program has exited!!! |
|---|
| 11 | # |
|---|
| 12 | # Copyright (C) 2007, 2008 Jean Delvare <khali@linux-fr.org> |
|---|
| 13 | # |
|---|
| 14 | # This program is free software; you can redistribute it and/or modify |
|---|
| 15 | # it under the terms of the GNU General Public License as published by |
|---|
| 16 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 17 | # (at your option) any later version. |
|---|
| 18 | # |
|---|
| 19 | # This program is distributed in the hope that it will be useful, |
|---|
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | # GNU General Public License for more details. |
|---|
| 23 | # |
|---|
| 24 | # You should have received a copy of the GNU General Public License |
|---|
| 25 | # along with this program; if not, write to the Free Software |
|---|
| 26 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 27 | # |
|---|
| 28 | # |
|---|
| 29 | echo 'This program will search your sensors for pulse width modulation (pwm)' |
|---|
| 30 | echo 'controls, and test each one to see if it controls a fan on' |
|---|
| 31 | echo 'your motherboard. Note that many motherboards do not have pwm' |
|---|
| 32 | echo 'circuitry installed, even if your sensor chip supports pwm.' |
|---|
| 33 | echo |
|---|
| 34 | echo 'We will attempt to briefly stop each fan using the pwm controls.' |
|---|
| 35 | echo 'The program will attempt to restore each fan to full speed' |
|---|
| 36 | echo 'after testing. However, it is ** very important ** that you' |
|---|
| 37 | echo 'physically verify that the fans have been to full speed' |
|---|
| 38 | echo 'after the program has completed.' |
|---|
| 39 | echo |
|---|
| 40 | |
|---|
| 41 | DELAY=5 # 3 seconds delay is too short for large fans, thus I increased it to 5 |
|---|
| 42 | MAX=255 |
|---|
| 43 | |
|---|
| 44 | DIR=/proc/sys/dev/sensors |
|---|
| 45 | if [ ! -d $DIR ] |
|---|
| 46 | then |
|---|
| 47 | if [ -d "/sys/class/hwmon" ] |
|---|
| 48 | then |
|---|
| 49 | SYSFS=2 |
|---|
| 50 | DIR="/sys/class/hwmon" |
|---|
| 51 | elif [ -d "/sys/bus/i2c/devices" ] |
|---|
| 52 | then |
|---|
| 53 | SYSFS=1 |
|---|
| 54 | DIR="/sys/bus/i2c/devices" |
|---|
| 55 | else |
|---|
| 56 | echo $0: 'No sensors found! (modprobe sensor modules?)' |
|---|
| 57 | exit 1 |
|---|
| 58 | fi |
|---|
| 59 | fi |
|---|
| 60 | |
|---|
| 61 | cd $DIR |
|---|
| 62 | if [ "$SYSFS" = "2" ] |
|---|
| 63 | then |
|---|
| 64 | PREFIX='hwmon*/device' |
|---|
| 65 | else |
|---|
| 66 | PREFIX='*-*' |
|---|
| 67 | fi |
|---|
| 68 | DEVICES=`echo $PREFIX` |
|---|
| 69 | if [ "$PREFIX" = "$DEVICES" ] |
|---|
| 70 | then |
|---|
| 71 | echo $0: 'No sensors found! (modprobe sensor modules?)' |
|---|
| 72 | exit 1 |
|---|
| 73 | fi |
|---|
| 74 | |
|---|
| 75 | MATCH=$PREFIX/'pwm[1-9]' |
|---|
| 76 | PWM=`echo $MATCH` |
|---|
| 77 | if [ "$SYSFS" = "1" -a "$MATCH" = "$PWM" ] |
|---|
| 78 | then |
|---|
| 79 | # Deprecated naming scheme (used in kernels 2.6.5 to 2.6.9) |
|---|
| 80 | MATCH=$PREFIX/'fan[1-9]_pwm' |
|---|
| 81 | PWM=`echo $MATCH` |
|---|
| 82 | fi |
|---|
| 83 | if [ "$MATCH" = "$PWM" ] |
|---|
| 84 | then |
|---|
| 85 | echo $0: 'There are no pwm-capable sensor modules installed' |
|---|
| 86 | exit 1 |
|---|
| 87 | fi |
|---|
| 88 | |
|---|
| 89 | if [ -n "$SYSFS" ] |
|---|
| 90 | then |
|---|
| 91 | MATCH=$PREFIX/'fan[1-9]_input' |
|---|
| 92 | else |
|---|
| 93 | MATCH=$PREFIX/'fan[1-9]' |
|---|
| 94 | fi |
|---|
| 95 | FAN=`echo $MATCH` |
|---|
| 96 | if [ "$MATCH" = "$FAN" ] |
|---|
| 97 | then |
|---|
| 98 | echo $0: 'There are no fan-capable sensor modules installed' |
|---|
| 99 | exit 1 |
|---|
| 100 | fi |
|---|
| 101 | |
|---|
| 102 | # $1 = padding |
|---|
| 103 | # Only works with Linux 2.6 |
|---|
| 104 | function print_devices() |
|---|
| 105 | { |
|---|
| 106 | for device in $DEVICES |
|---|
| 107 | do |
|---|
| 108 | echo "$1$device is `cat $device/name`" |
|---|
| 109 | done |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | # $1 = pwm file name |
|---|
| 113 | function is_pwm_auto() |
|---|
| 114 | { |
|---|
| 115 | if [ -n "$SYSFS" ] |
|---|
| 116 | then |
|---|
| 117 | ENABLE=${1}_enable |
|---|
| 118 | if [ -f $ENABLE ] |
|---|
| 119 | then |
|---|
| 120 | if [ "`cat $ENABLE`" -gt 1 ] |
|---|
| 121 | then |
|---|
| 122 | return 0 |
|---|
| 123 | fi |
|---|
| 124 | fi |
|---|
| 125 | fi |
|---|
| 126 | |
|---|
| 127 | return 1 |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | # $1 = pwm file name |
|---|
| 131 | function pwmdisable() |
|---|
| 132 | { |
|---|
| 133 | if [ -n "$SYSFS" ] |
|---|
| 134 | then |
|---|
| 135 | ENABLE=${1}_enable |
|---|
| 136 | # No enable file? Just set to max |
|---|
| 137 | if [ ! -f $ENABLE ] |
|---|
| 138 | then |
|---|
| 139 | echo $MAX > $1 |
|---|
| 140 | return 0 |
|---|
| 141 | fi |
|---|
| 142 | |
|---|
| 143 | # Try pwmN_enable=0 |
|---|
| 144 | echo 0 2>/dev/null > $ENABLE |
|---|
| 145 | if [ "`cat $ENABLE`" -eq 0 ] |
|---|
| 146 | then |
|---|
| 147 | # Success |
|---|
| 148 | return 0 |
|---|
| 149 | fi |
|---|
| 150 | |
|---|
| 151 | # It didn't work, try pwmN_enable=1 pwmN=255 |
|---|
| 152 | echo 1 2>/dev/null > $ENABLE |
|---|
| 153 | if [ "`cat $ENABLE`" -ne 1 ] |
|---|
| 154 | then |
|---|
| 155 | echo "$ENABLE stuck to `cat $ENABLE`" >&2 |
|---|
| 156 | return 1 |
|---|
| 157 | fi |
|---|
| 158 | |
|---|
| 159 | echo $MAX > $1 |
|---|
| 160 | if [ "`cat $1`" -ge 190 ] |
|---|
| 161 | then |
|---|
| 162 | # Success |
|---|
| 163 | return 0 |
|---|
| 164 | fi |
|---|
| 165 | |
|---|
| 166 | # Nothing worked |
|---|
| 167 | echo "$1 stuck to `cat $1`" >&2 |
|---|
| 168 | return 1 |
|---|
| 169 | else |
|---|
| 170 | echo $MAX 0 > $1 |
|---|
| 171 | fi |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | # $1 = pwm file name |
|---|
| 175 | function pwmenable() |
|---|
| 176 | { |
|---|
| 177 | if [ -n "$SYSFS" ] |
|---|
| 178 | then |
|---|
| 179 | ENABLE=${1}_enable |
|---|
| 180 | if [ -w $ENABLE ] |
|---|
| 181 | then |
|---|
| 182 | echo 1 2>/dev/null > $ENABLE |
|---|
| 183 | if [ $? -ne 0 ] |
|---|
| 184 | then |
|---|
| 185 | return 1 |
|---|
| 186 | fi |
|---|
| 187 | fi |
|---|
| 188 | echo $MAX > $1 |
|---|
| 189 | else |
|---|
| 190 | echo $MAX 1 > $1 |
|---|
| 191 | fi |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | # $1 = pwm file name; $2 = pwm value 0-255 |
|---|
| 195 | function pwmset() |
|---|
| 196 | { |
|---|
| 197 | echo $2 > $1 |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | if [ -n "$SYSFS" ] |
|---|
| 201 | then |
|---|
| 202 | echo 'Found the following devices:' |
|---|
| 203 | print_devices " " |
|---|
| 204 | echo |
|---|
| 205 | fi |
|---|
| 206 | |
|---|
| 207 | echo 'Found the following PWM controls:' |
|---|
| 208 | for i in $PWM |
|---|
| 209 | do |
|---|
| 210 | echo " $i" |
|---|
| 211 | if [ -w $i ] |
|---|
| 212 | then |
|---|
| 213 | # First check if PWM output is in automatic mode |
|---|
| 214 | if is_pwm_auto $i |
|---|
| 215 | then |
|---|
| 216 | echo "$i is currently setup for automatic speed control." |
|---|
| 217 | echo 'In general, automatic mode is preferred over manual mode, as' |
|---|
| 218 | echo 'it is more efficient and it reacts faster. Are you sure that' |
|---|
| 219 | echo -n 'you want to setup this output for manual control? (n) ' |
|---|
| 220 | read X |
|---|
| 221 | if [ "$X" = "" -o "$X" != "y" -a "$X" != "Y" ] |
|---|
| 222 | then |
|---|
| 223 | continue |
|---|
| 224 | fi |
|---|
| 225 | fi |
|---|
| 226 | |
|---|
| 227 | pwmdisable $i |
|---|
| 228 | if [ $? -ne 0 ] |
|---|
| 229 | then |
|---|
| 230 | echo "Manual control mode not supported, skipping $i." |
|---|
| 231 | elif [ "$GOODPWM" = "" ] |
|---|
| 232 | then |
|---|
| 233 | GOODPWM=$i |
|---|
| 234 | else |
|---|
| 235 | GOODPWM="$GOODPWM $i" |
|---|
| 236 | fi |
|---|
| 237 | else |
|---|
| 238 | NOTROOT=1 |
|---|
| 239 | fi |
|---|
| 240 | done |
|---|
| 241 | |
|---|
| 242 | if [ "$GOODPWM" = "" ] |
|---|
| 243 | then |
|---|
| 244 | echo 'There are no usable PWM outputs.' |
|---|
| 245 | exit 1 |
|---|
| 246 | fi |
|---|
| 247 | |
|---|
| 248 | echo |
|---|
| 249 | echo "Giving the fans some time to reach full speed..." |
|---|
| 250 | sleep $DELAY |
|---|
| 251 | echo 'Found the following fan sensors:' |
|---|
| 252 | for i in $FAN |
|---|
| 253 | do |
|---|
| 254 | # this will return the first field if there's only one (sysfs) |
|---|
| 255 | S=`cat $i | cut -d' ' -f2` |
|---|
| 256 | if [ "$S" = "0" -o "$S" = "-1" ] |
|---|
| 257 | then |
|---|
| 258 | echo " $i current speed: 0 ... skipping!" |
|---|
| 259 | else |
|---|
| 260 | echo " $i current speed: $S RPM" |
|---|
| 261 | if [ "$GOODFAN" = "" ] |
|---|
| 262 | then |
|---|
| 263 | GOODFAN=$i |
|---|
| 264 | SPEEDS=$S |
|---|
| 265 | else |
|---|
| 266 | GOODFAN="$GOODFAN $i" |
|---|
| 267 | SPEEDS="$SPEEDS $S" |
|---|
| 268 | fi |
|---|
| 269 | fi |
|---|
| 270 | done |
|---|
| 271 | echo |
|---|
| 272 | |
|---|
| 273 | if [ "$GOODFAN" = "" ] |
|---|
| 274 | then |
|---|
| 275 | echo 'There are no working fan sensors, all readings are 0.' |
|---|
| 276 | echo 'Make sure you have a 3-wire fan connected.' |
|---|
| 277 | echo 'You may also need to increase the fan divisors.' |
|---|
| 278 | echo 'See doc/fan-divisors for more information.' |
|---|
| 279 | exit 1 |
|---|
| 280 | fi |
|---|
| 281 | |
|---|
| 282 | if [ "$NOTROOT" = "1" ] |
|---|
| 283 | then |
|---|
| 284 | echo 'As you are not root, we cannot write the PWM settings.' |
|---|
| 285 | echo 'Please run as root to continue.' |
|---|
| 286 | exit 1 |
|---|
| 287 | fi |
|---|
| 288 | |
|---|
| 289 | echo 'Warning!!! This program will stop your fans, one at a time,' |
|---|
| 290 | echo "for approximately $DELAY seconds each!!!" |
|---|
| 291 | echo 'This may cause your processor temperature to rise!!!' |
|---|
| 292 | echo 'If you do not want to do this hit control-C now!!!' |
|---|
| 293 | echo -n 'Hit return to continue: ' |
|---|
| 294 | read X |
|---|
| 295 | echo |
|---|
| 296 | |
|---|
| 297 | PLOTTER=gnuplot |
|---|
| 298 | STEP=15 |
|---|
| 299 | PDELAY=2 |
|---|
| 300 | # Use a smaller step for low PWM values as this is typically where the |
|---|
| 301 | # more important fan speed changes are happening. |
|---|
| 302 | STEP2=2 |
|---|
| 303 | STEP2_BELOW=31 |
|---|
| 304 | |
|---|
| 305 | function pwmdetail() |
|---|
| 306 | { |
|---|
| 307 | P=$1 |
|---|
| 308 | F=$2 |
|---|
| 309 | PLOT= |
|---|
| 310 | |
|---|
| 311 | type $PLOTTER > /dev/null 2>&1 |
|---|
| 312 | if [ $? -eq 0 ] |
|---|
| 313 | then |
|---|
| 314 | echo -n "Would you like to generate a graphical plot using $PLOTTER (y)? " |
|---|
| 315 | read X |
|---|
| 316 | if [ "$X" = "y" -o "$X" = "Y" -o "$X" = "" ] |
|---|
| 317 | then |
|---|
| 318 | PLOT=y |
|---|
| 319 | fi |
|---|
| 320 | fi |
|---|
| 321 | |
|---|
| 322 | if [ "$PLOT" = "y" ] |
|---|
| 323 | then |
|---|
| 324 | TMP1=`mktemp -t pwmtest1.XXXXXXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1; } |
|---|
| 325 | TMP2=`mktemp -t pwmtest2.XXXXXXXXXX` || { rm -f $TMP1 ; echo "$0: Cannot create temporary file" >&2; exit 1; } |
|---|
| 326 | echo "set xlabel \"PWM: $P\"" > $TMP1 |
|---|
| 327 | echo "set ylabel \"FAN: $F (RPM)\"" >> $TMP1 |
|---|
| 328 | echo 'set nokey' >> $TMP1 |
|---|
| 329 | echo 'set xrange [0:255]' >> $TMP1 |
|---|
| 330 | echo "plot \"$TMP2\" with lines" >> $TMP1 |
|---|
| 331 | echo 'pause -1 " Hit return to continue..."' >> $TMP1 |
|---|
| 332 | > $TMP2 |
|---|
| 333 | fi |
|---|
| 334 | |
|---|
| 335 | let pwm=$MAX |
|---|
| 336 | pwmenable $P |
|---|
| 337 | while [ $pwm -ge 0 ] |
|---|
| 338 | do |
|---|
| 339 | pwmset $P $pwm |
|---|
| 340 | sleep $PDELAY |
|---|
| 341 | if [ $? -ne 0 ] |
|---|
| 342 | then |
|---|
| 343 | pwmdisable $P |
|---|
| 344 | echo '^C received, aborting...' |
|---|
| 345 | rm -f $TMP1 $TMP2 |
|---|
| 346 | exit 1 |
|---|
| 347 | fi |
|---|
| 348 | # this will return the first field if there's only one (sysfs) |
|---|
| 349 | S=`cat $F | cut -d' ' -f2` |
|---|
| 350 | echo " PWM $pwm FAN $S" |
|---|
| 351 | if [ "$PLOT" = "y" ] |
|---|
| 352 | then |
|---|
| 353 | echo "$pwm $S" >> $TMP2 |
|---|
| 354 | fi |
|---|
| 355 | if [ "$S" = "0" -o "$S" = "-1" ] |
|---|
| 356 | then |
|---|
| 357 | pwmdisable $P |
|---|
| 358 | echo " Fan Stopped at PWM = $pwm" |
|---|
| 359 | if [ $pwm -eq $MAX ] |
|---|
| 360 | then |
|---|
| 361 | echo " This fan appears to stop when the PWM is enabled;" |
|---|
| 362 | echo " perhaps the fan input shares a pin with the PWM output" |
|---|
| 363 | echo " on the sensor chip." |
|---|
| 364 | echo " You cannot control this fan with this PWM output." |
|---|
| 365 | rm -f $TMP1 $TMP2 |
|---|
| 366 | echo |
|---|
| 367 | return 0 |
|---|
| 368 | fi |
|---|
| 369 | break |
|---|
| 370 | fi |
|---|
| 371 | if [ $pwm -lt $STEP2_BELOW ] |
|---|
| 372 | then |
|---|
| 373 | let pwm=$pwm-$STEP2 |
|---|
| 374 | else |
|---|
| 375 | let pwm=$pwm-$STEP |
|---|
| 376 | fi |
|---|
| 377 | done |
|---|
| 378 | pwmdisable $P |
|---|
| 379 | if [ "$PLOT" = "y" ] |
|---|
| 380 | then |
|---|
| 381 | $PLOTTER $TMP1 |
|---|
| 382 | rm -f $TMP1 $TMP2 |
|---|
| 383 | fi |
|---|
| 384 | echo |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | for i in $GOODPWM |
|---|
| 388 | do |
|---|
| 389 | echo Testing pwm control $i ... |
|---|
| 390 | pwmenable $i |
|---|
| 391 | if [ $? -ne 0 ] |
|---|
| 392 | then |
|---|
| 393 | echo "Manual control mode not supported, skipping." |
|---|
| 394 | continue |
|---|
| 395 | fi |
|---|
| 396 | pwmset $i 0 |
|---|
| 397 | sleep $DELAY |
|---|
| 398 | if [ $? -ne 0 ] |
|---|
| 399 | then |
|---|
| 400 | pwmdisable $i |
|---|
| 401 | echo '^C received, restoring PWM and aborting...' |
|---|
| 402 | exit 1 |
|---|
| 403 | fi |
|---|
| 404 | let pwmactivecount=0 |
|---|
| 405 | let count=1 |
|---|
| 406 | for j in $GOODFAN |
|---|
| 407 | do |
|---|
| 408 | OS=`echo $SPEEDS | cut -d' ' -f$count` |
|---|
| 409 | # this will return the first field if there's only one (sysfs) |
|---|
| 410 | S=`cat $j | cut -d' ' -f2` |
|---|
| 411 | echo " $j ... speed was $OS now $S" |
|---|
| 412 | pwmdisable $i |
|---|
| 413 | let threshold=2*$OS/3 |
|---|
| 414 | if [ $S -lt $threshold ] |
|---|
| 415 | then |
|---|
| 416 | echo " It appears that fan $j" |
|---|
| 417 | echo " is controlled by pwm $i" |
|---|
| 418 | # |
|---|
| 419 | # a PWM can control more than one fan.... |
|---|
| 420 | # |
|---|
| 421 | if [ $pwmactivecount -eq 0 ] |
|---|
| 422 | then |
|---|
| 423 | let pwmactivecount=1 |
|---|
| 424 | pwmactive="$i ${pwmactive}" |
|---|
| 425 | fanactive="$j ${fanactive}" |
|---|
| 426 | else |
|---|
| 427 | fanactive="$j+${fanactive}" #not supported yet by fancontrol |
|---|
| 428 | fi |
|---|
| 429 | sleep $DELAY |
|---|
| 430 | if [ $? -ne 0 ] |
|---|
| 431 | then |
|---|
| 432 | echo '^C received, aborting...' |
|---|
| 433 | exit 1 |
|---|
| 434 | fi |
|---|
| 435 | # this will return the first field if there's only one (sysfs) |
|---|
| 436 | S=`cat $j | cut -d' ' -f2` |
|---|
| 437 | if [ $S -lt $threshold ] |
|---|
| 438 | then |
|---|
| 439 | echo " Fan $j has not returned to speed, please investigate!" |
|---|
| 440 | else |
|---|
| 441 | echo -n "Would you like to generate a detailed correlation (y)? " |
|---|
| 442 | read X |
|---|
| 443 | if [ "$X" = "y" -o "$X" = "Y" -o "$X" = "" ] |
|---|
| 444 | then |
|---|
| 445 | pwmdetail $i $j |
|---|
| 446 | fi |
|---|
| 447 | fi |
|---|
| 448 | else |
|---|
| 449 | echo " no correlation" |
|---|
| 450 | fi |
|---|
| 451 | let count=count+1 |
|---|
| 452 | done |
|---|
| 453 | echo |
|---|
| 454 | if [ "$pwmactivecount" = "0" ] |
|---|
| 455 | then |
|---|
| 456 | echo "No correlations were detected." |
|---|
| 457 | echo "There is either no fan connected to the output of $i," |
|---|
| 458 | echo "or the connected fan has no rpm-signal connected to one of" |
|---|
| 459 | echo "the tested fan sensors. (Note: not all motherboards have" |
|---|
| 460 | echo "the pwm outputs connected to the fan connectors," |
|---|
| 461 | echo "check out the hardware database on http://www.almico.com/forumindex.php)" |
|---|
| 462 | echo |
|---|
| 463 | echo -n "Did you see/hear a fan stopping during the above test (n)? " |
|---|
| 464 | read X |
|---|
| 465 | if [ "$X" = "y" -o "$X" = "Y" ] |
|---|
| 466 | then |
|---|
| 467 | pwmactive="$i ${pwmactive}" |
|---|
| 468 | fi |
|---|
| 469 | echo |
|---|
| 470 | fi |
|---|
| 471 | done |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | echo 'Testing is complete.' |
|---|
| 475 | echo 'Please verify that all fans have returned to their normal speed.' |
|---|
| 476 | echo |
|---|
| 477 | echo 'The fancontrol script can automatically respond to temperature changes' |
|---|
| 478 | echo 'of your system by changing fanspeeds.' |
|---|
| 479 | echo -n 'Do you want to set up its configuration file now (y)? ' |
|---|
| 480 | |
|---|
| 481 | read X |
|---|
| 482 | if [ "$X" = "n" -o "$X" = "N" ] |
|---|
| 483 | then |
|---|
| 484 | exit |
|---|
| 485 | fi |
|---|
| 486 | |
|---|
| 487 | if [ -n "$SYSFS" ] |
|---|
| 488 | then |
|---|
| 489 | MATCH=$PREFIX/'temp[1-9]_input' |
|---|
| 490 | else |
|---|
| 491 | MATCH=$PREFIX/'temp[1-9]' |
|---|
| 492 | fi |
|---|
| 493 | TEMPS=`echo $MATCH` |
|---|
| 494 | if [ "$MATCH" = "$TEMPS" ] |
|---|
| 495 | then |
|---|
| 496 | echo $0: 'There are no temperature-capable sensor modules installed' |
|---|
| 497 | exit 1 |
|---|
| 498 | fi |
|---|
| 499 | |
|---|
| 500 | function AskPath { |
|---|
| 501 | echo -n 'What should be the path to your fancontrol config file (/etc/fancontrol)? ' |
|---|
| 502 | |
|---|
| 503 | read X |
|---|
| 504 | if [ "$X" = "y" -o "$X" = "Y" -o "$X" = "" ] |
|---|
| 505 | then |
|---|
| 506 | X=/etc/fancontrol |
|---|
| 507 | fi |
|---|
| 508 | if [ -f $X ] |
|---|
| 509 | then |
|---|
| 510 | FCCONFIG=$X |
|---|
| 511 | else |
|---|
| 512 | echo -n "$X does not exist, shall I create it now (y)? " |
|---|
| 513 | read Y |
|---|
| 514 | if [ "$Y" = "y" -o "$Y" = "Y" -o "$Y" = "" ] |
|---|
| 515 | then |
|---|
| 516 | touch $X |
|---|
| 517 | chmod 0660 $X |
|---|
| 518 | chown root.root $X |
|---|
| 519 | FCCONFIG=$X |
|---|
| 520 | else |
|---|
| 521 | AskPath |
|---|
| 522 | fi |
|---|
| 523 | fi |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | AskPath |
|---|
| 527 | |
|---|
| 528 | function LoadConfig { |
|---|
| 529 | echo "Loading configuration from $1 ..." |
|---|
| 530 | INTERVAL=`egrep '^INTERVAL=.*$' $1 | sed -e 's/INTERVAL= *//g'` |
|---|
| 531 | FCTEMPS=`egrep '^FCTEMPS=.*$' $1 | sed -e 's/FCTEMPS= *//g'` |
|---|
| 532 | FCFANS=`egrep '^FCFANS=.*$' $1 | sed -e 's/FCFANS= *//g'` |
|---|
| 533 | MINTEMP=`egrep '^MINTEMP=.*$' $1 | sed -e 's/MINTEMP= *//g'` |
|---|
| 534 | MAXTEMP=`egrep '^MAXTEMP=.*$' $1 | sed -e 's/MAXTEMP= *//g'` |
|---|
| 535 | MINSTART=`egrep '^MINSTART=.*$' $1 | sed -e 's/MINSTART= *//g'` |
|---|
| 536 | MINSTOP=`egrep '^MINSTOP=.*$' $1 | sed -e 's/MINSTOP= *//g'` |
|---|
| 537 | MINPWM=`egrep '^MINPWM=.*$' $1 | sed -e 's/MINPWM= *//g'` |
|---|
| 538 | MAXPWM=`egrep '^MAXPWM=.*$' $1 | sed -e 's/MAXPWM= *//g'` |
|---|
| 539 | |
|---|
| 540 | # Check for configuration change |
|---|
| 541 | local item |
|---|
| 542 | for item in $FCFANS |
|---|
| 543 | do |
|---|
| 544 | if [ ! -f "`echo $item | sed -e 's/=.*$//'`" ] |
|---|
| 545 | then |
|---|
| 546 | echo "Configuration appears to be outdated, discarded" |
|---|
| 547 | FCTEMPS="" |
|---|
| 548 | FCFANS="" |
|---|
| 549 | MINTEMP="" |
|---|
| 550 | MAXTEMP="" |
|---|
| 551 | MINSTART="" |
|---|
| 552 | MINSTOP="" |
|---|
| 553 | MINPWM="" |
|---|
| 554 | MAXPWM="" |
|---|
| 555 | return 0 |
|---|
| 556 | fi |
|---|
| 557 | done |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | LoadConfig $FCCONFIG |
|---|
| 561 | |
|---|
| 562 | function TestMinStart { |
|---|
| 563 | echo |
|---|
| 564 | echo 'Now we increase the PWM value in 10-unit-steps.' |
|---|
| 565 | echo 'Let the fan stop completely, then press return until the' |
|---|
| 566 | echo "fan starts spinning. Then enter 'y'." |
|---|
| 567 | echo 'We will use this value +20 as the starting speed.' |
|---|
| 568 | let fanok=0 |
|---|
| 569 | let fanval=0 |
|---|
| 570 | |
|---|
| 571 | pwmenable $pwms |
|---|
| 572 | until [ "$fanok" = "1" ] |
|---|
| 573 | do |
|---|
| 574 | let fanval=fanval+10 |
|---|
| 575 | if [ $fanval -gt 240 ] ; then let fanval=$MAX ; let fanok=1 ; fi |
|---|
| 576 | echo -n "Setting $pwms to $fanval..." |
|---|
| 577 | pwmset $pwms $fanval |
|---|
| 578 | read FANTEST |
|---|
| 579 | if [ "$FANTEST" != "" ] ; then let fanok=1 ; fi |
|---|
| 580 | done |
|---|
| 581 | pwmdisable $pwms |
|---|
| 582 | |
|---|
| 583 | let fanval=fanval+20 |
|---|
| 584 | if [ $fanval -gt 240 ] ; then let fanval=$MAX ; fi |
|---|
| 585 | echo "OK, using $fanval" |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | function TestMinStop { |
|---|
| 589 | echo |
|---|
| 590 | echo 'Now we decrease the PWM value in 10-unit-steps.' |
|---|
| 591 | echo 'Let the fan reach full speed, then press return until the' |
|---|
| 592 | echo "fan stops spinning. Then enter 'y'." |
|---|
| 593 | echo 'We will use this value +20 as the minimum speed.' |
|---|
| 594 | let fanok=0 |
|---|
| 595 | let fanval=$MAX |
|---|
| 596 | |
|---|
| 597 | pwmenable $pwms |
|---|
| 598 | until [ "$fanok" = "1" ] |
|---|
| 599 | do |
|---|
| 600 | let fanval=fanval-10 |
|---|
| 601 | if [ $fanval -lt 0 ] ; then let fanval=0 ; let fanok=1 ; fi |
|---|
| 602 | echo -n "Setting $pwms to $fanval..." |
|---|
| 603 | pwmset $pwms $fanval |
|---|
| 604 | read FANTEST |
|---|
| 605 | if [ "$FANTEST" != "" ] ; then let fanok=1 ; fi |
|---|
| 606 | done |
|---|
| 607 | pwmdisable $pwms |
|---|
| 608 | |
|---|
| 609 | let fanval=fanval+20 |
|---|
| 610 | if [ $fanval -gt $MAX ] ; then let fanval=$MAX ; fi |
|---|
| 611 | echo "OK, using $fanval" |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | function SaveConfig { |
|---|
| 615 | echo |
|---|
| 616 | echo "Saving configuration to $FCCONFIG..." |
|---|
| 617 | tmpfile=`mktemp -t pwmcfg.XXXXXXXXXX` || { echo "$0: Cannot create temporary file" >&2; exit 1; } |
|---|
| 618 | trap " [ -f \"$tmpfile\" ] && /bin/rm -f -- \"$tmpfile\"" 0 1 2 3 13 15 |
|---|
| 619 | egrep -v '(INTERVAL|FCTEMPS|FCFANS|MAXTEMP|MINTEMP|MINSTART|MINSTOP|MINPWM|MAXPWM)' $FCCONFIG >$tmpfile |
|---|
| 620 | echo -e "INTERVAL=$INTERVAL\nFCTEMPS=$FCTEMPS\nFCFANS=$FCFANS\nMINTEMP=$MINTEMP\nMAXTEMP=$MAXTEMP\nMINSTART=$MINSTART\nMINSTOP=$MINSTOP" >>$tmpfile |
|---|
| 621 | [ -n "$MINPWM" ] && echo "MINPWM=$MINPWM" >>$tmpfile |
|---|
| 622 | [ -n "$MAXPWM" ] && echo "MAXPWM=$MAXPWM" >>$tmpfile |
|---|
| 623 | mv $tmpfile $FCCONFIG |
|---|
| 624 | #check if file was written correctly |
|---|
| 625 | echo 'Configuration saved' |
|---|
| 626 | } |
|---|
| 627 | |
|---|
| 628 | INTERVAL=10 |
|---|
| 629 | PS3='select (1-n): ' |
|---|
| 630 | DEFMINTEMP=0 |
|---|
| 631 | DEFMAXTEMP=60 |
|---|
| 632 | DEFMINSTART=150 |
|---|
| 633 | DEFMINSTOP=100 |
|---|
| 634 | |
|---|
| 635 | #the section below has a high potential for usability improvements |
|---|
| 636 | echo |
|---|
| 637 | echo 'Select fan output to configure, or other action:' |
|---|
| 638 | select pwms in $pwmactive "Change INTERVAL" "Just quit" "Save and quit" "Show configuration"; do |
|---|
| 639 | case $pwms in |
|---|
| 640 | "Change INTERVAL") |
|---|
| 641 | echo |
|---|
| 642 | echo "Current interval is $INTERVAL seconds." |
|---|
| 643 | echo -n "Enter the interval at which fancontrol should update PWM values (in s):" |
|---|
| 644 | read INTERVAL ;; #check user input here |
|---|
| 645 | "Just quit") |
|---|
| 646 | exit ;; |
|---|
| 647 | "Save and quit") |
|---|
| 648 | SaveConfig |
|---|
| 649 | exit ;; |
|---|
| 650 | "Show configuration") |
|---|
| 651 | echo |
|---|
| 652 | echo "Common Settings:" |
|---|
| 653 | echo "INTERVAL=$INTERVAL" |
|---|
| 654 | for pwmo in $pwmactive |
|---|
| 655 | do |
|---|
| 656 | echo |
|---|
| 657 | echo "Settings of ${pwmo}:" |
|---|
| 658 | echo " Depends on `echo $FCTEMPS |sed -e 's/ /\n/g' |egrep \"${pwmo}\" |sed -e 's/.*=//g'`" |
|---|
| 659 | echo " Controls `echo $FCFANS |sed -e 's/ /\n/g' |egrep \"${pwmo}\" |sed -e 's/.*=//g'`" |
|---|
| 660 | echo " MINTEMP=`echo $MINTEMP |sed -e \"s/ /\n/g\" |egrep \"${pwmo}\" |sed -e \"s/.*=//g\"`" |
|---|
| 661 | echo " MAXTEMP=`echo $MAXTEMP |sed -e \"s/ /\n/g\" |egrep \"${pwmo}\" |sed -e \"s/.*=//g\"`" |
|---|
| 662 | echo " MINSTART=`echo $MINSTART |sed -e \"s/ /\n/g\" |egrep \"${pwmo}\" |sed -e \"s/.*=//g\"`" |
|---|
| 663 | echo " MINSTOP=`echo $MINSTOP |sed -e \"s/ /\n/g\" |egrep \"${pwmo}\" |sed -e \"s/.*=//g\"`" |
|---|
| 664 | XMINP=`echo $MINPWM | sed -e "s/ /\n/g" | egrep "${pwmo}" | sed -e "s/.*=//g"` |
|---|
| 665 | [ -n "$XMINP" ] && echo " MINPWM=$XMINP" |
|---|
| 666 | XMAXP=`echo $MAXPWM | sed -e "s/ /\n/g" | egrep "${pwmo}" | sed -e "s/.*=//g"` |
|---|
| 667 | [ -n "$XMAXP" ] && echo " MAXPWM=$XMAXP" |
|---|
| 668 | done |
|---|
| 669 | echo ;; |
|---|
| 670 | |
|---|
| 671 | "`echo ${pwmactive} |sed -e 's/ /\n/g' | egrep \"${pwms}\"`" ) |
|---|
| 672 | pwmsed=`echo ${pwms} | sed -e 's/\//\\\\\//g'` #escape / for sed |
|---|
| 673 | echo |
|---|
| 674 | if [ -n "$SYSFS" ] |
|---|
| 675 | then |
|---|
| 676 | echo 'Devices:' |
|---|
| 677 | print_devices "" |
|---|
| 678 | echo |
|---|
| 679 | fi |
|---|
| 680 | echo 'Current temperature readings are as follows:' |
|---|
| 681 | for j in $TEMPS |
|---|
| 682 | do |
|---|
| 683 | # this will return the first field if there's only one (sysfs) |
|---|
| 684 | S=`cat $j | cut -d' ' -f3` |
|---|
| 685 | if [ -n "$SYSFS" ] |
|---|
| 686 | then |
|---|
| 687 | let S="$S / 1000" |
|---|
| 688 | fi |
|---|
| 689 | echo "$j $S" |
|---|
| 690 | done |
|---|
| 691 | FAN=`echo $fanactive|cut -d' ' -f$REPLY` |
|---|
| 692 | FCFANS="`echo $FCFANS | sed -e "s/${pwmsed}[^ ]* *//g\"` ${pwms}=$FAN" |
|---|
| 693 | echo |
|---|
| 694 | echo "Select a temperature sensor as source for ${pwms}:" |
|---|
| 695 | select tempss in $TEMPS "None (Do not affect this PWM output)"; do |
|---|
| 696 | if [ "$tempss" = "None (Do not affect this PWM output)" ] |
|---|
| 697 | then |
|---|
| 698 | |
|---|
| 699 | break; |
|---|
| 700 | else |
|---|
| 701 | if [ "$FCTEMPS" = "" ] |
|---|
| 702 | then |
|---|
| 703 | FCTEMPS="${pwms}=${tempss}" |
|---|
| 704 | else |
|---|
| 705 | FCTEMPS="`echo $FCTEMPS | sed -e "s/${pwmsed}[^ ]* *//g\"` ${pwms}=${tempss}" |
|---|
| 706 | fi |
|---|
| 707 | fi |
|---|
| 708 | echo |
|---|
| 709 | echo 'Enter the low temperature (degree C)' |
|---|
| 710 | echo -n "below which the fan should spin at minimum speed ($DEFMINTEMP): " |
|---|
| 711 | read XMT |
|---|
| 712 | if [ "$XMT" = "" ] |
|---|
| 713 | then |
|---|
| 714 | XMT=$DEFMINTEMP |
|---|
| 715 | fi |
|---|
| 716 | if [ "$MINTEMP" = "" ] |
|---|
| 717 | then |
|---|
| 718 | MINTEMP="${pwms}=${XMT}" |
|---|
| 719 | else |
|---|
| 720 | MINTEMP="`echo $MINTEMP | sed -e \"s/${pwmsed}[^ ]* *//g\"` ${pwms}=${XMT}" |
|---|
| 721 | fi |
|---|
| 722 | echo |
|---|
| 723 | echo 'Enter the high temperature (degree C)' |
|---|
| 724 | echo -n "over which the fan should spin at maximum speed ($DEFMAXTEMP): " |
|---|
| 725 | read XMT |
|---|
| 726 | if [ "$XMT" = "" ] |
|---|
| 727 | then |
|---|
| 728 | XMT=$DEFMAXTEMP |
|---|
| 729 | fi |
|---|
| 730 | if [ "$MAXTEMP" = "" ] |
|---|
| 731 | then |
|---|
| 732 | MAXTEMP="${pwms}=${XMT}" |
|---|
| 733 | else |
|---|
| 734 | MAXTEMP="`echo $MAXTEMP | sed -e \"s/${pwmsed}[^ ]* *//g\"` ${pwms}=${XMT}" |
|---|
| 735 | fi |
|---|
| 736 | echo |
|---|
| 737 | echo "Enter the minimum PWM value (0-$MAX)" |
|---|
| 738 | echo -n "at which the fan STARTS spinning (press t to test) ($DEFMINSTART): " |
|---|
| 739 | read XMV |
|---|
| 740 | if [ "$XMV" = "" ] |
|---|
| 741 | then |
|---|
| 742 | XMV=$DEFMINSTART |
|---|
| 743 | fi |
|---|
| 744 | if [ "$XMV" = "t" -o "$XMV" = "T" ] |
|---|
| 745 | then |
|---|
| 746 | TestMinStart |
|---|
| 747 | XMV=$fanval |
|---|
| 748 | fi |
|---|
| 749 | if [ "$MINSTART" = "" ] |
|---|
| 750 | then |
|---|
| 751 | MINSTART="${pwms}=${XMV}" |
|---|
| 752 | else |
|---|
| 753 | MINSTART="`echo $MINSTART | sed -e \"s/${pwmsed}[^ ]* *//g\"` ${pwms}=${XMV}" |
|---|
| 754 | fi |
|---|
| 755 | echo |
|---|
| 756 | echo "Enter the minimum PWM value (0-$MAX)" |
|---|
| 757 | echo -n "at which the fan STOPS spinning (press t to test) ($DEFMINSTOP): " |
|---|
| 758 | read XMV |
|---|
| 759 | if [ "$XMV" = "" ] |
|---|
| 760 | then |
|---|
| 761 | XMV=$DEFMINSTOP |
|---|
| 762 | fi |
|---|
| 763 | if [ "$XMV" = "t" -o "$XMV" = "T" ] |
|---|
| 764 | then |
|---|
| 765 | TestMinStop |
|---|
| 766 | XMV=$fanval |
|---|
| 767 | fi |
|---|
| 768 | if [ "$MINSTOP" = "" ] |
|---|
| 769 | then |
|---|
| 770 | MINSTOP="${pwms}=${XMV}" |
|---|
| 771 | else |
|---|
| 772 | MINSTOP="`echo $MINSTOP | sed -e \"s/${pwmsed}[^ ]* *//g\"` ${pwms}=${XMV}" |
|---|
| 773 | fi |
|---|
| 774 | echo |
|---|
| 775 | echo "Enter the PWM value (0-$XMV) to use when the temperature" |
|---|
| 776 | echo -n "is below the low temperature limit (0): " |
|---|
| 777 | read XMINP |
|---|
| 778 | if [ -n "$XMINP" ] |
|---|
| 779 | then |
|---|
| 780 | if [ "$MINPWM" = "" ] |
|---|
| 781 | then |
|---|
| 782 | MINPWM="${pwms}=${XMINP}" |
|---|
| 783 | else |
|---|
| 784 | MINPWM="`echo $MINPWM | sed -e \"s/${pwmsed}[^ ]* *//g\"` ${pwms}=${XMINP}" |
|---|
| 785 | fi |
|---|
| 786 | fi |
|---|
| 787 | echo |
|---|
| 788 | echo "Enter the PWM value ($XMV-$MAX) to use when the temperature" |
|---|
| 789 | echo -n "is over the high temperature limit ($MAX): " |
|---|
| 790 | read XMAXP |
|---|
| 791 | if [ -n "$XMAXP" ] |
|---|
| 792 | then |
|---|
| 793 | if [ "$MAXPWM" = "" ] |
|---|
| 794 | then |
|---|
| 795 | MAXPWM="${pwms}=${XMAXP}" |
|---|
| 796 | else |
|---|
| 797 | MAXPWM="`echo $MAXPWM | sed -e \"s/${pwmsed}[^ ]* *//g\"` ${pwms}=${XMAXP}" |
|---|
| 798 | fi |
|---|
| 799 | fi |
|---|
| 800 | echo |
|---|
| 801 | break; |
|---|
| 802 | done ;; |
|---|
| 803 | |
|---|
| 804 | *) |
|---|
| 805 | grep $pwm |
|---|
| 806 | |
|---|
| 807 | echo "No such option. Enter a number." ;; |
|---|
| 808 | esac |
|---|
| 809 | done |
|---|