# 2021-01-13 #「Linux」- 休眠(Hibernation)

计算中的「休眠」(或挂起到磁盘)指的是“关闭计算机,同时保持其状态”。休眠时,计算机将RAM的内容保存到非易失性存储设备中。恢复后,计算机与进入休眠状态之前完全一样。

休眠的不同实现(在Linux中)

以下三者都称之为“休眠到硬盘”(suspend-to-disk)。

swsusp - Software Suspend

在Linux内核中,休眠由swsusp实现,它内置于2.6系列中。

TuxOnIce - Suspend2

另一种实现是TuxOnIce,它可以作为3.4版本内核的补丁。TuxOnIce提供诸如支持对称多处理和抢占的优点。

uswsusp - userspace software suspend

另一个替代实现是uswsusp。

测试系统是否支持休眠

安装pm-utils软件包,因为要用到其中的pm-is-supported命令:

#!/bin/sh
pm-is-supported --hibernate
echo $?

如果状态值为“0”,则表示支持休眠。

休眠时RAM保存的位置

系统休眠(systemctl hibernate)时,”计算机将RAM的内容保存到非易失性存储设备中”,“非易失性存储设备”通常是SWAP分区。

为什么说是SWAP分区?
因为在执行systemctl hibernate进行休眠时,我遇到了一个错误“Failed to hibernate system via logind: Not enough swap space for hibernation”错误提示…………调整了SWAP分区的大小后,系统可以进行正常休眠动作。

笔记本关闭盖子时自动休眠

如果你使用的是systemd服务,那可以修改/etc/systemd/logind.conf文件,在[Login]部分添加如下行:

HandleLidSwitch=hibernate
HandleLidSwitchExternalPower=hibernate
HandleLidSwitchDocked=hibernate

然后,重启systemd-logind.service服务(systemctl restart systemd-logind.service)。!!!这会退出所有登录会话!!!

参数的含义如下:

 

HandleLidSwitch - 盖子关闭时触发,下面的两种情况除外。
HandleLidSwitchExternalPower - 如果系统连接到外部电源,则在盖子关闭时触发。
HandleLidSwitchDocked - 如果系统插入扩展坞,或者连接了多个显示器,则在盖子关闭时触发。

 

可以根据自己的需要设置不同的值,取值有:ignore, poweroff, reboot, halt, suspend, hibernate, hybrid-sleep, suspend-then-hibernate, lock, kexec

要读的文章

# TODO 与Linux休眠有关的必读文章
Hibernate feature in Linux

Simple Hibernate with Arch Linux
Hibernate laptop on lid close with systemd-boot on Arch Linux

Power management
Power management/Suspend and hibernate

What happens when you try to hibernate but there is not enough swap available?

Hibernation
HibernationHibernate_Without_Swap_Partition

have to reload ath_pci after suspend+resume
DebuggingKernelHibernate

遇到的问题

#1 Failed to write /sys/power/state: Device or resource busy

# TODO 系统休眠错误,Failed to write /sys/power/state: Device or resource busy
# May 04 21:31:21 laptop systemd-sleep[18403]: Failed to write /sys/power/state: Device or resource busy
# 有人遇到过相同的错误:「[SOLVED] Unable to initiate suspend to ram」。实际上也没有解决,靠升级降级的方式来处理这个问题。还有重新安装系统的………………
# 使用journalctl -f -u systemd-hibernate.service来查看休眠日志。

参考文献

 

 


posted @ 2021-01-13 21:49  研究林纳斯写的  阅读(324)  评论(0编辑  收藏  举报