Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fedora 34 hibernation with swapfile, only for hibernation and resume

Fedora34 hibernation

This guide helps to configure the hibernation on a default Fedora34 installation by using a swap file. The Fedora34 installation comes with btrfs as default filesystem. Also, it comes with a zram swap device:

$ swapon
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   8G   0B  100

This device reserves a physical memory area in which all the content will be compressed (at its input) and uncompressed (at its output). But, we cannot hibernate with this type of swap space as it is only in memory.

Apart from that, this guide also assumes the installer default LUKS encryption.

Motivation

Nowadays we have lots of RAM in our laptops. In my case, a guy with a laptop for programming, i only use swap space for hibernation. When i move from one location to another, i dont want to loose all my open stuff nor consuming battery while i am moving.

I want to preserve the zram swap device of the default configuration.

Solution

This are the steps we need to perform in our system:

  1. The hibernation is triggered by the user.
  2. The swap file is activated.
  3. The zram device is deactivated. If there are any memory pages present in zram, they will be moved to the activated (2) swap file.
  4. Hibernate the laptop.

And the following sequence for resuming:

  1. Power on the computer.
  2. Restore system state from the swap file at boot time.
  3. Activate the zram device.
  4. Deactivate the swap file. That could cause the zram device to start compressing data.

Seems there are efforts in order to make this much better, even with dynamic sized swap files, so only generating swap files for hibernation with the needed size on each moment, and more user friendly.

This guide will make use of a fixed size swap file, since probably for going further is better to just contribute in the mentioned systemd issue .

Steps

Default Fedora34 installations comes with btrfs as default filesystem. Such file system comes with the subvolume feature. Subvolumes are not partitions. They are just a logical separations of a filesystem at a file level. Some kind of operations like snapshots, will try to include the swap file. We need to prevent this by isolating the swap file into its own subvolume:

btrfs subvolume create /swap

The above will ensure the swap file will not be taken into account in other snaphots as they are not recursive in other subvolumes.

Next is to create our swapfile for hibernation. Ensure the specified size is enough for saving the contents of the RAM + the uncompressed contents of the zram swap space. As root:

touch /swap/swapfile
chattr +C /swap/swapfile  ## Needed to disable Copy On Write on the file.
fallocate --length 33GiB /swap/swapfile  ## Please, calculate your size as mentioned in above comments.
chmod 600 /swap/swapfile 
mkswap /swap/swapfile 

Now lets prepare the path of the resume operation. We need add to the initramfs the necessary modules for resuming the system. We need to create a new file at /etc/dracut.conf.d/resume.conf with the following content:

add_dracutmodules+=" resume "

After that we need to regenerate the initramfs:

dracut -f

Next, we have to add the resume and resume_offset into the GRUB_CMDLINE_LINUX, so that Grub can instruct the kernel coordinates where the swap file resides, in order to resume the system.

For gathering the resume param we need the partition UUID in which the swap file is stored:

$ findmnt -no UUID -T /swap/swapfile
dbb0f71f-8fe9-491e-bce7-4e0e3125ecb8

Now lets gather the last needed data, we need the resume_offset. That is, the physical offset of our swap file in the file system:

sudo filefrag -v /swap/swapfile | head -n 4 | tail -n 1 | awk '{print $4}'

The output should be something similar to: 2459934..

Now we need to instruct GRUB to initialize the kernel with this coordinates. Edit the /etc/defaut/grub and grab there the parameters we just calculated:

GRUB_CMDLINE_LINUX="rd.luks.uuid=luks-4369a407-2be1-4f37-9764-ff848a0f2089 resume=UUID=dbb0f71f-8fe9-491e-bce7-4e0e3125ecb8 resume_offset=2459934 rhgb quiet"

Now lets re-configure the grub (UEFI setup assumed):

grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

Note: We only want the swap file to be used when the hibernation takes place and in the resume stage. we are not going to configure fstab entries. To achieve that, one of the options is to use systemd. We are going to configure 2 systemd services. One for preparing the hibernation and the other one for resuming it:

For enabling the swap file and disabling the zram swap device before hibernation, lets create the file /etc/systemd/system/hibernate-preparation.service:

[Unit]
Description=Enable swap file and disable zram before hibernate
Before=systemd-hibernate.service

[Service]
User=root
Type=oneshot
ExecStart=/bin/bash -c "/usr/sbin/swapon /swap/swapfile && /usr/sbin/swapoff /dev/zram0"

[Install]
WantedBy=systemd-hibernate.service

The order is important in the above service definition. First of all we enable the swap file, which should have enough space to store the contents of the "in use RAM", plus the contents of the uncompressed zram swap device. Secondly, we disable the zram swap device. At that moment, the kernel will start moving all the memory pages from the zram swap device to the swap file if needed. After all of that, the hibernation will take place only in the swap file. Last step for the above script to have effect is to install this service in systemd:

# systemctl enable hibernate-preparation.service
Created symlink /etc/systemd/system/systemd-hibernate.service.wants/hibernate-preparation.service → /etc/systemd/system/hibernate-preparation.service.

We need to disable the swap file when the system just resumed. Lets create the /etc/systemd/system/hibernate-resume.service file:

[Unit]
Description=Disable swap after resuming from hibernation
After=hibernate.target

[Service]
User=root
Type=oneshot
ExecStart=/usr/sbin/swapoff /swap/swapfile

[Install]
WantedBy=hibernate.target

Then enable it by:

# systemctl enable hibernate-resume.service

Created symlink /etc/systemd/system/hibernate.target.wants/hibernate-resume.service → /etc/systemd/system/hibernate-resume.service.

In order to avoid false positives regarding to swap space, due to the zram device existence, we need to disable some checks:

mkdir -p /etc/systemd/system/systemd-logind.service.d/
cat <<-EOF | sudo tee /etc/systemd/system/systemd-logind.service.d/override.conf
[Service]
Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1
EOF

mkdir -p /etc/systemd/system/systemd-hibernate.service.d/
cat <<-EOF | sudo tee /etc/systemd/system/systemd-hibernate.service.d/override.conf
[Service]
Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1
EOF

Now you must reboot your computer, in order the steps until here take effect.

We also need to allow systemd-sleep system to read the swap file in SELinux . One option for allowing this is to make it fail. After that, use the audit2allow to do the white listing. Lets go step by step:

  1. Try to hibernate:

    # systemctl hibernate

    This should fail right now. Probably returning you to the display manager login. Also, logging details at /var/log/audit/audit.log .

  2. We can check the event happened with audit2allow inspecting the log, it will ouput something similar to this entry among others:

    # audit2allow -w -a
    
    type=AVC msg=audit(1630262756.460:2098): avc:  denied  { search } for  pid=26180 comm="systemd-sleep" name="swap" dev="dm-0" ino=256 scontext=system_u:system_r:systemd_sleep_t:s0 tcontext=system_u:object_r:unlabeled_t:s0 tclass=dir permissive=0
    	Was caused by:
    		Missing type enforcement (TE) allow rule.
    
    		You can use audit2allow to generate a loadable module to allow this access.3.
  3. To see what rule we must allow, just type:

    # audit2allow -a
    
    #============= systemd_sleep_t ==============
    allow systemd_sleep_t unlabeled_t:dir search;

    The above output can be among others rules.

  4. Instruct SELinux to allow further attemps by executing the following commands:

    # cd /tmp
    # audit2allow -a -M systemd_sleep
    ******************** IMPORTANT ***********************
    To make this policy package active, execute:
    
    semodule -i systemd_sleep.pp
    
    # semodule -i systemd_sleep.pp

We should be ready to hibernate now. Lets just try it by:

# systemctl hibernate

After resuming, the swap file should be deactivated, so continuing with the default zram swap device setup:

$ swapon
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   8G   0B  100

As a bonus, you can enable gnome environment hibernate buttons by installing this extension.

Please check that all your devices state are properly restored after resuming.

Troubleshooting

If you are having unexpected problems, inspectioning the journal will be of help to see errors in systemd scripts:

journalctl -f
@LydiaMarieWilliamson

This comment has been minimized.

Copy link

@LydiaMarieWilliamson LydiaMarieWilliamson commented Sep 11, 2021

Two things:
(1) without a clear explanation of precisely where you're getting the magic number 33GiB from and how it is computed, it's not possible to use this - or any other - part of your description.
(2) without an explanation of how or why LUKS encryption is being assumed, where in the description to follow this assumption is being used (and why), and what to do if you're not using LUKS, then it is likewise impossible to use your description.

@eloylp

This comment has been minimized.

Copy link
Owner Author

@eloylp eloylp commented Sep 17, 2021

Two things:
(1) without a clear explanation of precisely where you're getting the magic number 33GiB from and how it is computed, it's not possible to use this - or any other - part of your description.
(2) without an explanation of how or why LUKS encryption is being assumed, where in the description to follow this assumption is being used (and why), and what to do if you're not using LUKS, then it is likewise impossible to use your description.

Thanks for your feedback @LydiaMarieWilliamson . I think the description already specifies an idea on how to calculate the space for the swap file. However, i added a comment in the code for the quick reader.

This guide assumes the default LUKS installation of Fedora34. I extended the description in order to clarify this. This is an option provided by the default Fedora34 installer. I think this guide should also work with an unencrypted setup, but still not tested. Please, if you do that, your feedback would make this better. Thanks !

@miXwui

This comment has been minimized.

Copy link

@miXwui miXwui commented Oct 9, 2021

Thanks a ton for the write-up! Saved me a ton of time. A few additions:

I received this error in journalctl:

swapon: /swap/swapfile: insecure permissions 0644, 0600 suggested.

Fix: chmod 600 /swap/swapfile

Also, I'm not sure how your use of filefrag got you the correct offset on btrfs. Hibernation wasn't working, and I got these errors in journalctl:

kernel: PM: Cannot find swap device, try swapon -a
kernel: PM: Cannot get swap writer

Turns out on btrfs, the filefrag tool gives the incorrect offset. To calculate the correct offset I followed this:
https://wiki.archlinux.org/title/Power_management/Suspend_and_hibernate#Hibernation_into_swap_file_on_Btrfs

I then updated resume_offset in grub, which worked! Note I'm not running LUKS encryption. So I can confirm it works with zram + btrfs, although I did have an issue where it got stuck from hibernation -- unsure why, might've been a fluke/my hardware.

@miXwui

This comment has been minimized.

Copy link

@miXwui miXwui commented Oct 23, 2021

Another update:

The above systemd services enable/disable the swapfile before/after hibernate, but not before/after suspend-then-hibernate with HibernateDelaySec.

Ideally, during suspend-then-hibernate:

  1. the suspended system wakes from HibernateDelaySec
  2. swapfile is enabled and zram is disabled
  3. system proceeds with hibernation

Because 2. wasn't implemented, I received these errors:

Failed to find location to hibernate to: Function not implemented
Failed to prepare for hibernation: Function not implemented

A systemd service to prepare for hibernation (enable swapfile and disable zram) can be triggered before suspend and after all of suspend-then-hibernate completes.

A full suspend-then-hibernate operation can be:

  • system suspends, then wakes up before HibernateDelaySec e.g. via a manual power button press, and then remains on
  • system suspends, then wakes up to HibernateDelaySec, and then hibernates
  • system suspends, then wakes up to HibernateDelaySec, fails to hibernate, and then goes back into suspend

hybrid-sleep is also possible, but I won't get into that here.

Ideally the hibernation preparation script (enable swapfile and disable zram) triggers only before hibernation, which is in the middle of suspend-then-hibernate.

However, it's not possible to hook into suspend-then-hibernate with a system service: systemd/systemd#18092
since the suspend and hibernate stages are lumped into a singular suspend-then-hibernate service. I tried testing with ConditionEnvironment=SYSTEMD_SLEEP_ACTION=hibernate, but it didn't work since it's only possible for a service to start before or after the entire suspend-then-hibernate.

