Thursday, March 25, 2021

Enabling Hibernation in Ubuntu 20.04 LTS using a Swap File

Enabling of the hibernation option on Ubuntu 20.04 LTS didn't work in the way I used to do. So, I had to explore further and do it slightly differently. This blog post records those things I did to get hibernation option working.

1. First of all, create a swap file using the following commands.

sudo fallocate -l 17G /swapfile

sudo chmod 600 /swapfile

sudo mkswap /swapfile

sudo swapon /swapfile


Once done, add an entry to the end of /etc/fstab file to make use of this swap file as follows.

/swapfile none swap sw 0 0

2. Check the UUID of the partition where the swap file is located using the following command.

cat /etc/fstab

Take note of the UUID string, which we will need in a later step.

3. Check the offset to the swapfile with in the storage device using either of the following commands. Take note of that offset value.

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

sudo swap-offset /swapfile

4. Now, open the /etc/default/grub file and add update the correct line as follows.

Original line:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

Updated line:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=a27fc21e-3315-4497-99aa-1fe7fad64091 resume_offset=9807872"

Note that the UUID value and the resume offset value are found using the above steps 2 and 3.

Once this grub file is updated, run the following command to take the changes effect.

sudo update-grub

5. Test whether the hibernation option is working now, use either of the following commands. I personally prefer the second command as it provides some verbose output while the system is being hibernated and being resumed later.

sudo systemctl hibernate

sudo pm-hibernate

Cheers!


References:

  1. https://linuxize.com/post/create-a-linux-swap-file/
  2. https://askubuntu.com/questions/1240123/how-to-enable-hibernate-option-in-ubuntu-20-04
  3. https://wiki.archlinux.org/index.php/Power_management/Suspend_and_hibernate#Hibernation_into_swap_file