[HowTo] Add a backing SWAP device to zram (and enable hibernation)

Introduction

What is zram? How to use it? Please see this related guide:

One of the main drawbacks to zram is its incompatibility with hibernation.
For this and other reasons zram users may wish to create an additional ‘backing’ SWAP device.

For quick-install steps skip to this section.


Creating a swap file

SWAP is a subject all its own, so for our purposes we will focus on the example of a swap file.

The ArchWiki has a handy introduction here:
https://wiki.archlinux.org/title/Swap#Swap_file

Instructions

Or follow the guide here.

Click to show

The first consideration is what size for the swap.

In order to ensure hibernation is successful even during the heaviest workloads it is advisable to make swap the size of your available RAM and zram combined.

Click to show calculation command

This bash string should calculate your total available ram, zram size, add them, and round up to the nearest whole number.

_hibned=$(awk "BEGIN {print $(zramctl | tail -1 | awk -F '[^0-9]*' '{ print $3 }')+$(awk '/MemTotal/ { printf "%.3f \n", $2/1024/1024 }' /proc/meminfo); exit}"); printf "%.0f\n" "$_hibned"

With this size in mind we can now create the swap file. We will assume 16gb here.

sudo mkswap -U clear --size 16G --file /swapfile

We will also want to add this swap to our /etc/fstab file.
The line should look like this. Note the priority. It should be lower than your zram priority.

/swapfile                                 none           swap    defaults,pri=0 0 0

After creating your backing swap file and adding it to /etc/fstab it is time to reboot!
Hibernation should now work as expected.


Another word about priority

If you followed this guide and the zram guide then the priorities should already be acceptable.
The intention is to make sure that zram has a higher priority than the backing swap device. In the given example zram should have a priority of 100 and our /swapfile should have a priority of 0.
This ensures that during general operation the system will make use of the more performant zram and, because systemd automatically disregards zram while hibernating, the otherwise ignored /swapfile will be used for hibernation.


Quick Install

If you just want a few commands then these are for you.

Click to show
printf '\nFinding optimal swapfile size.\n'; _hibned=$(awk "BEGIN {print $(zramctl | tail -1 | awk -F '[^0-9]*' '{ print $3 }')+$(awk '/MemTotal/ { printf "%.3f \n", $2/1024/1024 }' /proc/meminfo); exit}"); _roundhibned=$(printf "%.0f\n" "$_hibned"); printf "\n%s""$_roundhibned"" GB\n\n"
printf '\nCreating swap file.\n\n'; sudo mkswap -U clear --size "$_roundhibned"G --file /swapfile; printf '\nDone.\n\n'
printf '\nAdding line to /etc/fstab.\n\n'; if [ "$(tail -c1 /etc/fstab; printf x)" != $'\nx' ]; then printf "\n" | sudo tee -a /etc/fstab; fi; printf '/swapfile                                 none           swap    defaults,pri=0 0 0\n' | sudo tee -a /etc/fstab; printf '\nDone.\n\nTime to REBOOT.\n\n'

Hibernation

Hibernation may require extra configuration.
In the case of a swap file, as is used the the examples above, refer to this guide:
[HowTo] Configure hibernation with a swap file


More Information

ArchWiki swapfile-zram hibernation page
Power management/Suspend and hibernate - ArchWiki

4 Likes

Thanks a lot for the quick install!
Been using separate partition as I used to do everything manually but this time I’m doing testing and the default install just makes one partition for everything so this saved me the reinstall or resizing the current partition and make a new one.
Pretty sweet contribution!

1 Like

Just to be clear … hibernation itself can require its own configurations depending on the system.
Please see the related guide on swap file hibernation here: