HACKER Q&A
📣 cyka178

Why is the Linux community struggling to implement hibernation?


Today I have found out that hibernation is by default disabled in my Ubuntu 18.04 distribution. After this, I found this 11 month old post https://discourse.ubuntu.com/t/re-visiting-hibernate-on-ubun... where I realized that the Linux community does not seem to be able to implement a working implementation of hibernate. Is there any reason why this is a difficult problem? I would like to have an option in my OS like VM's have where everything that is currently running is saved on disk and can be resumed later without issues.


  👤 gvb Accepted Answer ✓
Hardware (peripherals) have state. Reading the hardware state is difficult and sometimes impossible so recreating that state correctly after powering down is very difficult. Then multiply that difficulty across all the different hardware that can be in a PC and it becomes extremely difficult to do on an arbitrary laptop/PC.

Add to that difficulty the fact that many hardware (peripheral) vendors provide incomplete documentation making it difficult or impossible to implement "off-nominal" situations like hardware state reading/writing-restoring.

VMs have an advantage that the peripherals are limited in types and numbers and the state is "virtual" so the state is directly accessible (r/w) in the virtualizing driver software, not buried in physical hardware. When you "hibernate" a VM, no physical device actually powers down and no (buried in hardware) state is lost.


👤 Kliment
After setting up a swap partition of sufficient size, hibernate worked with no issues whatsoever on my very recent hardware and kubuntu. The main reason it's disabled by default on ubuntu is that the default partition setup isn't compatible with hibernation for most memory sizes. You need a swap configuration that's large enough to fit your entire RAM and also your current swap. I find it works at least as well as on any other OS. I currently have it configured to auto-hibernate on low battery or after some number of hours of sleep. From my point of view the Linux community is doing absolutely great on that front, and I've never had a machine where hibernate didn't work correctly. The main issue here is the defaults on one distribution (ubuntu) make it a massive pain to enable.

👤 deckard1
It's going to depend on your hardware and probably your distro.

I have a ThinkPad running Archlinux. I have suspend-then-hibernate mode enabled, which means when I close my laptop lid it suspends for 2 hours (configurable) and then puts itself into hibernate. If I decide to open the laptop within 2 hours, it's available instantly. Otherwise, it takes maybe 20 seconds to reanimate from hibernation. People often say, why not just shutdown and reboot? The obvious answer is that I don't want to close out a dozen Chrome tabs, close all my terminals, all my files, and open everything up every time I need to walk away from the computer.

I have secure boot turned on. Hibernation is done to a swap file (I can simply delete the file if I need the space). I have LUKS encryption enabled. I'm dual booting with Windows. And yes, it all does work perfectly fine.

You can have your cake and eat it too. It just takes a bit of research and a bit of time to setup.


👤 k__
Not even MacOS gets it right if you confront it with the same problem that Linux is facing.

I switched the SSD of my MBP 2 years ago and since then everything is out of control. Sometimes the thing keeps running when I close the lit and when I open it again the battery is empty. Sometimes it simply shuts down entirely and has to boot from scratch when I open it again.

I tried reinstalling the OS and whatnot, nothing worked.


👤 hospadar
Personal hot take: It's super hard and no one cares very much.

With SSDs, booting is fast. Most apps that I care about which have complex state (browser, IDE) do a much better job of saving their state than the OS can do (browsers & IDEs can and do easily restore sessions, etc).


👤 severine
Interesting read here: https://mjg59.dreamwidth.org/55845.html ("Making hibernation work under Linux Lockdown")

Discussed in HN: https://news.ycombinator.com/item?id=26285683 (113 points, 39 days ago, 64 comments)


👤 pixelmonkey
One reason is that a big community effort to rewrite the suspend-to-disk layer, which made it more reliable, called TuxOnIce, was kept out of the mainline kernel for a number of reasons, and thus a lot of "community energy" for solving the problem has been sapped out. Also, this is a uniquely "desktop Linux" problem, so exactly the kind of problem that rarely gets commercially sponsored developer hours thrown at it.

