I just need a second pair of eyes on my config because I don’t know the ins and outs of systemd. Someone advised me I should make my unit partOf = [ "suspend.target" ];, with the rationale being that swapoff may race with the resumption process, but online I’ve never seen that mentioned.
services.upower = {
enable = true;
criticalPowerAction = "HybridSleep";
percentageAction = 10;
percentageCritical = 15;
percentageLow = 20;
};
# NOTE: swap space is required for HybridSleep to work. This properly shuts
# down the system when it sleeps for 2+ hours. Failing to shut down properly
# RISKS FILESYSTEM CORRUPTION. So don't try hacking a Sleep-only option. The
# hypothetical SSD wear is better than corrupting your FS.
swapDevices = []; # intentionally left empty
zramSwap.enable = true; # prefer zram to disk swap
# normally automatically populated with swapDevices, but we don't want swap
# in normal operation, only when hibernating:
boot.resumeDevice = "/dev/disk/by-label/TODOLABEL";
# TODO:
# 1. create a swap partition
# 2. mkswap -L LABEL the partition
# 3. s/TODOLABEL/<the label>/ here
# https://serverfault.com/a/922002
systemd.services.systemd-logind.environment = {
SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK = "1";
};
systemd.services.hybrid-sleep-swap = {
enable = true;
partOf = [ "suspend.target" ];
before = [ "systemd-hybrid-sleep.service" ];
requiredBy = [ "systemd-hybrid-sleep.service" ];
unitConfig = {
StopWhenUnneeded = true;
RefuseManualStop = true;
RefuseManualStart = true;
DefaultDependencies = false;
};
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.util-linux}/bin/swapon /dev/disk/by-label/TODOLABEL";
ExecStop = "${pkgs.util-linux}/bin/swapoff /dev/disk/by-label/TODOLABEL";
};
description =
"Enable swap device before invoking HybridSleep, disable after resuming";
};
Why don’t you just enable the swap at boot? What are you trying to achieve here?
In Linux some swap space is always recommended to avoid thrashing in near-OOM conditions and contrary to popular belief, it does not decrease the system performance.
If you really don’t trust the kernel to handle page swaps correctly, you can set the vm.swappines sysctl to a low value, even zero, but still use the swap for hibernation.
AFAICT this is still pretty heavily debated. A lot of people have strong opinions one way or the other, but I’ve yet to see solid evidence in either direction; the arguments usually boil down to poor quality testing and/or dogma and/or intuition.
I’d be very curious to know their reasoning for thinking this. I can’t imagine what it would be.
Hm, every time I stumble upon this I have to check the kernel docs on what “swappiness” means exactly again.
Setting it to 0 just means that the kernel will only start swapping pages once you’ve run out of memory.
I think that would in practice exacerbate the types of behavior people who want to disable swapping dislike - mostly I see the use case being that you’d prefer the OOM killer just kills something than to first have your system slow down to a crawl because of thrashing for a while. Not so common in the SSD world anymore, but if you have your swap on spinning rust… Unfortunately having any swap at all delays when the OOM killer kicks in.
And yeah, apparently there is still no way to make something more desktop-friendly happen with the kernel alone.
Hah, it’s enabled by default, too. Would be interesting to know more about your use case here @hyphenrf, it’s quite likely there are better solutions than whatever you’re hacking together.
As a certified swap hater … just keep the swap on. The last thing you likely want is a race condition between your laptop dying and successful hibernation.
If frequent memory contention is such an issue to the point that swap would get frequently used in daily computing, set up earlyoom and configure it to prioritize whatever you feel most comfortable forcibly quitting. (But I don’t think this post is about memory contention anyway, so I’ll leave that be.)