[systemd-devel] how to let systemd hibernate start/stop the swap area?

Christoph Anton Mitterer calestyo at scientia.org
Wed Mar 29 02:43:38 UTC 2023


Hey folks.


I guess many people nowadays will run without any swap for the normal
paging use, but only have it for hibernation (at least on laptops).

Since there's nothing yet like [0] I wanted to set up systemd to
automatically start/stop the swap right before/after hibernate/resume.
And I also thought this would be a good example to be added to systemd-
suspend.service(8).


Two problems, though:


1) I fail to get it (fully) working. ^^

My understanding is that 
   # systemctl hibernate
starts hibernate.target, which in turn Requires=systemd-
hibernate.service, which does the actual beef via systemd-sleep.

So I tried with a .swap unit like:
   [Unit]
   Before=systemd-hibernate.service
   StopWhenUnneeded=true
   
   [Install]
   RequiredBy=systemd-hibernate.service
   
   [Swap]
   What=/my/swapfile
   Options=noauto

That does work, when:
   # systemctl start systemd-hibernate.service
but it doesn't when:
   # systemctl hibernate
which I don't understand, since I though that would start the target,
which would pull in and thus start the service, which before pulls in
starts my swapfile.


Also, I'm not really sure whether the above is the most systemdic
way... like should I use something like BindsTo= instead of RequiredBy=
+ StopWhenUnneeded=true?
Though AFAIU, I cannot set a reverse BindsTo=, can I?
Also, I think BindsTo sounds conceptually wrong for that dependency...
as does PartsOf=.

But I'd be happy if someone could tell me what would be the best way to
do it (and also happy to provide that as example in a PR, if wanted).


Also would it be better to explicitly set DefaultDependencies=no?
I guess not, for the case when the swap alone is manually started and
the system is shutdown.

For the sake of giving a general example I wondered whether one should
add something that sets vm.swappiness to 0 or so.
But a) this would affect other swap areas, too, b) not sure whether it
would be easily possible to re-set it to the old value (that would need
to be stored somewhere) after resume. Again, something like [0] would
be the better solution here.




2) The whole thing seems to not work with auto-detection from desktop
environments.
I use Cinnamon, when clicking the shutdown icon, it shows me Suspend,
Restart, Cancel, Shotdown.

At least the Suspend seems to be somehow detected via systemd, cause
when I set AllowSuspend=false in sleep.conf, it disappears.

So I guess systemd thinks hibernation isn't possible, because there's
now swap active, and tell that to the GUI.
Guess I'd need some override, for that.


Thanks,
Chris.


[0] https://patchwork.kernel.org/project/linux-mm/patch/20210726171106.v4.1.I09866d90c6de14f21223a03e9e6a31f8a02ecbaf@changeid/#24386433


More information about the systemd-devel mailing list