You are not logged in.

#1 2023-11-02 16:26:20

SilentPuffer101
Member
Registered: 2016-07-21
Posts: 8

[SOLVED] Hibernating/suspend to disk without activating swap.

Hi,

Would it be possible to have the system hibernate to a dedicated swap partition (just for hibernating) without swapping it on?

I do have a swap partition but I don't really intend using it as swap since it's slow. Instead, I intend to dedicate it as a storage for the hibernated image.
I have already set the resume kernel command line parameter.

But even so, unless I have a swap active, I always get an error when trying to hibernate

$ cat /proc/swaps 
Filename                                Type            Size            Used            Priority
$ systemctl hibernate
Call to Hibernate failed: Not enough swap space for hibernation

Apologies, if this is a duplicate question.


EDIT1: Un-paraphrasing.
EDIT2: Solution posted below, in #11.

Last edited by SilentPuffer101 (2023-11-06 13:46:41)

Offline

#2 2023-11-02 16:39:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,462
Website

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

I've not used swap or hibernation in ages (probably at least a decade) so I'm a bit out of the loop.  But one option to consider would be to activate swap, but just adjust the various performance settings such that it would really never be used for memory swapping.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2023-11-02 16:43:13

frostschutz
Member
Registered: 2013-11-15
Posts: 1,647

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

Indirectly, you could set swappiness to 0. Enabling swap shortly before hibernating, and disabling it directly after resuming, might also work.

But other than that, swap is swap...

Last edited by frostschutz (2023-11-02 16:43:54)

Offline

#4 2023-11-02 20:02:23

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,066

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

I don't really intend using it as swap since it's slow

https://chrisdown.name/2018/01/02/in-de … -swap.html

But even when setting the resume kernel command line parameter, I get an error when trying to hibernate without first enabling the swap partition before hibernation.

Please don't paraphrase, https://bbs.archlinux.org/viewtopic.php?id=57855

Offline

#5 2023-11-03 03:45:13

SilentPuffer101
Member
Registered: 2016-07-21
Posts: 8

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

frostschutz wrote:

Indirectly, you could set swappiness to 0.

That kind of sounds like a bad idea, according to this https://chrisdown.name/2018/01/02/in-de … %20%3D%200

frostschutz wrote:

Enabling swap shortly before hibernating, and disabling it directly after resuming, might also work.

Sounds plausible, but it appears that I have to do serious rigging in systemd-sleep to allow doing that.
I am not knowledgeable in scripting so I don't think I can do that, yet.

seth wrote:

I don't really intend using it as swap since it's slow

https://chrisdown.name/2018/01/02/in-de … -swap.html

Thanks, I've completely read the article. Though, my swap storage is a spinning hard disk so it is very slow when being used as swap. The fact that it is the shingled type does not help as shingled-type reportedly performs worse than the conventional-type for random IO.

I agree with article, that swap has benefits of providing backed storage for anonymous pages and even dirty pages so that when it needs to borrow some of their space, the memory management can just "swap out" the un-backed memory pages to a backed storage until the swapped out pages will be needed. Trashing happens if the swapped out pages are 'hot' or will be used again soon.

Though as per observation, swapping in the disk is most often sequential in nature so it's okay, while swapping out tends to be random.
My storage device is still slow to be suited for highly random I/O and thus I would want to avoid it.
It is not like I am disgusted with the idea of swapping.

Offline

#6 2023-11-03 07:26:11

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,066

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

That kind of sounds like a bad idea

It's also the closest you'll get to having no swap with a swap partition/file present. The point of the article is that the latter does typically not really offer the performance benefits you might be seeking.

Backt to the OP

seth wrote:

*what* exact error do you you run into *when* and in response to *what*?

Offline

#7 2023-11-03 09:46:07

frostschutz
Member
Registered: 2013-11-15
Posts: 1,647

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

One way to find out - try it and see. If it doesn't work out, you can always change it.

You could also add some zram-swap or a (small) SSD-swap and give it a higher priority than your HDD-hibernate-swap. That way, unless your high priority swap is already fully occupied, the HDD swap would never be used for anything other than hibernation.

Offline

#8 2023-11-03 10:54:35

SilentPuffer101
Member
Registered: 2016-07-21
Posts: 8

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

seth wrote:

That kind of sounds like a bad idea

It's also the closest you'll get to having no swap with a swap partition/file present. The point of the article is that the latter does typically not really offer the performance benefits you might be seeking.

