CentOS7/rhel7 T440P 控制散热风扇转速等级

来源:互联网 发布:twitter是什么软件 编辑:程序博客网 时间:2024/04/29 12:22

本人 T440P 机器平时都工作在高计算状态,左侧键盘温感比较大,于是生起了控制风扇之意。借于Linux 系统的灵活'折腾',实现过程整理了一下,分享给有需要的人。

思路:
通过lm_sensors 获取各传感器数值, thinkpad_acpi 提供风扇的接口和方法,然后跑一个后台脚本定期去获取传感器的数据,对做逻辑判断来控制风扇的等级。

效果:
使用交流电源时,自定义控制风扇等级;使用电池时,交由机器自行散热;同时将脚本做成系统服务随系统自动启动。


以下是具体的操作步骤:


1. 安装相应rpm包

root ]# yum install -y  lm_sensors  tlp  tlp-rdw acpid


2. 使用sensors-detect检测并生成内核模块列表

root ]# sensors-detect --auto

检查获取各传感器的温度

[root@t440p modules]# sensors acpitz-virtual-0Adapter: Virtual devicetemp1:        +54.0°C  (crit = +200.0°C)nouveau-pci-0200Adapter: PCI adapterGPU core:     +0.60 V  temp1:            N/A  (high = +95.0°C, hyst =  +3.0°C)                       (crit = +105.0°C, hyst =  +5.0°C)                       (emerg = +135.0°C, hyst =  +5.0°C)thinkpad-isa-0000Adapter: ISA adapterfan1:           0 RPMcoretemp-isa-0000Adapter: ISA adapterPhysical id 0:  +56.0°C  (high = +84.0°C, crit = +100.0°C)Core 0:         +54.0°C  (high = +84.0°C, crit = +100.0°C)Core 1:         +56.0°C  (high = +84.0°C, crit = +100.0°C)

3.打开thinkpad_acpi 的风扇控制使能

做法,在加载thinpad_acpi这个内核模块的时候 传递一个打开风扇控制的变量

root ]# modprobe thinkpad_acpi   fan_control=1

要实现开机加载模块时设置 

root ]# echo 'options thinkpad_acpi fan_control=1' > /etc/modprobe.d/thinkfan.conf


4. 逻辑控制脚本

root ]# vim usr/bin/thinkfan    ### 内容如下,并给其执行权限

root ]# chmod +x  /usr/bin/thinkfan

#!/bin/sh# this script for bol@redhat.comtrap 'echo level auto > /proc/acpi/ibm/fan; exit' SIGTERM   ### 捕获退出信号# Limit temperature mint_min=(0 48 54 58 62 64 66 68 69)# Limit temperature maxt_max=(50 54 58 62 64 66 68 70 32767)#We start at max just to be safe STATE=5MAX_TEMP=0# a dirty function to get the temperature get_max_temperature(){   NEW_MAX_TEMP=0   NEW_MAX_TEMP=$(/usr/bin/sensors | grep Physical |awk '{print $4}'|cut -c 2-3)   if [ $NEW_MAX_TEMP -gt 0 ]; then       MAX_TEMP=$NEW_MAX_TEMP   fi }# the function regulating the fan regula_fan(){    # Getting the state    if [[ $MAX_TEMP -gt ${t_min[1]} ]] && [[ $MAX_TEMP -le ${t_max[1]} ]]; then        STATE=1    elif [[ $MAX_TEMP -gt ${t_min[2]} ]] && [[ $MAX_TEMP -le ${t_max[2]} ]]; thenSTATE=2    elif [[ $MAX_TEMP -gt ${t_min[3]} ]] && [[ $MAX_TEMP -le ${t_max[3]} ]]; thenSTATE=3    elif [[ $MAX_TEMP -gt ${t_min[4]} ]] && [[ $MAX_TEMP -le ${t_max[4]} ]]; thenSTATE=4    elif [[ $MAX_TEMP -gt ${t_min[5]} ]] && [[ $MAX_TEMP -le ${t_max[5]} ]]; thenSTATE=5    elif [[ $MAX_TEMP -gt ${t_min[6]} ]] && [[ $MAX_TEMP -le ${t_max[6]} ]]; thenSTATE=6    elif [[ $MAX_TEMP -gt ${t_min[7]} ]] && [[ $MAX_TEMP -le ${t_max[7]} ]]; thenSTATE=7    elif [[ $MAX_TEMP -gt ${t_min[8]} ]] && [[ $MAX_TEMP -le ${t_max[8]} ]]; thenSTATE=8    else        STATE=9    fi    case $STATE in        1) echo level 1 > /proc/acpi/ibm/fan;;        2) echo level 2 > /proc/acpi/ibm/fan;;        3) echo level 3 > /proc/acpi/ibm/fan;;        4) echo level 4 > /proc/acpi/ibm/fan;;        5) echo level 5 > /proc/acpi/ibm/fan;;        6) echo level 6 > /proc/acpi/ibm/fan;;        7) echo level 7 > /proc/acpi/ibm/fan;;        8) echo level full-speed > /proc/acpi/ibm/fan;;   ### 风扇全速        :) echo level auto > /proc/acpi/ibm/fan;;           ### 其他异常,auto模式    esac } while [[ 1 ]]; do    AC=$(cat /sys/class/power_supply/AC/online)    if [ $AC -eq 1 ]; then      get_max_temperature      regula_fan    else      echo level auto > /proc/acpi/ibm/fan    ### 电池模式时,auto 模式    fi    sleep 6      ### 执行频率,每隔6秒,大家可自行修改    #statements done


5 .将脚本以系统服务的方式自启动

root ]# vim /etc/systemd/system/thinkfan-ctl.service

[Unit]Description=thinkfan-ctrlAfter=lm_sensors.serviceRequires=lm_sensors.service[Service]Type=idleExecStart=/bin/nohup /usr/bin/thinkfan ExecStop=/bin/pkill thinkfan RemainAfterExit=yes[Install]WantedBy=multi-user.target

让 SystemD 重新加载配置文件

root ]# systemctl daemon-reload


6. 启停 thinkfan-ctl 服务

root ]# systemctl enable  thinkfan-ctl.service       ### 开机自启动

root ]# systemctl start/stop  thinkfan-ctl.service   ### 启/停

[root@t440p ~]# systemctl status thinkfan-ctl● thinkfan-ctl.service - thinkfan-ctrl   Loaded: loaded (/etc/systemd/system/thinkfan-ctl.service; enabled; vendor preset: disabled)   Active: active (running) since 二 2016-12-06 14:32:34 CST; 53min ago  Process: 19315 ExecStop=/bin/pkill thinkfan (code=exited, status=0/SUCCESS) Main PID: 19339 (thinkfan)   CGroup: /system.slice/thinkfan-ctl.service           ├─19339 /bin/sh /usr/bin/thinkfan           └─31939 sleep 212月 06 14:32:34 t440p.redhat.com systemd[1]: Started thinkfan-ctrl.12月 06 14:32:34 t440p.redhat.com systemd[1]: Starting thinkfan-ctrl...


7. 压测看看效果

在终端A中跑

user] $  dd if=/dev/zero of=/dev/null  

开另一终端B中,查看风扇等级和转速~

[user@t440p ~]$ cat /proc/acpi/ibm/fan status:enabledspeed:5617level:5commands:level <level> (<level> is 0-7, auto, disengaged, full-speed)commands:enable, disablecommands:watchdog <timeout> (<timeout> is 0 (off), 1-120 (seconds))


另附一份 rpm包,可直接使用:https://pan.baidu.com/s/1qYaXTj2

参考:

http://thinkwiki.de/Thinkfan

http://vmlinz.is-programmer.com/posts/25834.html

1 0
原创粉丝点击