How to enable hibernation in Arch Linux

How to enable hibernation in Arch Linux

Now, I love Arch Linux but surely there are some things that needs some work before you can use it for your Laptop or maybe even your desktop, depending on your needs. For me, one of those things were hibernation, and somehow I often forget how to get it working. In this post I’ll walk you through how to setup hibernation when using a laptop with systemd-boot. Because yeah, if you use GRUB or Syslinux or don’t have UEFI, then you need a different solution. Sigh.. Well. Lets get to it!

This guide expects you to have a working install of Arch Linux, and as I explained in my Arch Linux install guide, you also need to have a swap-partition. That’s it.

As everything else, this is rather simple but, for me, it took hours to understand. I might be getting too old or Arch Linux might be so KISS that I feel stupid most of the time. One of those will only get worse while the other might improve over time – YMMW. If you don’t understand my guide here there is always the fantastic Arch Linux wiki on power management which this guide also relies heavily on.

So. First we need to tell Arch which partition is your SWAP so we need our trusty text editor. I have become quite fond of Vim lately but if you rather want to use Nano, then it’s all good.

vim /etc/fstab

Here you need to find your UUID for the SSWAP-partition. Mine is something like UUID=ecf95bf8-3d45-4faf-9d12-bcf2ed7ce12b. To tell Arch that this is the right partition we need to edit our bootloader. Therefore, you should copy the UUID. Okay, let’s move on.
sudo vim /boot/loader/entries/arch.conf

Here we need to tell the boot loader which partition we want to hibernate to. Add: “options resume=Your-partitions-UUID-here” to the end of the file and save it.

Pretty simple. Now we need to change the initramfs. Sounds scary? Don’t worry.
sudo vim /etc/mkinitcpio.conf

Here you need to add “resume” in the hooks-section. Note that it has to be after “udev”. In my case it looks like this:
HOOKS="base udev resume autodetect modconf block filesystems keyboard fsck.

To append the hooks you also need to rebuild the kernel
sudo mkinitcpio.

Now everything is set up and you only need to hibernate your pc.
systemctl hibernate.

But, we’re not really done yet. There is still one more thing that we can do. I’ve often worked on important work and have been close to running out of power. Closing the lid is not enough since it could be long before i get to another power source. Therefore I would like my laptop to hibernate, when it reaches close to zero, say 2-3%? That way I won’t have to think too much about remaining battery life. Since we already have already set up the hibernate function there is only one thing left. We need to make an udev-rule – wow! Sounds complicated? Not at all.

First we need to create the file for our udev-rule.
sudo vim /etc/udev/rules.d/99-lowbat.rules

Then we need to paste this text in. The will make Arch look at the battery indicator and hibernate the laptop if the battery level drops below 5%.

# Hibernate the system when battery level drops to 5% or lower
SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-5]", RUN+="/usr/bin/systemctl hibernate"

My Thinkpad x250 is a beast when it comes to conserving power so 5% will be about 30-40 minutes, but according to the Arch Linux wiki the battery indicator is not 100% precise. So I’ll have to tweak it over time.

Well.. That wraps it up. I hope this was what you were looking for.