Hibernate doesn't work anymore

I used to be able to run systemctl hibernate, but since I upgraded to 22.11 (and run nixos-generate-config) hibernation won’t work anymore.
It gives this error:
Failed to hibernate system via logind: Sleep verb "hibernate" not supported

I have a swap partition that is 16gb and a RAM that is 32gb. But it was working. So in case there is a new restriction, I added a swap file of 32gb:

swapDevices = [ { device = "/dev/disk/by-uuid/2538e281-3d89-481f-a708-544f1ac97840"; } { device = "/var/lib/swapfile"; size = 32*1024; } ];

and added this option too:

boot.resumeDevice = "/var/lib/swapfile";

…to no effect.

Can’t find much info on this topic, could anyone help, please?

Just FYI I don’t think NixOS supports hibernating to a swap file rather than a regular block device.

I think it is possible:

https://www.worldofbs.com/nixos-framework/

@alvatar Ah, TIL that the kernel knows how to resume from swapfiles. You do have to use the resume_offset= kernel param though, and you have to set the boot.resumeDevice NixOS option to the device of the file system containing the swap file, or else NixOS will default to the first swap device you have configured that starts with /dev.

I actually did that (following the instructions in that link), to no effect.
These are my current config params:

boot.resumeDevice = "/dev/disk/by-uuid/91fa8fc1-b272-4deb-b659-cbae6ecc8a9d";                                                                             
boot.kernelParams = [ "systemd.unified_cgroup_hierarchy=0" "resume_offset=13465600" ];
swapDevices = [ { device = "/dev/disk/by-uuid/2538e281-3d89-481f-a708-544f1ac97840"; } { device = "/var/lib/swapfile"; size = 32*1024; } ];

I also tried removing the device that is a partition altogether.

Ok, so it finally works. But I’m not sure what was it. I did change a bunch of things in my config, related to power management, like enabling powertop. But I think this is what did the trick:

security.protectKernelImage = false;

Oh, yes. That would do it :slight_smile:

    (mkIf config.security.protectKernelImage {
      # Disable hibernation (allows replacing the running kernel)
      boot.kernelParams = [ "nohibernate" ];
      # Prevent replacing the running kernel image w/o reboot
      boot.kernel.sysctl."kernel.kexec_load_disabled" = mkDefault true;
    })
2 Likes

On my laptop I’ve also recently upgraded NixOS from the previous installation of 20.09 to 22.11 and the hibernate function is broken now. However, I never saw the error message mentioned by @alvatar. My power button is configured to hibernate (via Gnome Settings) and I use it a lot. Now, after booting back up, I have to login and all programs are closed, just like after a shutdown. It’s the same with systemctl hibernate (with or without sudo). I also have a big swap partition, but I already had it before the re-installation. I followed the advice to set security.protectKernelImage = false, but without luck. In the logs the only relevant lines seems to be the following:

$ journalctl --boot=-1
...
mrt 30 16:46:50 tad-lispy-nixos systemd[1]: Starting Pre-Sleep Actions...
mrt 30 16:46:50 tad-lispy-nixos systemd[1]: pre-sleep.service: Deactivated successfully.
mrt 30 16:46:50 tad-lispy-nixos systemd[1]: Finished Pre-Sleep Actions.
mrt 30 16:46:50 tad-lispy-nixos systemd[1]: Reached target Sleep.
mrt 30 16:46:50 tad-lispy-nixos systemd[1]: Starting Hibernate...
mrt 30 16:46:50 tad-lispy-nixos kernel: PM: Image not found (code -16)
mrt 30 16:46:50 tad-lispy-nixos systemd-sleep[2493]: Entering sleep state 'hibernate'...
mrt 30 16:46:50 tad-lispy-nixos kernel: PM: hibernation: hibernation entry
$ journalctl --boot=0
... 
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0x00000000-0x00000fff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0x000a0000-0x000fffff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0x09bff000-0x09ffffff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0x0a200000-0x0a20cfff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xc7d39000-0xc7e90fff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xc7f00000-0xc7f00fff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xc7f0d000-0xc7f0dfff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xc7f0e000-0xc7f0efff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xc7f1c000-0xc7f1cfff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xca0b6000-0xca0b6fff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xcae1e000-0xcc33afff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xcc33b000-0xcc385fff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xcc386000-0xcc6f9fff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xcc6fa000-0xcc6fafff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xcc6fb000-0xcc986fff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xcc987000-0xcd1fefff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xce000000-0xcfffffff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xd0000000-0xefffffff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xf0000000-0xf7ffffff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xf8000000-0xfcffffff]
mrt 30 16:47:37 tad-lispy-nixos kernel: PM: hibernation: Registered nosave memory: [mem 0xfd000000-0xffffffff]
...

