Does it still make sense to use Swap with Btrfs?

At some point in the past I did some research on btrfs, luks etc.

Not that I use it myself - but for educational purpose.

I remember writing an essay on the topic

How can I see if something is written swap or not? Usually I have no access on it. If nothing is written that it is useless.

Following through all your essay does it mean that swap on btrfs with hibernation finally works on an encrypted device?

It has been my experience that a swap partition is rarely written to, as I have more than enough RAM to cover most situations. Then there are times when that swap might be desperately needed, due to whatever processes I’m using consuming all available RAM, and needing to swap that data to disk. I’d rather not suffer a snails crawl system performance just because I didn’t wan’t to have the swap space available.

That said, if you will never perform intensive operations demanding so much from your system, then maybe you can do without it; but as I don’t know what your usage is, I won’t recommend it.

If you ever hibernate your machine, the swap area is where the data is stored (although there is also the STR option), so there’s another reason not to lose it.

I should emphesize “never seen”, does not mean it has not been used.
For logs, I am not sure.
How I do it is if I do things like described above, situations where the swap might be needed:
Use htop and look at the “swp” if it is being used OR
lsblk -o name,path,mountpoint,fsused

1 Like

Since linux 5.1 - if I recall correctly - it has been possible to use a swapfile with btrfs.

So with any LTS >= 5.4 you can have a swapfile with btrfs.

In any case with encrypted systems the swap is a weak point and therefore a swapfile is an ideal usecase.

All my ‘essays’ has been tested on bare metal installations - it is how I learn - and my essays is - sort of - my method of documenting how to achieve reach the goal described.

Or to help someone with a specific issue - if I find the topic interesting enough - I go to great length to educate myself - and provide a topic which can be referenced when similar issues surface.

I use hibernation on my notebook with BTRFS without any problems in Manjaro with Cinnamon. I have 32 GB RAM and a swap partition with 37 GB.

The following steps are necessary for this to run:

  1. in order for waking up from hibernation to work, the swap partition in the boot loader must be passed to the kernel with the resume option. With GRUB this can be set up permanently by first editing the file /etc/default/grub as follows: resume is added to the line GRUB_CMDLINE_LINUX together with the swap partition.
    This is what it looks like for me:

  2. resum must be added to HOOKS in the file /etc/mkinitcpio.conf (after udev!)

  3. sudo mkinitcpio -P && sudo grub-mkconfig -o /boot/grub/grub.cfg

  4. now hibernation has worked for me.


This line is ADDED? You use the UUID from the swap partition you find in -lsblk?

Could you explain this more concretely for practical application? Where exactly and what needs to be added exactly in /etc/mkinitcpio.conf (after udev!)?

  1. Clear description. Just entering the commands.

If clear I will try it out for the already set up notebook without encryption and swap hibernation (not swap file hibernation).

For another notebook I will try out the instruction of @linux-aarhus . I suppose it is for a swap but not a swap file installation.

Thanks to you all. I very hope these 2 versions will work finally after many struggles I had before. Give me some time. I will give feedback to the results.

The HOOKS section of /etc/mkinitcpio.conf is an array which will look like either HOOKS=" ... " or HOOKS=( ... ). You should add resume to the content of HOOKS, but place it after udev wherever it is listed in the array.

This requires using the terminal, and nano text editor, or whichever editor you use, like this:

sudo nano /etc/mkinitcpio.conf

Make sure you only edit as @ManLuxer instructed, as editing the wrong line can potentially be painful.

I hope this helps.

The topic is about checking the swap as there was an issue with Calamares installer which did not write the configuration correct.

The topic is not about adding swap to an encrypted btrfs system - it is about verifying the correct setup.

Calamares setup

I just did a default installation of Manjaro Linux on bare-metal with Calamares installer using encryption, btrfs, swapfile and hibernation.

The resulting Manjaro Linux system contains the following settings relating to encryption and resume - UUIDs will be different for other use cases.

If using systemd - hooks will be named different

btrfs reference configuration

  • HOOKS are named differently if using systemd instead of base
  • UUIDs should be replaced with actual UUIDs

----- reference - blkid ------

NAME                                          FSTYPE      FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
├─nvme0n1p1                                   vfat        FAT32       AD2E-732A                             298,7M     0% /boot/efi
└─nvme0n1p2                                   crypto_LUKS 1           016788d5-5b28-4c2f-98f1-d8d017164dac                
  └─luks-016788d5-5b28-4c2f-98f1-d8d017164dac btrfs                   dbf4f88d-9a4b-405c-affd-0b03f1a86ade  456,8G     2% /var/log

----- reference - fstab ------

# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a device; this may
# be used with UUID= as a more robust way to name devices that works even if
# disks are added and removed. See fstab(5).
# <file system>             <mount point>  <type>  <options>  <dump>  <pass>
UUID=AD2E-732A                            /boot/efi      vfat    umask=0077 0 2
/dev/mapper/luks-016788d5-5b28-4c2f-98f1-d8d017164dac /              btrfs   subvol=/@,defaults,discard=async,ssd 0 0
/dev/mapper/luks-016788d5-5b28-4c2f-98f1-d8d017164dac /home          btrfs   subvol=/@home,defaults,discard=async,ssd 0 0
/dev/mapper/luks-016788d5-5b28-4c2f-98f1-d8d017164dac /var/cache     btrfs   subvol=/@cache,defaults,discard=async,ssd 0 0
/dev/mapper/luks-016788d5-5b28-4c2f-98f1-d8d017164dac /var/log       btrfs   subvol=/@log,defaults,discard=async,ssd 0 0
/dev/mapper/luks-016788d5-5b28-4c2f-98f1-d8d017164dac /swap          btrfs   subvol=/@swap,defaults,discard=async,ssd 0 0
/swap/swapfile                            swap           swap    defaults,noatime 0 0
tmpfs                                     /tmp           tmpfs   defaults,noatime,mode=1777 0 0

----- reference - openswap.conf ------

## cryptsetup open $swap_device $crypt_swap_name
## get uuid using e.g. lsblk -f
## one can optionally provide a keyfile device and path on this device
## to the keyfile
## additional arguments are given to mount for keyfile_device
## has to start with --options (if so desired)
## additional arguments are given to cryptsetup
## --allow-discards options is desired in case swap is on SSD partition
cryptsetup_options="--type luks"

----- reference - mkinitcpio.conf ------

# vim:set ft=sh
# The following modules are loaded before any boot hooks are
# run.  Advanced users may wish to specify all system modules
# in this array.  For instance:
#     MODULES=(usbhid xhci_hcd)
# This setting includes any additional binaries a given user may
# wish into the CPIO image.  This is run last, so it may be used to
# override the actual binaries included by a given hook
# BINARIES are dependency parsed, so you may safely ignore libraries
# This setting is similar to BINARIES above, however, files are added
# as-is and are not parsed in any way.  This is useful for config files.
# This is the most important setting in this file.  The HOOKS control the
# modules and scripts added to the image, and what happens at boot time.
# Order is important, and it is recommended that you do not change the
# order in which HOOKS are added.  Run 'mkinitcpio -H <hook name>' for
# help on a given hook.
# 'base' is _required_ unless you know precisely what you are doing.
# 'udev' is _required_ in order to automatically load modules
# 'filesystems' is _required_ unless you specify your fs modules in MODULES
# Examples:
##   This setup specifies all modules in the MODULES setting above.
##   No RAID, lvm2, or encrypted root is needed.
#    HOOKS=(base)
##   This setup will autodetect all modules for your system and should
##   work as a sane default
#    HOOKS=(base udev autodetect modconf block filesystems fsck)
##   This setup will generate a 'full' image which supports most systems.
##   No autodetection is done.
#    HOOKS=(base udev modconf block filesystems fsck)
##   This setup assembles a mdadm array with an encrypted root file system.
##   Note: See 'mkinitcpio -H mdadm_udev' for more information on RAID devices.
#    HOOKS=(base udev modconf keyboard keymap consolefont block mdadm_udev encrypt filesystems fsck)
##   This setup loads an lvm2 volume group.
#    HOOKS=(base udev modconf block lvm2 filesystems fsck)
##   NOTE: If you have /usr on a separate partition, you MUST include the
#    usr and fsck hooks.
HOOKS=(base udev autodetect modconf block keyboard keymap consolefont plymouth encrypt filesystems)
# Use this to compress the initramfs image. By default, gzip compression
# is used. Use 'cat' to create an uncompressed image.
# Additional options for the compressor
# Decompress kernel modules during initramfs creation.
# Enable to speedup boot process, disable to save RAM
# during early userspace. Switch (yes/no).

----- reference - crypttab ------

# /etc/crypttab: mappings for encrypted partitions.
# Each mapped device will be created in /dev/mapper, so your /etc/fstab
# should use the /dev/mapper/<name> paths for encrypted devices.
# See crypttab(5) for the supported syntax.
# NOTE: You need not list your root (/) partition here, but it must be set up
#       beforehand by the initramfs (/etc/mkinitcpio.conf). The same applies
#       to encrypted swap, which should be set up with mkinitcpio-openswap
#       for resume support.
# <name>               <device>                         <password> <options>
luks-016788d5-5b28-4c2f-98f1-d8d017164dac UUID=016788d5-5b28-4c2f-98f1-d8d017164dac     /crypto_keyfile.bin luks

----- reference - grub default ------

