Zum Inhalt

Schlagwort: suspend

How to hibernate and resume from swap file in Ubuntu 20.04 using full disk encryption

On my laptop, I am running full disk encryption (LUKS with a single encrypted ext4 partition), and a single, large swap file as large as my system RAM (16GB).

Here's how to make it work:

Make your swapfile have at least the size of your systems RAM:

sudo swapoff /swapfile
sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh '[0-9]*') count=1024 conv=notrunc
sudo mkswap /swapfile
sudo swapon /swapfile

Note the UUID of the partiton containing your swapfile:

$ sudo findmnt -no SOURCE,UUID -T /swapfile
/dev/nvme0n1p5 20562a02-cfa6-42e0-bb9f-5e936ea763d0

Reconfigure the package uswsusp correctly:

sudo apt -y install uswsusp
sudo dpkg-reconfigure -pmedium uswsusp
# Answer "Yes" to continue without swap space
# Select "/dev/disk/by-uuid/20562a02-cfa6-42e0-bb9f-5e936ea763d0" replace the UUID with the result from the previous findmnt command
# Encrypt: "No"

Edit the SystemD hibernate service using sudo systemctl edit systemd-hibernate.service and fill it with the following content:

[Service]
ExecStart=
ExecStartPre=-/bin/run-parts -v -a pre /lib/systemd/system-sleep
ExecStart=/usr/sbin/s2disk
ExecStartPost=-/bin/run-parts -v --reverse -a post /lib/systemd/system-sleep

Note the offset of your swapfile relative to the partition start:

$ sudo swap-offset /swapfile
resume offset = 34818

Tell grub to resume by editiing your etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=20562a02-cfa6-42e0-bb9f-5e936ea763d0 resume_offset=34818 quiet splash"

Update grub:

sudo update-grub

Create /etc/initramfs-tools/conf.d/resume

RESUME=UUID=20562a02-cfa6-42e0-bb9e-5e936ea763d0 resume_offset=34816
# Resume from /swapfile

Update initramfs:

sudo update-initramfs -u -k all

Now you can just hibernate your system with

sudo systemctl hibernate
28 Comments

Asus ZenBook 13 (UX331UN) with Ubuntu: Battery drain while in suspend

When i got my shiny new ZenBook 13 UX331UN in July, i immediately wiped it and installed Ubuntu 18.04 (Bionic Beaver).

But I soon realized something was wrong with suspend: The laptop would go to sleep and over night the battery would either almost (10-ish percent) or completely drain, leaving the laptop completely powered off.

On other laptops the suspend state would eat about 5% charge per day, leaving you able to suspend around 3 weeks in theory, but something was using the battery in suspend here.

Here's the problem analysis:

Suspend your laptop. Wake it up again. Now check the output of

sudo journalctl | grep "PM: suspend" | tail -2

May 13 18:41:00 mex kernel: PM: suspend entry (s2idle)
May 13 20:52:36 mex kernel: PM: suspend exit

If you end up with 

Your laptop is only entering "s2idle" sleep (which is basicallynot saving much more than with blanking out screen and disabling the network). What we really want instead is the "deep" sleep.

You can also check

cat /sys/power/mem_sleep

which yields

[ s2idle] deep

meaning the s2idle state is active in standby, because it's written in brackets.

The permanent fix:

Edit your grub bootloader config to add a parameter to your Linux kernel when it boots. Type

sudo gedit /etc/default/grub

and replace the line GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" with

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash mem_sleep_default=deep"

Save the file and exit. Now run

sudo update-grub && sudo reboot

To apply the changes and reboot. Test. Done.

IMPORTANT: If you now have troble waking your laptop up, DO NOT USE THE NVIDIA DRIVERS. They suck. Use noveau.

EDIT: The above works and is tested, but i now have the problem that the laptop won't resume when on battery. When it's on power supply it can resume. Any ideas!?

EDIT: You're  not alone: https://bugzilla.kernel.org/show_bug.cgi?id=201307 and https://askubuntu.com/questions/1071855/ubuntu-18-04-does-not-wake-up-from-suspend-on-asus-zenbook

16 Comments