/ Bash

TLP & upower: centos7笔记本电池充电阈值,和低电量自动休眠关机

  • yum安装tlp
 [root@centosX61 ~]# yum install -y tlp tlp-rdw
 已加载插件:fastestmirror Loading mirror speeds from cached hostfile
 * base: mirror.bit.edu.cn
 * epel: mirror.lzu.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
 正在解决依赖关系 --> 正在检查事务 ---> 软件包
 tlp.noarch.0.1.0-1.el7 将被 安装
 --> 正在处理依赖关系
 /usr/bin/perl,它被软件包 tlp-1.0-1.el7.noarch 需要
 ....
 usbutils.x86_64 0:007-5.el7 完毕!
  • 启动tlp并查看笔记本信息
 [root@centosX61 ~]# tlp start TLP
 started in AC mode. 
[root@centosX61 ~]# tlp-stat -s
 --- TLP 1.0 -------------------------------------------- +++
 System Info System = LENOVO ThinkPad X61 7675L12
 BIOS = 7NETC2WW (2.22 )
 Release = "CentOS Linux release 7.4.1708 (Core) "
 Kernel = 3.10.0-693.11.6.el7.x86_64
 #1 SMP Thu Jan 4 01:06:37 UTC 2018
 x86_64
 /proc/cmdline = BOOT_IMAGE=/vmlinuz-3.10.0-693.11.6.el7.x86_64
 root=/dev/mapper/cl-root
 ro crashkernel=auto
 rd.lvm.lv=cl/root
 rd.lvm.lv=cl/swap
 rhgb quiet LANG=zh_CN.UTF-8
 Init system = systemd
 Boot mode = BIOS (CSM, Legacy) +++
 TLP Status State = enabled
 Last run = 22时37分17秒, 213 sec(s) ago
 Mode = AC
 Power source = AC
  • 查看笔记本电池信息
 [root@centosX61 ~]# tlp-stat -b
 --- TLP 1.0 --------------------------------------------
 +++
 ThinkPad Battery Features
 tp-smapi = inactive (kernel module 'tp_smapi' not installed)
 tpacpi-bat = inactive (unsupported hardware)
 +++
 Battery Status
 /sys/class/power_supply/BAT0/manufacturer = SANYO
 /sys/class/power_supply/BAT0/model_name = 92P116
 /sys/class/power_supply/BAT0/cycle_count = (not supported)
/sys/class/power_supply/BAT0/energy_full_design = 31680 [mWh]
 /sys/class/power_supply/BAT0/energy_full = 30910 [mWh]
 /sys/class/power_supply/BAT0/energy_now = 30770 [mWh]
 /sys/class/power_supply/BAT0/power_now = 0 [mW]
 /sys/class/power_supply/BAT0/status = Unknown
 Charge = 99.5 [%]
 Capacity = 97.6 [%]
  • 查看tlp电池管理配置参数
 [root@centosX61 ~]# tlp-stat -c
 --- TLP 1.0 --------------------------------------------
 +++
 Configured Settings:
 /etc/default/tlp TLP_ENABLE=1
 TLP_DEFAULT_MODE=AC
 TLP_PERSISTENT_DEFAULT=0
 DISK_IDLE_SECS_ON_AC=0
 DISK_IDLE_SECS_ON_BAT=2
 MAX_LOST_WORK_SECS_ON_AC=15
 MAX_LOST_WORK_SECS_ON_BAT=60
 CPU_HWP_ON_AC=balance_performance
 CPU_HWP_ON_BAT=balance_power
 SCHED_POWERSAVE_ON_AC=0
 SCHED_POWERSAVE_ON_BAT=1
 NMI_WATCHDOG=0
 ENERGY_PERF_POLICY_ON_AC=performance

