Prerequisites

Check if the kernel supports Hibernation

cat /sys/power/state

It should list disk as an option on the list

Check if a Swap file or partition is available

free -h

If Swap is listed as 0, it means a swap is not available and needs to be created.

Create a Swap file

  • Create a swap file.
sudo fallocate -l 6G /swapfile

6G can be updated with a recommended swap size for the machine which is listed under How much swap do I need? section at help.ubuntu.com

  • Permissions
sudo chmod 600 /swapfile
  • Format as swap
sudo mkswap /swapfile
  • Activate the swap
sudo swapon /swapfile
echo '/swapfile none swap defaults 0 0' | sudo tee -a /etc/fstab

Configure Hibernation

List the swap

cat /proc/swaps
  • Swap file is generally listed as /swapfile
  • Swap partition is listed as /dev/sdxx

Swap UUID

Get the UUID for the swap

findmnt -no UUID -T /swapfile

It looks something like below

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Swap offset

This is only needed when a swap file is available. Get the offset

sudo filefrag -v /swapfile | awk '{ if($1=="0:"){print $4} }'

It looks something like below

9999999..

Update Kernel Options

For a Swap File

sudo kernelstub -a "resume=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
sudo kernelstub -a "resume_offset=9999999"

For a Swap Partition, resume_offset option is not needed

sudo kernelstub -a "resume=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Add below line to /etc/initramfs-tools/conf.d/resume. Create the file if not present

For a Swap File

resume=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx resume_offset=9999999

For a Swap Partition

resume=UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Update the configurations

sudo update-initramfs -u

Test Hibernation

Remember to save your work before trying this out

sudo systemctl hibernate

References