Hibernation in 18.04 [duplicate]

Hibernation on my Ubuntu 18.04 did not work out of the box (I mean sudo systemctl hibernate). So I made several attempts to fix that some time ago and unfortunately can not remember exactly what I did.

But now:

sudo hibernate

successfully hibernates the system and on the next boot the system state successfully restores, except for the login screen does not appear, which is not good. And if I have many applications running than after such a resume the system might be frozen for up to 20 minutes (I see applications' windows, can move mouse pointer, but the system does not respond to clicks) but after that works normally.

sudo systemctl hibernate

does something, shuts down the system, but on the next boot I see several messages delete orphaned node and finally clean boot, as if there were no hibernation.

Please help me clear it up and enable hibernation in the gui interface.

At the same time suspend to memory and resume from memory works fine without a problem, including the login screen on resume.

The primary question I have is: Which of the two mechanisms hibernate or systemctl hibernate should I use with 18.04?

Details

The system has swap partition

$ lsblk | grep SWAP
└─sda5   8:5    0  16,8G  0 part [SWAP]
$ free -m
              total        used        free      shared  buff/cache   available
Mem:          15894        3386        9945        1311        2561       11848
Swap:         17163        2150       15013

and in /etc/default/grub it has

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=/dev/sda5" GRUB_CMDLINE_LINUX=""

and

$ cat /sys/power/state
freeze mem disk
$ cat /sys/power/disk
[platform] shutdown reboot suspend test_resume

[SOLVED] :-)

Finally I managed to bring hibernation to work. As long as I do not have deep understanding of the subject, I'd rather describe what I did. So

  1. Completely removed pm-utils and uswsusp, then sudo update-initramfs -c -k all and reboot

After this hibernation attempt ended up in clean boot instead of resume. So then

  1. Reinstalled systemd then changed device names to UUIDs so as

in /etc/default/grub

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=2c8ec945-6967-4538-93ef-49eb4df6f2a1"

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

RESUME=UUID=2c8ec945-6967-4538-93ef-49eb4df6f2a1

then

$ sudo update-initramfs -c -k all
$ sudo update-grub
$ reboot

After this sudo systemctl hibernate and resume succeeded but without login screen, so

  1. In Settings (gui) for power button action I assigned "Hibernate"

Now when I press power button the system hibernates and then restores through login screen.

Many thanks to everyone involved


UPD: Now I have found an even better solution - use swap file

I verified this on fresh Ubuntu 18.10 desktop install and suppose it to work with fresh 18.04 desktop install too as they both use swap file by default i.e. they do not allocate swap partition by default. But the default swap file is 2Gb regardless of the system memory size, so it should be increased.

The complete description is for example here.


There's two excellent answers to hibernation questions with a bit more detail here:

Little Ancient Forest Kami's answer to : Ubuntu 18.04 can't resume after hibernate

Zanna's answer to : How can I hibernate on Ubuntu 16.04?

I don't normally use Hibernate but was attempting to use it recently whilst chasing down a bug and realised that in order to work on 18.04 you need to add a line to /etc/default/grub to tell it where to resume from.

On 16.04 it wasn't necessary for me to add the 'resume=' parameter to grub, so I think the change has happened relatively recently.

You're probably best to use the UUID (Universally Unique Identifier) and can use the 'Disks' program to find out the UUID of your swap partition and just copy and paste it from there.

Use sudo nano /etc/default/grub to add to the line

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

so that it has the extra kernel parameter resume=UUID=theUUIDofyourswappartition in between the quotes.

Yours will be different, but in my case the line is:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=549816d3-0ed8-44fa-a7e4-968db519a141"

(exit nano using Ctrl+x, press y to make the changes and enter to accept the filename)

You need to run sudo update-grub to make your changes effective, but when you next reboot you should be able to hibernate without problems.

As far as I'm aware, the functionality of hibernate is to resume to where you left off, so is not designed to go to the login screen and ask for your password.

I don't know if you can change it so that it will ask for your password, but it might be worth asking a separate question to see if someone else knows what to do.


Swapfile

If you have a swapfile instead of a separate swap partition then you also need to add the extra resume_offset= parameter.

The Arch Linux Wiki has a great section on this but basically your swap file should be on the main partition that you have Ubuntu installed on.

In order to find out the offset to put in resume_offset= you can open a terminal and type

sudo filefrag -v /swapfile

you'll get something that looks like this:

ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..   32767:      34816..     67583:  32768:            
   1:    32768..   63487:      67584..     98303:  30720:            
   2:    63488..   96255:     100352..    133119:  32768:      98304:
   3:    96256..  126975:     133120..    163839:  30720:   
etc...

and you want the first number, in the first line that's under physical_offset (in this case 34816)

So just as an example, using the UUID and physical offset of my machine with a Swapfile, I've changed the GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" line to be

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=09e601cd-5bac-491a-9115-fda1b2eb4664 resume_offset=34816"

Don't forget to run sudo update-grub and reboot, but after that hibernate should work correctly.