ENERGY_PERF_POLICY_ON_BAT=powersave
 DISK_DEVICES="sda sdb"
 DISK_APM_LEVEL_ON_AC="254 254"
 DISK_APM_LEVEL_ON_BAT="128 128"
 SATA_LINKPWR_ON_AC=max_performance
 SATA_LINKPWR_ON_BAT=min_power
 AHCI_RUNTIME_PM_TIMEOUT=15
 PCIE_ASPM_ON_AC=performance
 PCIE_ASPM_ON_BAT=powersave
 RADEON_POWER_PROFILE_ON_AC=high
 RADEON_POWER_PROFILE_ON_BAT=low
 RADEON_DPM_STATE_ON_AC=performance
 RADEON_DPM_STATE_ON_BAT=battery
 RADEON_DPM_PERF_LEVEL_ON_AC=auto
 RADEON_DPM_PERF_LEVEL_ON_BAT=auto
 WIFI_PWR_ON_AC=off
 WIFI_PWR_ON_BAT=on WOL_DISABLE=Y
 SOUND_POWER_SAVE_ON_AC=0
 SOUND_POWER_SAVE_ON_BAT=1
 SOUND_POWER_SAVE_CONTROLLER=Y
 BAY_POWEROFF_ON_AC=0
 BAY_POWEROFF_ON_BAT=0
 BAY_DEVICE="sr0"
 RUNTIME_PM_ON_AC=on
 RUNTIME_PM_ON_BAT=auto
 USB_AUTOSUSPEND=1
 USB_BLACKLIST_BTUSB=0
 USB_BLACKLIST_PHONE=0
 USB_BLACKLIST_WWAN=1
 RESTORE_DEVICE_STATE_ON_STARTUP=0
  • 修改电池配置参数
vi /etc/default/tlp
# 说明:后文找到了upower,定义电池电量低于多少时,自动关机
...
# 这段是cpu频率设置,电池模式我给降低了,0-20,因为是服务器,如果停电还是别折腾了
# Set Intel P-state performance: 0..100 (%).
# Limit the max/min P-state to control the power dissipation of the CPU.
# Values are stated as a percentage of the available performance.
# Requires an Intel Core i processor with intel_pstate driver.
#CPU_MIN_PERF_ON_AC=0
#CPU_MAX_PERF_ON_AC=100
CPU_MIN_PERF_ON_BAT=0
CPU_MAX_PERF_ON_BAT=20
...
# 这段是充电阈值的设置,我给打开设置了 75-95
# Battery charge thresholds (ThinkPad only, tp-smapi or acpi-call kernel module
# required). Charging starts when the remaining capacity falls below the
# START_CHARGE_THRESH value and stops when exceeding the STOP_CHARGE_THRESH value.
# Main / Internal battery (values in %)
START_CHARGE_THRESH_BAT0=75
STOP_CHARGE_THRESH_BAT0=95
# Ultrabay / Slice / Replaceable battery (values in %)
START_CHARGE_THRESH_BAT1=75
STOP_CHARGE_THRESH_BAT1=95
...
  • tlp充电阈值依赖tp-smapi or acpi-call,centos7.4没装成功
# 安装 dkms
yum install -y dkms
# 安装 tp_smapi
https://rpmfind.net/linux/rpm2html/search.php?query=kmod(tp_smapi)
wget https://rpmfind.net/linux/Mandriva/devel/cooker/i586/media/contrib/release/dkms-tp_smapi-0.41-1-mdv2012.0.noarch.rpm
[root@centosX61 ~]# rpm -ivh dkms-tp_smapi-0.41-1-mdv2012.0.noarch.rpm
警告:dkms-tp_smapi-0.41-1-mdv2012.0.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 26752624: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:dkms-tp_smapi-0.41-1             ################################# [100%]

Creating symlink /var/lib/dkms/tp_smapi/0.41-1/source ->
                 /usr/src/tp_smapi-0.41-1

DKMS: add completed.
Error! echo
Your kernel headers for kernel 3.10.0-693.21.1.el7.x86_64 cannot be found at
/lib/modules/3.10.0-693.21.1.el7.x86_64/build or /lib/modules/3.10.0-693.21.1.el7.x86_64/source.
Error! echo
Your kernel headers for kernel 3.10.0-693.21.1.el7.x86_64 cannot be found at
/lib/modules/3.10.0-693.21.1.el7.x86_64/build or /lib/modules/3.10.0-693.21.1.el7.x86_64/source.
警告:%post(dkms-tp_smapi-0.41-1.noarch) 脚本执行失败,退出状态码为 1
# centos7.4的kernnel内核版本为3.10.0-693.21.1.el7.x86_64
# tp-smapi的rpm最后一个包才支持到2.6.33.5,所以没装上,不知道还有没有高招
  • upower --------- 安装
