Skip to content
Home ยป How to Create a Swap File in the btrfs filesystem?

How to Create a Swap File in the btrfs filesystem?

The previous article explained how to create a swap file on the Linux server. But the steps in the article can only run if you use the xfs or ext4 filesystem. If you use btrfs in your filesystem, then the article will not be able to run.

 

Problem

How to create a swap file in the btrfs filesystem?

 

Solution

I have an openSUSE server and use the btrfs filesystem as shown in the image below:

create a swap file in the btrfs
Check the filesystem

 

And my server does not have a swap space:

Check the swap

 

Therefore, so that my server can have a 2GB swap space, I have to create a swap file by running the commands below:

sudo truncate -s 0 /swapfile
sudo chattr +C /swapfile
sudo fallocate -l 2G /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
cat /proc/swaps
Process to create a swap file in btrfs filesystem

 

From the picture above, you can see that the server has a swap space. After that, you have to add the below script to /etc/fstab so that the swap space remains when you reboot the server by running the script below:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
create a swap file in the btrfs
Add the swap in the fstab file

 

Note

Swap files in btrfs are supported with the following limitations:

  • A swap file can’t be on a snapshotted subvolume. Instead, we recommend that you create a subvolume on which to place the swap file.
  • Btrfs doesn’t support swap files on file systems that span several devices.

 

References

btrfs.readthedocs.io
forum.endeavouros.com
docs.oracle.com
askubuntu.com

image_pdfimage_print
Visited 20 times, 1 visit(s) today
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *