This page looks best with JavaScript enabled

BTRFS + SWAP + HIBERNATE

 ·   ·  ☕ 2 min read · 👀... views

How to enable hibernation in EndeavourOS+BTRFS+swapfile

First, we need to make sure that the swap file is at least of the size of the ram.

1
free -h
1
2
3
               total        used        free      shared  buff/cache   available
Mem:            15Gi       3.3Gi        10Gi       1.1Gi       2.9Gi        11Gi
Swap:           16Gi          0B        16Gi

Ok, so it is larger than the memory. If it is not, then we need to recreate the swap file with a larger size. To do that, first stop and remove the existing swapfile (is this case, the default location of swapfile is in /swap/swapfile).

1
2
sudo swapoff /swap/swapfile # stops the swap
sudo rm -f /swap/swapfile # remove the swapfile

Then we create a new swapfile. Usually it can be done with the dd command, but btrfs has a special command already for this.

1
2
sudo btrfs filesystem mkswapfile /swap/swapfile -s 17G
sudo swapon /swap/swapfile

Now we should be a swapfile that is larger than our memory.

Next, we need to enable the resume hook for our initramfs generator. The most common generator is mkinitcpio. In that case, we can add resume in /etc/mkinitcpio.conf like below:

1
HOOKS=(base udev autodetect modconf block kbd_backlight encrypt filesystems keyboard fsck resume)

But endeavourOS has switched to Dracut. In this case, we edit the file /etc/ostree-mkinitcpio.conf and add the resume flag like below:

1
HOOKS="base udev autodetect modconf block kbd_backlight encrypt filesystems keyboard fsck resume"

Then we rebuild the initramfs with sudo dracut-rebuild.

Next we grab two important variables about the swapfile in btrfs filesystem. The first one is the UUID of the swapfile:

1
sudo findmnt -no UUID -T /swap/swapfile # <the-uuid> = a44a89ea-0560-4bff-bbf1-1855ef63e6e6

Then we get the offset of the swapfile. For non-btrfs partitions, it can be obtained with findmnt as well, but for btrfs, it does not work. For btrfs, we need to obtain the offset by:

1
sudo btrfs inspect-internal map-swapfile -r /swap/swapfile # <offset> = 373845

After getting these two information, we need to add these to /etc/default/grub in the GRUB_CMDLINE_LINUX_DEFAULT variable like below:

1
2
3
4
5
6
7
# GRUB boot loader configuration

GRUB_DEFAULT='0'
GRUB_TIMEOUT='5'
GRUB_DISTRIBUTOR='EndeavourOS'
GRUB_CMDLINE_LINUX_DEFAULT='nowatchdog nvme_load=YES loglevel=3 resume_offset=373845 resume=UUID=a44a89ea-0560-4bff-bbf1-1855ef63e6e6'
GRUB_CMDLINE_LINUX=""

Then create the grub configs by running sudo grub-mkconfig -o /boot/grub/grub.cfg.

Finally reboot the system. Now test if hibernation works by running sudo systemctl hibernate. This will turn your computer off. Then upon starting, this will boot straight to the last session you were.

Share on

Rahat Zaman
WRITTEN BY
Rahat Zaman
Graduate Research Assistant, School of Computing