Creating Swapfile Btrfs

  1. sudo btrfs subvolume create @swap

  2. sudo mkdir /swap

  3. sudo mount -o subvol=@swap /dev/XXX /swap ## replace XXX with your actual partition for example sda2, nvme0n1p2

  4. cd /swap

  5. sudo truncate -s 0 swapfile

  6. sudo chattr +C swapfile

  7. sudo btrfs property set swapfile compression none

  8. sudo dd if=/dev/zero of=swapfile bs=1M count=512 status=progress

  9. sudo chmod 600 swapfile

  10. sudo mkswap swapfile

  11. sudo swapon swapfile

  12. Now Edit the /etc/fstab and add the following two lines:

UUID=NNNN /swap btrfs subvol=@swap ## replace NNNN with the UUID of the partition where @swap resides

/swap/swapfile none swap defaults 0 0


I have been trying to follow the guide above but it seems I am doing something wrong, as far as I see I created the @swap subvolume and its inside / Once I get to the 3rd command its not working the way it is probably supposed to or I am doing something wrong.

nvme0n1 is the drive/partition? I have eos installed on, when I run ls in my root directory after I create the btrfs @swap subvolume I get:

bin   dev  home  lib64	opt   root  sbin  @swap  sys  usr
boot  etc  lib	 mnt	proc  run   srv   swap	 tmp  var

Is @swap supposed to be there?

lsblk:

seb@PrimeCore/ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:0    0 465.8G  0 disk
├─sda1        8:1    0     1K  0 part
└─sda5        8:5    0 465.8G  0 part
sdb           8:16   0 223.6G  0 disk
├─sdb1        8:17   0   500M  0 part
└─sdb2        8:18   0 223.1G  0 part
sdc           8:32   0   1.8T  0 disk
└─sdc1        8:33   0   1.8T  0 part /mnt/diamond-drive
sdd           8:48   0   3.6T  0 disk
└─sdd1        8:49   0   3.6T  0 part /mnt/titan-drive
nvme0n1     259:0    0 931.5G  0 disk
├─nvme0n1p1 259:1    0   300M  0 part /boot/efi
├─nvme0n1p2 259:2    0 922.4G  0 part /var/log
│                                     /home
│                                     /var/cache
│                                     /
└─nvme0n1p3 259:3    0   8.8G  0 part

I tried running the 3rd command from the guide and tried different partitions I dont know what I am doing wrong here…

sudo mount -o subvol=@swap /dev/nvme0n1p2 /swap
mount: /swap: mount(2) system call failed: No such file or directory.
       dmesg(1) may have more information after failed mount system call.              

sudo mount -o subvol=@swap /dev/nvme0n1 /swap
mount: /swap: /dev/nvme0n1 already mounted or mount point busy.
       dmesg(1) may have more information after failed mount system call.

Is this maybe not the best guide or am I more likely doing something wrong?

This is the wrong “root”.

That command needs to be run in the root of your btrfs partition which you may not have mounted.

You can mount it like this:

sudo mkdir /mnt/btrfs
sudo mount /dev/nvme0n1p2 /mnt/btrfs -o subvolid=5

I dont quite understand this, this is really confusing, once I mount it do I just do cd /mnt/btrfs then create the swap subvolume there?

One word is being used for two different things.

The “root” of your filesystem(aka /) is mounted on a subvolume not the “root” of the btrfs partition.

Those instructions want you to create a subvolume at the root of the partition.

Yes. That is correct.

sudo mount -o subvol=@swap /dev/nvme0n1 /swap

then I run this while still in the /mnt/btrfs directory? then just continue with the rest of the steps?

You can run that command from anywhere.

The rest of the steps should work now.

seb@PrimeCore/swap sudo btrfs property set swapfile compression none swapfile compression none
ERROR: failed to set compression for swapfile: Invalid argument

did you double copy/paste there? There is too much command.

1. sudo btrfs property set swapfile compression none
ERROR: failed to set compression for swapfile: Invalid argument

Sorry it just does that in my terminal, something wrong with the terminal due to zsh, I dont know why it does that but the command was entered properly

Its actually something to do with alacritty, I switched to konsole and tried the command in that, here is what I get:

seb@PrimeCore~ cd /swap
 seb@PrimeCore/swap sudo btrfs property set ./swapfile compression none
[sudo] password for seb: 
ERROR: failed to set compression for ./swapfile: Invalid argument
 seb@PrimeCore/swap

Either type out the command, or paste it in a text editor and edit it before pasting it in the terminal emulator.

I think this is actually a btrfs issue/change.

After disabling CoW, the compression property is no longer available.

It looks like this step is no longer needed.

btrfs now disables compression when CoW is disabled.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0e852ab8974cd2b5946766b2d9baf82c78ace03d

1 Like