frostschutz wrote:

One way to find out - try it and see. If it doesn't work out, you can always change it.

You could also add some zram-swap or a (small) SSD-swap and give it a higher priority than your HDD-hibernate-swap. That way, unless your high priority swap is already fully occupied, the HDD swap would never be used for anything other than hibernation.

After some researching, it seems to be possible by wrapping systemd-sleep to activate the swap for hibernation.
Unfortunately, none of the concepts I found are stable to the point that I could literally implement then forget.
On the contrary, all concepts based on this approach are hacky, appear to the fragile in handling failed attempts, and increases the likelihood of breaking some PCIe device's power management (like nvidia).

It is not worth pursuing the trouble so I may as well get an SSD for a small swap with higher priority, then the HDD swap for the hibernation image with lower priority, and deal memory contention events as I encounter them.
Also, taking setting the tweaks such as the swappiness=0.

seth wrote:

Backt to the OP

seth wrote:

*what* exact error do you you run into *when* and in response to *what*?

I am not sure I understand the question, but I'll try answering it.

I get a

Call to Hibernate failed: Not enough swap space for hibernation

from systemctl when I try to hibernate the system while the resume swap partition is not active.

Offline

#9 2023-11-03 13:57:10

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,066

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

Because you can bypass that check, see the edit in https://bbs.archlinux.org/viewtopic.php?id=288848

Offline

#10 2023-11-04 13:08:14

SilentPuffer101
Member
Registered: 2016-07-21
Posts: 8

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

seth wrote:

Because you can bypass that check, see the edit in https://bbs.archlinux.org/viewtopic.php?id=288848

That's similar, if not exactly, the kind of thing that I'm trying to accomplish. Maybe I should have tried searching the forums a bit deeper for little bits like this.
I'll try that over the weekend and report back.

Offline

#11 2023-11-06 13:40:11

SilentPuffer101
Member
Registered: 2016-07-21
Posts: 8

Re: [SOLVED] Hibernating/suspend to disk without activating swap.

seth wrote:

Because you can bypass that check, see the edit in https://bbs.archlinux.org/viewtopic.php?id=288848

Yes, I stand corrected. That post's solution IS exactly what I need.
The hack is quite elegant, if I say so to myself. As a bonus, it also supports suspend-then-hibernate and hybrid-sleep.

From thread you've linked, taking zram-hibernate and tuxedo control center's service files as additional inspiration,
I've made some changes to the OP's systemd service file:

  • This will immediately swapoff the resume partition after resuming.

  • This will activate the resume partition everytime the system suspends.

  • This assumes that the resume partition is large enough for normal hibernation to work.

  • This assumes that the resume partition already set in /sys/power/resume.

  • Caveat: Not designed to work with Swap Files.

  • Caveat: Activates SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK in logind

  • Caveat: Requires systemd

Here is how I made the proof of concept:

  1. Create drop ins for logind
    Following zram-hibernate's example, create a ".conf" and put it in "/etc/systemd/system/systemd-logind.service.d"

    For example, "/etc/systemd/system/systemd-logind.service.d/disable-hibernate-swap-check.conf"

    [Service]
    Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1

    Script below will create the .conf file:

    install -dm755 /etc/systemd/system/systemd-logind.service.d/
    echo -e "[Service]\nEnvironment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1" > /etc/systemd/system/systemd-logind.service.d/disable-hibernate-swap-check.conf
    chmod 755 /etc/systemd/system/systemd-logind.service.d/disable-hibernate-swap-check.conf
  2. Write a new systemd service

    For example, "/etc/systemd/system/swap-hibernate.service"

    [Unit]
    Description=Activate resume partition swap before suspend/hibernate
    Before=sleep.target
    StopWhenUnneeded=yes
    
    [Service]
    Type=oneshot
    ExecStart=-/bin/bash -c '/usr/bin/swapon --priority "-99" /dev/block/$(/bin/cat /sys/power/resume)'
    ExecStop=-/bin/bash -c '/usr/bin/swapoff /dev/block/$(/bin/cat /sys/power/resume)'
    RemainAfterExit=yes
    
    [Install]
    RequiredBy=sleep.target
  3. Enable the new systemd service

    systemctl daemon-reload
    systemctl enable swap-hibernate.service
  4. Reboot before testing

Offline

Board footer

Powered by FluxBB