How do I begin to investigate this?

Setting the boot.resumeDevice option in configuration.nix fixed it for me.

According to the documentation of this option:

If left empty, the swap partitions are used.

But in my case it wasn’t so. It can be checked with

$ cat /sys/power/resume
0:0

That’s before the change. After the change for me it is:

$ cat /sys/power/resume
259:2

As so very often, the Arch Wiki lead me to the solution :heart:

1 Like

@tad-lispy Hm, you shouldn’t have to manually set boot.resumeDevice unless you’re using boot.initrd.systemd.enable.

I don’t. You can see my configuration in the linked repo.

I had the same problem. In my case it was caused by nixos/zfs: introduce option to control hibernation by bryanasdev000 · Pull Request #171680 · NixOS/nixpkgs · GitHub, which unfortunately didn’t warn people about this happening as I suggested here :confused:

So the solution, but make sure you understand what this means, is to do

boot.zfs.forceImportRoot = false;
boot.zfs.allowHibernation = true;

In case this doesn’t fix it for you, some other options will have added nohibernate to boot.kernelParams. You can figure out where by looking at the definitions of boot.kernelParams:

$ nix repl
Welcome to Nix 2.13.3. Type :? for help.

nix-repl> nixos = import <nixpkgs/nixos> {} 

nix-repl> :p nixos.config.boot.kernelParams 
[ "nohibernate" "loglevel=4" ]

nix-repl> :p nixos.options.boot.kernelParams.definitionsWithLocations
[ { file = "/etc/nixpkgs/nixos/modules/tasks/filesystems/zfs.nix"; value = [ "nohibernate" ]; } { file = "/etc/nixpkgs/nixos/modules/system/boot/systemd.nix"; value = [ ]; } { file = "/etc/nixpkgs/nixos/modules/system/boot/kernel.nix"; value = [ "loglevel=4" ]; } ]

This shows that the zfs.nix file adds the "nohibernate" option.

Similarly for Flakes:

❯ nix repl
Welcome to Nix 2.13.3. Type :? for help.

nix-repl> :lf /etc/nixos                                              
Added 9 variables.

nix-repl> nixos = outputs.nixosConfigurations.zion

nix-repl> :p nixos.config.boot.kernelParams        
[ "nohibernate" "loglevel=4" ]

nix-repl> :p nixos.options.boot.kernelParams.definitionsWithLocations 
[ { file = "/nix/store/xqb9d50fr30x4gp394h2v4av1dvxdhfp-source/nixos/modules/tasks/filesystems/zfs.nix"; value = [ "nohibernate" ]; } { file = "/nix/store/xqb9d50fr30x4gp394h2v4av1dvxdhfp-source/nixos/modules/system/boot/systemd.nix"; value = [ ]; } { file = "/nix/store/xqb9d50fr30x4gp394h2v4av1dvxdhfp-source/nixos/modules/system/boot/kernel.nix"; value = [ "loglevel=4" ]; } ]

(this should really be added to a tutorial somewhere!)

6 Likes

This worked for me! I just have the standard swap partition setup from the guide, and hibernation has never worked for me. Something is clearly up with the purported default behavior.

1 Like

This was a useful hit: If you have boot.initrd.systemd.enable, then setting boot.resumeDevice can help (on 23.11) :slight_smile: