How do I create swapfile in btrfs?

I want to create swapfile of 8gb.

Swapfiles in btrfs require special handling.

You need to create a new subvol to hold the swapfile, create the swapfile as normal and then disable compression and copy-on-write on the swapfile.

More detailed instructions are here:

I have btrfs do I create swapfile of 8gb

Not too sure how to if your system is already installed, there might be something here

If you are doing an install however you can create the partitions manually, I can tell you how to do this

… and you should ask the question public as a Thread not asking for private support, as this will not be visible for other users with the same issue later on …


moved this as a public topic so everyone can join helping you :wink: @syed

1 Like

As we talked before on PM at telegram you have a BTRFS install with automatic setup for BTRFS subvols without any swapfile or partition.
So what needs to be done is:

  1. creating a subvol (BTRFS) for holding the swapfile
  2. creating the swapfile
  3. creating entries for grub and fstab

But someone else could help out here as I am not that familiar with BTRFS.
I just work on that script over here, but still not 100% sure about all the steps:

Would be nice to get some help on both tasks :wink:

1 Like

and sorry @dalto i made a little mess with my attempt to move this I think :wink:

1 Like

Pretty sure it was a mess before that. :wink:

1 Like

i am also not sure if poster will reach here at all may simply will go to reinstall with ext4 …

I wonder if the swapfile shouldn’t reside in a subvolume on it’s own, like @swap or @swapfile ?

The proper way to initialize a swap file is to first create a non-snapshotted subvolume to host the file

Here is an example of a setup that I did once. However I have never tried so far to make hibernation work on it.

BTRFS swapfile
1. sudo btrfs subvolume create @swap  ## at the root of the BTRFS partition

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

Do you also have a dedicated subvolume for the swapfile?

It should if you want to take snapshots. You can’t take snapshots of the subvolume holding the swapfile.

1 Like

this is what I say before

1 Like

I need to get a new pair of

BTW, I am following the steps to set up the hibernation, manually :blush:
I just finished mkinitcpio -P.
I’ll try to hibernate and report.

I am not getting it quite right:

$ systemctl hibernate 
Failed to hibernate system via logind: Not enough swap space for hibernation

I should probably post in a new thread.

you can stay here as it will help anyways to see how it works :wink:

1 Like

i just tested the scripts here on two installs and I see that it is working for encrypted BTRFS swapfile install… but the one for unencrypted has some issue getting the UUID for the root device.

if encrypted I can use:
sudo blkid -o device | grep luks to get the needed UUID for resume= but this is indeed not working for unencrypted…

what command would help to get the UUID there?

so it would be what I see in /etc/fstab for root device:
UUID=e2bf9de5-77fb-4f71-ah1e-88ea14d46955 / btrfs ***
what can be identified by the / ?

I suspect this was my issue as well.

findmnt / -o UUID -n


1 Like