Skip to content

Instantly share code, notes, and snippets.

@buhron
Last active January 11, 2026 19:25
Show Gist options
  • Select an option

  • Save buhron/1f94400482d97fe9a0b8d47ace32d409 to your computer and use it in GitHub Desktop.

Select an option

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
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

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.)

If you use btrfs, I found this tutorial for you.
you can call this swapfile anything but I'll call it swapfile.hibernationonly but feel free to name it your own

$ sudo dd if=/dev/zero of=/swapfile.hibernationonly bs=1G count=[YOUR RAM SIZE]
  • Replace [YOUR RAM SIZE] with your ram size.
  • Replace /swapfile.hibernationonly with your swapfile name
$ sudo fallocate --length [HALF RAM] /swapfile.hibernationonly
  • Replace [HALF RAM] with half your ram
$ sudo chmod 600 /swapfile.hibernationonly
$ sudo mkswap /swapfile.hibernationonly

2. fstab

You can use a fstab but I use a script and systemd

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