Easy to do with latest version with btrfs-progs. I got it from nixpkgs.
Requirements:
- btrfs-progs >= 6.1
- Secure boot disabled
Create the swapfile
https://btrfs.readthedocs.io/en/latest/btrfs-man5.html#swapfile-support
(#
indicates that the command needs to be run with root user)
# truncate -s 0 /swapfile
# chattr +C /swapfile
# grep MemTotal /proc/meminfo # this * 1024
# fallocate --length 67163226112 /swapfile # replace the value with your memory capacity
# chmod 0600 /swapfile
# mkswap /swapfile
# swapon /swapfile
Set fstab
Append the following line to /etc/fstab
/swapfile none swap sw 0 0
Update grub to enable hibernation
- Get information to build the kernel parameters
SWAP_UUID=$(findmnt -no UUID -T /swapfile)
RESUME_OFFSET=$(btrfs inspect-internal map-swapfile -r /swapfile)
- Append kernel parameters to
/etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="... resume=$SWAP_UUID resume_offset=$RESUME_OFFSET"
Reflect hibernate enabled in the UI
Update the following file:
/etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla
And append the following:
[Re-enable hibernate by default in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes
[Re-enable hibernate by default in logind]
Identity=unix-user:*
Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions;org.freedesktop.login1.hibernate-ignore-inhibit
ResultActive=yes
Done!
Sources
- https://ubuntuhandbook.org/index.php/2021/08/enable-hibernate-ubuntu-21-10/
- https://btrfs.readthedocs.io/en/latest/btrfs-man5.html#swapfile-support
- https://superuser.com/a/1613639