Pm-utils 电源管理 (简体中文)

From ArchWiki

Jump to: navigation, search
Image:Tango-preferences-desktop-locale.png This page was marked to be translated.
If you understand both "original" and "target" languages of this page, please help complete the translation.
i18n
English
简体中文

Contents

简介

pm-utils 是一个全新的电源挂起和电源状态设置框架。它被设计来替代 powersave 包中的相关脚本。

通常HAL使用pm-utilsl来执行一系列的hacks来解决驱动所不能提供的电源挂起方面的操作。可以很方便的在特定目录当中配置自定义的挂钩,挂钩可以由管理员来建立或者是被包含在安装包当中,如果安装包需要监视电源的挂起和电源状态的改变。

可以和 Cpufrequtils 包联合使用,为笔记本和台式机提供完整的电源管理方案。

安装

pm-utils 包可以在 Extra 的软件仓库中得到:

# pacman -S pm-utils

基本配置

休眠 (把数据保存到磁盘的方式)

要使用休眠功能,需要以root的身份编辑 /boot/grub/menu.lst ,并在内核选项中添加 resume=/path/to/swap/drive (e.g. /dev/sda2) 例如:

# (0) Arch Linux
title  Arch Linux
root   (hd0,0)
kernel /vmlinuz26 root=/dev/sda3 resume=/dev/sda2 ro vga=0
initrd /kernel26.img

当计算机进入休眠后,计算机会把内存中的所有信息保存到交换分区(swap partition)... 那么要求你的交换分区有足够的空间来保存RAM信息。

在非root下执行挂起/休眠

因为必须在root权限下允许 pm-utils 脚本,一般情况下,你希望没有root密码的普通用户来执行这些脚本。这时,你需要使用visudo来编辑 /etc/sudoers 文件,例如:

# visudo

添加如下几行,记住把 username 替换成你自己的登录名:

username  ALL = (ALL) NOPASSWD: /usr/sbin/pm-hibernate
username  ALL = (ALL) NOPASSWD: /usr/sbin/pm-suspend

保存并推出 visudo

之后,你可以在普通用户下允许下面的脚本:

$ sudo pm-hibernate

或者是

$ sudo pm-suspend

同时,把你的用户加入到 power 组,这样桌面小应用程序可以使用挂起操作。如果你不这样做,当你使用如 gnome shutdown 的程序来进行挂起/休眠操作的时候,计算机会发出烦人的声音和锁住屏幕。

# gpasswd -a username power

完成以上步骤后,你可以使用 gnome 的电源管理工具 (也许也能使用 kpowersave) 来进行自动的挂起/休眠,比如在合上笔记本上盖和电池电量不足的时候...

高级配置

主要的配置文件是/usr/lib/pm-utils/defaults. 但是建议你不要去修改这个文件,因为此文件会随着软件包的升级而被重置。如果需要配置可以修改Y/etc/pm/config.d/

你可以将下列简单的配置以"modules" 或 "config"的命名加入/etc/pm/config.d中,这个设置将会替代系统的一般配置。

SUSPEND_MODULES="button uhci_hcd"

配置文件中一些可以使用的变量

SUSPEND_MODULES="button" # 列表当中的模块将在系统挂起时被卸载。

如何关闭监测(钩子,hook)程序

如果你不喜欢某个监测程序或觉得某个监测程序无用甚至影响正常使用的话,你可以将他视为一个Bug来报告。当然,我们也可以很简单的去禁用它。你可以通过管理/etc/pm/sleep.d/目录中所对应设备的文件控制监测程序的运行与否。例如:

touch /etc/pm/sleep.d/45pcmcia

你就可以建立一个/usr/lib/pm-utils/sleep.d/45pcmcia文件,来禁用pcmcia。

对于这个假的钩子程序不要设置可执行属性位。

建立你自己的钩子

如果你想建立一些只属于你自己的挂起/休眠设置,你只需要简单的把你的钩子放到/etc/pm/hooks。这个文件夹中的钩子会在挂起的时候按照字母顺序执行,并在唤醒过程中按照字母逆序执行(因此通常它们的名字以两位数字开头使得顺序更加明了)。

以下展示的是一个非实用的钩子,它会把一些信息写入你的log文件:

#!/bin/bash
case $1 in
    hibernate)
        echo "Hey guy, we are going to suspend to disk!"
        ;;
    suspend)
        echo "Oh, this time we're doing a suspend to RAM. Cool!"
        ;;
    thaw)
        echo "oh, suspend to disk is over, we are resuming..."
        ;;
    resume)
        echo "hey, the suspend to RAM seems to be over..."
        ;;
    *)  echo "somebody is calling me totally wrong."
        ;;
esac

把以上内容写入/etc/pm/sleep.d/66dummy文件中,并执行chmod +x /etc/pm/sleep.d/66dummy,之后在挂起/唤醒过程中它将产生一些无实际作用的行。

注意: 所有的钩子都以root用户执行。这意味着你需要在创建临时文件、检查PATH环境变量等时候多加小心,以避免安全问题。

How it Works

