How to use swap file for hibernation and hybrid sleep


What are hibernation and hybrid sleep:
Sleep is a power-saving state where current state (opened programs and documents) is in RAM. It still drain a little battery, but resuming the session is quick.
Hibernation is a power-saving state where current state is saved in hard drive (in swap). It doesn’t drain battery but is slower to resume session.
Hybrid sleep is a combination of sleep and hibernation, current state is save in RAM and swap, so resuming session is quick, but in case of power outage or battery low, you can resume your session from swap file.

For being able to use hibernation of hybrid sleep, you need at least as much swap as you have RAM. In case you have chosen to create a swap file or partition during the installation, without hibernation option, then your swap size is too small. You need to unactivate it with sudo swapoff -a and create a new swap file by following this how-to.

Note: this document focus on swap file because the installer already have an option for having a swap partition with hibernation.

Verify that hibernates works on your computer

cat /sys/power/state

if the output includes disk it should be good.

Add resume module to dracut

kate /etc/dracut.conf

insert this line at the bottom (keep the blanks around resume)

add_dracutmodules+=" resume "

and save/quit.

Find the UUID and offset of your swap file

By default, the installer creates a swap partition as /swapfile. If you doubt about the name of the swap file, get it with cat /proc/swaps

Find out the UUID (identifier) of the partition on which the swap file is located

findmnt -no UUID -T /swapfile

Get the offset (ie the file position as maintained by the Linux kernel) of the swap file

sudo filefrag -v /fedora.swap | head -n 4 | tail -n 1 | awk '{print $4}'

You should get something like 35391488..

Configure the session resume in the boot manager (GRUB)

Edit /etc/default/grub in order to boot with the resume and resume offset kernel parameters.

:warning: Make sure your edits of this file are correct, or else your computer may fail to boot!

kate /etc/default/grub

In this file, at the end of the GRUB_CMDLINE_LINUX_DEFAULT line, before the ending " , add the following:


Replace the values with the correct UUID and offset values.

For instance

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash logo.nologo acpi_osi=Linux acpi_osi='!Windows 2012' acpi_backlight=vendor audit=0 rd.timeout=120 dm_mod.use_blk_mq=1 rd.systemd.show_status=0 systemd.show_status=0 resume=UUID=df134229-679f-413c-bef8-30e52675aa5b resume_offset=35391488"

Then update GRUB with the new configuration file

sudo grub2-mkconfig -o /boot/grub2/grub.cfg


Finally test with command line

systemctl hibernate

or with the menu which should now integrate hibernate option (a snow flake with zzz)

Note: you can check (or tune) the behaviour in case of low battery with this file /etc/UPower/UPower.conf

# If HybridSleep isn't available, Hibernate will be used
# If Hibernate isn't available, PowerOff will be used