https://en.wikipedia.org/wiki/TuxOnIce


👤 dorfsmay
As a side note, if you have a recent laptop, there is probably a setting in the BIOS for sleep mode, Windows 10 vs Linux.

This makes a huge difference on Linux, and I hear the windows mode is somewhat experimental and not working so well yet even with Windows.

https://mobile.twitter.com/dorfsmay/status/13629639921094778...


👤 rickspencer3
I was involved in these discussion for Ubuntu ... back in 2011.

You can see some discussion here: https://bugs.launchpad.net/ubuntu/+source/policykit-desktop-...

and even more here: https://bugs.launchpad.net/ubuntu/+source/policykit-desktop-...

net/net even back then, no matter how much effort we put into it, we could never make it work on even most machines because there was just too much variability. The OEM team could make it work for specific machines that Ubuntu was pre-installed on, but otherwise, hibernate was just a bug farm.


👤 _joel
Hibernate with LUKS (albeit after a lot of hacking) works pretty well on Debian Buster. I've resumed and hibernated for many cycles with no issues. Wifi comes back etc. It never used to be the case, in my experience at least

👤 wing-_-nuts
I have to be honest, I struggle to think of a scenario where simply putting the computer to sleep isn't the better option. If power draw is a concern, just power the computer off? It's certainly not worth having a 16 or 32 GB swap partition on my machine just to enable hibernate

👤 ashneo76
I don't think this is true.

Regularly my MBP dies with the lid closed with no option to hibernate. After which, it refuses to start for 15 minutes after being plugged in, before it agrees to turn on. To me, that is user hostile.

Windows hides the hibernate option until you do some magic incantation. Often upon restart, wifi driver is disabled or cannot be enabled and I have to restart.

On Linux, At least with a decent swap size, all I have to do is `systemctl hibernate` and it works like magic. LUKS and sd-boot are what I use as well.


👤 ineedasername
My very naïve understanding of hibernation was that basically RAM was written to disk as some sort of image, and coming back from hibernation means loading that back into RAM... I'm getting the impression that this is very much not true.

I can't, however, easily find an explanation of how actual hibernation is different from this simplistic understanding.... would someone care to explain?


👤 pedro2
Thanks to Secure Boot (AFAIK), you can't implement hibernation until you implement hibernation file signing.

Sleep works fine for ages. Works better on Linux on my 2015 laptop than on Windows (hint: if you have a KIRA laptop, suspend stops borking after you update the drivers for the touchpad to a ~2019 version).


👤 hobonate
Frustrated with my Fedora Linux configuration, I recently got hibernate to work with my Gentoo Linux system.

I have a LUKS (but not LVM) encrypted root and swap partition on /dev/sda3 and /dev/sda4 respectively.

I use the command "su && mount /boot && genkernel --luks initramfs" to generate the initramfs.

I give the initramfs the following parameters: "crypt_root=/dev/sda3 root=/dev/mapper/root crypt_swap=/dev/sda4 resume=/dev/mapper/swap". This makes my initramfs ask for my password for the partitions, then decrypt the partition, then mount them and resume from hibernation if possible.

I add this parameter by setting GRUB_CMDLINE_LINUX in /etc/default/grub, and then running "grub-mkconfig -o /boot/grub".

When I want to hibernate I use the following command: "exec loginctl hibernate". My window managers let me bind this to a keyboard combination.

I am using openrc as my init system (with elogind), and sway/GNOME as my window manager. The computer uses legacy BIOS (SeaBIOS) and the storage uses GUID partition table with the grub bootloader. Hope this helps.


👤 rurban
You are misleaded. Only the Debian and Ubuntu communities are struggling with technical issues, such as hibernation. What do you expect from lawyers running a distro?

The technical linux communities, like Redhat, Suse or Arch figured it out. See eg https://www.ctrl.blog/entry/fedora-hibernate.html


👤 fsflover
Hibernation and suspend work fine in Linux on hardware designed for Linux.

👤 phendrenad2
Maybe some important context that's missing for a lot of people is - on Windows hibernation is disabled by default too. So perhaps the question is, is hibernation still relevant in 2021?

👤 twobitshifter
Reading replies, it seems hibernation is not something that users ask for. What they ask for is to restore the program(s) to the state that they left it in. Files open, state loaded in memory etc. This should be an easier problem than a special boot sequence that is restoring hardware state. There’s probably one or two programs open at a time, that I don’t want to lose my place in, maybe some that I’ve written myself. If we wanted to add a “save state” option in the title bar would that be a tractable problem?

Of course app developers can make a restart less painful too. Web browsers have already implemented restoring tabs from the last use, and Outlook will automatically save any of your drafts. For any work in the cloud, you really just need a pick up where you left off cue.


👤 chme
Not even Microsoft get hibernation right all the time. So this isn't a issue that only Linux has.

Every time my desktop PC running Windows 10 tries to wake up from a suspend-to-disk or even suspend-to-RAM state, it resets its BIOS and RTC.


👤 WesolyKubeczek
Hibernation is kinda useless on my laptop with 64 GiB of RAM. Most of my machines have 32, none less than 16.

Hibernation is not a very viable scenario for me. I have uses for a 64 GiB chunk of my solid state storage, thank you very much.


👤 phibz
I have a Dell precision laptop from 5 years ago. Every single piece of hardware is supported in Linux, including the smart card reader and the dual Nvidia Quadro and Intel graphics. With Ubuntu 18.04 hibernation, which was enabled, would sometimes cause graphic glitches when resuming. Fixing them required a reboot. From 18.10 onward the issue has been resolved completely. Perhaps this is because of special attention from Dell and Canonical? Linux is listed as full supported by Dell for this laptop. I did ensure I had a swap partition larger than my available RAM.

👤 mwfunk
My day-to-day Linux usage was mostly in the '90s and 2000s, but back then anything related to power management was just like that. Like, power management stuff was generally implemented and available, but often disabled by default because it was only 99% reliable when it needed to be 99.99999% reliable.

Back then the consensus was that it's a really hard problem for all sorts of reasons (including but not limited to proprietary hardware, crappy hardware, etc.), which of course it is, but that given a few years and the shallowness of all bugs under the collective eyeballs of the entire Linux community it was inevitable that it would eventually become rock solid. I just remember a lot of people being really really really insistent about it, and when sophomore CS undergrads on Slashdot are really insistent about things being true then you know it must be true. I'm seeing the same advice here today, so you're in luck! Give it a couple years, it'll be fine. Either that or "Windows doesn't do it that great either". Either that or "nobody actually needs to use power management features and if you do you are computering wrong". Either that or "I've never had a single problem with power management on Linux but I've had tons of problems with it on macOS and Windows so there!" Aren't you glad you asked?!?


👤 throwaway823882
I've been using hibernation in Linux for over a decade. Sometimes it's as simple as appending "resume=/dev/my-swap-disk" to your bootloader kernel args.

I think distros don't ship with it by default because it's still slightly tricky to get right in some cases. Hibernating on some hardware can crash or lock your machine. Remember that most Linux distros are still just a puddle of random tools and glue; it's never going to be as polished as a commercial OS.


👤 guenthert
Is it really worse it though? Today (and for a number of years now) RAM is cheap enough to stuff even low-end laptops with multiple GiB. Unless you have a very fast storage subsystem, hibernating and restoring from the frozen image takes an awful long time. These days many applications, including web browsers (which for many is the only application they ever need), offer to restore the last state, hence you get mostly the same benefit (plus garbage collection).

👤 turminal
Because ACPI is garbage.

👤 brudgers
Why haven’t computer companies implemented hibernation for Linux?

If a laptop that won’t hibernate is a defect, it’s a manufacturing defect.

It’s not the fault of volunteers working on a free operating system.


👤 pessimizer
Hibernation never fails for me with Debian, but sleep sometimes gets in a weird argument with some peripheral where it immediately wakes again. My solution for the sleep problem is actually to hibernate, and after bringing the system up from hibernation, sleep works again.

👤 dyingkneepad
It's been a while since I worked with Hibernation (S4 suspend) on Linux, so forgive me if any of the details is wrong. Hibernation involves booting a "clean" Kernel and then switching on the fly to the previously stored Kernel after everything is up.

0. This is a highly unusual operation. There is no other code that exercises this scenario of switching form a hot state to another hot state, not even S3 sleep or runtime suspend (AKA s0ix, or PCI D3 Hot) although they are a little similar. You need to completely freeze the hardware (including invalidate every cache, stop all firmware, etc), then switch to the other Kernel and make sure this other Kernel can resume operation from the state the hardware is, which is probably different from the state left at boot or at S3.

1. AFAIK the hardware (BIOS, firmware, etc.) cannot detect this as well as it can detect, for example, S3, so it can not properly react. I know, sometimes hardware/firmware gets in the way, but sometimes it helps by setting state your driver may not otherwise be aware it needs to set.

2. Testing S4 is a pain. Basically if you want to guarantee S4 works you have to put the machine to S4, wake it up, run your whole test suite again, then put to S4 and wake up a few more times and then test again. Some bugs don't show up in the first S4 cycle.

3. Debugging S4 is even worse, since it involves two different Kernels and a lot of wasted time checking for everything. Do you think devs run S4 cycles before sending patches to the mailing list?

4. With machines booting so fast and both S3 and runtime suspend working so well (not necessarily on Linux), there is little reason to even want to hibernate. Seriously, why would I ever want to use S4?

4.5. Colorary: S4 bugs will always be very low priority for the devs I pay, unless I have a paying client explicitly yelling about it.

5. Every driver in your system needs to be properly working. Just a single broken driver may screw everything in your S4 cycles. So unless you're running on a machine where some dev team explicitly tested S4 to make sure it works, then there's a chance it won't work properly.

6. It's not impossible to get this right, it's not impossible to architect drivers so the S4 code benefits from the S3 code. It's not impossible to architect a driver so it's properly initialize and work on whatever state the registers are. But it's just almost never the priority to ensure S4 works.

Edit:

The driver I worked on was nicely designed and we tried to take advantage of S3 for S4 as much as we could. Still, sometimes an S4 bug would appear and would result in tons of debugging hours just for it. I remember one specific case where the hardware got a new cache and we forgot to invalidate it before switching, and that caused very weird memory corruption. We wasted probably 2 "man months" in debugging hours before the one line fix was committed. We had a client that was doing like 50 S4 cycles in their test procedures, and the bug for some reason would only appear after 2 S4 cycles.


👤 dwheeler
I frankly don't trust hibernation on any computer, using any operating system. Too much can go wrong, and testing it is so hard that I don't think any organization really has it solved. It's also hard to beat the power drain of "off".

If you shut down the computer when it's not in use, the problems stated never happen. That's not a solution everyone will use, I guess, but it completely eliminates the problems and is very simple to do. I wish more problems had "simple and complete" solutions :-).


👤 lettergram
I have setup hibernation on a arch laptop -

https://austingwalters.com/increasing-battery-life-on-an-arc...

Effectively it will idle for 2 hrs and if I don’t reopen the lid it’ll shut down (saving state to disk). Works really well, boot up is slower than I’d like, but battery life is great. Pretty happy, tbh.


👤 dathinab
Don't know. Tbh. hibernation always worked for me, but I ironically close to never used it.

Through there is some hardware for which you will have a hard time to make hibernation work and the only reason why it works on windows is because the vendor explicitly provided a proprietary/closed source driver for their hardware.

And besides this there is the simple reason that Linux is server first. Servers generally don't do hibernation.


👤 ryan-allen
In order to get around this I got a 4500U PN50 [0] and two power supplies, so I can take my 'work computer' home with me, it's a compromise but it's good!

[0] https://www.asus.com/au/Displays-Desktops/Mini-PCs/All-serie...


👤 Kaze404
Is it? I use Hibernate every single day on my EndeavourOS laptop, and I didn't have to configure anything. It perfectly as far as I can tell.

