Suspend and hibernate

From Gentoo Wiki
Jump to: navigation, search
This page contains changes which are not marked for translation.


This article describes how to suspend or hibernate a Gentoo system.

Installation

Kernel

Make sure support for suspend and hibernation has been activated (CONFIG_SUSPEND) and (CONFIG_HIBERNATION):

KERNEL
Power management and ACPI options --->
    [*] Suspend to RAM and standby
    [*] Hibernation (aka 'suspend to disk')

Software

The following software can be used for in-kernel default suspend/hibernate implementation, namely, swsusp.

root #emerge --ask sys-power/suspend

Another alternative is sys-power/hibernate-script which can be used with swsusp.

Optional: Install upower and consolekit

Note
There has been no activity in the ConsoleKit repository since 2017 and elogind is the recommended replacement.

Some desktop environments might require sys-power/upower and ConsoleKit with USE="pm-utils" [1] for showing "Suspend" and "Hibernate" buttons in their menu.

root #emerge --ask sys-power/upower
root #echo "sys-auth/consolekit pm-utils" >> /etc/portage/package.use
root #emerge --ask --oneshot sys-auth/consolekit

Deploying elogind instead of ConsoleKit

sys-power/upower should be installed. sys-power/pm-utils should not be installed.

Note
You must make sure sys-auth/consolekit is not installed. It is not sufficient just to remove it from any runlevel, because D-Bus will launch ConsoleKit even if it has not been added to any runlevel.
root #emerge --ask --oneshot sys-auth/elogind

The elogind equivalents for the pm-utils commands pm-suspend, pm-hibernate and pm-suspend-hybrid are as follows, and can be launched as root user or from a user account:

root #loginctl suspend
root #loginctl hibernate
root #loginctl hybrid-sleep

When using elogind instead of ConsoleKit, copy any suspend/resume and hibernate/thaw hook scripts from the directory /etc/pm/sleep.d/ to the directory /lib64/elogind/system-sleep/ and modify them to cater for the new $1 ('pre' or 'post') and $2 ('suspend', 'hibernate', or 'hybrid-sleep'). For example, in the case of elogind a hook script could have the following format:

CODE
#!/bin/bash
case $1/$2 in
  pre/*)
    # Put here any commands you want to be run when suspending or hibernating.
    ;;
  post/*)
    # Put here any commands you want to be run when resuming from suspension or thawing from hibernation.
    ;;
esac

Available suspend modes

To see available suspend modes use

root #cat /sys/power/state

for swsusp, default implementation.

Those two file will list at least ACPI S2/4 power down methods on modern hardware. New hardware would also support S5 method which is a rough S4 method. ACPI S2 correspond to suspend to ram (ram method in swsusp terms and 3 in ToI terms); S4 hibernation to disk (disk in swsusp terms and 4 in ToI terms; S5 hibernation to disk (5 in ToI terms).

Swsusp users can choose between platfom, meaning ACPI, or shutdown methods which can be echo-ed to /sys/power/disk sysfs file.

Suspend to RAM

Preferred commands to suspend are:

root #pm-suspend

or

root #s2ram

or, if using elogind instead of ConsoleKit

root #loginctl suspend

For suspend (to ram) for hibernate-script users:

root #hibernate-ram

or

root #hibernate

to hibernate (to disk.)

A more raw method is to

root #echo mem > /sys/power/state

Suspend to disk

Warning

If you are using genkernel initramfs, you may experience bug #156445 which makes it impossible to resume after suspend-to-disk. There is a number of ways to avoid it, from editing initramfs (e.g. as described here) to dropping genkernel completely. sys-kernel/genkernel-next works just fine.

For suspend to disk to operate a swap partition or swap file must exist.

The swap file should be active beforehand and should be echoed on the appropriate file before any attempt to suspend/hibernate.

root #echo /dev/sda1 > /sys/power/resume

A more raw method is to:

root #echo disk > /sys/power/state

Do not forget to probe:

root #cat /sys/power/tuxonice/swap/headerlocations

before issuing an actual command and append the result to kernel command line argument prepended with `resume='. This will suffice to resume from a block device or swap file for ToI. However, it's more complicated for a swapfile with swsusp.

Suspend to disk with sys-power/pm-utils

Yet another way to achieve disk hibernation is to use hibernate to swap partition and pm-utils.

First, make sure a swap partition has been set:

root #swapon -s

For this example, we will assume it's /dev/sdc2

Edit /etc/default/grub and append the resume kernel option to GRUB_CMDLINE_LINUX_DEFAULT pointing to the swap partition.

FILE /etc/default/grubGRUB Config
GRUB_CMDLINE_LINUX_DEFAULT="resume=/dev/sdc2"

Rebuild the GRUB config:

root #grub-mkconfig -o /boot/grub/grub.cfg

Update initramfs:

root #genkernel --install initramfs

Add the following to /etc/pm/config.d/gentoo (see bug 338239):

FILE /etc/pm/config.d/gentoo
SLEEP_MODULE="kernel"

Reboot the system:

root #reboot

Next, try

root #pm-hibernate

Suspend to disk with sys-auth/elogind

First, make sure a swap partition has been set, grub.cfg rebuilt and the initramfs (if any) updated as shown above.

Reboot the system:

root #loginctl reboot

Next, try

root #loginctl hibernate

Suspend to disk with swapfile

You can use suspend to disk with a swapfile. When you have a functional swapfile you need to configure kernel parameters (via GRUB, etc.).

First find UUID of device where your swapfile resides. For example: /dev/sda1.

Note
If swapfile resides in a LVM volume, GRUB must be compiled with LVM support. Otherwise, the system will not wake up and will be cold started.
root #blkid /dev/sda1

Find offset of swapfile on given partition using swap-offset utility from sys-power/suspend:

root #swap-offset /path/to/swapfile

After that edit GRUB config and add required parameters to boot string:

FILE /etc/defaults/grubGRUB defaults
GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=<UUID_of_partition> resume_offset=<offset_of_swapfile>"

Rebuild GRUB config:

root #grub-mkconfig -o /boot/grub/grub.cfg

Reboot the system and check used kernel parameters:

user $cat /proc/cmdline

It should now be possible to hibernate the system.

Note
Remember, swap file must contain all memory used by running processes and memory based filesystems like tmpfs or zram prior to hibernating. However, unless specifically set, the hibernation image is compressed. Setting hibernation image size to half of the amount of installed RAM is a safe value in most cases. One of the cases where this does not fully apply is when the system has a high usage of zswap which means that memory may already be compressed.

Troubleshooting

If troubleshooting suspend the powersave log might be useful:

root #less /var/log/pm-powersave.log

Also classic kernel buffer comes handy:

user $dmesg

Can not resume after suspend

In case resuming from suspend does not work, disable the security chip setting in BIOS/UEFI and try again.

Make sure you change the /etc/pm/config.d/gentoo file as described above.

See also

External resources

References