# GRUB boot loader configuration
GRUB_CMDLINE_LINUX_DEFAULT="quiet cryptdevice=UUID=016788d5-5b28-4c2f-98f1-d8d017164dac:luks-016788d5-5b28-4c2f-98f1-d8d017164dac root=/dev/mapper/luks-016788d5-5b28-4c2f-98f1-d8d017164dac splash udev.log_priority=3"
# Preload both GPT and MBR modules so that they are not missed
GRUB_PRELOAD_MODULES="part_gpt part_msdos"
# Uncomment to enable booting from LUKS encrypted devices
# Set to 'countdown' or 'menu' to change timeout behavior,
# press ESC key to display menu.
# Uncomment to use basic console
# Uncomment to disable graphical terminal
# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command 'videoinfo'
# Uncomment to allow the kernel use the same resolution used by grub
# Uncomment if you want GRUB to pass to the Linux kernel the old parameter
# format "root=/dev/xxx" instead of "root=/dev/disk/by-uuid/xxx"
# Uncomment to disable generation of recovery mode menu entries
# Uncomment and set to the desired menu colors.  Used by normal and wallpaper
# modes only.  Entries specified as foreground/background.
# Uncomment one of them for the gfx desired, a image background or a gfxtheme
# Uncomment to get a beep at GRUB start
#GRUB_INIT_TUNE="480 440 1"
# Uncomment to make GRUB remember the last selection. This requires
# setting 'GRUB_DEFAULT=saved' above.
# Uncomment to disable submenus in boot menu
# Uncomment this option to enable os-prober execution in the grub-mkconfig command
# Uncomment to ensure that the root filesystem is mounted read-only so that
# systemd-fsck can run the check automatically. We use 'fsck' by default, which
# needs 'rw' as boot parameter, to avoid delay in boot-time. 'fsck' needs to be
# removed from 'mkinitcpio.conf' to make 'systemd-fsck' work.
# See also Arch-Wiki:

If modifying an existing system remember

  • rebuild init
    sudo mkinitcpio -P
  • rebuild grub config
    sudo grub-mkconfig -o /boot/grub/grub.cfg

It should like this then in etc/mkinitcpio.conf, isn’t it?

HOOKS="base udev resume autodetect modconf block keyboard keymap consolefont plymouth encrypt filesystems"

Still open question:

    … GRUB_CMDLINE_LINUX=“resume=UUID=197xc8b4-a57v-2973-m42c-d5afd8f0fex72”

This line is ADDED? You use the UUID from the swap partition you find in -lsblk?

In the file /etc/mkinitcpio.conf look for the entry HOOKS and add the word “resume” to the list.
This is how it looks for me:
HOOKS=(base udev autodetect modconf block keyboard keymap resume filesystems)

1 Like

That seems fine.

Yes, that would be the correct UUID.

I’ll just add that you should probably avoid copy/paste to the grub file; noting the quotation marks used in the forum “ which have been known to cause issues.


Don’t forget to run sudo mkinicpio -P after editing /etc/mkinitcpio.conf so your kernels actually gets updated.
Then make sure you have correct UUID on your resume partition in /etc/default/grub (blkid to show UUID) then sudo update-grub

1 Like

How do I know if I use systemd instead of base? If using systemd for any reason what is different due to your reference configuration.

If your system has base then don’t bother.

It is mentioned for the sake of a random by-passer reading it

you are writing

It means your reference configuration is based on base. But the standard in Manjaro KDE Plasma seems systemd. Also I am using systemd. Checking just with ps -p 1 -o comm=
Also have not found your mentioned “base” but “init” for example.
How should the reference configuration look like with systemd than? Or have I understood you wrong?

The reference configuration is Manjaro Linux default for this particular usecase.

There is a topic discussing which HOOKS would be correct and what syntax should be used

And comment referencing what choices can be made and why one would choose to do so.

I don’t think it applies for this discussion - which is using the default Calamares to install btrfs with encryption and swap.

Please educate yourself using the provided links above

For the first notebook I have installed the configuration as you described. Here summarized with all comments. Of course I used the UUID of my own Swap partition. “resume” was already set by installation.

But hibernation does not work. If I press hibernation a dark screen appears as if it wants to turn off but shortly later the login screen appears.

I) btrfs with swap with hibernation without encryption
(source by ManLuxer) and updated by me.
In order for waking up from hibernation to work, the swap partition in the boot loader must be passed to the kernel with the resume option.
First you must do an Calamaris installer with swap-hibernation-btrfs but no encryption.

  1. With GRUB this can be set up permanently by first editing the file
    sudo nano /etc/default/grub or
    sudo micro /etc/default/grub if you have micro editor installed. I like it more than the standard nano.
    resume is added to the line GRUB_CMDLINE_LINUX="" together with the swap partition UUID.
    The swap partition UUID you can find by lsblk. For more information you may use sudo blkid It should look like this but with your UUID
    sudo update-grub

  2. resume must be added to HOOKS in the file /etc/mkinitcpio.conf (after udev!).
    The modified line finally may look like this:
    HOOKS="base udev autodetect modconf block keyboard keymap consolefont plymouth encrypt resume filesystems"
    Avoid copy/paste to the grub file; noting the quotation marks used in the forum “ which have been known to cause issues.

  3. sudo mkinitcpio -P && sudo grub-mkconfig -o /boot/grub/grub.cfg

  4. Now hibernation DOES NOT work.

Thanks for the information!

No necro-bumping, please.