setting up hibernation in arch linux

Jul 25, 2022 | linux, guide

If you have just installed a basic window manager on your arch setup, it is highly likely that hibernation is not set up on your system. I hibernate my system very often and therefore, it is one of the first things that I set up on my system. This is a guide that I have prepared by stealing simplifying instructions from this archwiki page.

set up a swap file (or partition)

I recommend using a swap file. Although there exists much debate about the performance difference of using a swap partition vs. swap file, for a regular user the difference is unnoticeable and one added benefit of using a swapfile is that you can easily move a swap file around, delete old ones and create new ones (with a different size) at ease. The last feature is helpful when you have changed your RAM capacity. If you are looking for instructions to set up hibernation using a swap partition (instead of a swapfile), you can find many guides online.

Here I list instructions to create and setup a swap file:

gathering UUID and file offset values

You can find the swap device UUID using the command:

findmnt -no UUID -T /swapfile

In order to find the value of swap_file_offset, you need to run the following commands on your system.

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

Or,

you can install the uswsusp package and run swap-offset /swapfile.

editing grub config file

Edit the file at /etc/default/grub, preferably using sudoedit

sudoedit /etc/default/grub

Find the line that contains GRUB_CMDLINE_LINUX_DEFAULT and add the following options after it: (or if GRUB_CMDLINE_LINUX exists then add the following options to it)

GRUB_CMDLINE_LINUX="resume=UUID=<swap_device UUID> resume_offset=<swap_file_offset>"
# or you can edit `GRUB_CMDLINE_LINUX_DEFAULT`

Then, run sudo update-grub to update the grub config file.

add resume option to the list of hooks

Edit the file /etc/mkinitcpio.conf and add resume to the list of hooks.

# notice that resume is now added to the list of hooks
HOOKS=(base udev resume autodetect modconf block filesystems keyboard fsck)
#                   ^

Create a new image with the new config file by running sudo mkinitcpio -p

reboot your system

After rebooting you can test if your swapfile is working by running swapon -s. If you see a line that starts with /swapfile then it is working.

Test the hibernation by running systemctl hibernate.

If everything went well, your system should hibernate and resume successfully.

[optional] storing your swapfile in /home

If your swapfile is located in your /home directory (like in /home/pritom/swapfiile), you may not be able to use it for hibernation unless you make the following changes to turn off sandboxing option for \home directory.

run systemctl edit systemd-logind and add the following in the file that is opened:

[Service]
ProtectHome=read-only

I found this workaround here.

~ end of post ~