The concept is quite easy: the main script (pm-action, called via symlinks as either pm-suspend, pm-hibernate or pm-suspend-hybrid) executes so-called "hooks", executable scripts, in the alphabetical sorted order with the parameter suspend (suspend to RAM) or hibernate (suspend to disk). Once all hooks are done, it puts the machine to sleep. After the machine has woken up again, all those hooks are executed in reverse order with the parameter resume (resume from RAM) or thaw (resume from disk). The hooks do various stuff, for example preparing the bootloader, stopping the bluetooth subsystem or unloading of critical modules.

Both pm-suspend and pm-hibernate are usually called from HAL, initiated by desktop applets as gnome-power-manager or kpowersave.

Note: suspend-hybrid is a placeholder right now, it is not completely implemented.

There is also the possibility to set the machine into high-power and low-power mode, the command pm-powersave is used with an additional parameter of true or false. It works basically the same as the suspend framework.

The hooks for suspend are placed in

  • /usr/lib/pm-utils/sleep.d (distribution / package provided hooks)
  • /etc/pm/sleep.d (hooks added by the system administrator)

The hooks for the power state are placed in

  • /usr/lib/pm-utils/power.d (distribution / package provided hooks)
  • /etc/pm/power.d (hooks added by the system administrator)

Hooks in /etc/pm/ take precedence over those in /usr/lib/pm-utils/, so the system administrator can override the defaults provided by the distribution.

Troubleshooting

If suspend or hibernate did not work correctly, you will probably find some information in the logfile /var/log/pm-suspend.log, for example which hooks were run and what the output of them was.

Resume Hook

It has been suggested that some systems require the resume hook be added to the initrd image, otherwise the kernel will not resume. To do so, edit /etc/mkinitcpio.conf as root and add resume to the HOOKS array:

HOOKS="base udev autodetect ide scsi sata resume filesystems "

Note that this is an example, and your HOOKS array may look different.

resume must be placed after 'ide', 'scsi' and/or 'sata' but before 'filesystems'. Of course there has to be an appropriate 'resume' file in /lib/initcpio/hooks, it should already be there, as it is part of the package 'mkinitcpio'.

Finally, you must rebuild the initrd image for these changes to take effect:

# mkinitcpio -p kernel26

Note: If you use a custom kernel then you might have to change the value of the '-p' option.

Tips and Tricks / FAQ

Triggering suspend manually

If you want to trigger suspend manually for debugging, without using HAL and other frameworks, call pm-suspend or pm-hibernate as root.

Using another sleep backend (like uswsusp)

create a file with a SLEEP_MODULE variable, like this:

$ cat /etc/pm/config.d/module 
SLEEP_MODULE=uswsusp

I don't know but you may have to chmod +x it. to list available modules, use:

pacman -Ql pm-utils | grep module.d

Having the hd power management level automatically set again on resume

do it like this:

$ cat /etc/pm/sleep.d/50-hdparm_pm 
#!/bin/dash

if [ -n "$1" ] && ([ "$1" = "resume" ] || [ "$1" = "thaw" ]); then
	hdparm -B 254 /dev/sda > /dev/null
fi

Restarting the mouse

On some laptops the mouse will hang after an otherwise successful suspend. One way to remedy this is to force a reinit of the PS/2 driver (here i8042) through a hook in /etc/pm/hooks (see hooks)

#!/bin/sh  
echo -n "i8042" > /sys/bus/platform/drivers/i8042/unbind
echo -n "i8042" > /sys/bus/platform/drivers/i8042/bind

It seems to not do anything / where is the logfile

If it seem to not do anything when called via the desktop applets, then try to call pm-suspend or pm-hibernate manually from a root shell in a terminal. Maybe you'll already get some output that will point you to the problem. The suspend scripts also write a logfile at /var/log/pm-suspend.log.

Add sleep modes to Openbox menu

Openbox users can add the new scripts as additional shutdown options within the Openbox menu by adding the items to a new or existing sub-menu in ~/.config/openbox/menu.xml, for example:

<menu id="64" label="Shutdown">
	<item label="Lock"> <action name="Execute"> <execute>xscreensaver-command -lock</execute> </action> </item>
	<item label="Logout"> <action name="Exit"/> </item>
	<item label="Reboot"> <action name="Execute"> <execute>sudo shutdown -r now</execute> </action> </item>
	<item label="Poweroff"> <action name="Execute"> <execute>sudo shutdown -h now </execute> </action> </item>
	<item label="Hibernate"> <action name="Execute"> <execute>sudo pm-hibernate</execute> </action> </item>
	<item label="Suspend"> <action name="Execute"> <execute>sudo pm-suspend</execute> </action> </item>
</menu>

Other Resources

HAL Quirk Site - Common solutions and frequently asked questions
Cpufrequtils - CPU Frequency Scaling and CPU Power schemes
SpeedStep - More information on CPU frequency scaling (some of which is obsolete)


Credits

This wiki entry was originally sourced from the OpenSUSE Wiki (Licensed under GPL). A big thank you goes to the pm-utils developers and documenters for their time.

Personal tools