Hibernation, also known as “suspend to disk”, is the most efficient power saving mode in terms of energy consumption. On hibernation, the state of the random access memory is stored on disk, and the machine is completely power down. Although efficient, hibernation is commonly not recommended if using a solid state drive, because each time the system enters this power state, a lot of data must be written to disk, which as we know, has a limited number of write cycles. For this and other reasons, as the the low number of machines on which hibernation works reliably on Linux, Fedora decided to disable this power state by default.
In this tutorial we see how to restore hibernation on recent versions of Fedora.
In this tutorial you will learn:
- What is hibernation and why Fedora decided to disable it
- How to enable hibernation on recent versions of Fedora
- How to disable zram on Fedora
Software requirements and conventions used
|Category||Requirements, Conventions or Software Version Used|
|Software||No specific software needed|
|Conventions||# – requires given linux-commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux-commands to be executed as a regular non-privileged user
Hibernation is know as P4 sleeping state in the ACPI terminology. Under Linux, when the system is put into this state, all the content of the RAM is compressed and saved on the disk, in a swap partition, which must be big enough to accommodate it. The big advantage of this state is that, after the image is stored on the disk, the machine is completely powered down, so it is ideal to preserve battery power while preserving opened applications. When the system is powered on, if everything goes as expected, the image is reloaded into RAM, so that the user can resume its work as he never left it.
Hibernation is usually discouraged when using a solid state drive, due to how this type of support works. SSD sectors have a limited number of read-write cycles, and hibernation usually requires a lot of data being written to disk. This is only one of the reasons why hibernation has been disabled on Fedora. Here is a summary of the other reasons:
- Hibernation on Linux is not compatible with secure boot
- Hibernation is hard to implement and doesn’t always work reliably
- Hibernation requires a quite large swap partition (depending on the ram size)
- Saving ram to disk can be dangerous from a security point of view, if swap is not encrypted
When UEFI Secure Boot is activate, the machine firmware verifies that the kernel of a distribution is signed and trusted, and when booting normally Fedora obviously passes the test. When rebooting after hibernating the system, the entire content of the memory is replaced with the image previously stored on the swap space, which cannot be verified. This is why, at least currently, hibernation is incompatible with secure boot on Linux.
Hibernation is also quite hard to implement often due to ACPI bugs which exist at the firmware level, so it doesn’t work reliably on every machine, and if resuming after hibernation doesn’t work the user can loose data.
In order for hibernation to work, a swap partition must be created on the disk; its dimension varies depending on the available RAM size. The recommended dimensions suggested by Red Hat, if one wants to enable hibernation, are the following:
|RAM SIZE||RECOMMENDED SWAP SPACE WITH HIBERNATION|
|≤ 2GB||3X RAM|
|2GB – 8GB||2X RAM|
|8GB – 64GB||1.5X RAM|
|>64GB||Hibernation not recommended|
On recent versions of Fedora the Anaconda installer doesn’t create a swap partition by default, because Fedora switched to
zram. What is zram? Zram is a Linux kernel module which creates a compressed block device in RAM: basically, when the system needs to swap, instead of storing data to a swap partition on the disk, which is slow, it keeps data in RAM, but compresses it in the zram block device. Since RAM is volatile, however, the zram block device cannot be used for hibernation, therefore a traditional swap partition must be created.
To verify Fedora is using zram, we can issue the following command:
$ swapon --show NAME TYPE SIZE USED PRIO /dev/zram0 partition 7.6G 25.8M 100
From the output of the command, we can clearly see swap is implemented on the
Hibernation, undoubtedly has also its pros, since, as we already said, it is the most efficient energy saving mode. If we don’t mind disabling Secure Boot, we are confident hibernation works reliably on our machine (or we want to test it), and we want to enable it on Fedora, we have to follow the few steps we will see in this tutorial.
Step 1 – Disabling UEFI secure boot
In order to disable UEFI secure boot, we have to enter our machine firmware settings management interface. This is typically done by interrupting the boot process at an very early stage, by clicking a certain key which may vary depending on our machine brand and model. Secure boot settings are often found under the “Authentication” or “Security” tab of the machine firmware settings:
What we want to do is to set “Secure Boot” to “disabled”, than save changes and exit.
Step 2 – Creating a swap partition
As we already said, for hibernation to work, we have to create a “classic” swap partition on our disk, if we don’t have one already. To create the partition we can use our favorite partitioning tool. Once the partition is created, in order to use it as a swap space, we must “format” it using the
mkswap command. Supposing our partition is
/dev/sda3, for example, we would run:
$ sudo mkswap /dev/sda3
To activate the swap partition right away, instead, we can use the
$ sudo swapon /dev/sda3
We need our swap partition to be automatically enabled at boot, therefore we need to add an entry for it in our /etc/fstab file. The best way to reference the partition in it, its by using its
UUID (Universally Unique IDentifier). One method we can retrieve it is by using the lsblk command. Still supposing our swap partition to be /dev/sda3, we could run:
$ lsblk --noheadings -o UUID /dev/sda3
The fstab entry for a swap partition should look quite similar to this:
UUID=<swap-partition-uuid> none swap defaults 0 0
We talked about the fstab syntax in another tutorial, so take a look at it to better understand it. Here we can briefly say that the first column of the entry contains a reference to the swap partition (by its UUID, in this case) and the second specifies where the partition must be mounted (swap is not mounted, so we just used “none” as value). The third column contains the filesystem type (swap), the fourth, the mount options (here we used “defaults”). The fifth column contains a boolean value which establish whether the filesystem content should be dumped at boot or not, and finally, the sixth, the order in which the filesystems should be checked (a value of 0 disables the check). Once we created the fstab entry for our swap partition, we should modify the initramfs.
Modifying the initramfs
In order to support resuming from hibernation, we have to modify the dracut configuration, so that the “resume” module is added to the kernel(s) initramfs. What we want to do, is to create a new file inside the
/etc/dracut.conf.d/ directory. Here we will name it
resume.conf. Its content should be the following:
add_dracutmodules+=" resume "
After we save the file, we must regenerate the existing initramfs. We do it by running the following command:
$ sudo dracut --regenerate-all --force
To be sure the “resume” module has been added to the initramfs, we can run:
$ sudo lsinitrd -m
Take a look at the “Modules” section of the output generated by the command. The “resume” module should appear in the list:
======================================================================== Early CPIO image ======================================================================== drwxr-xr-x 3 root root 0 Oct 28 21:55 . -rw-r--r-- 1 root root 2 Oct 28 21:55 early_cpio drwxr-xr-x 3 root root 0 Oct 28 21:55 kernel drwxr-xr-x 3 root root 0 Oct 28 21:55 kernel/x86 drwxr-xr-x 2 root root 0 Oct 28 21:55 kernel/x86/microcode -rw-r--r-- 1 root root 208896 Oct 28 21:55 kernel/x86/microcode/GenuineIntel.bin ======================================================================== Version: dracut-055-6.fc35 dracut modules: systemd systemd-initrd systemd-sysusers nss-softokn dbus-broker dbus i18n network-manager network ifcfg drm plymouth crypt dm kernel-modules kernel-modules-extra kernel-network-modules lvm resume rootfs-block terminfo udev-rules dracut-systemd usrmount base fs-lib shutdown ========================================================================
As the next step we need to add some parameters to the kernel command line. Let’s see how.
Modifying the kernel command line
The last thing we need to do in order to allow hibernation on our machine, is to add the “resume” parameter to the kernel command line and use a reference to the swap partition as its value. In order to do that we need to modify the
/etc/default/grub file and append the following to GRUB_CMDLINE_LINUX:
If using LVM setup, or LVM on LUKS, we also have to add another parameter to the grub command line if not already present:
GRUB_CMDLINE_LINUX="[...] rd.lvm.lv=<volume_group_name>/<swap_logical_volume_name> resume=UUID=<swap-partition-uuid>"
As reported in the kernel configuration, the rd.lvm.lv directive is used to specify what logical volumes must be activated on early boot. The directive can be specified multiple times, in fact if you are using that kind of setup, you should find the same directive used to activate the logical volume which hosts the root filesystem. After we save the file, we must regenerate the grub configuration, so we run:
$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Hibernating the machine
Once we performed all the needed steps, what we want to do is to hibernate the machine. Since we enabled hibernation, the related entry should now appear under GNOME shell power settings, under the “Power Button Behavior” choice menu:
Once we set the action and we press the power button, the system should be hibernated. The system can also be hibernated by issuing the following command:
$ systemctl hibernate
If everything goes well, after few seconds, the machine should be turned off. When we reboot the machine the image saved on the swap space should be resumed, and we should find everything where we left it. Try to hibernate and resume a couple of times, just to be sure everything goes as expected. If you notice some kind of bug and you want to disable hibernation, just reverse the previous steps.
Disabling zram (optional)
If we find hibernation works reliably on our machine, and we decide we want to disable zram, we can simply uninstall the
$ sudo dnf remove zram-generator-default
Notice, however, that this step is not needed, since if a traditional swap partition exists, the system is intelligent enough to use it for hibernation, even if the zram device exists.
Hibernation is a very efficient power saving mode, but there are quite few reasons why Fedora decided to disable it on recent releases. In this tutorial we saw how to perform the steps needed to re-enable hibernation on the latest version of Fedora, and how to actually hibernate the system. Does hibernation work for you? Let us know!