Hibernation (also known as suspend to disk) is one of the many features which most Linux distributions do not officially support. Suspending to disk is not an easy feature to implement and involves certain risks. The process starts at the kernel.
For a system to enter the hibernation state, the kernel sleeps all active processes pausing the OS and dumping the RAM to non-volatile memory and finally goes into the power-off state where a complete power loss does not result in the loss of data. On restart, it might be slower to exit the state as the memory content is loaded back into RAM before the computer completely starts.
I am working on Pop OS 22.04 with a custom partition structure using BTRFS filesystem and no encrypted partition. While this entry is focused on Pop OS, other Linux-based OS, Ubuntu and Ubuntu-based distributions might find some of its content relevant.
This entry assumes the reader already has a BTRFS system with no swap partition or file.
Enabling suspend to disk in Pop OS requires the following steps:
- Disable the default Zram
- Create a swap sub-volume and swapfile
- Update kernel stubs
- Test and enable hibernation
There are some limitations to the implementations of Linux swap in BTRFS 1. A few of these require that the filesystem must be only a single device and have only a single data profile, no copy-on-write, no compression, etc.
Disable the default Zram
Pop OS now has Zram enabled by default. With this module enabled, the kernel can create a RAM device with active disk compression of files and or inactive memory pages. The RAM device can be used for swap or as a disk for storage of temporary files.
For hibernation, Zram is not a suitable option. The module may be used for swap in RAM to speed up operation, however, contents in the RAM are volatile and do not persist after a power loss.
Terminal
Create swap sub-volume and swapfile
The sub-volume
BTRFS sub-volumes are mountable file trees (not block devices or logical volumes) with support for more than a single file tree on different path levels. The filesystem has a top-level sub-volume with an ID of 5. For the swap sub-volume to be created on this level, the root sub-volume needs to be mounted with the target sub-volume ID 5.
Terminal
text
Your drive will have different names and layouts. The current device is NVME drive and uses a PCI driver, hence the device names begin with
. SCSI/PATA/SATA/USB devices may have different names similar to
,
, et cetera.
The target partition is which is the BTRFS partition with the root
and home sub-volumes mounted.
Terminal
The mount options used above should be adjusted per use case. For instance,
depending on the version of BTRFS progs, the may not be required
as BTRFS will enable or disable SSD optimisations based on the disk type
2. The swap sub-volume will be created on the root level.
Listing of the BTRFS sub-volumes will show the new
on
.
diff
The swapfile
The size of the swap file is proportional to the size of the system RAM. For
suspending to disk, we need a swapfile bigger than the RAM. shows the
system memory information.
text
Based on the output, the total memory on our system is , hence a swapfile
of
is enough to accommodate the contents of the RAM at hibernation.
This step of the process requires an updated version of BTRFS, at least
version 6.1 and above. The version provides easy commands to make a swapfile
and to retrieve the value1.
Terminal
Terminal
Enable swap
Update mount entries in to include the swap sub-volume and the swap file.
Terminal
The new output of should include a row with the swap information.
text
Update kernel stubs
With the swapfile now available, it only serves to store inactive memory pages when the system is running out of memory. Adding options which can be passed to the kernel at boot time will ensure that the system is resumed after hibernation.
The option specifies with disk partition and
points to
the exact location of the swapfile. The
is required when using a
swapfile, otherwise, only the
is required for a swap partition.
The following command shows the swapfile offset.
text
Terminal
Test and enable hibernation
Ensure hibernation works by executing the following command.
Terminal
Check the system logs for error messages. Any of the following commands should provide additional information about the problem.
Terminal
Common error messages include:
- No such device: Check that you have the correct kernel options. Ensure
the
and
options are correct.
- No space left on device: The swapfile or swap partition does not have enough space. Your swap space may be less than the RAM or there are multiple swap spaces with at least one not having enough space.
Add Hibernate and Hybrid Sleep options to the power menu, using Hibernate Status Button GNOME-Shell extension.
Ultimately, add the following policy kit.
Terminal