Hibernate laptop on lid close with systemd-boot on Arch Linux

Josh Sherman
5 min read
Linux Arch Linux

Want to start this post off by thanking my buddy Dean for writing this great guide to setting up hibernate with Arch Linux.

Unfortunately, when I went to consult this post the other day to get things setup on my Galago Pro his site was down.

Funny enough, it was because of a botched pacman -Syuclassic Arch ;)

Anyway, I’m just making good on my promise to steal his post so that I could reference it from my own site in the future :P

Not a complete rip though, as his guide assumes that you use Grub. With Arch, I use systemd-boot (formerly gummi-boot) and the steps are a smidge different to get things configured properly.

Funny enough while doing a bit more research on this I came across a post talking about using the Gnome Tweak Tool to simply select “hibernate” instead of “suspend” on lid close.

This doesn’t seem to be the case any longer, or perhaps it’s still something you can do with Ubuntu’s tricked out version of GNOME.

Also worth noting that the reason you may want to hibernate instead of suspend on lid close of your laptop is that your battery won’t drain in the same way that it does with suspend in Linux.

Hibernating instead of suspend also introduces the need of pressing the power button to wake the machine back up. It takes a bit of extra time to wake up so if you’re a speed demon and need your machine to immediately wake up, you may want to avoid hibernation.

I’m going to assume that you already have an Arch Linux system up and running and that you’re using systemd-boot.

I’m also going to assume that you don’t already have a swap partition or file setup. In my experience since moving back to Linux, there’s really no need to have a swap partition or file unless you are doing hibernation or have an extremely old machine.

It’s also recommended to use swap files instead of partitions as you can resize swap without needing to fuck with your disk’s partitions.

First, let’s make, configure and enable our swap file:

# This is now produces a swapfile "with holes"
# sudo fallocate -l 32G /swapfile # Change 32G to your RAM amount

# This works much better, adjust "count" accordingly
sudo dd if=/dev/zero of=/swapfile bs=1M count=32768 status=progress

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

With our swap file ready to swap, we will need to add it to your /etc/fstab:

sudo vim /etc/fstab

And add this line to the bottom of the file:

/swapfile    none    swap    default     0    0

Next, since we’re using a swap file and not a partition, we will need to know the swap file’s offset so that we can configure the resume offset:

sudo filefrag -v /swapfile | sed -n '4p' | awk '{print $4+0}'

That will spit out a number that you should jot down or copy to your clipboard so that we can use it in our next step.

YMMV on this next step as it’s contingent on what you named your boot entry:

sudo vim /boot/leader/entries/arch.conf

You should already have an options line in there. At the end of the line you will need to add resume=**your root disk** and resume_offset=**swap file offset**. The line should look fairly close to this:

options root=UUID=SOMEUUID rw resume=/dev/sda1 resume_offset=12345

Now that our boot loader entry is properly configured we will need to add resume to our initial ramdisk’s hooks:

sudo vim /etc/mkinitcpio.conf

Find the line that starts with HOOKS= and add in resume after udev:

HOOKS=( base udev **resume** autodetect modconf block filesystems keyboard fsck )

And then recreate your initial ramdisk for your kernel (linux or linux-lts for most people):

sudo mkinitcpio -p linux

All that’s left to do is to tell systemd-logind to hibernate instead of suspending when the laptop’s lid is close:

sudo vim /etc/systemd/logind.conf

Find the line that starts with #HandleLidSwitch=, remove the comment and make sure it’s being set to hibernate:

HandleLidSwitch=hibernate

You may also want to tweak some other things like HandleLidSwitchExternalPower if you want to ensure you always hibernate and HandleSuspendKey if you happen to have a suspend key and want to make sure it forces a hibernation as well.

At this point, we’re ready to reboot and test things out:

sudo reboot

If everything worked out, your system will reboot properly and when you close your laptop’s lid it should take a moment to write RAM to disk and then appear to complete shut off.

When you open it, it won’t resume until you press the power button briefly and then should look like it’s booting up and present you with your lock screen if you happen to use one.

Thanks again to my buddy Dean for his awesome tutorial and if you happen to leverage this post to cover some other boot manager, drop me a comment!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles