15

How can I enable hibernation on Linux Mint 20 Cinnamon (Ubuntu 20) and prevent unwanted wakeups?

terdon
  • 234,489
  • 66
  • 447
  • 667
Texno
  • 171
  • 1
  • 1
  • 5
  • just a minor heads-up: for `swapon --show` you don't need `sudo`. - be rather stingy than generous with `sudo`s; elevated privileges should be given sparingly! – DJCrashdummy Sep 29 '21 at 15:06
  • another one for enabling hibernate in the GUI: it is advised to rather use `/etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla` instead of `/var/lib/...` *(sorry i can't link the source, because it shows a 404, but it was directly from help.ubuntu.com.)* **||** also the line for **logind** should be `Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions;org.freedesktop.login1.hibernate-ignore-inhibit` instead of just `Action=org.freedesktop.login1.hibernate`. – DJCrashdummy Sep 29 '21 at 15:17
  • This solution didn't work for Linux Mint 20.2. However, the `Hibernate` button has appeared - it restarted OS. Finally, found solution [Enabling hibernation in Linux Mint 20.2](https://forums.linuxmint.com/viewtopic.php?t=357914). Increased swap partition to double as RAM and configured as first answer and now it recovers fine. – catch23 Nov 26 '21 at 12:18
  • Thank you for taking the time to write this up, but please post things in the Q&A format. If you want to share your knowlegde, then ask a question and post the answer yourself, but we do need everything to be in the form of a question or answer. I will post your answer as a community wiki, but please ping me if you see this and would like post it as a regular answer so you can get your well-deserved rep. – terdon May 10 '22 at 13:59

1 Answers1

3

The following was originally posted as part of the question, so I (terdon) am reproducing it here.


UPDATE: I have encountered some inconsistencies with my script in section 4 that is meant to be executed on every wake-up. Turns out, /proc/acpi/wakeup settings are only reset sometimes. Not on every shutdown/restart/hibernate/suspend. So, from time to time, my script would enable wakeups. I have updated the script. Now it checks if it says enabled anywhere in /proc/acpi/wakeup and only then outputs to it.

This is, in a way, a memo for myself in the future. I do not understand why hibernation on linux is not a simple feature that just works and why enabling it has to be so complicated. I hope it also helps people having questions about hibernation.


  1. First, you need to make sure you have a large enough swap file. Swap is where your RAM is saved on disk when you hibernate.
  • Check the size of your swap either through System Monitor -> Resources or by running sudo swapon --show. You probably do not have enough. Your swap size should be somewhat larger than you RAM size. There are some guides online. I go for 5GB swap on my 4GB RAM machine.
  • How to make your swap larger depends on how you have it set up. Maybe you need to create a larger swap file, maybe you need to open GParted and simply resize partitions there. I have an encrypted LVM with /dev/vgmint/root and /dev/vgmint/swap volumes. You cannot resize LVM while it is mounted, so you boot from a USB stick with Linux Mint. There you can use Disks app to unlock your encrypted LVM and use this beautiful guide to safely reduce the size of your /dev/vgmint/root volume:
    • Force check file system sudo e2fsck -f /dev/vgmint/root
    • Shrink your file system sudo resize2fs /dev/vgmint/root 180G. Replace 180G with about 90% of the size you want the final volume to be.
    • Reduce your volume to its final size sudo lvreduce -L 200G /dev/vgmint/root, where 200G is your volume's final size.
    • Grow your file system to take up the rest of the free space of your volume sudo resize2fs /dev/vgmint/root
  • Then you can run sudo lvextend -l 100%FREE /dev/vgmint/swap_1 to extend your swap volume with the free space you have just created.
  • Now, we need to update swap. We are done with a USB stick live system, boot into your regular system. If you check the size of your swap again, you will see that the size of it has not changed, even though we just gave it more space. We need to create a new swap. Run sudo swapoff -a to disable all swaps and run sudo mkswap /dev/vgmint/swap_1 to create a new one.
  1. Now, test if your hardware supports hibernation and add hibernation button back by reading this lovely article:
    • Open terminal, run sudo pm-hibernate. Your computer should hibernate. Boot it up again and make sure it restores everything. If it does, then your hardware supports hibernation.
    • Now, we will enable hibernation icon. Create file:
      sudo -i
      cd /var/lib/polkit-1/localauthority/50-local.d/
      nano com.ubuntu.enable-hibernate.pkla
      
    • Paste the following content into that file:
      [Re-enable hibernate by default in upower]
      Identity=unix-user:*
      Action=org.freedesktop.upower.hibernate
      ResultActive=yes
      
      [Re-enable hibernate by default in logind]
      Identity=unix-user:*
      Action=org.freedesktop.login1.hibernate
      ResultActive=yes
      
    • Save, restart. Now you should have "Hibernate" option when you press your power button.
  2. Power management. This is probably specific to Linux Mint with Cinnamon. I want my laptop to hibernate after a certain period of inactivity, but it is not possible to set this up using Power Management app. To edit inner Cinnamon settings, I use dconf editor (sudo apt install dconf-editor). Open it up, go to /org/cinnamon/settings-daemon/plugins/power/ or just search for power. sleep-inactive-battery-type is what I am after - set it to 'hibernate'. While here, I like to turn off use-time-for-policy and use battery percentages instead of time remaining to determine "battery low", "battery critical" and "battery action" states. Battery percentage is a real value, while time remaining is an estimate and can vary a lot. You can also set percentage thresholds using percentage-low, percentage-critical and percentage-action. Take a look a round, there are some interesting settings in this tab. Be careful though.
  3. Now, the last piece of the puzzle prevent unwanted wakeups from suspend/hibernation. Write sudo cat /proc/acpi/wakeup. You will see which devices are enabled and can lead to unwanted wakeups from hibernation. Devices that cause you trouble need to be disabled on boot and on return from suspend/hibernation.
  • Here is how to disable a device: echo DEVICE_NAME | sudo tee /proc/acpi/wakeup. (Thanks to this thread.)
  • Figure out which devices cause you problems (I have disabled all) and write a bash script that would disable them. (File might need to be owned by root, since root will be executing it.) It would look something like:
#!/bin/bash

filename='/proc/acpi/wakeup'
n=0
fix=false
while read line; do
if [[ "$line" == *"enabled"* ]]; then
  fix=true
  #break
fi
n=$((n))
done < $filename

if [[ "$fix" == "true" ]]; then
  echo RP01 | tee /proc/acpi/wakeup
  echo RP02 | tee /proc/acpi/wakeup
  echo RP03 | tee /proc/acpi/wakeup
  echo RP05 | tee /proc/acpi/wakeup
  echo RP06 | tee /proc/acpi/wakeup
  echo XHC1 | tee /proc/acpi/wakeup
  echo LID0 | tee /proc/acpi/wakeup
fi

Do not forget to make your script executable for root. sudo su and then chmod +x /your-script.sh

  • Now, we need to create a systemctl service that will be calling the script. (Thanks to this article and many other posts on this website for inspiration). I call the service wakeups. Create the file using sudo nano /etc/systemd/system/wakeups.service. The contents of the file:
[Unit]
Description=Fix unwanted wakeups from suspend

[Service]
Type=oneshot
TimeoutSec=0
StandardOutput=syslog
User=root
ExecStart=/path-to-your-script/script.sh

[Install]
WantedBy=multi-user.target suspend.target hibernate.target
  • Enable the service by running systemctl enable wakeups.service
  1. Alright, that is all it takes to have hibernate properly functioning! Test and make sure everything works correctly when booting regularly and when returning from hibernate/suspend.
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
terdon
  • 234,489
  • 66
  • 447
  • 667