To test hibernation (or hibernate via CLI):
systemctl hibernate
To change swap file size:
Find the swap file, disable swap, and delete the file:
swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
sudo swapoff /swapfile
sudo rm /swapfile
Create new swap space by writing from /dev/zero (if) to a new file (of). bs is block size, count is how many times to write that block size. So this would be 16,384 1M blocks, or 16GB:
sudo dd if=/dev/zero of=/swapfile bs=1M count=16384
Give it RW permission for root:
sudo chmod 600 /swapfile
Make it into a swapfile:
sudo mkswap /swapfile
Enable swap:
sudo swapon /swapfile
Reboot for the change to take effect.
To enable hiberation using the swap file:
If needed, add it to the mounts list (may already be done):
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Then find its UUID and offset:
blkid
That will return, among other things, the UUID.
Then get the offset to where the file lives on the partition:
sudo filefrag -v /swapfile
On line 0 of the output, look for the start number under `physical_offset`. This will be the third column in the results, directly under the "p" in `physical_offset`.
Then edit the Grub config:
sudo gedit /etc/default/grub
Where it says `GRUB_CMDLINE_LINUX_DEFAULT` it will probably say just "quiet splash". Add a space after "splash" then "resume=UUID=XXXXX resume_offset=YYYYYY" where XXXXX and YYYYY are your UUID and offset values from above.
Then update grub, reboot, and you should be able to hibernate:
sudo update-grub
sudo reboot
To add Hibernate to the Power menu, install "Hibernate Status Button" Gnome Extension, edit this configuration file:
sudo nano /etc/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla
And add this to it:
[Enable hibernate in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes
[Enable hibernate 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
Credits:
https://ubuntuhandbook.org/index.php/2021/08/enable-hibernate-ubuntu-21-10/
https://askubuntu.com/questions/1264568/increase-swap-in-20-04
https://github.com/arelange/gnome-shell-extension-hibernate-status