Skip to content

Instantly share code, notes, and snippets.

@buhron
Last active April 25, 2025 02:40
Show Gist options
  • Save buhron/1f94400482d97fe9a0b8d47ace32d409 to your computer and use it in GitHub Desktop.
Save buhron/1f94400482d97fe9a0b8d47ace32d409 to your computer and use it in GitHub Desktop.
Use hibernation with zram using temporary swapfiles
[Unit]
Description=Activate swapfile used for hibernation
After=network.target
[Service]
ExecStart=/usr/bin/env swapon /swapfile.hibernationonly
[Install]
WantedBy=multi-user.target
scroll down/click here for the real content

if you have the ability approximately 98% of all humans (10 and older) (according to a very true source made by.... me!) and that is the ability to use your eyes to inform the gist title to yourself, you use zram and want to use hibernation

0. IMPORTANT MODULE

You need to put the resume module in your initramfs
mkinitcpio:

  • Edit /etc/mkinitcpio.conf
  • Find HOOKS= and place resume in between block and filesystems
  • Regenerate initramfs using sudo mkinitcpio -P dracut (most distros):
  • Create a file named /etc/dracut.conf.d/resume.conf
  • Put the content add_dracutmodules+=" resume " in the file
  • Regenerate initramfs using sudo dracut -f After that reboot the system

1. swap swap

i use zram because currently I unfortunately use a HDD and have only 4 GB of ram. You definitely have better specs, but I don't :(
anyway we will need to use a swapfile as zram does not support hibernation (Swap and swapfiles are stored on disk, however zram is stored on memory. Hibernating on zram sounds exactly like sleeping, but linux has zram as its frenemy. The system will crash and reboot if you use hibernation on zram. Period.)
why do I keep saying swap ITS A SWAPFILE
ok ANYWAY. If you use btrfs, I found this tutorial for you. However I don't use btrfs so lets continue.
We will call this swapfile swapfile.hibernationonly but feel free to name it your own. Anyways lets continue

$ sudo dd if=/dev/zero of=/swapfile.hibernationonly bs=1G count=[YOUR RAM SIZE]
  • Replace [YOUR RAM SIZE] with your ram size.
  • For some reason dd doesnt work and you have to manually interrupt dd as soon as it hits your ram size (use ls -lh) After that do
$ sudo fallocate --length [HALF RAM] /swapfile.hibernationonly
  • Replace [HALF RAM] with half your ram
    • You might not need to put half your ram. I put half my ram but go do whatever make the swap...
$ sudo chmod 600 /swapfile.hibernationonly
$ sudo mkswap /swapfile.hibernationonly

2. fstab fstab

I was planning to use a fstab to activate the swapfile but systemd didn't like setting a priority and set it higher then my zram (I don't use zram-generator since it only allowed me to use 512 mb of zram, I use a script I made and a systemd unit). Setting a higher priority causes Linux to stay on that swap with the priority

To turn on the swapfile AFTER zram (for those who use zram-generator) use the systemd unit provided at the top of the page, download it and place it in /etc/systemd/system Thats it ig

3. CMDLINE

You need to update the command line. the UUID is basically the same as your root partition UUID, you can use PARTUUID as well To get the resume-offset run:

$ sudo filefrag -v /swapfile.hibernationonly | awk '{if($1=="0:"){print $4}}' | sed 's/\.\.//'\n

GRUB:

  • Edit /etc/default/grub
  • at the GRUB_DEFAULT_CMDLINE line add this to the quotation marks
    • resume=UUID/PARTUUID=[YOUR (PART)UUID HERE] resume-offset=[resume-offset you got earlier]
      • UUID/PARTUUID - whether you chose UUID or PARTUUID put your choiceb= there
      • [YOUR (PART)UUID HERE] - your root partition UUID or PARTUUID
      • [resume-offset you got earlier] - The resume-offset you got earlier in the tutorial UKI:
  • Edit /etc/kernel/cmdline
  • Pretty much like GRUB except you don't add the command line parameters to the GRUB_DEFAULT_CMDLINE, you add it to the file For any other bootloader refer to your braincells for changing the command line

4. THE ULTIMATE TEST

After rebooting, run the command:

$ (sudo) systemctl hibernate
  • (sudo) - Some distros require root privileges for hibernation, use sudo if it does
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment