Does it still make sense to use Swap with Btrfs?

Some time ago I tried to install swap or swap file with hibernation using encrypted btrfs.
Manjaro installation does not support this type of hibernation.
Now I have set up a new notebook btrfs with swap hibernation but without encryption on a new notebook. Hibernation does not work either.
All with KDE Plasma.

My questions to you forum guys:

  1. Does it make any sense to use swap with 16GB RAM or 32GB RAM?
  2. If you have installed btrfs with swap hibernation, how can you get rid of the hibernation function because it does not work?
  3. Can you just delete the swap partition if it is not needed and reassign it to another partition or do you need to be aware of any other system adjustments that need to be made?

Yes.

Iā€™m not sure I follow. You said its not supported ā€¦ so you did it manually?
So undo what you did?
You might just be referring to the hibernation lines in/etc/default/grub ?

Yes, butā€¦
There are always things to be ware of when messing with partitioning/filesystems.
Iā€™m not sure about btrfs specific ones.
The free space cannot be ā€˜movedā€™, you will only be able to expand the partition from the left of the free space to the right to reclaim the space.
And this should be followed by enabling zram, a swapfile, or some other alternative. Because despite what some may say, see the first section.

1 Like

I used the Calamaris installer. Even though you can select swap with hibernation and btrfs installation, hibernation does not work with btrfs on Manjaro. I tested it systematically. You can find the relevant link at the top.
Itā€™s a bit poor of Manjaro that it still doesnā€™t work.

I do not know about using zram ? Does it makes sense to install on modern computers?

I could just delete the swap partition and use the space for something else. But I probably also need to look in /etc/fstab and maybe /etc/default/grub.

Question: A swap partition seems useful anyway. Is there any harm or downside to keeping swap with a hibernated installation, even if hibernation does not work? If it is ok, I will just leave it.

Some people might argue that you donā€™t need a swap at all, if you have a lot of RAM available. Iā€™m not sure how I stand on that argument. However, I would tend to keep it, regardless, especially if the extra space isnā€™t critical for another purpose.

Itā€™s easier to leave it in case you find you need it later, rather than remove it and find you should have kept it.

Just my 2 cents. Cheers.

2 Likes

+1 to above.
I have a similar setup as OP.
Given up on hibernation since sleep works fine for me. (I also have nvidia witch is another hurdle with hibernation)

I have NEVER seen anything written to my swap partition but I still keep it. Not like storage space is the expensive part in a computer nowadays.

I use a partition, not defined in fstab but rather with with swapon.

Interestingly, I am at this moment on another freshly installed btrfs system with WAY less memory and since I did not think about (read, forgot) swap in the creation of this system, there is none, at least that I can find. xD
I guess I have a perfect opportunity to test if this is a problem or not. :smiley:

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:
    GRUB_CMDLINE_LINUX="resume=UUID=197xc8b4-a57v-2973-m42c-d5afd8f0fex72"

  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.

4 Likes

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
nvme0n1                                                                                                                   
ā”œā”€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
                                                                                                                          /var/cache
                                                                                                                          /swap
                                                                                                                          /home                                               

----- 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
swap_device=/dev/disk/by-uuid/2788eb78-074d-4424-9f1d-ebffc9c37262
crypt_swap_name=cryptswap
 
## one can optionally provide a keyfile device and path on this device
## to the keyfile
keyfile_device=/dev/mapper/cryptroot
keyfile_filename=etc/keyfile-cryptswap
 
## additional arguments are given to mount for keyfile_device
## has to start with --options (if so desired)
#keyfile_device_mount_options="--options=subvol=__active/__"
 
## 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
# MODULES
# 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)
MODULES=(crc32c-intel)
 
# BINARIES
# 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
BINARIES=()
 
# FILES
# 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.
FILES=(/crypto_keyfile.bin)
 
# HOOKS
# 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)
 
# COMPRESSION
# Use this to compress the initramfs image. By default, gzip compression
# is used. Use 'cat' to create an uncompressed image.
#COMPRESSION="gzip"
#COMPRESSION="bzip2"
#COMPRESSION="lzma"
#COMPRESSION="xz"
#COMPRESSION="lzop"
#COMPRESSION="lz4"
#COMPRESSION="zstd"
 
# COMPRESSION_OPTIONS
# Additional options for the compressor
#COMPRESSION_OPTIONS=()
 
# MODULES_DECOMPRESS
# Decompress kernel modules during initramfs creation.
# Enable to speedup boot process, disable to save RAM
# during early userspace. Switch (yes/no).
#MODULES_DECOMPRESS="yes"

----- 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_DEFAULT=saved
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="Manjaro"
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"
GRUB_CMDLINE_LINUX=""
 
# 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
#GRUB_ENABLE_CRYPTODISK=y
 
# Set to 'countdown' or 'menu' to change timeout behavior,
# press ESC key to display menu.
GRUB_TIMEOUT_STYLE=hidden
 
# Uncomment to use basic console
GRUB_TERMINAL_INPUT=console
 
# Uncomment to disable graphical terminal
#GRUB_TERMINAL_OUTPUT=console
 
# 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'
GRUB_GFXMODE=auto
 
# Uncomment to allow the kernel use the same resolution used by grub
GRUB_GFXPAYLOAD_LINUX=keep
 
# 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"
#GRUB_DISABLE_LINUX_UUID=true
 
# Uncomment to disable generation of recovery mode menu entries
GRUB_DISABLE_RECOVERY=true
 
# Uncomment and set to the desired menu colors.  Used by normal and wallpaper
# modes only.  Entries specified as foreground/background.
GRUB_COLOR_NORMAL="light-gray/black"
GRUB_COLOR_HIGHLIGHT="green/black"
 
# Uncomment one of them for the gfx desired, a image background or a gfxtheme
#GRUB_BACKGROUND="/usr/share/grub/background.png"
GRUB_THEME="/usr/share/grub/themes/manjaro/theme.txt"
 
# 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.
#GRUB_SAVEDEFAULT="true"
 
# Uncomment to disable submenus in boot menu
#GRUB_DISABLE_SUBMENU=y
 
# Uncomment this option to enable os-prober execution in the grub-mkconfig command
GRUB_DISABLE_OS_PROBER=false
 
# 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: https://wiki.archlinux.org/index.php/Fsck#Boot_time_checking
#GRUB_ROOT_FS_RO=true
GRUB_ENABLE_CRYPTODISK=y

If modifying an existing system remember

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

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.

2 Likes

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