# upower: 对TLP的补充,解决电量行为管理
[root@centosX61 default]# yum install -y upower
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror01.idc.hinet.net
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 upower.x86_64.0.0.99.4-2.el7 将被 安装
--> 正在处理依赖关系 libplist.so.3()(64bit),它被软件包 upower-0.99.4-2.el7.x86_64 需要
--> 正在处理依赖关系 libimobiledevice.so.6()(64bit),它被软件包 upower-0.99.4-2.el7.x86_64 需要
--> 正在检查事务
---> 软件包 libimobiledevice.x86_64.0.1.2.0-1.el7 将被 安装
--> 正在处理依赖关系 libusbmuxd.so.4()(64bit),它被软件包 libimobiledevice-1.2.0-1.el7.x86_64 需要
---> 软件包 libplist.x86_64.0.1.12-3.el7 将被 安装
--> 正在检查事务
---> 软件包 libusbmuxd.x86_64.0.1.0.10-5.el7 将被 安装
--> 解决依赖关系完成
...
# 查看upower服务的状态详情
[root@centosX61 default]# systemctl status upower
● upower.service - Daemon for power management
   Loaded: loaded (/usr/lib/systemd/system/upower.service; disabled; vendor preset: enabled)
   Active: active (running) since 六 2018-10-06 09:05:23 CST; 2min 44s ago
     Docs: man:upowerd(8)
 Main PID: 16178 (upowerd)
    Tasks: 3
   Memory: 3.4M
   CGroup: /system.slice/upower.service
           └─16178 /usr/libexec/upowerd

10月 06 09:05:23 centosX61 systemd[1]: Starting Daemon for power management...
10月 06 09:05:23 centosX61 systemd[1]: Started Daemon for power management.
# 设置upower服务为开机自启
[root@centosX61 default]# systemctl enable upower
Created symlink from /etc/systemd/system/graphical.target.wants/upower.service to /usr/lib/systemd/system/upower.service.
  • upower -------- 设置
vi /etc/UPower/UPower.conf
...
# 打开百分比的电量级别定义,便于后续关联动作的设置
# Policy for warnings and action based on battery levels
#
# Whether battery percentage based policy should be used. The default
# is to use the time left, change to true to use the percentage, which
# should work around broken firmwares. It is also more reliable than
# the time left (frantically saving all your files is going to use more
# battery than letting it rest for example).
# default=true
UsePercentageForPolicy=true

# 自定义级别 60/53/50 因为放家里,断电电量下降就需要关,所以电量值设置很高
# When UsePercentageForPolicy is true, the levels at which UPower will
# consider the battery low, critical, or take action for the critical
# battery level.
#
# This will also be used for batteries which don't have time information
# such as that of peripherals.
#
# If any value is invalid, or not in descending order, the defaults
# will be used.
#
# Defaults:
# PercentageLow=10
# PercentageCritical=3
# PercentageAction=2
PercentageLow=60
PercentageCritical=53
PercentageAction=50
...
# 电量低下时的动作,关闭系统
# The action to take when "TimeAction" or "PercentageAction" above has been
# reached for the batteries (UPS or laptop batteries) supplying the computer
#
# Possible values are:
# PowerOff
# Hibernate
# HybridSleep
#
# If HybridSleep isn't available, Hibernate will be used
# If Hibernate isn't available, PowerOff will be used
CriticalPowerAction=PowerOff
...
#保存后,重新加载一下upower服务
[root@centosX61 default]# systemctl restart upower
  • 根据tlp可以写电池休眠脚本
    1. 获取电池状态和电量值
    2. 如果非充电且电量小于多少则休眠
    3. 加入crontab定时执行
    4. 目前用upower的配置文件已实现,断电后低于65%自动关机
    5. 充电阈值因为centos7.4的内核3.10不被tp-smapi支持而没生效