Swap in Sub volume on BTRFS

Good morning everyone.
I have swap enabled on a file in the main subvolume, but TimeShift obviously refuses to work.
I’d like to move the swap file to a dedicated subvolume and would like to know if the procedure below is correct.
Thanks in advance.

  1. Creating and Mounting the @swap Subvolume
    Open a terminal and run these commands to create the subvolume and the folder to mount it in:
    Bash

Create the subvolume in the root
sudo btrfs subvolume create /@swap

Create the mount folder
sudo mkdir /swap

Now we need to make the system mount this subvolume at startup. Open the /etc/fstab file:
Bash
sudo nano /etc/fstab

Copy the UUID of your Btrfs disk (you can see it from other existing lines, such as / or /home) and add this line:
Plaintext
UUID=YOUR_UUID_HERE /swap btrfs subvolume=@swap,defaults,noatime 0 0

Save with Ctrl+O, press Enter, and exit with Ctrl+X.

Now mount the newly configured subvolume:
Bash
sudo mount /swap

  1. Creating the swapfile with the native command
    Now that we’re inside the isolated subvolume, let’s use the Btrfs-specific command. Replace 4g with the desired size (e.g., 8g if you want 8 gigabytes):

Bash
Navigate to the folder
cd /swap

Create and configure the swapfile in one go (e.g., 4 gigabytes)
sudo btrfs swapfile create --size 4g ./swapfile

This single command has already created the file, disabled CoW (+C), removed compression, and formatted the file as swap.

  1. Permanently enable swap
    To test it right away, enable swap on your system:
    Bash
    sudo swapon ./swapfile

Finally, to have Manjaro load it automatically on every reboot, reopen the /etc/fstab file:
Bash
sudo nano /etc/fstab

And add this line right at the bottom of the file:
Plaintext
/swap/swapfile none swap defaults 0 0

Save (Ctrl+O, Enter) and exit (Ctrl+X).
Final check
To make sure everything is up and running, you can run the command:
Bash
swapon --show

You’ll see a line showing your new ./swapfile associated with the /swap directory, ready for use and completely isolated from your system snapshots.

1 Like

The procedure you’ve outlined is indeed correct. :wink:

2 Likes

If I understand btrfs correctly, a swap file on btrfs is created in such a way that it isn’t included in snapshots (because CoW is disabled for it).

However, this also means the swap file cannot be used for hibernation. After a rollback, the state within the file would not match the state of the kernel and its modules. :footprints:

Edit:

says:

Users should be aware of the impact when using swapfiles alongside other data. This is especially discouraged for root filesystem.

and:

There are some limitations of the implementation in BTRFS and Linux swap subsystem:

  • filesystem - must be only single device
  • filesystem - must have only single data profile
  • subvolume - cannot be snapshotted if it contains any active swapfiles
  • swapfile - must be preallocated (i.e. no holes)
  • swapfile - must be NODATACOW (i.e. also NODATASUM, no compression)

:footprints:

By calamares, yes, but not if the user decides to create a swap file themselves. We’ve seen it happen before around this neck of the woods.

The best solution however is still to use a swap partition. That’s how UNIX was designed to work. Swap files only became popular because of MS-Windows and macOS.

2 Likes

I’ve helped a few people get a swap file working on btrfs.

I feel bad about myself doing that now.

It’s a very flexible filesystem, it almost lets you do whatever you want! Even if it’s a bad thing. :confused:

(There are just so many other better options, regardless of your situation. I just can’t even…)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.