計算中的「休眠」(或掛起到磁盤)指的是“關閉計算機,同時保持其狀態”。休眠時,計算機將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來查看休眠日志。
參考文獻
Wikipedia/Hibernation (computing)
ArchLinux/Power management/Suspend and hibernate
What is the difference between Hibernate and Suspend
What happens when hibernating Linux while running from a live disc?