How to hibernate in Ubuntu 18.04

This is one of the probably many ways to enable hibernate in Ubuntu. Unfortunately, hibernate still does not work out-of-the-box.
ahmadnurhadirmw - This is one of the probably many ways to enable hibernate in Ubuntu. Unfortunately, hibernate still does not work out-of-the-box. And while it is a highly desired feature for laptop users, there is also no easy way to switch it on.

Here I will describe the steps which I needed to take to enable hibernate on my Lenovo ThinkPad E580, with Xubuntu 18.04.

Step 1: Secure Boot

Before doing anything with hibernate, something about secure boot: these two do not go well together. There is a plethora of reasons which I will not discuss here. There are 2 options: 1) disable secure boot in your BIOS, or 2) use a non-signed kernel.

If you are dual-booting Windows with Ubuntu, disabling secure boot is annoying: Windows will not boot normally. Without secure boot, you will have to enter your Bitlocker decryption key on each boot. Alternatively, you will have to toggle the secure boot option for each switch between the operating systems.

My solution: use a non-signed kernel. I am honestly not sure what the consequences are of using an unsigned kernel: I guess it completely disables the concept of “secure boot” for Linux, but otherwise we have to disable it anyway. Also: this works for my system, maybe this approach does not work on other systems. It is also disputed whether GRUB should allow booting unsigned kernels:

Either way, go to: where you can download “official” Ubuntu-build kernels. Pick one, download the deb files for you architecture (you can skip the -lowlatency ones), and install them using:
sudo dpkg -i *.deb

Check if hibernate is supported now by:
cat /sys/power/state
If “disk” is one of them: congratulations! Step 1 achieved!

Step 2: Enabling encrypted swap with fixed key

I prefer to write the state of my system to an encrypted disk: in case someone steals your laptop, your memory dump will not be readable. For this, we need to create an encrypted swap which can be unlocked by a fixed key, rather than a random key.

For enabling encrypted swap, I followed this guide:
Some details are different. For example, encrypted swap is not enabled by default anymore after installing Ubuntu. The steps I took are the following, please consult the link above for details on each step.
swapon –summary # Check and record your swap partition
sudo swapoff -a
sudo cryptsetup luksFormat --cipher aes-xts-plain64 --verify-passphrase --key-size 256 /dev/nvme0n1p7
sudo cryptsetup luksOpen /dev/nvme0n1p7 cryptswap1
sudo mkswap /dev/mapper/cryptswap1
Now edit /etc/fstab:
sudo nano -w /etc/fstab
and replace the original line with information on the swap parition with:
/dev/mapper/cryptswap1 none swap sw 0 0
Save the file and continue with:
sudo swapon -a
Then edit /etc/crypttab and add the following line:
cryptswap1    /dev/nvme0n1p7    none    luks
Also edit /etc/initramfs-tools/conf.d/resume , and replace the existing line with:
Finally, edit /etc/default/grub and add:
Now run:
sudo update-initramfs -u -k all
sudo update-grub
to update grub.

When booting, the screen will ask you for your passphrase. If everything went well, the login-prompt will appear, and the encrypted swap is mounted (check with: swapon –summary).

Step 3: Test hibernate

You can now test if hibernate works. Issue:
sudo pm-hibernate
and your system should go to sleep.

If your system wakes up properly, resuming works!

Step 4: Enabling the option in the menu

Now that hibernate works, let’s integrate the functionality in the system, so you can access is from the menu.

Edit or create: /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla with the following contents:
[Re-enable hibernate by default in upower]

[Re-enable hibernate by default in logind]
Depending on your window manager, the options are readily available or not. Rebooting is the easy option.

Step 5: Waking up problems

On some systems, resuming does not work properly. On my ThinkPad E580, I initially had 3 problems: the LAN adapter did not work, the Bluetooth connection at some point simply died, and I sometimes had a black screen after resume.

These are 3 different problems.

Problem 1:

For the LAN adapter, unloading the module prior to hibernate and reloading on resume was sufficient. To achieve this, I followed this guide:
My LAN module is the r8169, so I created a file /etc/suspend-modules.conf with: r8169
Then create a file /lib/systemd/system-sleep/suspend-modules with content:

case $1 in
        for mod in $(</etc/suspend-modules.conf); do
            modprobe -r $mod
        for mod in $(</etc/suspend-modules.conf); do
            modprobe $mod
and make it executable:
sudo chmod a+x /lib/systemd/system-sleep/suspend-modules

Problem 2:

The other issue was a result from xHCI dying at some point, also resulting in disabling all USB devices. My /var/log/syslog showed these messages:
May 17 19:11:51 my kernel: [23775.839044] xhci_hcd 0000:00:14.0: xHC is not running.
May 17 19:11:51 my kernel: [23775.856855] xhci_hcd 0000:00:14.0: xHCI host controller not responding, assume dead
May 17 19:11:51 my kernel: [23775.856877] xhci_hcd 0000:00:14.0: HC died; cleaning up
I figured out that I needed these options to my GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub (see also this bugreport: ):
pci=nomsi iommu=soft
and running
sudo update-grub
This solved the problem for me.

Problem 3:

Only when you are running the XFCE desktop (like with Xubuntu):
Sometimes, XFCE returns a black screen after logging in, mainly after resuming from suspend (see bug: ).

The following command returns the XFCE desktop:

xfsettingsd --replace
which I added as a keyboard shortcut, so I could easily restore my desktop when needed. Putting your computer to hibernate requires some effort, but is fairly straightforward. Waking-up problems are much harder to debug, and may have different sources/solutions.

Post a Comment