Haemin's blog

Setting hibernate with swapfile in Ubuntu MATE 22.04/btrfs

haemin

Easy to do with latest version with btrfs-progs. I got it from nixpkgs.

Requirements:

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

SWAP_UUID=$(findmnt -no UUID -T /swapfile)
RESUME_OFFSET=$(btrfs inspect-internal map-swapfile -r /swapfile)
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

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top