[How to] Enable hibernate UEFI+LUKS+BTRFS+SYSTEMD_INITCPIO using a swapfile

DO THIS AT YOUR OWN RISKS

READ FULL DOCUMENTATION AT Power management/Suspend and hibernate - ArchWiki

Prerequisites

  • All things listed in the topic title.
  • way to recover if something fails to boot like a secondary initramfs or secondary kernel with it’s own initramfs.
  • plenty of space for a swapfile with the ability to allocate continuous space for it. may need to defrag but it’s bit risky but if this on a fresh install it should not be a concern.
  • backup data in case you do something stupid.

Goal

Achieve hibernation with least modification to system with swap encryption.

Steps

  1. Assuming your btrfs subvolume @ is mounted at / , create a subvolume for swap by running sudo btrfs subvolume create /swap. and you will see /swap appear in your file system. This needed because snapshots cannot be created for subvolumes containing active swap files if we like to use snapper for our root directory.

  2. Create swap file sudo btrfs filesystem mkswapfile --size 15g --uuid clear /swap/swapfile then output will look like this create swapfile /swap/swapfile size 15.00GiB (16106127360). swap of for 16Gig ram allocating 15GiB is fine since hibernation will compress ram anyway. If you have lot of Ram like more than 32G it’s not feasible dedicate full amount, for such cases refer this.

  3. add this to your /etc/ftab

/swap/swapfile none swap nofail,x-systemd.device-timeout=1s 0 0

nofail,x-systemd.device-timeout=1s part is to prevent hangups when booting from snapshots

  1. edit /etc/mkinitcpio.conf. resume hook is not needed when using systemd hook.The order is important, check out mkinitcpio before edditing your configuration. Mine is

HOOKS=(base systemd autodetect microcode kms modconf block keyboard sd-vconsole plymouth sd-encrypt filesystems fsck)

  1. Rebuild initial ramdisk with sudo mkinitcpio -p linux-cachyos

caveats

Changing kernel cmdline is not needed because

When the system is running on UEFI, systemd-sleep(8) will automatically pick a suitable swap space to hibernate into, and the information of the used swap space is stored in HibernateLocation EFI variable. Upon next boot, systemd-hibernate-resume(8) reads the location off the EFI variable and the system resumes. This means the following steps are not necessary unless the system is using legacy BIOS or you want to choose a different swap space from the automatically-selected one.

If you have nvidia and having issuse check this NVIDIA/Tips and tricks - ArchWiki

1 Like