Enable hibernation in CachyOS alongside zram swap with full disk encryption on BTRFS

How to enable hibernation in CachyOS alongside zram swap with full disk encryption on BTRFS

I use CachyOS on my laptop at the moment. I like to use the hibernation option, especially suspend-then-hibernate, but it’s not easy to enable in CachyOS, because there is no swap partition or swapfile created by default, because it utilizes zram. I wanted to use hibernation without losing the advantages of zram, so I went for a large swapfile with low priority to enable hibernation alongside zram. I use the default filesystem of CachyOS - BTRFS and I use full disk encryption. Here’s how I did it. Do this at your own risk, I am not responsible for mistakes I made or you make.

Steps to enable hibernation

  1. Create a subvolume for your swapfile

    sudo btrfs subvolume create /swap

  2. Calculate the size of your swapfile

    It’s your zram size + your RAM size. You can see the size of your zram with:

    swapon --show

    Mine was 30GB and my RAM size is 30GB, so my swapfile needs to be 60GB. That’s a lot of space, but for me not a problem.

  3. Create the swapfile

    sudo btrfs filesystem mkswapfile --size 60g --uuid clear /swap/swapfile

  4. Find the UUID of the (virtual) device where the swapfile resides, copy it for later use

    sudo findmnt -no UUID -T /swap/swapfile

    You will get something like 58239ee3-aa2b-32ef-b7b2-2f591ee2251b

  5. Aquire swap file offset, copy it for later use

    sudo btrfs inspect-internal map-swapfile -r /swap/swapfile

    You will something like 52635

  6. Add kernel parameters you acquired is step 4 and 5

    Edit the file /etc/sdboot-manage.conf and add resume=UUID=step 4 and resume_offset=step 5 in LINUX_OPTIONS=. It will look something like this:

    LINUX_OPTIONS="zswap.enabled=0 resume=UUID=58239ee3-aa2b-32ef-b7b2-2f591ee2251b resume_offset=52635 nowatchdog splash"

  7. Regenerate all systemd-boot entries with the following command

    sudo sdboot-manage gen

  8. Configure initramfs - add resume hook

    Edit the file /etc/mkinitcpio.conf and add resume into HOOKS= It must be after udev and encrypt. It will look something like this:

    HOOKS=(base udev autodetect microcode kms modconf block keyboard keymap consolefont plymouth encrypt resume filesystems)

  9. Regenerate initramfs

    sudo mkinitcpio -P

  10. Add swapfile to fstab with low priority

    Edit the file /etc/fstab and add on the last line:

    /swap/swapfile none swap defaults,pri=0 0 0

  11. Enable swap on running system

    sudo swapon /swap/swapfile

  12. Check if swap is enabled

    swapon --show

    You should now see something like

    NAME           TYPE       SIZE USED PRIO
    /swap/swapfile file        60G   0B    0
    /dev/zram0     partition 30,7G 6,3M  100

  13. Reboot your system

After reboot, hibernation should be enabled in the settings of your Desktop Environment (if you use one). You can also test it on the command line with:

sudo systemctl hibernate

Suspend then hibernate

If you’re a fan of suspend-then-hibernate, like me (ideal for laptops if you ask me), you should edit the file /etc/systemd/sleep.conf, remove the # in front of HibernateDelaySec= and add the number of seconds. I have 7200 seconds (2 hours). You also need to configure it in your Desktop Environment after that. Gnome has no setting for that, but KDE Plasma (Settings-Power Managment) does.

/etc/systemd/sleep.conf

[Sleep]
#AllowSuspend=yes
#AllowHibernation=yes
#AllowSuspendThenHibernate=yes
#AllowHybridSleep=yes
#SuspendState=mem standby freeze
#HibernateMode=platform shutdown
#MemorySleepMode=
HibernateDelaySec=7200
#HibernateOnACPower=yes
#SuspendEstimationSec=60min

Sources used:

Boot Manager Configuration in CachyOS

Suspend and Hibernate at ArchWiki

BTRFS at ArchWiki

Framework Community