Quantcast
Viewing all articles
Browse latest Browse all 101

Update on hibernation in Fedora Workstation

Goals and rationale

Hibernation stores the state of the whole operating system — the contents of memory used by the kernel and all programs — on disk. The machine is then completely powered off. Upon next boot, this state is restored and the old kernel and all the programs that were running continue execution.

Hibernation is nowadays used less often, because “suspend” — the state where CPU is powered down, but the contents of memory are preserved, works fine on most laptops and other small devices. But if the suspend is implemented poorly and it drains the battery too quickly, or if the user needs to completely power off the device for some reasons, hibernation can still be useful.

We need a storage area for hibernation. The kernel allows two options:
– either a single large-enough swap device, usually a partition,
– or a single large-enough swap file on some file system.

Fedora Linux installations by default do not use a normal swap device or file. Instead, a zram device is created, which is an in-memory compressed swap area. It is not suitable for hibernation. This means that hibernation does not work out-of-the-box on Fedora Linux. This guide describes how to create a swap file to enable hibernation.

Limitations

This method only works on UEFI!

To check that the system uses UEFI:

bootctl

If this commands prints “Not booted with EFI”, then the method described below won’t work. Refer to the original Hibernation in Fedora Workstation (for Fedora Linux 36) instead.

Create and enable a swap file:

SWAPSIZE=$(free | awk '/Mem/ {x=$2/1024/1024; printf "%.0fG", (x<2 ? 2*x : x<8 ? 1.5*x : x) }')
sudo btrfs subvolume create /var/swap
sudo chattr +C /var/swap
sudo restorecon /var/swap
sudo mkswap --file -L SWAPFILE --size $SWAPSIZE /var/swap/swapfile
sudo bash -c 'echo /var/swap/swapfile none swap defaults 0 0 >>/etc/fstab'
sudo swapon -av
sudo bash -c 'echo add_dracutmodules+=\" resume \" > /etc/dracut.conf.d/resume.conf'
sudo dracut -f

This should print a message that swap was enabled on /var/swap/swapfile. The swap file is added to fstab, so it’ll be permanently active. This is a good thing, it should make the system more reliable in general.

Now we are ready to test hibernation:

systemctl hibernate

After the system has shut down, boot it again and let one of the kernels start. The machine should return to the previous state from before hibernation.

This method does not require further configuration because systemd automatically stores the location of the swap file before entering hibernation in an UEFI variable, and then after the reboot, reads that variable and instruct the kernel to resume from this location. This only works on UEFI systems, but is otherwise quite simple and robust.

Reverting the changes

sudo swapoff -v /var/swap/swapfile
sudo sed -r -i '/.var.swap.swapfile/d' /etc/fstab
sudo btrfs subvolume delete /var/swap
sudo rm /etc/dracut.conf.d/resume.conf

After that, reenable SecureBoot if appropriate.

Troubleshooting

This process mail fail in two ways:

  • either going into hibernation fails, i.e. the kernel does not save the state and the machine does not actually power off,
  • or loading of saved state fails, and we end up with a fresh boot.

In both cases, the first step is to look at journalctl -b, in particular any error lines.


Viewing all articles
Browse latest Browse all 101

Trending Articles