Difference between revisions of "Power management/Suspend and hibernate"

From ArchWiki
Jump to navigation Jump to search
(→‎Hibernation: Add section about delayed hibernation)
(→‎Hibernation into swap file on Btrfs: Remove warning about unfixed bug; It is fixed in v245)
 
(183 intermediate revisions by 61 users not shown)
Line 1: Line 1:
 
[[Category:Power management]]
 
[[Category:Power management]]
[[ja:Suspend and Hibernate]]
+
[[es:Power management (Español)/Suspend and hibernate]]
 +
[[ja:サスペンドとハイバネート]]
 +
[[ru:Power management/Suspend and hibernate]]
 +
[[zh-hans:Power management/Suspend and hibernate]]
 
{{Related articles start}}
 
{{Related articles start}}
 
{{Related|Uswsusp}}
 
{{Related|Uswsusp}}
{{Related|TuxOnIce}}
 
 
{{Related|systemd}}
 
{{Related|systemd}}
{{Related|pm-utils}}
+
{{Related|Power management}}
{{Related|Suspending to RAM with hibernate-script}}
 
{{Related|Suspending to Disk with hibernate-script}}
 
 
{{Related articles end}}
 
{{Related articles end}}
 +
 
Currently there are three methods of suspending available: '''suspend to RAM''' (usually called just '''suspend'''), '''suspend to disk''' (usually known as '''hibernate'''), and '''hybrid suspend''' (sometimes aptly called '''suspend to both'''):
 
Currently there are three methods of suspending available: '''suspend to RAM''' (usually called just '''suspend'''), '''suspend to disk''' (usually known as '''hibernate'''), and '''hybrid suspend''' (sometimes aptly called '''suspend to both'''):
  
 
* '''Suspend to RAM''' method cuts power to most parts of the machine aside from the RAM, which is required to restore the machine's state. Because of the large power savings, it is advisable for laptops to automatically enter this mode when the computer is running on batteries and the lid is closed (or the user is inactive for some time).
 
* '''Suspend to RAM''' method cuts power to most parts of the machine aside from the RAM, which is required to restore the machine's state. Because of the large power savings, it is advisable for laptops to automatically enter this mode when the computer is running on batteries and the lid is closed (or the user is inactive for some time).
  
* '''Suspend to disk''' method saves the machine's state into [[Swap|swap space]] and completely powers off the machine. When the machine is powered on, the state is restored. Until then, there is zero power consumption.
+
* '''Suspend to disk''' method saves the machine's state into [[swap space]] and completely powers off the machine. When the machine is powered on, the state is restored. Until then, there is [[Wikipedia:Standby power|zero]] power consumption.
  
 
* '''Suspend to both''' method saves the machine's state into swap space, but does not power off the machine. Instead, it invokes usual suspend to RAM. Therefore, if the battery is not depleted, the system can resume from RAM. If the battery is depleted, the system can be resumed from disk, which is much slower than resuming from RAM, but the machine's state has not been lost.
 
* '''Suspend to both''' method saves the machine's state into swap space, but does not power off the machine. Instead, it invokes usual suspend to RAM. Therefore, if the battery is not depleted, the system can resume from RAM. If the battery is depleted, the system can be resumed from disk, which is much slower than resuming from RAM, but the machine's state has not been lost.
Line 34: Line 35:
  
 
See main article [[Uswsusp]].
 
See main article [[Uswsusp]].
 
=== tuxonice ===
 
 
TuxOnIce is a fork of the kernel implementation of suspend/hibernate that provides kernel patches to improve the default implementation. It requires a custom kernel to achieve this purpose.
 
 
See main article [[TuxOnIce]].
 
  
 
== High level interfaces ==
 
== High level interfaces ==
  
{{Note|The end goal of these packages is to provide binaries/scripts that can be invoked to perform suspend/hibernate. Actually hooking them up to power buttons or menu clicks or laptop lid events is usually left to other tools. To automatically suspend/hibernate on certain power events, such as laptop lid close or battery depletion percentage, you may want to look into running [[Acpid]].}}
+
The end goal of these packages is to provide binaries/scripts that can be invoked to perform suspend/hibernate. Actually hooking them up to power buttons or menu clicks or laptop lid events is usually left to other tools. To automatically suspend/hibernate on certain power events, such as laptop lid close or battery depletion percentage, you may want to look into running [[Acpid]].
  
 
=== systemd ===
 
=== systemd ===
  
[[systemd]] provides native commands for suspend, hibernate and a hybrid suspend, see [[Power management#Power management with systemd]] for details.
+
[[systemd]] provides native commands for suspend, hibernate and a hybrid suspend, see [[Power management#Power management with systemd]] for details. This is the default interface used in Arch Linux.
  
See [[Power management#Sleep hooks]] for additional information on configuring suspend/hibernate hooks. Also see {{ic|man systemctl}}, {{ic|man systemd-sleep}}, and {{ic|man systemd.special}}.
+
See [[Power management#Sleep hooks]] for additional information on configuring suspend/hibernate hooks. Also see {{man|1|systemctl}}, {{man|8|systemd-sleep}}, and {{man|7|systemd.special}}.
  
=== pm-utils ===
+
== Hibernation ==
  
pm-utils is a set of shell scripts that encapsulate the backend's suspend/hibernate functionality. It comes with a set of pre- and post-suspend tweaks and various hooks to customize the process.
+
In order to use hibernation, you need to create a [[swap]] partition or file. You will need to point the kernel to your swap using the {{ic|1=resume=}} kernel parameter, which is configured via the boot loader. You will also need to [[#Configure the initramfs|configure the initramfs]]. This tells the kernel to attempt resuming from the specified swap in early userspace. These three steps are described in detail below.
  
See main article [[pm-utils]].
+
{{Note|
 
+
* See [[Dm-crypt/Swap encryption#With suspend-to-disk support]] when using [[encryption]].
== Suspend to RAM ==
+
* {{Pkg|linux-hardened}} does not support hibernation, see {{Bug|63648}}.
 
+
}}
Suspend to RAM should work out of the box.
 
 
 
== Hibernation ==
 
 
 
In order to use hibernation, you need to create swap partition or swap file. See [[Swap]] for details.
 
  
 
=== About swap partition/file size ===
 
=== About swap partition/file size ===
Line 71: Line 61:
 
: ''{{ic|/sys/power/image_size}} controls the size of the image created by the suspend-to-disk mechanism. It can be written a string representing a non-negative integer that will be used as an upper limit of the image size, in bytes. The suspend-to-disk mechanism will do its best to ensure the image size will not exceed that number. However, if this turns out to be impossible, it will try to suspend anyway using the smallest image possible. In particular, if "0" is written to this file, the suspend image will be as small as possible. Reading from this file will display the current image size limit, which is set to 2/5 of available RAM by default.''
 
: ''{{ic|/sys/power/image_size}} controls the size of the image created by the suspend-to-disk mechanism. It can be written a string representing a non-negative integer that will be used as an upper limit of the image size, in bytes. The suspend-to-disk mechanism will do its best to ensure the image size will not exceed that number. However, if this turns out to be impossible, it will try to suspend anyway using the smallest image possible. In particular, if "0" is written to this file, the suspend image will be as small as possible. Reading from this file will display the current image size limit, which is set to 2/5 of available RAM by default.''
  
You may either decrease the value of {{ic|/sys/power/image_size}} to make the suspend image as small as possible (for small swap partitions), or increase it to possibly speed up the hibernation process.
+
You may either decrease the value of {{ic|/sys/power/image_size}} to make the suspend image as small as possible (for small swap partitions), or increase it to possibly speed up the hibernation process. See [[Systemd#Temporary files]] to make this change persistent.
 +
 
 +
The suspend image cannot span multiple swap partitions and/or swap files. It must fully fit in one swap partition or one swap file.[https://www.kernel.org/doc/Documentation/power/swsusp.txt]
  
 
=== Required kernel parameters ===
 
=== Required kernel parameters ===
  
The kernel parameter {{ic|1=resume=''swap_partition''}} has to be used. The configuration depends on the used [[boot loader]], some examples are given below. According to the boot loader configuration, use either kernel name (for example {{ic|/dev/sda1}}), or [[UUID]] path.
+
The [[kernel parameter]] {{ic|1=resume=''swap_device''}} must be used. Any of the [[persistent block device naming]] methods can be used as {{ic|''swap_device''}}. For example:
  
==== Example for GRUB ====
+
* {{ic|1=resume=UUID=4209c845-f495-4c43-8a03-5363dd433153}}
 +
* {{ic|1=resume="PARTLABEL=Swap partition"}}
 +
* {{ic|1=resume=/dev/archVolumeGroup/archLogicalVolume}} -- if swap is on a [[LVM]] logical volume
  
The kernel name, e.g. {{ic|/dev/sda1}}, can only be used if [[GRUB#Persistent_block_device_naming|Persistent block device naming]] is opted out in GRUB2.
+
Generally, it is preferred to use the same naming method for both the {{ic|resume}} and {{ic|root}} parameters.
  
For example, with [[GRUB]] you can use {{ic|GRUB_CMDLINE_LINUX_DEFAULT}} variable:
+
==== Hibernation into swap file ====
  
{{hc|/etc/default/grub|2=
+
{{Warning|[[Btrfs#Swap file|Btrfs]] on Linux kernel before version 5.0 does not support swap files. Failure to heed this warning may result in file system corruption. While a swap file may be used on Btrfs when mounted through a loop device, this will result in severely degraded swap performance.}}
GRUB_CMDLINE_LINUX_DEFAULT="resume=/dev/disk/by-uuid/4209c845-f495-4c43-8a03-5363dd433153"
 
}}
 
  
Don't forget to run {{ic|grub-mkconfig -o /boot/grub/grub.cfg}} afterwards.
+
Using a swap file instead of a swap partition requires an additional kernel parameter {{ic|1=resume_offset=''swap_file_offset''}}.
  
{{Tip|As an alternative, for GRUB2 you may try this solution from [http://wiki.debian.org/Grub#Configuring_grub_v2 debian wiki], which automatically adds your first swap partition to {{ic|1=resume=}} parameter to all found linux entries.}}
+
{{Note|The {{ic|resume}} parameter must point to the [[block device]] where the swap file resides. For a stacked block device such as an encrypted container, RAID or LVM, it means that {{ic|resume}} must point to the unlocked/mapped device that contains the file system with the swap file.}}
  
==== Example for gummiboot ====
+
The value of {{ic|''swap_file_offset''}} can be obtained by running {{ic|filefrag -v ''swap_file''}}, the output is in a table format and the required value is located in the first row of the {{ic|physical_offset}} column. For example:
  
If [[gummiboot]] is used as boot manager, you have to add the {{ic|1=resume=''swap_partition''}} parameter in the options-list of the entry file. 
+
{{hc|# filefrag -v /swapfile|
 +
Filesystem type is: ef53
 +
File size of /swapfile is 4294967296 (1048576 blocks of 4096 bytes)
 +
ext:    logical_offset:        physical_offset: length:  expected: flags:
 +
  0:        0..      0:      '''38912'''..    38912:      1:           
 +
  1:        1..  22527:      38913..    61439:  22527:            unwritten
 +
  2:    22528..  53247:    899072..    929791:  30720:      61440: unwritten
 +
...
 +
}}
  
For example if your efi system partition is mounted in {{ic|/boot}}, then the option list should look like this:
+
In the example the value of {{ic|''swap_file_offset''}} is the first {{ic|38912}} with the two periods.
  
{{hc|/boot/loader/entries/arch.conf|2=
+
{{Tip|
options root=/dev/sda''X'' rw resume=''swap_partition''
+
* The following command may be used to identify {{ic|''swap_device''}}: {{ic|1=findmnt -no SOURCE -T /swapfile}}
 +
* The following command may be used to identify {{ic|''swap_file_offset''}}: {{ic|1=filefrag -v /swapfile {{!}} awk '{ if($1=="0:"){print $4} }'}}
 +
* The value of {{ic|''swap_file_offset''}} can also be obtained by running {{ic|swap-offset ''swap_file''}}. The ''swap-offset'' binary is provided within the set of tools [[uswsusp]]. If using this method, then these two parameters have to be provided in {{ic|/etc/suspend.conf}} via the keys {{ic|resume device}} and {{ic|resume offset}}. No reboot is required in this case.
 
}}
 
}}
  
==== Hibernation into swap file ====
+
{{Note|The kernel parameters will only take effect after rebooting. To be able to hibernate right away, obtain the volume's major and minor device numbers from [[lsblk]] and echo them in format {{ic|''major'':''minor''}} to {{ic|/sys/power/resume}} and the resume offset to {{ic|/sys/power/resume_offset}}. For example, if the swap file is on volume {{ic|8:2}} and has the offset {{ic|38912}}:
 +
 
 +
# echo 8:2 > /sys/power/resume
 +
# echo 38912 > /sys/power/resume_offset
  
Using a swap file instead of a swap partition requires an additional kernel parameter {{ic|1=resume_offset=''swap_file_offset''}}.
+
See https://www.kernel.org/doc/Documentation/power/swsusp.txt.}}
  
The value of {{ic|''swap_file_offset''}} can be obtained by running {{ic|filefrag -v ''swap_file''}}, the output is in a table format and the required value is located in the first row of the {{ic|physical_offset}} column. For example:
+
{{Tip|You might want to decrease the [[swappiness]] for your swapfile if the only purpose is to be able to hibernate and not expand RAM.}}
  
{{hc|# filefrag -v /swapfile|<nowiki>
+
==== Hibernation into swap file on Btrfs ====
Filesystem type is: ef53
 
File size of /swapfile is 4294967296 (1048576 blocks of 4096 bytes)
 
ext:    logical_offset:        physical_offset: length:  expected: flags:
 
  0:        0..      0:      38912..    38912:      1:           
 
  1:        1..  22527:      38913..    61439:  22527:            unwritten
 
  2:    22528..  53247:    899072..    929791:  30720:      61440: unwritten
 
...
 
</nowiki>}}
 
  
In the example the value of {{ic|''swap_file_offset''}} is {{ic|38912}}.
+
Hibernation onto a swapfile is supported by recent versions of systemd [https://github.com/systemd/systemd/issues/9559].
  
{{Tip|The value of {{ic|''swap_file_offset''}} can also be obtained by running {{ic|swap-offset ''swap_file''}}. The ''swap-offset'' binary is provided by package {{AUR|uswsusp-git}}.}}
+
The resume_offset number can be computed using the [https://github.com/osandov/osandov-linux/blob/master/scripts/btrfs_map_physical.c tool btrfs_map_physical.c].
 +
Do not try to use the filefrag tool, on [[Btrfs]] the "physical" offset you get from filefrag is not the real physical offset on disk; there is a virtual disk address space in order to support multiple devices. [https://bugzilla.kernel.org/show_bug.cgi?id=202803]
  
{{Note|
+
Download or copy the [https://github.com/osandov/osandov-linux/blob/master/scripts/btrfs_map_physical.c tool btrfs_map_physical.c] into a file named {{ic|btrfs_map_physical.c}}, then compile it,
* Please note that in the kernel parameter {{ic|resume}} you have to provide the device of the partition that contains the swap file, not swap file itself! The parameter {{ic|resume_offset}} informs the system where the swap file starts on the resume device.
 
* If using [[uswsusp]], then these two parameters have to be provided in {{ic|/etc/suspend.conf}} via the keys {{ic|resume device}} and {{ic|resume offset}}.
 
}}
 
  
=== Recreate initial ramdisk ===
+
$ gcc -O2 -o btrfs_map_physical btrfs_map_physical.c
  
If you use an initramfs (default Arch systems do), you must add the {{ic|resume}} hook into the HOOKS in the configuration of [[mkinitcpio]]:
+
and run it. An example output is shown below.
  
{{hc|/etc/mkinitcpio.conf|2=
+
{{hc|# ./btrfs_map_physical ''/path/to/swapfile''|
# resume must be placed after block and lvm2, but before filesystems
+
FILE OFFSET  EXTENT TYPE  LOGICAL SIZE  LOGICAL OFFSET  PHYSICAL SIZE  DEVID  PHYSICAL OFFSET
HOOKS="... block lvm2 '''resume''' filesystems ..."
+
0            regular      4096          2927632384      268435456      1      '''4009762816'''
 +
4096        prealloc    268431360    2927636480      268431360      1      4009766912
 +
268435456    prealloc    268435456    3251634176      268435456      1      4333764608
 +
536870912    prealloc    268435456    3520069632      268435456      1      4602200064
 +
805306368    prealloc    268435456    3788505088      268435456      1      4870635520
 +
1073741824  prealloc    268435456    4056940544      268435456      1      5139070976
 +
1342177280  prealloc    268435456    4325376000      268435456      1      5407506432
 +
1610612736  prealloc    268435456    4593811456      268435456      1      5675941888
 
}}
 
}}
  
{{Warning|You must not use the [[Mkinitcpio#Common_hooks|systemd hook]], because it does not support resuming from hibernation. See {{Bug|37028}} for details.}}
+
Note the the first physical offset returned by this tool. In this example, we use {{ic|4009762816}}. Also note the pagesize that can be found with {{ic|getconf PAGESIZE}}.
  
Finally, you must rebuild the initrd image for these changes to take effect:
+
To compute the {{ic|resume_offset}} value, divide the physical offset by the pagesize. In this example, it is {{ic|1=4009762816 / 4096 = 978946}}.
  
# mkinitcpio -p linux
+
=== Configure the initramfs ===
  
{{Note|If you use a custom kernel, then you might have to change the value of the {{ic|-p}} option.}}
+
* When an [[initramfs]] with the {{ic|base}} hook is used, which is the default, the {{ic|resume}} hook is required in {{ic|/etc/mkinitcpio.conf}}. Whether by label or by UUID, the swap partition is referred to with a udev device node, so the {{ic|resume}} hook must go ''after'' the {{ic|udev}} hook. This example was made starting from the default hook configuration:
  
=== Delayed hibernation ===
+
:{{bc|1=HOOKS=(base udev autodetect keyboard modconf block filesystems '''resume''' fsck)}}
  
A useful feature for laptops is to suspend, but after some time, automatically wake up and hibernate. This may be desirable to prevent depleting the battery or for security reasons (e.g. when using full disk encryption). An example script is located [http://superuser.com/questions/298672/linuxhow-to-hibernate-after-a-period-of-sleep here]. See also [https://bbs.archlinux.org/viewtopic.php?pid=1420279#p1420279 this post] for an updated systemd sleep hook.
+
:Remember to [[regenerate the initramfs]] for these changes to take effect.
 +
 
 +
:{{Note|[[LVM]] users should add the {{ic|resume}} hook after {{ic|lvm2}}.}}
 +
 
 +
* When an initramfs with the {{ic|systemd}} hook is used, a resume mechanism is already provided, and no further hooks need to be added.
  
 
== Troubleshooting ==
 
== Troubleshooting ==
Line 153: Line 159:
 
You might want to tweak your '''DSDT table''' to make it work. See [[DSDT]] article
 
You might want to tweak your '''DSDT table''' to make it work. See [[DSDT]] article
  
=== VAIO Users ===
+
=== Suspend/hibernate does not work, or does not work consistently ===
 +
 
 +
There have been many reports about the screen going black without easily viewable errors or the ability to do anything when going into and coming back from suspend and/or hibernate. These problems have been seen on both laptops and desktops. This is not an official solution, but switching to an older kernel, especially the LTS-kernel, will probably fix this.
 +
 
 +
Also problem may arise when using hardware watchdog timer (disabled by default, see {{ic|1=RuntimeWatchdogSec=}} in {{man|5|systemd-system.conf|OPTIONS}}). Bugged watchdog timer may reset the computer before the system finished creating the hibernation image.
 +
 
 +
Sometimes the screen goes black due to device initialization from within the initramfs. Removing any modules you might have in [[Mkinitcpio#MODULES]] and rebuilding the initramfs, can possibly solve this issue, specially graphics drivers for [[Kernel_mode_setting#Early_KMS_start|early KMS]]. Initializing such devices before resuming can cause inconsistencies that prevents the system resuming from hibernation. This does not affect resuming from RAM. Also, check the blog article [https://01.org/blogs/rzhang/2015/best-practice-debug-linux-suspend/hibernate-issues best practices to debug suspend issues].
 +
 
 +
Moving from the [[radeon]] video driver to the newer [[AMDGPU]] driver could also help to make the hibernation and awakening process successful.
 +
 
 +
For Intel graphics drivers, enabling early KMS may help to solve the blank screen issue. Refer to [[Kernel mode setting#Early KMS start]] for details.
 +
 
 +
After upgrading to kernel 4.15.3, resume may fail with a static (non-blinking) cursor on a black screen. [[Blacklisting]] the module {{ic|nvidiafb}} might help. [https://bbs.archlinux.org/viewtopic.php?id=234646]
  
Add acpi_sleep=nonvs kernel flag to your loader, and you are done!
+
Laptops with Intel CPU that load {{ic|intel_lpss_pci}} module for touchpad, may face kernel panic on resume (blinking caps lock) [https://bbs.archlinux.org/viewtopic.php?id=231881]. The module needs to be added to [[initramfs]] as:
  
=== Suspend/hibernate doesn't work ===
+
{{hc|head=/etc/mkinitcpio.conf|output=
 +
MODULES=(... intel_lpss_pci ...)
 +
}}
  
There have been many reports about the screen going black without easily viewable errors or the ability to do anything when going into and coming back from suspend and/or hibernate. These problems have been seen on both laptops and desktops. This is not an official solution, but switching to an older kernel, especially the LTS-kernel, will probably fix this.
+
Then [[regenerate the initramfs]].
 +
 
 +
=== Wake-on-LAN ===
 +
 
 +
If [[Wake-on-LAN]] is active, the network interface card will consume power even if the computer is hibernated.
 +
 
 +
=== Instantaneous wakeups from suspend ===
 +
 
 +
For some Intel Haswell systems with the LynxPoint and LynxPoint-LP chipset, instantaneous wakeups after suspend are reported. They are linked to erroneous BIOS ACPI implementations and how the {{ic|xhci_hcd}} module interprets it during boot. As a work-around reported affected systems are added to a blacklist (named {{ic|XHCI_SPURIOUS_WAKEUP}}) by the kernel case-by-case.[https://bugzilla.kernel.org/show_bug.cgi?id=66171#c6]
 +
 
 +
Instantaneous resume may happen, for example, if a USB device is plugged during suspend and ACPI wakeup triggers are enabled. A viable work-around for such a system, if it is not on the blacklist yet, is to disable the wakeup triggers. An example to disable wakeup through USB is described as follows.[https://bbs.archlinux.org/viewtopic.php?pid=1575617]
 +
 
 +
To view the current configuration:
 +
 
 +
{{hc|$ cat /proc/acpi/wakeup|
 +
Device  S-state  Status  Sysfs node
 +
...
 +
EHC1      S3    *enabled  pci:0000:00:1d.0
 +
EHC2      S3    *enabled  pci:0000:00:1a.0
 +
XHC      S3    *enabled  pci:0000:00:14.0
 +
...
 +
}}
 +
 
 +
The relevant devices are {{ic|EHC1}}, {{ic|EHC2}} and {{ic|XHC}} (for USB 3.0). To toggle their state you have to echo the device name to the file as root.
 +
 
 +
# echo EHC1 > /proc/acpi/wakeup
 +
# echo EHC2 > /proc/acpi/wakeup
 +
# echo XHC > /proc/acpi/wakeup
 +
 
 +
This should result in suspension working again. However, this settings are only temporary and would have to be set at every reboot. To automate this take a look at [[systemd#Writing unit files]]. See [https://bbs.archlinux.org/viewtopic.php?pid=1575617#p1575617 BBS thread] for a possible solution and more information.
 +
 
 +
If you use {{ic|nouveau}} driver, the reason of instantaneous wakeup may be a bug in that driver, which sometimes prevents graphics card from suspension. One possible workaround is unloading {{ic|nouveau}} kernel module right before going to sleep and loading it back after wakeup. To do this, create the following script:
 +
 
 +
{{hc|/usr/lib/systemd/system-sleep/10-nouveau.sh|2=
 +
#!/bin/bash
 +
 
 +
case $1/$2 in
 +
  pre/*)
 +
    # echo "Going to $2..."
 +
    /usr/bin/echo "0" > /sys/class/vtconsole/vtcon1/bind
 +
    /usr/bin/rmmod nouveau
 +
    ;;
 +
  post/*)
 +
    # echo "Waking up from $2..."
 +
    /usr/bin/modprobe nouveau
 +
    /usr/bin/echo "1" > /sys/class/vtconsole/vtcon1/bind
 +
    ;;
 +
esac
 +
}}
 +
 
 +
The first echo line unbinds nouveaufb from the framebuffer console driver (fbcon). Usually it is {{ic|vtcon1}} as in this example, but it may also be another {{ic|vtcon*}}. See {{ic|/sys/class/vtconsole/vtcon*/name}} which one of them is a "frame buffer device" [https://nouveau.freedesktop.org/wiki/KernelModeSetting/].
 +
 
 +
=== System does not power off when hibernating ===
 +
 
 +
When you hibernate your system, the system should power off (after saving the state on the disk). Sometimes, you might see the power LED is still glowing. If that happens, it might be instructive to set the {{ic|HibernateMode}} to {{ic|shutdown}} in {{man|5|sleep.conf.d}}:
 +
 
 +
{{hc|/etc/systemd/sleep.conf.d/hibernatemode.conf|2=
 +
[Sleep]
 +
HibernateMode=shutdown
 +
}}
 +
 
 +
With the above configuration, if every thing else is setup correctly, on invocation of a {{ic|systemctl hibernate}} the machine will shutdown saving state to disk as it does so.

Latest revision as of 08:43, 14 March 2020

Currently there are three methods of suspending available: suspend to RAM (usually called just suspend), suspend to disk (usually known as hibernate), and hybrid suspend (sometimes aptly called suspend to both):

  • Suspend to RAM method cuts power to most parts of the machine aside from the RAM, which is required to restore the machine's state. Because of the large power savings, it is advisable for laptops to automatically enter this mode when the computer is running on batteries and the lid is closed (or the user is inactive for some time).
  • Suspend to disk method saves the machine's state into swap space and completely powers off the machine. When the machine is powered on, the state is restored. Until then, there is zero power consumption.
  • Suspend to both method saves the machine's state into swap space, but does not power off the machine. Instead, it invokes usual suspend to RAM. Therefore, if the battery is not depleted, the system can resume from RAM. If the battery is depleted, the system can be resumed from disk, which is much slower than resuming from RAM, but the machine's state has not been lost.

There are multiple low level interfaces (backends) providing basic functionality, and some high level interfaces providing tweaks to handle problematic hardware drivers/kernel modules (e.g. video card re-initialization).

Low level interfaces

Though these interfaces can be used directly, it is advisable to use some of high level interfaces to suspend/hibernate. Using low level interfaces directly is significantly faster than using any high level interface, since running all the pre- and post-suspend hooks takes time, but hooks can properly set hardware clock, restore wireless etc.

kernel (swsusp)

The most straightforward approach is to directly inform the in-kernel software suspend code (swsusp) to enter a suspended state; the exact method and state depends on the level of hardware support. On modern kernels, writing appropriate strings to /sys/power/state is the primary mechanism to trigger this suspend.

See kernel documentation for details.

uswsusp

The uswsusp ('Userspace Software Suspend') is a wrapper around the kernel's suspend-to-RAM mechanism, which performs some graphics adapter manipulations from userspace before suspending and after resuming.

See main article Uswsusp.

High level interfaces

The end goal of these packages is to provide binaries/scripts that can be invoked to perform suspend/hibernate. Actually hooking them up to power buttons or menu clicks or laptop lid events is usually left to other tools. To automatically suspend/hibernate on certain power events, such as laptop lid close or battery depletion percentage, you may want to look into running Acpid.

systemd

systemd provides native commands for suspend, hibernate and a hybrid suspend, see Power management#Power management with systemd for details. This is the default interface used in Arch Linux.

See Power management#Sleep hooks for additional information on configuring suspend/hibernate hooks. Also see systemctl(1), systemd-sleep(8), and systemd.special(7).

Hibernation

In order to use hibernation, you need to create a swap partition or file. You will need to point the kernel to your swap using the resume= kernel parameter, which is configured via the boot loader. You will also need to configure the initramfs. This tells the kernel to attempt resuming from the specified swap in early userspace. These three steps are described in detail below.

Note:

About swap partition/file size

Even if your swap partition is smaller than RAM, you still have a big chance of hibernating successfully. According to kernel documentation:

/sys/power/image_size controls the size of the image created by the suspend-to-disk mechanism. It can be written a string representing a non-negative integer that will be used as an upper limit of the image size, in bytes. The suspend-to-disk mechanism will do its best to ensure the image size will not exceed that number. However, if this turns out to be impossible, it will try to suspend anyway using the smallest image possible. In particular, if "0" is written to this file, the suspend image will be as small as possible. Reading from this file will display the current image size limit, which is set to 2/5 of available RAM by default.

You may either decrease the value of /sys/power/image_size to make the suspend image as small as possible (for small swap partitions), or increase it to possibly speed up the hibernation process. See Systemd#Temporary files to make this change persistent.

The suspend image cannot span multiple swap partitions and/or swap files. It must fully fit in one swap partition or one swap file.[1]

Required kernel parameters

The kernel parameter resume=swap_device must be used. Any of the persistent block device naming methods can be used as swap_device. For example:

  • resume=UUID=4209c845-f495-4c43-8a03-5363dd433153
  • resume="PARTLABEL=Swap partition"
  • resume=/dev/archVolumeGroup/archLogicalVolume -- if swap is on a LVM logical volume

Generally, it is preferred to use the same naming method for both the resume and root parameters.

Hibernation into swap file

Warning: Btrfs on Linux kernel before version 5.0 does not support swap files. Failure to heed this warning may result in file system corruption. While a swap file may be used on Btrfs when mounted through a loop device, this will result in severely degraded swap performance.

Using a swap file instead of a swap partition requires an additional kernel parameter resume_offset=swap_file_offset.

Note: The resume parameter must point to the block device where the swap file resides. For a stacked block device such as an encrypted container, RAID or LVM, it means that resume must point to the unlocked/mapped device that contains the file system with the swap file.

The value of swap_file_offset can be obtained by running filefrag -v swap_file, the output is in a table format and the required value is located in the first row of the physical_offset column. For example:

# filefrag -v /swapfile
Filesystem type is: ef53
File size of /swapfile is 4294967296 (1048576 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..       0:      38912..     38912:      1:            
   1:        1..   22527:      38913..     61439:  22527:             unwritten
   2:    22528..   53247:     899072..    929791:  30720:      61440: unwritten
...

In the example the value of swap_file_offset is the first 38912 with the two periods.

Tip:
  • The following command may be used to identify swap_device: findmnt -no SOURCE -T /swapfile
  • The following command may be used to identify swap_file_offset: filefrag -v /swapfile | awk '{ if($1=="0:"){print $4} }'
  • The value of swap_file_offset can also be obtained by running swap-offset swap_file. The swap-offset binary is provided within the set of tools uswsusp. If using this method, then these two parameters have to be provided in /etc/suspend.conf via the keys resume device and resume offset. No reboot is required in this case.
Note: The kernel parameters will only take effect after rebooting. To be able to hibernate right away, obtain the volume's major and minor device numbers from lsblk and echo them in format major:minor to /sys/power/resume and the resume offset to /sys/power/resume_offset. For example, if the swap file is on volume 8:2 and has the offset 38912:
# echo 8:2 > /sys/power/resume
# echo 38912 > /sys/power/resume_offset
See https://www.kernel.org/doc/Documentation/power/swsusp.txt.
Tip: You might want to decrease the swappiness for your swapfile if the only purpose is to be able to hibernate and not expand RAM.

Hibernation into swap file on Btrfs

Hibernation onto a swapfile is supported by recent versions of systemd [2].

The resume_offset number can be computed using the tool btrfs_map_physical.c. Do not try to use the filefrag tool, on Btrfs the "physical" offset you get from filefrag is not the real physical offset on disk; there is a virtual disk address space in order to support multiple devices. [3]

Download or copy the tool btrfs_map_physical.c into a file named btrfs_map_physical.c, then compile it,

$ gcc -O2 -o btrfs_map_physical btrfs_map_physical.c

and run it. An example output is shown below.

# ./btrfs_map_physical /path/to/swapfile
FILE OFFSET  EXTENT TYPE  LOGICAL SIZE  LOGICAL OFFSET  PHYSICAL SIZE  DEVID  PHYSICAL OFFSET
0            regular      4096          2927632384      268435456      1      4009762816
4096         prealloc     268431360     2927636480      268431360      1      4009766912
268435456    prealloc     268435456     3251634176      268435456      1      4333764608
536870912    prealloc     268435456     3520069632      268435456      1      4602200064
805306368    prealloc     268435456     3788505088      268435456      1      4870635520
1073741824   prealloc     268435456     4056940544      268435456      1      5139070976
1342177280   prealloc     268435456     4325376000      268435456      1      5407506432
1610612736   prealloc     268435456     4593811456      268435456      1      5675941888

Note the the first physical offset returned by this tool. In this example, we use 4009762816. Also note the pagesize that can be found with getconf PAGESIZE.

To compute the resume_offset value, divide the physical offset by the pagesize. In this example, it is 4009762816 / 4096 = 978946.

Configure the initramfs

  • When an initramfs with the base hook is used, which is the default, the resume hook is required in /etc/mkinitcpio.conf. Whether by label or by UUID, the swap partition is referred to with a udev device node, so the resume hook must go after the udev hook. This example was made starting from the default hook configuration:
HOOKS=(base udev autodetect keyboard modconf block filesystems resume fsck)
Remember to regenerate the initramfs for these changes to take effect.
Note: LVM users should add the resume hook after lvm2.
  • When an initramfs with the systemd hook is used, a resume mechanism is already provided, and no further hooks need to be added.

Troubleshooting

ACPI_OS_NAME

You might want to tweak your DSDT table to make it work. See DSDT article

Suspend/hibernate does not work, or does not work consistently

There have been many reports about the screen going black without easily viewable errors or the ability to do anything when going into and coming back from suspend and/or hibernate. These problems have been seen on both laptops and desktops. This is not an official solution, but switching to an older kernel, especially the LTS-kernel, will probably fix this.

Also problem may arise when using hardware watchdog timer (disabled by default, see RuntimeWatchdogSec= in systemd-system.conf(5)). Bugged watchdog timer may reset the computer before the system finished creating the hibernation image.

Sometimes the screen goes black due to device initialization from within the initramfs. Removing any modules you might have in Mkinitcpio#MODULES and rebuilding the initramfs, can possibly solve this issue, specially graphics drivers for early KMS. Initializing such devices before resuming can cause inconsistencies that prevents the system resuming from hibernation. This does not affect resuming from RAM. Also, check the blog article best practices to debug suspend issues.

Moving from the radeon video driver to the newer AMDGPU driver could also help to make the hibernation and awakening process successful.

For Intel graphics drivers, enabling early KMS may help to solve the blank screen issue. Refer to Kernel mode setting#Early KMS start for details.

After upgrading to kernel 4.15.3, resume may fail with a static (non-blinking) cursor on a black screen. Blacklisting the module nvidiafb might help. [4]

Laptops with Intel CPU that load intel_lpss_pci module for touchpad, may face kernel panic on resume (blinking caps lock) [5]. The module needs to be added to initramfs as:

/etc/mkinitcpio.conf
MODULES=(... intel_lpss_pci ...)

Then regenerate the initramfs.

Wake-on-LAN

If Wake-on-LAN is active, the network interface card will consume power even if the computer is hibernated.

Instantaneous wakeups from suspend

For some Intel Haswell systems with the LynxPoint and LynxPoint-LP chipset, instantaneous wakeups after suspend are reported. They are linked to erroneous BIOS ACPI implementations and how the xhci_hcd module interprets it during boot. As a work-around reported affected systems are added to a blacklist (named XHCI_SPURIOUS_WAKEUP) by the kernel case-by-case.[6]

Instantaneous resume may happen, for example, if a USB device is plugged during suspend and ACPI wakeup triggers are enabled. A viable work-around for such a system, if it is not on the blacklist yet, is to disable the wakeup triggers. An example to disable wakeup through USB is described as follows.[7]

To view the current configuration:

$ cat /proc/acpi/wakeup
Device  S-state   Status   Sysfs node
...
EHC1      S3    *enabled  pci:0000:00:1d.0
EHC2      S3    *enabled  pci:0000:00:1a.0
XHC       S3    *enabled  pci:0000:00:14.0
...

The relevant devices are EHC1, EHC2 and XHC (for USB 3.0). To toggle their state you have to echo the device name to the file as root.

# echo EHC1 > /proc/acpi/wakeup
# echo EHC2 > /proc/acpi/wakeup
# echo XHC > /proc/acpi/wakeup

This should result in suspension working again. However, this settings are only temporary and would have to be set at every reboot. To automate this take a look at systemd#Writing unit files. See BBS thread for a possible solution and more information.

If you use nouveau driver, the reason of instantaneous wakeup may be a bug in that driver, which sometimes prevents graphics card from suspension. One possible workaround is unloading nouveau kernel module right before going to sleep and loading it back after wakeup. To do this, create the following script:

/usr/lib/systemd/system-sleep/10-nouveau.sh
#!/bin/bash

case $1/$2 in
  pre/*)
    # echo "Going to $2..."
    /usr/bin/echo "0" > /sys/class/vtconsole/vtcon1/bind
    /usr/bin/rmmod nouveau
    ;;
  post/*)
    # echo "Waking up from $2..."
    /usr/bin/modprobe nouveau
    /usr/bin/echo "1" > /sys/class/vtconsole/vtcon1/bind
    ;;
esac

The first echo line unbinds nouveaufb from the framebuffer console driver (fbcon). Usually it is vtcon1 as in this example, but it may also be another vtcon*. See /sys/class/vtconsole/vtcon*/name which one of them is a "frame buffer device" [8].

System does not power off when hibernating

When you hibernate your system, the system should power off (after saving the state on the disk). Sometimes, you might see the power LED is still glowing. If that happens, it might be instructive to set the HibernateMode to shutdown in sleep.conf.d(5):

/etc/systemd/sleep.conf.d/hibernatemode.conf
[Sleep]
HibernateMode=shutdown

With the above configuration, if every thing else is setup correctly, on invocation of a systemctl hibernate the machine will shutdown saving state to disk as it does so.