| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# pwmconfig |
| 4 |
# Tests the pwm outputs of sensors and configures fancontrol |
| 5 |
# Supported Linux kernel versions: 2.6.5 and later |
| 6 |
# |
| 7 |
# Warning!!! This program will stop your fans, one at a time, |
| 8 |
# for approximately 5 seconds each!!! |
| 9 |
# This may cause your processor temperature to rise!!! |
| 10 |
# Verify that all fans are running at normal speed after this |
| 11 |
# program has exited!!! |
| 12 |
# |
| 13 |
# Copyright (C) 2007-2013 Jean Delvare <[email protected]> |
| 14 |
# |
| 15 |
# This program is free software; you can redistribute it and/or modify |
| 16 |
# it under the terms of the GNU General Public License as published by |
| 17 |
# the Free Software Foundation; either version 2 of the License, or |
| 18 |
# (at your option) any later version. |
| 19 |
# |
| 20 |
# This program is distributed in the hope that it will be useful, |
| 21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
# GNU General Public License for more details. |
| 24 |
# |
| 25 |
# You should have received a copy of the GNU General Public License |
| 26 |
# along with this program; if not, write to the Free Software |
| 27 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 28 |
# MA 02110-1301 USA. |
| 29 |
# |
| 30 |
# |
| 31 |
|
| 32 |
LM_VERSION='3.6.2' |
| 33 |
PIDFILE="/run/fancontrol.pid" |
| 34 |
USERDIR=$(pwd) |
| 35 |
|
| 36 |
if [ -f "$PIDFILE" ] |
| 37 |
then |
| 38 |
echo "File $PIDFILE exists. This typically means that the" |
| 39 |
echo "fancontrol daemon is running. You should stop it before running pwmconfig." |
| 40 |
echo "If you are certain that fancontrol is not running, then you can delete" |
| 41 |
echo "$PIDFILE manually." |
| 42 |
exit 1 |
| 43 |
fi |
| 44 |
|
| 45 |
if [ "$(id -u)" != "0" ] |
| 46 |
then |
| 47 |
echo "You need to be root to run this script." |
| 48 |
exit 1 |
| 49 |
fi |
| 50 |
|
| 51 |
echo "# pwmconfig version $LM_VERSION" |
| 52 |
echo 'This program will search your sensors for pulse width modulation (pwm)' |
| 53 |
echo 'controls, and test each one to see if it controls a fan on' |
| 54 |
echo 'your motherboard. Note that many motherboards do not have pwm' |
| 55 |
echo 'circuitry installed, even if your sensor chip supports pwm.' |
| 56 |
echo |
| 57 |
echo 'We will attempt to briefly stop each fan using the pwm controls.' |
| 58 |
echo 'The program will attempt to restore each fan to full speed' |
| 59 |
echo 'after testing. However, it is ** very important ** that you' |
| 60 |
echo 'physically verify that the fans have been to full speed' |
| 61 |
echo 'after the program has completed.' |
| 62 |
echo |
| 63 |
|
| 64 |
DELAY=5 # 3 seconds delay is too short for large fans, thus I increased it to 5 |
| 65 |
MAX=255 |
| 66 |
|
| 67 |
if [ -d "/sys/class/hwmon" ] |
| 68 |
then |
| 69 |
SYSFS=2 |
| 70 |
DIR="/sys/class/hwmon" |
| 71 |
PREFIX='hwmon*' |
| 72 |
elif [ -d "/sys/bus/i2c/devices" ] |
| 73 |
then |
| 74 |
SYSFS=1 |
| 75 |
DIR="/sys/bus/i2c/devices" |
| 76 |
PREFIX='*-*' |
| 77 |
else |
| 78 |
echo $0: 'No sensors found! (modprobe sensor modules?)' |
| 79 |
exit 1 |
| 80 |
fi |
| 81 |
|
| 82 |
cd $DIR |
| 83 |
DEVICES=$(echo $PREFIX) |
| 84 |
if [ "$PREFIX" = "$DEVICES" ] |
| 85 |
then |
| 86 |
echo $0: 'No sensors found! (modprobe sensor modules?)' |
| 87 |
exit 1 |
| 88 |
fi |
| 89 |
|
| 90 |
# We may need to adjust the device path |
| 91 |
if [ "$SYSFS" = "2" ] |
| 92 |
then |
| 93 |
OLD_DEVICES="$DEVICES" |
| 94 |
DEVICES="" |
| 95 |
|
| 96 |
for device in $OLD_DEVICES |
| 97 |
do |
| 98 |
if [ ! -r "$device/name" ] |
| 99 |
then |
| 100 |
device="$device/device" |
| 101 |
fi |
| 102 |
|
| 103 |
DEVICES="$DEVICES $device" |
| 104 |
done |
| 105 |
fi |
| 106 |
|
| 107 |
|
| 108 |
for device in $DEVICES |
| 109 |
do |
| 110 |
# Find available fan control outputs |
| 111 |
MATCH=$device/'pwm[1-9]' |
| 112 |
device_pwm=$(echo $MATCH) |
| 113 |
if [ "$SYSFS" = "1" -a "$MATCH" = "$device_pwm" ] |
| 114 |
then |
| 115 |
# Deprecated naming scheme (used in kernels 2.6.5 to 2.6.9) |
| 116 |
MATCH=$device/'fan[1-9]_pwm' |
| 117 |
device_pwm=$(echo $MATCH) |
| 118 |
fi |
| 119 |
if [ "$MATCH" != "$device_pwm" ] |
| 120 |
then |
| 121 |
PWM="$PWM $device_pwm" |
| 122 |
fi |
| 123 |
|
| 124 |
# Find available fan monitoring inputs |
| 125 |
MATCH=$device/'fan[1-9]_input' |
| 126 |
device_fan=$(echo $MATCH) |
| 127 |
if [ "$MATCH" != "$device_fan" ] |
| 128 |
then |
| 129 |
FAN="$FAN $device_fan" |
| 130 |
fi |
| 131 |
done |
| 132 |
|
| 133 |
if [ -z "$PWM" ] |
| 134 |
then |
| 135 |
echo $0: 'There are no pwm-capable sensor modules installed' |
| 136 |
exit 1 |
| 137 |
fi |
| 138 |
if [ -z "$FAN" ] |
| 139 |
then |
| 140 |
echo $0: 'There are no fan-capable sensor modules installed' |
| 141 |
exit 1 |
| 142 |
fi |
| 143 |
|
| 144 |
# $1 = padding |
| 145 |
function print_devices() |
| 146 |
{ |
| 147 |
local name device |
| 148 |
|
| 149 |
for device in $DEVICES |
| 150 |
do |
| 151 |
name=$(cat $device/name 2> /dev/null) |
| 152 |
[ -z "$name" ] && name="unknown (no name attribute)" |
| 153 |
echo "$1$device is $name" |
| 154 |
done |
| 155 |
} |
| 156 |
|
| 157 |
# $1 = pwm file name |
| 158 |
function is_pwm_auto() |
| 159 |
{ |
| 160 |
local ENABLE=${1}_enable |
| 161 |
|
| 162 |
if [ -f $ENABLE ] |
| 163 |
then |
| 164 |
if [ "$(cat $ENABLE)" -gt 1 ] |
| 165 |
then |
| 166 |
return 0 |
| 167 |
fi |
| 168 |
fi |
| 169 |
|
| 170 |
return 1 |
| 171 |
} |
| 172 |
|
| 173 |
# $1 = pwm file name |
| 174 |
function pwmdisable() |
| 175 |
{ |
| 176 |
local ENABLE=${1}_enable |
| 177 |
|
| 178 |
# No enable file? Just set to max |
| 179 |
if [ ! -f $ENABLE ] |
| 180 |
then |
| 181 |
echo $MAX > $1 |
| 182 |
return 0 |
| 183 |
fi |
| 184 |
|
| 185 |
# Try pwmN_enable=0 |
| 186 |
echo 0 2>/dev/null > $ENABLE |
| 187 |
if [ "$(cat $ENABLE)" -eq 0 ] |
| 188 |
then |
| 189 |
# Success |
| 190 |
return 0 |
| 191 |
fi |
| 192 |
|
| 193 |
# It didn't work, try pwmN_enable=1 pwmN=255 |
| 194 |
echo 1 2>/dev/null > $ENABLE |
| 195 |
if [ "$(cat $ENABLE)" -ne 1 ] |
| 196 |
then |
| 197 |
echo "$ENABLE stuck to $(cat $ENABLE)" >&2 |
| 198 |
return 1 |
| 199 |
fi |
| 200 |
|
| 201 |
echo $MAX > $1 |
| 202 |
if [ "$(cat $1)" -ge 190 ] |
| 203 |
then |
| 204 |
# Success |
| 205 |
return 0 |
| 206 |
fi |
| 207 |
|
| 208 |
# Nothing worked |
| 209 |
echo "$1 stuck to $(cat $1)" >&2 |
| 210 |
return 1 |
| 211 |
} |
| 212 |
|
| 213 |
# $1 = pwm file name |
| 214 |
function pwmenable() |
| 215 |
{ |
| 216 |
local ENABLE=${1}_enable |
| 217 |
|
| 218 |
echo 1 > $ENABLE 2>/dev/null |
| 219 |
if [ "$(cat $ENABLE)" -ne 1 ] |
| 220 |
then |
| 221 |
return 1 |
| 222 |
fi |
| 223 |
|
| 224 |
echo $MAX > $1 |
| 225 |
} |
| 226 |
|
| 227 |
# $1 = pwm file name; $2 = pwm value 0-255 |
| 228 |
function pwmset() |
| 229 |
{ |
| 230 |
echo $2 > $1 |
| 231 |
} |
| 232 |
|
| 233 |
echo 'Found the following devices:' |
| 234 |
print_devices " " |
| 235 |
echo |
| 236 |
|
| 237 |
echo 'Found the following PWM controls:' |
| 238 |
for i in $PWM |
| 239 |
do |
| 240 |
P=$(cat $i) |
| 241 |
echo " $i current value: $P" |
| 242 |
if [ -w $i ] |
| 243 |
then |
| 244 |
# First check if PWM output is in automatic mode |
| 245 |
if is_pwm_auto $i |
| 246 |
then |
| 247 |
echo "$i is currently setup for automatic speed control." |
| 248 |
echo 'In general, automatic mode is preferred over manual mode, as' |
| 249 |
echo 'it is more efficient and it reacts faster. Are you sure that' |
| 250 |
echo -n 'you want to setup this output for manual control? (n) ' |
| 251 |
read X |
| 252 |
if [ "$X" = "" -o "$X" != "y" -a "$X" != "Y" ] |
| 253 |
then |
| 254 |
continue |
| 255 |
fi |
| 256 |
fi |
| 257 |
|
| 258 |
pwmdisable $i |
| 259 |
if [ $? -ne 0 ] |
| 260 |
then |
| 261 |
echo "Manual control mode not supported, skipping $i." |
| 262 |
elif [ "$GOODPWM" = "" ] |
| 263 |
then |
| 264 |
GOODPWM=$i |
| 265 |
else |
| 266 |
GOODPWM="$GOODPWM $i" |
| 267 |
fi |
| 268 |
else |
| 269 |
echo "Can't write to $i, skipping." |
| 270 |
fi |
| 271 |
done |
| 272 |
|
| 273 |
if [ "$GOODPWM" = "" ] |
| 274 |
then |
| 275 |
echo 'There are no usable PWM outputs.' |
| 276 |
exit 1 |
| 277 |
fi |
| 278 |
|
| 279 |
echo |
| 280 |
echo "Giving the fans some time to reach full speed..." |
| 281 |
sleep $DELAY |
| 282 |
echo 'Found the following fan sensors:' |
| 283 |
for i in $FAN |
| 284 |
do |
| 285 |
S=$(cat $i) |
| 286 |
if [ "$S" = "0" -o "$S" = "-1" -o "$S" = "" ] |
| 287 |
then |
| 288 |
echo " $i current speed: 0 ... skipping!" |
| 289 |
else |
| 290 |
echo " $i current speed: $S RPM" |
| 291 |
if [ "$GOODFAN" = "" ] |
| 292 |
then |
| 293 |
GOODFAN=$i |
| 294 |
SPEEDS=$S |
| 295 |
else |
| 296 |
GOODFAN="$GOODFAN $i" |
| 297 |
SPEEDS="$SPEEDS $S" |
| 298 |
fi |
| 299 |
fi |
| 300 |
done |
| 301 |
echo |
| 302 |
|
| 303 |
if [ "$GOODFAN" = "" ] |
| 304 |
then |
| 305 |
echo 'There are no working fan sensors, all readings are 0.' |
| 306 |
echo 'Make sure you have a 3-wire fan connected.' |
| 307 |
echo 'You may also need to increase the fan divisors.' |
| 308 |
echo 'See doc/fan-divisors for more information.' |
| 309 |
exit 1 |
| 310 |
fi |
| 311 |
|
| 312 |
echo 'Warning!!! This program will stop your fans, one at a time,' |
| 313 |
echo "for approximately $DELAY seconds each!!!" |
| 314 |
echo 'This may cause your processor temperature to rise!!!' |
| 315 |
echo 'If you do not want to do this hit control-C now!!!' |
| 316 |
echo -n 'Hit return to continue: ' |
| 317 |
read X |
| 318 |
echo |
| 319 |
|
| 320 |
PLOTTER=gnuplot |
| 321 |
STEP=15 |
| 322 |
PDELAY=2 |
| 323 |
# Use a smaller step for low PWM values as this is typically where the |
| 324 |
# more important fan speed changes are happening. |
| 325 |
STEP2=2 |
| 326 |
STEP2_BELOW=31 |
| 327 |
|
| 328 |
function pwmdetail() |
| 329 |
{ |
| 330 |
local P=$1 F=$2 |
| 331 |
local X PLOT= |
| 332 |
local TMP1 TMP2 |
| 333 |
local threshold=100000 pwm S |
| 334 |
|
| 335 |
type $PLOTTER > /dev/null 2>&1 |
| 336 |
if [ $? -eq 0 ] |
| 337 |
then |
| 338 |
echo -n "Would you like to generate a graphical plot using $PLOTTER (y)? " |
| 339 |
read X |
| 340 |
if [ "$X" = "y" -o "$X" = "Y" -o "$X" = "" ] |
| 341 |
then |
| 342 |
PLOT=y |
| 343 |
fi |
| 344 |
else |
| 345 |
if [ -n "$DISPLAY" ] |
| 346 |
then |
| 347 |
echo "Note: If you had $PLOTTER installed, I could generate a graphical plot." |
| 348 |
fi |
| 349 |
fi |
| 350 |
|
| 351 |
if [ "$PLOT" = "y" ] |
| 352 |
then |
| 353 |
TMP1=$(mktemp -t pwmtest1.XXXXXXXXXX) || { echo "$0: Cannot create temporary file" >&2; exit 1; } |
| 354 |
TMP2=$(mktemp -t pwmtest2.XXXXXXXXXX) || { rm -f $TMP1 ; echo "$0: Cannot create temporary file" >&2; exit 1; } |
| 355 |
echo "set xlabel \"PWM: $P\"" > $TMP1 |
| 356 |
echo "set ylabel \"FAN: $F (RPM)\"" >> $TMP1 |
| 357 |
echo 'set nokey' >> $TMP1 |
| 358 |
echo 'set xrange [0:255]' >> $TMP1 |
| 359 |
echo "plot \"$TMP2\" with lines" >> $TMP1 |
| 360 |
echo 'pause -1 " Hit return to continue..."' >> $TMP1 |
| 361 |
> $TMP2 |
| 362 |
fi |
| 363 |
|
| 364 |
let pwm=$MAX |
| 365 |
pwmenable $P |
| 366 |
while [ $pwm -ge 0 ] |
| 367 |
do |
| 368 |
pwmset $P $pwm |
| 369 |
sleep $PDELAY |
| 370 |
if [ $? -ne 0 ] |
| 371 |
then |
| 372 |
pwmdisable $P |
| 373 |
echo '^C received, aborting...' |
| 374 |
rm -f $TMP1 $TMP2 |
| 375 |
exit 1 |
| 376 |
fi |
| 377 |
S=$(cat $F) |
| 378 |
# Fan speed should never increase significantly |
| 379 |
if [ $S -gt $threshold ] |
| 380 |
then |
| 381 |
echo " PWM $pwm FAN $S (probably incorrect)" |
| 382 |
else |
| 383 |
echo " PWM $pwm FAN $S" |
| 384 |
let threshold=S*6/5 |
| 385 |
fi |
| 386 |
|
| 387 |
if [ "$PLOT" = "y" ] |
| 388 |
then |
| 389 |
echo "$pwm $S" >> $TMP2 |
| 390 |
fi |
| 391 |
if [ "$S" = "0" -o "$S" = "-1" ] |
| 392 |
then |
| 393 |
pwmdisable $P |
| 394 |
echo " Fan Stopped at PWM = $pwm" |
| 395 |
if [ $pwm -eq $MAX ] |
| 396 |
then |
| 397 |
echo " This fan appears to stop when the PWM is enabled;" |
| 398 |
echo " perhaps the fan input shares a pin with the PWM output" |
| 399 |
echo " on the sensor chip." |
| 400 |
echo " You cannot control this fan with this PWM output." |
| 401 |
rm -f $TMP1 $TMP2 |
| 402 |
echo |
| 403 |
return 0 |
| 404 |
fi |
| 405 |
break |
| 406 |
fi |
| 407 |
if [ $pwm -lt $STEP2_BELOW ] |
| 408 |
then |
| 409 |
let pwm=$pwm-$STEP2 |
| 410 |
else |
| 411 |
let pwm=$pwm-$STEP |
| 412 |
fi |
| 413 |
done |
| 414 |
pwmdisable $P |
| 415 |
if [ "$PLOT" = "y" ] |
| 416 |
then |
| 417 |
$PLOTTER $TMP1 |
| 418 |
rm -f $TMP1 $TMP2 |
| 419 |
fi |
| 420 |
echo |
| 421 |
} |
| 422 |
|
| 423 |
for i in $GOODPWM |
| 424 |
do |
| 425 |
echo Testing pwm control $i ... |
| 426 |
pwmenable $i |
| 427 |
if [ $? -ne 0 ] |
| 428 |
then |
| 429 |
echo "Manual control mode not supported, skipping." |
| 430 |
continue |
| 431 |
fi |
| 432 |
pwmset $i 0 |
| 433 |
sleep $DELAY |
| 434 |
if [ $? -ne 0 ] |
| 435 |
then |
| 436 |
pwmdisable $i |
| 437 |
echo '^C received, restoring PWM and aborting...' |
| 438 |
exit 1 |
| 439 |
fi |
| 440 |
|
| 441 |
# Sample all current fan speeds at once, so that we can quickly |
| 442 |
# disable PWM and return all fans to full speed |
| 443 |
CURRENT_SPEEDS="$(cat $GOODFAN)" |
| 444 |
pwmdisable $i |
| 445 |
|
| 446 |
let pwmactivecount=0 |
| 447 |
let count=1 |
| 448 |
S_MIN= |
| 449 |
for j in $GOODFAN |
| 450 |
do |
| 451 |
OS=$(echo $SPEEDS | cut -d' ' -f$count) |
| 452 |
S=$(echo $CURRENT_SPEEDS | cut -d' ' -f$count) |
| 453 |
echo " $j ... speed was $OS now $S" |
| 454 |
let threshold=5*$OS/6 |
| 455 |
if [ $S -lt $threshold ] |
| 456 |
then |
| 457 |
echo " It appears that fan $j" |
| 458 |
echo " is controlled by pwm $i" |
| 459 |
# |
| 460 |
# a PWM can control more than one fan.... |
| 461 |
# |
| 462 |
if [ $pwmactivecount -eq 0 ] |
| 463 |
then |
| 464 |
let pwmactivecount=1 |
| 465 |
pwmactive="$i ${pwmactive}" |
| 466 |
fanactive="$j ${fanactive}" |
| 467 |
|
| 468 |
# Give all correlated fans time to return to full speed |
| 469 |
sleep $DELAY |
| 470 |
if [ $? -ne 0 ] |
| 471 |
then |
| 472 |
echo '^C received, aborting...' |
| 473 |
exit 1 |
| 474 |
fi |
| 475 |
else |
| 476 |
fanactive="$j+${fanactive}" |
| 477 |
fi |
| 478 |
|
| 479 |
# Keep track of the slowest fan controlled by one PWM |
| 480 |
if [ -z "$S_MIN" ] || [ $S -lt $S_MIN ] |
| 481 |
then |
| 482 |
S_MIN=$S |
| 483 |
fi |
| 484 |
|
| 485 |
S=$(cat $j) |
| 486 |
if [ $S -lt $threshold ] |
| 487 |
then |
| 488 |
echo " Fan $j has not returned to speed, please investigate!" |
| 489 |
else |
| 490 |
echo -n "Would you like to generate a detailed correlation (y)? " |
| 491 |
read X |
| 492 |
if [ "$X" = "y" -o "$X" = "Y" -o "$X" = "" ] |
| 493 |
then |
| 494 |
pwmdetail $i $j |
| 495 |
fi |
| 496 |
fi |
| 497 |
else |
| 498 |
echo " no correlation" |
| 499 |
fi |
| 500 |
let count=count+1 |
| 501 |
done |
| 502 |
echo |
| 503 |
if [ "$pwmactivecount" = "0" ] |
| 504 |
then |
| 505 |
echo "No correlations were detected." |
| 506 |
echo "There is either no fan connected to the output of $i," |
| 507 |
echo "or the connected fan has no rpm-signal connected to one of" |
| 508 |
echo "the tested fan sensors. (Note: not all motherboards have" |
| 509 |
echo "the pwm outputs connected to the fan connectors," |
| 510 |
echo "check out the hardware database on http://www.almico.com/forumindex.php)" |
| 511 |
echo |
| 512 |
echo -n "Did you see/hear a fan stopping during the above test (n)? " |
| 513 |
read X |
| 514 |
if [ "$X" = "y" -o "$X" = "Y" ] |
| 515 |
then |
| 516 |
pwmactive="$i ${pwmactive}" |
| 517 |
fanactive_min="0 $fanactive_min" |
| 518 |
fi |
| 519 |
echo |
| 520 |
else |
| 521 |
fanactive_min="$S_MIN $fanactive_min" |
| 522 |
fi |
| 523 |
done |
| 524 |
|
| 525 |
|
| 526 |
echo 'Testing is complete.' |
| 527 |
echo 'Please verify that all fans have returned to their normal speed.' |
| 528 |
echo |
| 529 |
echo 'The fancontrol script can automatically respond to temperature changes' |
| 530 |
echo 'of your system by changing fanspeeds.' |
| 531 |
echo -n 'Do you want to set up its configuration file now (y)? ' |
| 532 |
|
| 533 |
read X |
| 534 |
if [ "$X" = "n" -o "$X" = "N" ] |
| 535 |
then |
| 536 |
exit |
| 537 |
fi |
| 538 |
|
| 539 |
for device in $DEVICES |
| 540 |
do |
| 541 |
# Find available temperature monitoring inputs |
| 542 |
MATCH=$device/'temp[1-9]_input' |
| 543 |
device_temp=$(echo $MATCH) |
| 544 |
if [ "$MATCH" != "$device_temp" ] |
| 545 |
then |
| 546 |
TEMPS="$TEMPS $device_temp" |
| 547 |
fi |
| 548 |
done |
| 549 |
|
| 550 |
if [ -z "$TEMPS" ] |
| 551 |
then |
| 552 |
echo $0: 'There are no temperature-capable sensor modules installed' |
| 553 |
exit 1 |
| 554 |
fi |
| 555 |
|
| 556 |
function DevicePath() |
| 557 |
{ |
| 558 |
if [ -h "$1/device" ] |
| 559 |
then |
| 560 |
readlink -f "$1/device" | sed -e 's/^\/sys\///' |
| 561 |
fi |
| 562 |
} |
| 563 |
|
| 564 |
function DeviceName() |
| 565 |
{ |
| 566 |
if [ -r "$1/name" ] |
| 567 |
then |
| 568 |
cat "$1/name" | sed -e 's/[[:space:]=]/_/g' |
| 569 |
elif [ -r "$1/device/name" ] |
| 570 |
then |
| 571 |
cat "$1/device/name" | sed -e 's/[[:space:]=]/_/g' |
| 572 |
fi |
| 573 |
} |
| 574 |
|
| 575 |
function ValidateDevices() |
| 576 |
{ |
| 577 |
local OLD_DEVPATH="$1" OLD_DEVNAME="$2" outdated=0 |
| 578 |
local entry device name path |
| 579 |
|
| 580 |
for entry in $OLD_DEVPATH |
| 581 |
do |
| 582 |
device=$(echo "$entry" | sed -e 's/=[^=]*$//') |
| 583 |
path=$(echo "$entry" | sed -e 's/^[^=]*=//') |
| 584 |
|
| 585 |
if [ "$(DevicePath "$device")" != "$path" ] |
| 586 |
then |
| 587 |
echo "Device path of $device has changed" |
| 588 |
outdated=1 |
| 589 |
fi |
| 590 |
done |
| 591 |
|
| 592 |
for entry in $OLD_DEVNAME |
| 593 |
do |
| 594 |
device=$(echo "$entry" | sed -e 's/=[^=]*$//') |
| 595 |
name=$(echo "$entry" | sed -e 's/^[^=]*=//') |
| 596 |
|
| 597 |
if [ "$(DeviceName "$device")" != "$name" ] |
| 598 |
then |
| 599 |
echo "Device name of $device has changed" |
| 600 |
outdated=1 |
| 601 |
fi |
| 602 |
done |
| 603 |
|
| 604 |
return $outdated |
| 605 |
} |
| 606 |
|
| 607 |
function AskPath() |
| 608 |
{ |
| 609 |
echo -n 'What should be the path to your fancontrol config file (/etc/fancontrol)? ' |
| 610 |
|
| 611 |
read FCCONFIG |
| 612 |
if [ "$FCCONFIG" = "" ] |
| 613 |
then |
| 614 |
FCCONFIG="/etc/fancontrol" |
| 615 |
fi |
| 616 |
if [ ${FCCONFIG::1} != "/" ] |
| 617 |
then |
| 618 |
FCCONFIG=$(readlink -f "$USERDIR"/"$FCCONFIG") |
| 619 |
fi |
| 620 |
} |
| 621 |
|
| 622 |
AskPath |
| 623 |
|
| 624 |
function ClearConfig() |
| 625 |
{ |
| 626 |
FCTEMPS="" |
| 627 |
FCFANS="" |
| 628 |
MINTEMP="" |
| 629 |
MAXTEMP="" |
| 630 |
MINSTART="" |
| 631 |
MINSTOP="" |
| 632 |
MINPWM="" |
| 633 |
MAXPWM="" |
| 634 |
} |
| 635 |
|
| 636 |
function LoadConfig() |
| 637 |
{ |
| 638 |
local OLD_DEVPATH OLD_DEVNAME |
| 639 |
|
| 640 |
# Nothing to do |
| 641 |
if [ ! -f "$1" ] |
| 642 |
then |
| 643 |
ClearConfig |
| 644 |
return 0 |
| 645 |
fi |
| 646 |
|
| 647 |
echo "Loading configuration from $1 ..." |
| 648 |
INTERVAL=$(grep -E '^INTERVAL=.*$' $1 | sed -e 's/INTERVAL= *//g') |
| 649 |
OLD_DEVPATH=$(grep -E '^DEVPATH=.*$' $1 | sed -e 's/DEVPATH= *//g') |
| 650 |
OLD_DEVNAME=$(grep -E '^DEVNAME=.*$' $1 | sed -e 's/DEVNAME= *//g') |
| 651 |
FCTEMPS=$(grep -E '^FCTEMPS=.*$' $1 | sed -e 's/FCTEMPS= *//g') |
| 652 |
FCFANS=$(grep -E '^FCFANS=.*$' $1 | sed -e 's/FCFANS= *//g') |
| 653 |
MINTEMP=$(grep -E '^MINTEMP=.*$' $1 | sed -e 's/MINTEMP= *//g') |
| 654 |
MAXTEMP=$(grep -E '^MAXTEMP=.*$' $1 | sed -e 's/MAXTEMP= *//g') |
| 655 |
MINSTART=$(grep -E '^MINSTART=.*$' $1 | sed -e 's/MINSTART= *//g') |
| 656 |
MINSTOP=$(grep -E '^MINSTOP=.*$' $1 | sed -e 's/MINSTOP= *//g') |
| 657 |
MINPWM=$(grep -E '^MINPWM=.*$' $1 | sed -e 's/MINPWM= *//g') |
| 658 |
MAXPWM=$(grep -E '^MAXPWM=.*$' $1 | sed -e 's/MAXPWM= *//g') |
| 659 |
|
| 660 |
# Check for configuration change |
| 661 |
if ! ValidateDevices "$OLD_DEVPATH" "$OLD_DEVNAME" |
| 662 |
then |
| 663 |
echo "Configuration appears to be outdated, discarded" |
| 664 |
ClearConfig |
| 665 |
return 0 |
| 666 |
fi |
| 667 |
} |
| 668 |
|
| 669 |
LoadConfig "$FCCONFIG" |
| 670 |
|
| 671 |
# $1 = pwm value below which the fan is stopped |
| 672 |
function TestMinStart() |
| 673 |
{ |
| 674 |
local fanok FANTEST |
| 675 |
|
| 676 |
echo |
| 677 |
echo 'Now we increase the PWM value in 10-unit-steps.' |
| 678 |
echo 'Let the fan stop completely, then press return until the' |
| 679 |
echo "fan starts spinning. Then enter 'y'." |
| 680 |
echo 'We will use this value +20 as the starting speed.' |
| 681 |
let fanok=0 |
| 682 |
let fanval="$1" |
| 683 |
|
| 684 |
pwmenable $pwms |
| 685 |
until [ "$fanok" = "1" ] |
| 686 |
do |
| 687 |
if [ $fanval -gt 240 ] ; then let fanval=$MAX ; let fanok=1 ; fi |
| 688 |
echo -n "Setting $pwms to $fanval..." |
| 689 |
pwmset $pwms $fanval |
| 690 |
read FANTEST |
| 691 |
if [ "$FANTEST" != "" ] ; then let fanok=1 ; fi |
| 692 |
let fanval=fanval+10 |
| 693 |
done |
| 694 |
pwmdisable $pwms |
| 695 |
|
| 696 |
let fanval=fanval+20 |
| 697 |
if [ $fanval -gt 240 ] ; then let fanval=$MAX ; fi |
| 698 |
echo "OK, using $fanval" |
| 699 |
} |
| 700 |
|
| 701 |
# $1 = fan input to read the fan speed from |
| 702 |
function TestMinStop() |
| 703 |
{ |
| 704 |
local faninput=$1 |
| 705 |
local threshold=100000 |
| 706 |
local fanspeed |
| 707 |
|
| 708 |
echo |
| 709 |
echo 'Now we decrease the PWM value to figure out the lowest usable value.' |
| 710 |
echo 'We will use a slightly greater value as the minimum speed.' |
| 711 |
let fanval=$MAX |
| 712 |
|
| 713 |
pwmenable $pwms |
| 714 |
while [ $fanval -ge 0 ] |
| 715 |
do |
| 716 |
pwmset $pwms $fanval |
| 717 |
sleep $PDELAY |
| 718 |
fanspeed=$(cat $faninput) |
| 719 |
if [ $fanspeed -gt $threshold ] |
| 720 |
then |
| 721 |
echo " PWM $fanval -> $fanspeed RPM (probably incorrect)" |
| 722 |
break |
| 723 |
else |
| 724 |
echo " PWM $fanval -> $fanspeed RPM" |
| 725 |
if [ $fanspeed = "0" -o $fanspeed = "-1" ] |
| 726 |
then |
| 727 |
break |
| 728 |
fi |
| 729 |
let threshold=fanspeed*6/5 |
| 730 |
fi |
| 731 |
if [ $fanval -lt $STEP2_BELOW ] |
| 732 |
then |
| 733 |
let fanval=$fanval-$STEP2 |
| 734 |
else |
| 735 |
let fanval=$fanval-$STEP |
| 736 |
fi |
| 737 |
done |
| 738 |
pwmdisable $pwms |
| 739 |
|
| 740 |
if [ $fanval -lt $STEP2_BELOW ] |
| 741 |
then |
| 742 |
let 'fanval=fanval+2*STEP2' |
| 743 |
else |
| 744 |
let 'fanval=fanval+STEP' |
| 745 |
fi |
| 746 |
echo "OK, using $fanval" |
| 747 |
} |
| 748 |
|
| 749 |
# Remember the path and name of each device with at least one |
| 750 |
# reference (pwm, temp or fan) in the configuration file. |
| 751 |
# This function sets globals DEVPATH and DEVNAME as a side effect. |
| 752 |
function RememberDevices() |
| 753 |
{ |
| 754 |
local used entry device name path tempfandev pwmdev |
| 755 |
DEVPATH="" |
| 756 |
DEVNAME="" |
| 757 |
|
| 758 |
for device in $DEVICES |
| 759 |
do |
| 760 |
device=$(echo "$device" | sed -e 's/\/.*$//') |
| 761 |
|
| 762 |
used=0 |
| 763 |
for entry in $1 $2 |
| 764 |
do |
| 765 |
pwmdev=$(echo "$entry" | sed -e 's/\/.*$//') |
| 766 |
tempfandev=$(echo "$entry" | sed -e 's/^[^=]*=//' -e 's/\/.*$//') |
| 767 |
|
| 768 |
if [ "$device" = "$pwmdev" -o "$device" = "$tempfandev" ] |
| 769 |
then |
| 770 |
used=1 |
| 771 |
fi |
| 772 |
done |
| 773 |
if [ "$used" -eq 0 ] |
| 774 |
then |
| 775 |
continue |
| 776 |
fi |
| 777 |
|
| 778 |
# Record the device path and name. This lets the fancontrol |
| 779 |
# script check that they didn't change. If they did, then the |
| 780 |
# configuration file can no longer be trusted. |
| 781 |
path=$(DevicePath "$device") |
| 782 |
if [ -z "$DEVPATH" ] |
| 783 |
then |
| 784 |
DEVPATH="$device=$path" |
| 785 |
else |
| 786 |
DEVPATH="$DEVPATH $device=$path" |
| 787 |
fi |
| 788 |
|
| 789 |
name=$(DeviceName "$device") |
| 790 |
if [ -z "$DEVNAME" ] |
| 791 |
then |
| 792 |
DEVNAME="$device=$name" |
| 793 |
else |
| 794 |
DEVNAME="$DEVNAME $device=$name" |
| 795 |
fi |
| 796 |
done |
| 797 |
} |
| 798 |
|
| 799 |
function SaveConfig() |
| 800 |
{ |
| 801 |
local tmpfile |
| 802 |
|
| 803 |
RememberDevices "$FCTEMPS" "$FCFANS" |
| 804 |
|
| 805 |
echo |
| 806 |
echo "Saving configuration to $FCCONFIG..." |
| 807 |
tmpfile=$(mktemp -t pwmcfg.XXXXXXXXXX) || { echo "$0: Cannot create temporary file" >&2; exit 1; } |
| 808 |
trap " [ -f \"$tmpfile\" ] && /bin/rm -f -- \"$tmpfile\"" 0 1 2 3 13 15 |
| 809 |
echo "# Configuration file generated by pwmconfig, changes will be lost" >$tmpfile |
| 810 |
echo "INTERVAL=$INTERVAL" >>$tmpfile |
| 811 |
echo "DEVPATH=$DEVPATH" >>$tmpfile |
| 812 |
echo "DEVNAME=$DEVNAME" >>$tmpfile |
| 813 |
echo "FCTEMPS=$FCTEMPS" >>$tmpfile |
| 814 |
echo "FCFANS=$FCFANS" >>$tmpfile |
| 815 |
echo "MINTEMP=$MINTEMP" >>$tmpfile |
| 816 |
echo "MAXTEMP=$MAXTEMP" >>$tmpfile |
| 817 |
echo "MINSTART=$MINSTART" >>$tmpfile |
| 818 |
echo "MINSTOP=$MINSTOP" >>$tmpfile |
| 819 |
[ -n "$MINPWM" ] && echo "MINPWM=$MINPWM" >>$tmpfile |
| 820 |
[ -n "$MAXPWM" ] && echo "MAXPWM=$MAXPWM" >>$tmpfile |
| 821 |
mv $tmpfile "$FCCONFIG" |
| 822 |
chmod +r "$FCCONFIG" |
| 823 |
#check if file was written correctly |
| 824 |
echo 'Configuration saved' |
| 825 |
} |
| 826 |
|
| 827 |
INTERVAL=10 |
| 828 |
PS3='select (1-n): ' |
| 829 |
DEFMINTEMP=20 |
| 830 |
DEFMAXTEMP=60 |
| 831 |
DEFMINSTART=150 |
| 832 |
DEFMINSTOP=100 |
| 833 |
|
| 834 |
function filter_cfgvar() |
| 835 |
{ |
| 836 |
echo "$1" | sed -e 's/ /\n/g' \ |
| 837 |
| grep -E "$2" \ |
| 838 |
| sed -e 's/.*=//g' |
| 839 |
} |
| 840 |
|
| 841 |
# "select" won't repeat the list of options, so we enclose it in a |
| 842 |
# never-ending loop so that it starts over again with each iteration. |
| 843 |
# I admit it's not exactly nice, but I do not have a better idea to |
| 844 |
# keep usability at an acceptable level. |
| 845 |
while [ 1 ] ; do |
| 846 |
echo |
| 847 |
echo 'Select fan output to configure, or other action:' |
| 848 |
select pwms in $pwmactive "Change INTERVAL" "Just quit" "Save and quit" "Show configuration"; do |
| 849 |
case $pwms in |
| 850 |
"Change INTERVAL") |
| 851 |
echo |
| 852 |
echo "Current interval is $INTERVAL seconds." |
| 853 |
echo -n "Enter the interval at which fancontrol should update PWM values (in s): " |
| 854 |
read INTERVAL #check user input here |
| 855 |
break ;; |
| 856 |
"Just quit") |
| 857 |
exit ;; |
| 858 |
"Save and quit") |
| 859 |
SaveConfig |
| 860 |
exit ;; |
| 861 |
"Show configuration") |
| 862 |
echo |
| 863 |
echo "Common Settings:" |
| 864 |
echo "INTERVAL=$INTERVAL" |
| 865 |
for pwmo in $pwmactive |
| 866 |
do |
| 867 |
echo |
| 868 |
echo "Settings of ${pwmo}:" |
| 869 |
echo " Depends on $(filter_cfgvar "$FCTEMPS" "$pwmo")" |
| 870 |
echo " Controls $(filter_cfgvar "$FCFANS" "$pwmo")" |
| 871 |
echo " MINTEMP=$(filter_cfgvar "$MINTEMP" $pwmo)" |
| 872 |
echo " MAXTEMP=$(filter_cfgvar "$MAXTEMP" "$pwmo")" |
| 873 |
echo " MINSTART=$(filter_cfgvar "$MINSTART" "$pwmo")" |
| 874 |
echo " MINSTOP=$(filter_cfgvar "$MINSTOP" "$pwmo")" |
| 875 |
XMINP=$(filter_cfgvar "$MINPWM" "$pwmo") |
| 876 |
[ -n "$XMINP" ] && echo " MINPWM=$XMINP" |
| 877 |
XMAXP=$(filter_cfgvar "$MAXPWM" "$pwmo") |
| 878 |
[ -n "$XMAXP" ] && echo " MAXPWM=$XMAXP" |
| 879 |
done |
| 880 |
echo |
| 881 |
break ;; |
| 882 |
|
| 883 |
"$(echo ${pwmactive} |sed -e 's/ /\n/g' | grep -E "${pwms}")" ) |
| 884 |
pwmsed=$(echo ${pwms} | sed -e 's/\//\\\//g') #escape / for sed |
| 885 |
echo |
| 886 |
|
| 887 |
echo 'Devices:' |
| 888 |
print_devices "" |
| 889 |
echo |
| 890 |
|
| 891 |
echo 'Current temperature readings are as follows:' |
| 892 |
for j in $TEMPS |
| 893 |
do |
| 894 |
S=$(cat $j) |
| 895 |
let S="$S / 1000" |
| 896 |
echo "$j $S" |
| 897 |
done |
| 898 |
FAN=$(echo $fanactive|cut -d' ' -f$REPLY) |
| 899 |
FAN_MIN=$(echo $fanactive_min|cut -d' ' -f$REPLY) |
| 900 |
FCFANS="$(echo $FCFANS | sed -e "s/${pwmsed}[^ ]* *//g") ${pwms}=$FAN" |
| 901 |
echo |
| 902 |
echo "Select a temperature sensor as source for ${pwms}:" |
| 903 |
select tempss in $TEMPS "None (Do not affect this PWM output)"; do |
| 904 |
if [ "$tempss" = "None (Do not affect this PWM output)" ] |
| 905 |
then |
| 906 |
|
| 907 |
break; |
| 908 |
else |
| 909 |
if [ "$FCTEMPS" = "" ] |
| 910 |
then |
| 911 |
FCTEMPS="${pwms}=${tempss}" |
| 912 |
else |
| 913 |
FCTEMPS="$(echo $FCTEMPS | sed -e "s/${pwmsed}[^ ]* *//g") ${pwms}=${tempss}" |
| 914 |
fi |
| 915 |
fi |
| 916 |
echo |
| 917 |
echo 'Enter the low temperature (degree C)' |
| 918 |
echo -n "below which the fan should spin at minimum speed ($DEFMINTEMP): " |
| 919 |
read XMT |
| 920 |
if [ "$XMT" = "" ] |
| 921 |
then |
| 922 |
XMT=$DEFMINTEMP |
| 923 |
fi |
| 924 |
if [ "$MINTEMP" = "" ] |
| 925 |
then |
| 926 |
MINTEMP="${pwms}=${XMT}" |
| 927 |
else |
| 928 |
MINTEMP="$(echo $MINTEMP | sed -e "s/${pwmsed}[^ ]* *//g") ${pwms}=${XMT}" |
| 929 |
fi |
| 930 |
echo |
| 931 |
echo 'Enter the high temperature (degree C)' |
| 932 |
echo -n "over which the fan should spin at maximum speed ($DEFMAXTEMP): " |
| 933 |
read XMT |
| 934 |
if [ "$XMT" = "" ] |
| 935 |
then |
| 936 |
XMT=$DEFMAXTEMP |
| 937 |
fi |
| 938 |
if [ "$MAXTEMP" = "" ] |
| 939 |
then |
| 940 |
MAXTEMP="${pwms}=${XMT}" |
| 941 |
else |
| 942 |
MAXTEMP="$(echo $MAXTEMP | sed -e "s/${pwmsed}[^ ]* *//g") ${pwms}=${XMT}" |
| 943 |
fi |
| 944 |
|
| 945 |
if [ $FAN_MIN -eq 0 ] |
| 946 |
then |
| 947 |
echo |
| 948 |
echo "Enter the minimum PWM value (0-$MAX)" |
| 949 |
echo -n "at which the fan STOPS spinning (press t to test) ($DEFMINSTOP): " |
| 950 |
read XMSTOP |
| 951 |
|
| 952 |
if [ "$XMSTOP" = "" ] |
| 953 |
then |
| 954 |
XMSTOP=$DEFMINSTOP |
| 955 |
fi |
| 956 |
if [ "$XMSTOP" = "t" -o "$XMSTOP" = "T" ] |
| 957 |
then |
| 958 |
TestMinStop $FAN |
| 959 |
XMSTOP=$fanval |
| 960 |
fi |
| 961 |
else |
| 962 |
XMSTOP=0 |
| 963 |
fi |
| 964 |
if [ "$MINSTOP" = "" ] |
| 965 |
then |
| 966 |
MINSTOP="${pwms}=${XMSTOP}" |
| 967 |
else |
| 968 |
MINSTOP="$(echo $MINSTOP | sed -e "s/${pwmsed}[^ ]* *//g") ${pwms}=${XMSTOP}" |
| 969 |
fi |
| 970 |
|
| 971 |
if [ $FAN_MIN -eq 0 ] |
| 972 |
then |
| 973 |
echo |
| 974 |
echo "Enter the minimum PWM value ($XMSTOP-$MAX)" |
| 975 |
echo -n "at which the fan STARTS spinning (press t to test) ($DEFMINSTART): " |
| 976 |
read XMSTART |
| 977 |
|
| 978 |
if [ "$XMSTART" = "" ] |
| 979 |
then |
| 980 |
XMSTART=$DEFMINSTART |
| 981 |
fi |
| 982 |
if [ "$XMSTART" = "t" -o "$XMSTART" = "T" ] |
| 983 |
then |
| 984 |
TestMinStart $XMSTOP |
| 985 |
XMSTART=$fanval |
| 986 |
fi |
| 987 |
else |
| 988 |
XMSTART=$DEFMINSTART |
| 989 |
fi |
| 990 |
if [ "$MINSTART" = "" ] |
| 991 |
then |
| 992 |
MINSTART="${pwms}=${XMSTART}" |
| 993 |
else |
| 994 |
MINSTART="$(echo $MINSTART | sed -e "s/${pwmsed}[^ ]* *//g") ${pwms}=${XMSTART}" |
| 995 |
fi |
| 996 |
|
| 997 |
if [ $XMSTOP -gt 0 ] |
| 998 |
then |
| 999 |
echo |
| 1000 |
echo "Enter the PWM value (0-$XMSTOP) to use when the temperature" |
| 1001 |
echo -n "is below the low temperature limit (0): " |
| 1002 |
read XMINP |
| 1003 |
else |
| 1004 |
XMINP="" |
| 1005 |
fi |
| 1006 |
if [ -n "$XMINP" ] |
| 1007 |
then |
| 1008 |
if [ "$MINPWM" = "" ] |
| 1009 |
then |
| 1010 |
MINPWM="${pwms}=${XMINP}" |
| 1011 |
else |
| 1012 |
MINPWM="$(echo $MINPWM | sed -e "s/${pwmsed}[^ ]* *//g") ${pwms}=${XMINP}" |
| 1013 |
fi |
| 1014 |
fi |
| 1015 |
echo |
| 1016 |
echo "Enter the PWM value ($XMSTOP-$MAX) to use when the temperature" |
| 1017 |
echo -n "is over the high temperature limit ($MAX): " |
| 1018 |
read XMAXP |
| 1019 |
if [ -n "$XMAXP" ] |
| 1020 |
then |
| 1021 |
if [ "$MAXPWM" = "" ] |
| 1022 |
then |
| 1023 |
MAXPWM="${pwms}=${XMAXP}" |
| 1024 |
else |
| 1025 |
MAXPWM="$(echo $MAXPWM | sed -e "s/${pwmsed}[^ ]* *//g") ${pwms}=${XMAXP}" |
| 1026 |
fi |
| 1027 |
fi |
| 1028 |
echo |
| 1029 |
break; |
| 1030 |
done |
| 1031 |
break ;; |
| 1032 |
|
| 1033 |
*) |
| 1034 |
echo "No such option. Enter a number." |
| 1035 |
break ;; |
| 1036 |
esac |
| 1037 |
done |
| 1038 |
done |