How should I successfully hibernate Ubuntu 16.04?
I've tried almost every solution on the web but non of them was actually helpful.
I found my problem almost completely similar to this one
16.04hibernate
How should I successfully hibernate Ubuntu 16.04?
I've tried almost every solution on the web but non of them was actually helpful.
I found my problem almost completely similar to this one
Best Answer
Hibernation using
systemctl
and getting it working in tough casesFor me,
pm-hibernate
always fails. After some tweaks, I was able to hibernate using the interface of systemd (init system in 16.04 and above). I also managed to get it working on 17.04 with a swap file. This case study may be useful for others with problems.First try:
If that fails, begin troubleshooting: in the hibernate state (HTD or ACPI S4) the machine state is written to disk so that no power is needed to preserve it. The state is written either to a swap partition or to a swap file. Note: if using Btrfs DO NOT attempt to use a swap file as this may cause filesystem corruption
Your swap partition or swap file may need to be the same size as RAM to allow hibernation, but there is a good chance you will be able to hibernate if it is at least 2/5 the size of RAM, according to the Arch wiki page, so try other steps first before increasing swap size.
If your problem is that you get a clean boot instead of the expected resume, at a minimum you most likely need to set a boot parameter to find the disk image
Find your swap partition:
for me this returns (partial output)
where
/dev/mmcblk0p3
is the partition to specifyAdd a boot parameter:
To the line starting
GRUB_CMDLINE_LINUX_DEFAULT
addresume=/dev/YourSwapPartition
to the section in quotes (replace with the the partition you identified earlier). Using my example:Any time you change this file, you must run
sudo update-grub
or the changes will have no effect.Now you need to reboot. Then you can try to hibernate, by issuing the command:
To resume, press the power button and the system will boot.
If you still have problems, start debugging.
I include my case below as an example, but detailed information on debugging S states can be found in this blog and also this one.
Set some more boot parameters to capture more information. Remove
quiet
andsplash
and addinitcall_debug
andno_console_suspend
which will cause init system calls to be printed to the console so you can watch what is going wrong. I set this:Which helped me see what was going wrong on resume from hibernation. You can also try using
dmesg
.In my case, after resume I lost WiFi, and the kernel clearly was upset as most commands (for example reading anything from
/sys
, reloading modules or anysystemctl
command) would not work - the process would appear to start and just hang (all this would be returned to normal after reboot of course). Watching the system very slowly shut down and reading all the debug messages, I noticed that there were a lot of problems with "brcm", so I guessed my Broadcom wireless driver module was to blame. Sure enough I adjusted my hibernation procedure to unload the module first:on resume I reinsert the module
And everything worked perfectly. I also have to blacklist the
btsdio
module which seems to be incompatible withbrcmfmac
Update: Hibernation using a swap file on 17.04.
Once again with help from the Arch wiki page and some additional tinkering, I managed to get hibernation to work on 17.04 with a swap file. This required an additional boot parameter,
resume_offset=n
where n is the first number underphysical_offset
in the output ofsudo filefrag -v /swapfile
:Therefore, the additional boot parameter in my case is
resume_offset=34816
. You still need to set a boot parameter for the partition to resume from. This will be the root partition (or whatever partition your swap file is located on) My parameters are now:Where
/dev/mmcblk1p2
is my root partition (yours is more likely to be something like/dev/sda2
).During resume I saw the image loading successfully, but in my case (just an example - YMMVAPD) then some more drivers (
i2c_designware
) threw some errors and I got a complete system freeze on resume. Hibernation works if I unload those modules in addition tobrcmfmac
, but the system quickly becomes unusable without those modules. I therefore made a sort of script to unload the buggy modules and immediately reinsert them on resume:When I want to hibernate, I run
sudo bash script
. This works great.TL;DR
Use systemd, set a boot parameter for resume from swap, identify buggy drivers and unload them before initiating hibernation. If the system can't work for long without those modules or you need to unload several, it may be easier to use a simple script to initiate hibernation.