In the previous post we configured our machine with full disk encryption. If you are interested you can read it here.

In this post we will be setting up hibernation and hybrid suspend.

Hibernation means that the entire state of your machine will be written to disk, and when you start you computer the next time it will resume from where you left off.

Now what is hybrid suspend. Hybrid suspend is actually a combination of standby and hibernation. When you close the lid of you laptop, you will notice that some of the lights start blinking (if your laptop has any indicator for its power state). This means that your laptop is in standby mode. When you open the lid, you can continue, however when the computer runs out of power, everything you where working on, will be lost.

Hybrid suspend to the rescue; hybrid suspend will actually save you current computer state to disk, just like hibernation does however in stead off powering off the machine it will go into standby mode as long as there is power. It gives you the possibility to continue directly where you have left off. While if for some change you closed the lid of your laptop, and forgot to plugin your power cable you will still be able to continue your work when you power on the next time.

Hibernation

You might have seen the following error, when running sudo update-initramfs -u

update-initramfs: Generating /boot/initrd.img-5.0.0-13-generic
I: The initramfs will attempt to resume from /dev/dm-1
I: (/dev/mapper/system-swap)
I: Set the RESUME variable to override this.

This means the resume parameter has not been set, the systeem needs to now where to check for a previous state which will be our swap partition or `SWAP` file.

Get our SWAP location from /etc/fstab

$ cat /etc/fstab |grep swap
/dev/mapper/system-swap none            swap    sw              0       0

In my case I'm using a encrypted SWAP partition at /dev/mapper/system-swap.

There are two options for configuring resume, either through a grub option or to configure your initramfs to look for resume. We will be using the second method.

Runt he following command from a terminal.

$ sudo echo 'RESUME=/dev/mapper/system-swap' > /etc/initramfs-tools/conf.d/resume

Next step is to rebuild the initramfs.

$ sudo update-initramfs -u

Finally we have to make some changes to logind.conf

Uncomment the following in /etc/systemd/logind.conf

$ sudo vi /etc/systemd/logind.conf
HandlePowerKey=poweroff
HandleSuspendKey=suspend
HandleHibernateKey=hibernate
HandleLidSwitch=suspend
HandleLidSwitchExternalPower=suspend

We can now use hibernate with resume. You can test hibernation with the following command.

$ systemctl hibernate

Finally we want to get rid of the GUI password request we can do this by creating a sudoers file.

$ sudo visudo /etc/sudoers.d/hibernate

Write the following content:

{USERNAME} ALL=NOPASSWD: /bin/systemctl hibernate

Replace {USERNAME} with your own username

If you want to suspend without closing the lid of your laptop from command line, you can use the following command.

$ systemctl suspend

Hybrid-Suspend

Before you can use hybrid suspend, you need to get hibernation working first. If you have followed the Hibernation section of this guide. You are now ready to get Hybrid-Suspend working.

You can test for hybrid-suspend directly.

$ systemctl hybrid-sleep

Your machine will now go to standby and while doing this, you will see the HDD activity led flickering very fast, which means that the system is saving its current state to the SWAP partition.

Just like in the previous section, we don't want a request for a password when using this command.

$ sudo visudo /etc/sudoers.d/hybrid-sleep

Write the following content:

{USERNAME} ALL=NOPASSWD: /bin/systemctl hybrid-sleep

Replace {USERNAME} with your own username

Our final step in this guide will be to use hybrid suspend of our laptop when we close the lid. This will gives us the nice feature that if we close the lid of our laptop and forget about it. The current state is saved and we can continue our work, even if the battery runs out.

Edit /etc/systemd/logind.conf

$ sudo vi /etc/systemd/logind.conf

Replace suspend with hybrid-sleep

Old:

HandleSuspendKey=suspend
HandleLidSwitch=suspend
HandleLidSwitchExternalPower=suspend

New:

HandleSuspendKey=hybrid-sleep
HandleLidSwitch=hybrid-sleep
HandleLidSwitchExternalPower=hybrid-sleep

Restart the login daemon for the changes to take effect.

$ systemctl restart systemd-logind

This last command will force a restart of your desktop environment.

You can now test that it is working by closing the lid of you laptop, you will notice that your HDD activity led will blink very fast before your laptop will go into standby mode.

When you have tested it, you can confirm that is was working successful by checking the journal.

$ journalctl -xr|grep saving
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving done
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress: 100%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  90%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  80%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  70%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  60%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  50%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  40%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  30%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  20%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:  10%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Image saving progress:   0%
apr 30 21:50:40 {HOSTNAME} kernel: PM: Compressing and saving image data (1060873 pages)...

Now you have hybrid sleep fully working so even when you close the lid of your laptop and forget about it and your battery is drained you still can continue your work after connecting your laptop charger.

I hope this guide was helpful, if you have any comments, ideas for improving this guide. Please let me know.

If you found this post useful, subscribe to this blog.