Thankfully it's possible to drop a script into /usr/lib/systemd/system-sleep that runs at different stages. The arguments passed to these scripts are:

  • $1 (first argument): pre or post
  • $2 (second argument): the action, either suspend, hibernate, hybrid-sleep, or suspend-then-hibernate
  • SYSTEMD_SLEEP_ACTION environment variable can equal suspend, hibernate, hybrid-sleep, or suspend-after-failed-hibernate (thanks to this pull request: systemd/systemd#18110)

Documentation: https://www.freedesktop.org/software/systemd/man/systemd-suspend.service.html

So we should be able to enable swapfile and disable zram, in the script, at this stage:

  • $1=pre
  • $2=suspend-then-hibernate
  • SYSTEMD_SLEEP_ACTION=hibernate

But wait, not so fast!

When trying to enter hibernation after being woken up from HibernateDelaySec during suspend-then-hibernate at stage:

  • $1=post
  • $2=suspend-then-hibernate
  • SYSTEMD_SLEEP_ACTION=suspend

This happens:

  1. (Line 303) Execute hibernate: https://github.com/systemd/systemd/blob/main/src/sleep/sleep.c#L303
  2. (Line 218) Check hibernation location (in this case, the disabled swapfile): https://github.com/systemd/systemd/blob/main/src/sleep/sleep.c#L218
  3. (Line 224) Because swapfile starts as disabled, the function returns an error: https://github.com/systemd/systemd/blob/main/src/sleep/sleep.c#L224
  4. Line 237 is never reached to set SYSTEMD_SLEEP_ACTION=hibernate, so SYSTEMD_SLEEP_ACTION remains as suspend: https://github.com/systemd/systemd/blob/main/src/sleep/sleep.c#L237
  5. (Line 307) Finally it proceeds to suspend-after-failed-hibernate, going back into suspend: https://github.com/systemd/systemd/blob/main/src/sleep/sleep.c#L307

In summary, because the code checks for the hibernation location (in this case the presence of an enabled swapfile) before going into hibernation, and the swapfile starts as disabled, the entire operation errors. The system then goes back into suspend. SYSTEMD_SLEEP_ACTION stays as suspend and never changes to hibernate.

If the swapfile was enabled, then it'll successfully start hibernate, and SYSTEMD_SLEEP_ACTION would change to hibernate. But of course, the swapfile starts disabled because we use zram, and we want to enable the swapfile only if the system is going into hibernate.

So since it fails and goes straight back into suspend, we can't hook into:

  • $1=pre
  • $2=suspend-then-hibernate
  • SYSTEMD_SLEEP_ACTION=hibernate

The best we can do is hook into the stage before:

  • $1=post
  • $2=suspend-then-hibernate
  • SYSTEMD_SLEEP_ACTION=suspend

This means that in suspend-then-hibernate, our script to enable swapfile and disable zram will always run post suspend (waking up from suspend), irregardless if the system:

  1. automatically wakes up from HibernateDelaySec to go into hibernation or
  2. resumes from suspend before HibernateDelaySec due to pressing the power button, opening the lid, etc.

If 2. happens, the swapfile will be enabled and zram disabled, which is not what we want when resuming from suspend to stay on (skipping hibernate). But a service can disable the swapfile after the full suspend-then-hibernate operation completes, so it's only a brief moment.

TLDR: It's not possible to enable swap and disable zram only before hibernate during suspend-then-hibernate. The best way I found during suspend-then-hibernate was to enable swap and disable zram post resuming from suspend, irregardless if the system is going to hibernate or is staying on (like when the power button is pressed before HibernateDelaySec kicks in).

The services/scripts:

[a] systemd service enabling swapfile and disabling zram before suspend during suspend-then-hibernate. Caveat: could cause an issue if swapfile is on and zram is off during suspend in suspend-then-hibernate.

/etc/systemd/sleep/suspend-then-hibernate-preparation.service

[Unit]
Description=Enable swapfile and disable zram before hibernate during suspend-then-hibernate
Before=systemd-suspend-then-hibernate.service

[Service]
User=root
Type=oneshot
ExecStart=/bin/bash -c "/usr/sbin/swapon /swap/swapfile && /usr/sbin/swapoff /dev/zram0"

[Install]
WantedBy=systemd-suspend-then-hibernate.service

Enable it with systemctl enable suspend-then-hibernate-preparation.service

[b] script to drop into /usr/lib/systemd/system-sleep/ enabling swapfile and disabling zram post suspend (when waking up from suspend) during suspend-then-hibernate

Better than [a], but requires adding a script in /usr/lib/systemd/system-sleep/.

/usr/lib/systemd/system-sleep/suspend-then-hibernate-post-suspend.sh

#!/bin/bash

if [ "$1" = "post" ] && [ "$2" = "suspend-then-hibernate" ] && [ "$SYSTEMD_SLEEP_ACTION" = "suspend" ]
then
    echo "suspend-then-hibernate (post suspend): enabling swapfile and disabling zram"
    /usr/sbin/swapon /swap/swapfile && /usr/sbin/swapoff /dev/zram0
fi

Make sure the file is executable with chmod + x.

[c] systemd service disabling swapfile when suspend-then-hibernate completes, reversing [a] and/or [b]. zram seems to automatically enable on resume.

/etc/systemd/system/suspend-then-hibernate-resume.service

[Unit]
Description=Disable swapfile after resuming from hibernation during suspend-then-hibernate
After=suspend-then-hibernate.target

[Service]
User=root
Type=oneshot
ExecStart=/usr/sbin/swapoff /swap/swapfile

[Install]
WantedBy=suspend-then-hibernate.target

Enable it with systemctl enable suspend-then-hibernate-resume.service


Either combination of [a] + [c], [b] + [c], or [a] + [b] + [c] should work. I myself am running the current optimal solution [b] + [c].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment