Skip to content
hibernate BTRFS

BTRFS resume and hibernate setup

Change from swapfile to swapfile with hibernate for automatic BTRFS setups.

This will only work on installs using default auto-partition with BTRFS and swapfile!

only if you install in this way the script will work, if your setup is different ask at the forum !!

This script will recreate the default 500MB swapfile to 8GB and set up resume for grub and kernel images.

So that you can use hibernation with BTRFS and swapfile setup, as long as it does not get implemented into Calamares.

you can download it here:

unencrypted automatic BTRFS installs with only a swpafile created:


and run the script:

sudo ./BTRFS-resume-creator

if system uses full encryption (LUKS):


run it:

sudo ./BTRFS-encrypted-resume-creator

  • Never run this script more than once!
  • Never use in systems with a different setup than the default automatic BTRFS setup with swapfile!
  • Take a close look at the script before you run it!
  • If you are not sure, ask in the forum or in the chat before you dismantle your system into a thousand individual parts.

Great BIG BIG warning:

Do not simply use this script if you have an issue with swap or hibernation.. it will in most cases lead you to more issues.

Read the script before running it, as it may not fit your configuration.

#!/usr/bin/env bash

# -x = show every command executed
# -e = abort on failure
set -xe

main() {
    # :: swap :: #
    swapoff /swap/swapfile || true

    truncate -s 0 /swap/swapfile
    chattr +C /swap/swapfile

    btrfs property set /swap/swapfile compression none

    dd if=/dev/zero of=/swap/swapfile bs=1M count=8000 status=progress
    chmod 600 /swap/swapfile
    mkswap /swap/swapfile
    swapon /swap/swapfile

    # :: btrfs_map_physical :: #
    gcc -O2 -o btrfs_map_physical btrfs_map_physical.c

    # :: edit grub :: #
    offset=$(./btrfs_map_physical /swap/swapfile)
    offset_arr=($(echo ${offset}))
    offset_pagesize=($(getconf PAGESIZE))
    offset=$((offset_arr[25] / offset_pagesize))
    btrfsroot=`findmnt / -no UUID`

    sed -i "s#loglevel=3#resume=UUID=$btrfsroot loglevel=3#" /etc/default/grub
    sed -i "s/loglevel=3/resume_offset=$offset loglevel=3/" /etc/default/grub
    sed -i 's/keymap consolefont filesystems/keymap consolefont filesystems resume/' /etc/mkinitcpio.conf

    # :: remake images :: #
    grub-mkconfig -o /boot/grub/grub.cfg
    mkinitcpio -P

    # :: cleanup :: #
    rm -f btrfs_map_physical.c
    rm -f btrfs_map_physical

    echo 'All done, please reboot!'

main "$@"

Another simple way that may work easier for you:

Connect with us:

BTRFS resume and hibernate setup

%d bloggers like this: