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

From ArchWiki
Jump to: navigation, search
m (Kernel)
m (mismatched parentheses)
(20 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
[[Category:Power management]]
 
[[Category:Power management]]
 
{{Article summary start}}
 
{{Article summary start}}
{{Article summary text|Describes suspend(sleep-to-ram), hibernate(sleep-to-disk)}}
+
{{Article summary text|Describes suspending (suspend-to-RAM) and hibernating (suspend-to-disk) the system.}}
 
{{Article summary heading|Related articles}}
 
{{Article summary heading|Related articles}}
{{Article summary wiki|Suspend to RAM}}
 
{{Article summary wiki|Systemd}}
 
{{Article summary wiki|pm-utils}}
 
 
{{Article summary wiki|Uswsusp}}
 
{{Article summary wiki|Uswsusp}}
{{Article summary wiki|Tuxonice}}
+
{{Article summary wiki|TuxOnIce}}
 +
{{Article summary wiki|systemd}}
 +
{{Article summary wiki|pm-utils}}
 
{{Article summary wiki|Suspending to RAM with hibernate-script}}
 
{{Article summary wiki|Suspending to RAM with hibernate-script}}
 
{{Article summary wiki|Suspending to Disk with hibernate-script}}
 
{{Article summary wiki|Suspending to Disk with hibernate-script}}
 
{{Article summary end}}
 
{{Article summary end}}
 +
Currently there are available three methods of suspending: '''suspend to RAM''' (usually called just '''suspend'''), '''suspend to disk''' (usually known as '''hibernate'''), and '''hybrid suspend''' (sometimes aptly called '''suspend to both'''):
  
Manually suspending the operating system, either to memory (standby) or to disk (hibernate) sometimes provides the most efficient way to optimize battery life, depending on the usage pattern of the laptop. While there is relatively straightforward support in the linux kernel to support these operations, typically some adjustments have to made before initiating these operations (typically due to problematic drivers, modules or hardware). The following tools provide wrappers around the kernel interfaces to suspend/resume
+
* '''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).
  
* Wrappers: Systemd, pm-utils
+
* '''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.
* Backend: kernel, uswsusp, tuxonice
+
  
=Backend=
+
* '''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.
  
==Kernel==
+
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).
  
===Suspend===
+
== Low level interfaces ==
  
works-out-of-box
+
Though these interfaces can be used directly, it is advisable to use some of [[#High level interfaces|high level interfaces]] to suspend/hibernate. Using low level interfaces directly is significantly faster 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.
  
===Hibernation===
+
=== kernel (swsusp) ===
  
It needs a swap partiton.
+
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 {{ic|/sys/power/state}} is the primary mechanism to trigger this suspend.
  
'''About swap partition size'''
+
See [https://www.kernel.org/doc/Documentation/power/states.txt kernel documentation] for details.
  
Even if your swap partition is smaller than RAM, you still have a big chance in hibernating successfully. According to [http://www.mjmwired.net/kernel/Documentation/power/interface.txt kernel documentation]: "''{{ic|/sys/power/image_size}} controls the size of the image created by the suspend-to-disk mechanism [...] which is set to 2/5 of available RAM by default. [...] The suspend-to-disk mechanism will do its best to ensure the image size will not exceed that number.''" You may either decrease it due to a small swap partition or increase it in purpose of possible hibernation speed up.
+
=== uswsusp ===
  
'''Add Kernel Parameters'''
+
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.
  
The parameter {{ic|1=resume=SWAP_PARTITION}} has to be passed to the [[Kernel parameters]] (where {{ic|SWAP_PARTITION}} is either your swap device, i.e. {{ic|/dev/sda'''X'''}}, or your swap UUID). Consult [[Kernel parameters]] for more info.
+
See main article [[Uswsusp]].
  
  kernel /boot/vmlinuz-linux ... resume=/dev/sdaX
+
=== tuxonice ===
  
If you are using swap file instead of regular swap partition, read [[Swap#Swap file resuming]] for more info.
+
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.
  
  kernel /boot/vmlinuz-linux ... resume=/dev/sdaX resume_offset=12345
+
See main article [[TuxOnIce]].
  
You will also need to add the {{ic|resume}} hook to {{ic|mkinitcpio.conf}}:
+
== High level interfaces ==
  
{{hc|/etc/mkinitcpio.conf|2=# resume must be placed after ide scsi sata and/or lvm2, but before filesystems
+
{{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]].}}
HOOKS="... ide scsi sata lvm2 '''''resume''''' filesystems"}}
+
  
You must rebuild the initrd image for these changes to take effect:
+
=== systemd ===
  
{{bc|# mkinitcpio -p linux}}
+
[[systemd]] provides native commands for suspend, hibernate and a hybrid suspend, see [[Power Management#Power management with systemd]] for details.
  
{{Note|If you use a custom kernel, then you might have to change the value of the {{ic|-p}} option.}}
+
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}}.
  
==Uswsusp==
+
=== pm-utils ===
  
'''uswsusp''' (userspace software suspend) is a set of user space tools used for hibernation (suspend-to-disk) and suspend (suspend-to-RAM or standby) on Linux systems.
+
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.
  
Consulte [[Uswsusp]].
+
See main article [[pm-utils]].
  
==TuxOnIce==
+
== Suspend to RAM ==
  
'''TuxOnIce''' , an advanced suspend/hibernate framework which supports suspending to a swap-disk or a regular file with fast LZO-compression.
+
Suspend to RAM should work out of the box.
  
Consulte [[TuxOnIce]].
+
== Hibernation ==
  
=Wrappers=
+
In order to use hibernation, you need to create swap partition or swap file. See [[Swap]] for details.
  
==Systemd==
+
=== About swap partition/file size ===
  
'''Systemd''' handles some power-related ACPI events.
+
Even if your swap partition is smaller than RAM, you still have a big chance of hibernating successfully. According to [https://www.kernel.org/doc/Documentation/power/interface.txt kernel documentation]:
  
  # systemctl suspend  
+
: ''{{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.''
  # systemctl hibernate
+
  
Consulte [[Systemd#ACPI Power Management with systemd]]
+
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.
  
==pm-utils==
+
=== Required kernel parameters ===
 +
 
 +
The kernel parameter {{ic|1=resume=<swap_partition>}} has to be used. As {{ic|<swap_partition>}} you can use either kernel name of the swap partition, i.e. {{ic|/dev/sda1}}, or the [[UUID]] of the swap partition. See [[Kernel parameters]] for more examples for various bootloaders.
 +
 
 +
For example, with GRUB2 you can use {{ic|GRUB_CMDLINE_LINUX}} variable:
 +
 
 +
{{hc|/etc/default/grub|2=GRUB_CMDLINE_LINUX="resume=/dev/sda1"}}
 +
 
 +
And if you use UUID instead:
 +
 
 +
{{hc|/etc/default/grub|2=GRUB_CMDLINE_LINUX="resume=UUID=4209c845-f495-4c43-8a03-5363dd433153"}}
 +
 
 +
Don't forget to run {{ic|grub-mkconfig -o /boot/grub/grub.cfg}} afterwards.
 +
 
 +
{{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.}}
 +
 
 +
==== Hibernation into swap file ====
 +
 
 +
Using a swap file instead of a swap partition requires an additional kernel parameter {{ic|1=resume_offset=<Swap File Offset>}}.
 +
 
 +
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:
 +
{{hc|# filefrag -v /swapfile|<nowiki>
 +
ilesystem 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}}.
 +
 
 +
{{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}}.}}
 +
 
 +
{{Note|
 +
* Please note that in the kernel parameter {{ic|resume}} you have to provide the device of the partition that contains the swapfile, not swapfile itself! The parameter {{ic|resume_offset}} informs the system where the swapfile 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 ===
 +
 
 +
If you use an initramfs (default Arch systems do), you must add the {{ic|resume}} hook into the HOOKS in the configuration of [[mkinitcpio]]:
 +
 
 +
{{hc|/etc/mkinitcpio.conf|2=
 +
# resume must be placed after block and lvm2, but before filesystems
 +
HOOKS="... block lvm2 '''resume''' filesystems ..."
 +
}}
 +
 
 +
Finally, you must rebuild the initrd image for these changes to take effect:
 +
 
 +
# mkinitcpio -p linux
 +
 
 +
{{Note|If you use a custom kernel, then you might have to change the value of the {{ic|-p}} option.}}
  
'''pm-utils''' is a suspend and powerstate setting framework.
+
== Troubleshooting ==
  
  # pm-suspend
+
=== ACPI_OS_NAME ===
  # pm-hibernate
+
  
Consulte [[pm-utils]]
+
You might want to tweak your '''DSDT table''' to make it work. See [[DSDT]] article

Revision as of 04:30, 13 August 2013

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end Currently there are available three methods of suspending: 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 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.

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

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.

systemd

systemd provides native commands for suspend, hibernate and a hybrid suspend, see Power Management#Power management with systemd for details.

See Power Management#Sleep hooks for additional information on configuring suspend/hibernate hooks. Also see man systemctl, man systemd-sleep, and man systemd.special.

pm-utils

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.

See main article pm-utils.

Suspend to RAM

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

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.

Required kernel parameters

The kernel parameter resume=<swap_partition> has to be used. As <swap_partition> you can use either kernel name of the swap partition, i.e. /dev/sda1, or the UUID of the swap partition. See Kernel parameters for more examples for various bootloaders.

For example, with GRUB2 you can use GRUB_CMDLINE_LINUX variable:

/etc/default/grub
GRUB_CMDLINE_LINUX="resume=/dev/sda1"

And if you use UUID instead:

/etc/default/grub
GRUB_CMDLINE_LINUX="resume=UUID=4209c845-f495-4c43-8a03-5363dd433153"

Don't forget to run grub-mkconfig -o /boot/grub/grub.cfg afterwards.

Tip: As an alternative, for GRUB2 you may try this solution from debian wiki, which automatically adds your first swap partition to resume= parameter to all found linux entries.

Hibernation into swap file

Using a swap file instead of a swap partition requires an additional kernel parameter resume_offset=<Swap File Offset>.

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
ilesystem 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 38912.

Tip: The value of <Swap File Offset> can also be obtained by running swap-offset <Swap File>. The swap-offset binary is provided by package uswsusp-gitAUR.
Note:
  • Please note that in the kernel parameter resume you have to provide the device of the partition that contains the swapfile, not swapfile itself! The parameter resume_offset informs the system where the swapfile starts on the resume device.
  • If using uswsusp, then these two parameters have to be provided in /etc/suspend.conf via the keys resume device and resume offset.

Recreate initial ramdisk

If you use an initramfs (default Arch systems do), you must add the resume hook into the HOOKS in the configuration of mkinitcpio:

/etc/mkinitcpio.conf
# resume must be placed after block and lvm2, but before filesystems
HOOKS="... block lvm2 resume filesystems ..."

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

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

Troubleshooting

ACPI_OS_NAME

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