👤 shortformblog
I got hibernation working on my PopOS machine a while ago, but it took a lot of steps. The problem is that it requires a LOT of swap space. That machine has 32GB of RAM—and that means I have to have a dedicated swap partition at least that large to make it work.

Honestly, it would probably be easier to just run everything off a VM if having persistent states matters to you.


👤 shmerl
Research the way to make a swap file (no need for partitions) that has enough room to fit your RAM and hibernation could be usable.

References: https://wiki.archlinux.org/index.php/Power_management/Suspen...


👤 ivolimmen
I am a Linux enthausiast but I never got why people need/want hibernation. My Linux laptop (Dell XPS15) boot under 7 seconds... my desktop starts in 10ish (?) Even if it took 20 I am fine with that. My previous desktop did not even have an SSD and I was fine with a 1 minute boot.

👤 smivan
It's unfortunately non-trivial to set up and requires a fairly large swap partition

https://blog.ivansmirnov.name/how-to-set-up-hibernate-on-ubu...


👤 ratsmack
My brand new Lenovo laptop with Windows would fail to wake up from hibernation quite often. I decided that since it starts up in about 15 seconds from a cold start that I will just power it off every time instead. It powers down in about 10 seconds.

👤 yjftsjthsd-h
Maybe (almost) nobody cares? I'd bet that most cases where it'd be useful are served fine by sleep mode; S3 and company do use power, but it's so little as to be irrelevant for most people.

👤 aksss
Ironically, I think my new Windows laptop handles S4 far more reliably than it can do S3. S3 seems like a coin toss on new laptop platforms until maybe a year or more of driver updates have rolled out.

👤 Rompect
When I had Manjaro on a desktop machine, hibernation worked out of the box and pretty well. I didn't use it that frequently though. But this was without full system encryption.

👤 xaduha
People love kicking systemd for its sprawling nature and yet here you can see multiple people saying that `sudo systemctl hibernate` just works™

👤 davidbanham
I run Arch Linux on Thinkpads. I’ve never had an issue with hibernation.

If you stick to “common” hardware and a high quality distribution you should be fine.


👤 asciimov
One issue is CPU power states. Linux doesn't always do such a good job handling power states across various hardware configurations.

👤 j-james
I'm surprised to hear this. Hibernation has worked perfectly since installation on all of my Linux computers for the last ~2 years.

👤 creese
I've never had an issue with hibernation. Both suspend to ram and suspend to disk just work.

👤 fulafel
I think a version of the question including the hardware vendors could be more fruitful.

👤 tkainrad
It's a mess, sure. But it is very much possible to hibernate with Linux. I have yet to find a machine where it doesn't work with the way described here: https://tkainrad.dev/posts/setting-up-linux-workstation/#hib...

TL,dr: Set up large enough Swap and configure Grub correctly


👤 schainks
I have a whole presentation about this — TL;DR asking your BIOS to change power states in Linux is a black art that is performed via the fruits of (usually) reverse engineering or strategic guessing, especially in the UEFI world. Multiply that across all the different hardware combos that can exist, and you can see why this is supported on windows/closed source OSes and not Linux.

👤 da_big_ghey
Its not? I have using `systemctl hibernate` many time in each day for a year past

👤 senorsmile
It works fine on all of the laptops in my house. We are running kde on manjaro.

👤 atat7024
Same reason they're struggling with security.

Intel doesn't need it to sell chips.


👤 loosetypes
Can anyone speak to their experience with hibernate on system 76 laptops?

👤 luxuryballs
They can’t even make it remember my screen configuration on reboot...

👤 etherio
Works out of the box on my manjaro install.

👤 CyberRabbi
The better question is why are you struggling to volunteer your time to help make hibernation on Linux better?

👤 snvzz
There's many OS designs out there.

The Linux kernel is huge, complex and particularly messy in terms of internal structure. This makes even otherwise trivial modifications hard, and complex modifications extremely hard. The development overhead is brutal.

And hibernation just happens to be a scenario that's a nightmare to implement with such a design.