Hibernating is easy now?

Alternatively: S0ix is still awful

My Linux laptop runs out of battery all the time. It goes to sleep, and then it just drains and drains until it’s dead. It’s a problem we’ve largely solved on phones (and phone operating systems), but still struggle with on desktop operating systems.

I remember doing a lot of debugging back when I worked on Windows 8 to try to diagnose bad suspend times (those prototype devices were terrible in many other ways but the standby sticks out in my memory)

Anyways, before my last reformat (yes this is also a problem with arch), I had set up sleep, then hibernate. It took a lot of fiddling to work properly, due to the LUKS encryption of the hard drive. My previous memories of struggling here led me to put this off until this weekend.

Much to my surprise, this is _a lot_ easier now. You can mostly ignore the fact that there’s encryption at all. Just set up your swap drive and let systemd do the work. To round this blog post out, here’s the steps I followed for my particular setup:

mount /dev/mapper/root /mnt/realroot
brtfs subvolume create /mnt/realroot/@swap
btrfs filesystem mkswapfile --size 16g --uuid clear /mnt/realroot/swap/swapfile

Which is just normal btrfs goop to create a new top-level subvolume, and then I created a file there called swapfile with the appropriate magic.

All that’s left is to mount the btrfs subvolume (and then the swapfile itself) in /etc/fstab

# /dev/mapper/root/@swap to /swap
UUID=MY_BTRFS_UUID_HERE       /swap           btrfs           rw,relatime,ssd,space_cache=v2,subvolid=SUBVOL_ID_HERE,subvol=/@swap        0 0

# Once the btrfs @swap partition is mounted, we can load the swapfile
/swap/swapfile                                  none            swap            defaults 0 0

and literally the last step is just to add resume to the HOOKS section of /etc/mkinitcpio.conf (after filesystems)

No more hardcoding in the resume UUID, or the offsets, or any of that mess. The last thing is just to mess with your sleep settings to standby, and then hibernate. I just used KDE’s system settings to do the work here, and then overrode the delay in a conf file

❯ cat /etc/systemd/sleep.conf.d/hibernate.conf
[Sleep]
HibernateDelaySec=1800

Okay, that’s still… a lot of steps – but it’s a lot better than it used to be!