Open In App

How to Create Btrfs Filesystem in Linux and its Features

Last Updated : 01 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Btrfs was a project which was started back in 2007, it is a part of the Linux kernel. It is a copy-on-write filesystem that uses advanced features. It is highly scalable, and easy to maintain and repair. Since 2013 this file system is considered stable in the Linux kernel. The core and the most vital structure of btrfs was proposed by a researcher at IBM. Btrfs essentially focuses on catering to the needs which require high performance and large storage. It has a variety of features which are given below.

Features of Btrfs 

Some of the features of Btrfs are:

  • Snapshots
  • Self-healing
  • Dynamic inode allocation
  • Extent-based file storage
  • Deduplication
  • Provides online defragmentation, volume growth, and shrinking.
  • Supports file cloning.
  • It has the ability to handle swap files and swap partitions.

Note: Btrfs stands for B-tree file system

Create a btrfs filesystem in Linux

Let us see the steps to create a btrfs filesystem in Linux:

We will be installing btrfs on our system right from the beginning of the installation process, so after the system has booted up click on try ubuntu. Before we start the installation, we need to partition the disk manually, make some changes in the installer, and then we are good to go.

 

Let us type lsblk to see the existing disks:

lsblk

We have a virtual disk sda with 20GB storage. 

Step 1: Creating partitions in the disk:

Now we will be creating partitions, to do this gdisk will be used. First switch to the root user using the below command: 

sudo -i (without being in sudo mode gdisk will not work)

To start gdisk use the below command:

gdisk /dev/sda

 

Enter n to create a new partition, then enter the size of the partition you created. The name of the partition that we will create first will be EFI, and its size will be 500MB.

 

Note: The hex code for EFI is “ef00”.

Enter n to create a second partition, this time we will create a Linux swap partition of size 2 Gb.

 

The hex code is “8200” for this partition.

Now we will create a third and final partition, which is the root partition. All the settings will remain as defaults in this partition. We will not be creating a home partition because the btrfs filesystem already has a home subvolume.

 

Step 2: Save the Changes and confirm the partitions:

After creating the partitions, press w to write the changes and y to confirm those changes.

 

To see the created partitions type lsblk:

 

Step 3: Format the sda1 file to mount the partition:

The only thing that we have to do now is to format the sda1 with a FAT file system, without this we would not be able to mount this partition on the installer. To format use the below command:

mkfs.fat -F32 /dev/sda1

Step 4: Do the required changes in the below files:

Now we have to edit two files before proceeding to the installer so that it sets the correct mount options for the filesystem.

The files are:

1. /usr/lib/partman/mount.d/70btrfs

2. /usr/lib/partman/fstab.d/btrfs

 

In the above file a few changes have to be made in the options section, add this line:

noatime,space_cache, compress=zstd,discard=async

options = “${options:+$options,}subvol=@,noatime,space_cache, compress=zstd,discard=async

 

 

Save this file and exit. Now edit the second file:

 

In this file also add,noatime,space_cache, compress=zstd, and discard=async to the home_options and subvolume sections.

 

 

Note: Change the value of pass everywhere to 0.

Now locate the echo section in this file and edit that section too.

 

Step 5: Start the ubiquity installer and complete the installation steps.

Now start the ubiquity installer and complete the installation steps:

 

After some steps, select something else as we have manually created the partitions.

 

 

You can see the partitions that we made, click on sda2, and select it as a swap area. After that select sda3 select btrfs partition and mount position as root.

 

 

After these settings reboot the system, you have successfully installed the btrfs filesystem on your system.

Step 6: After the reboot, confirm the installation:

sudo btrfs subvolume list /

 

Confirm the partitions that were created:

cat  /etc/fstab

 

Some useful options in BTRFS:

Just like the other filesystems out there, Btrfs also has a lot of mount options. The most important mount options in btrfs are listed below:

  • ACL and NOACL
  • Compress and compress-force
  • subvol and subvoloid
  • autodefrag and noautodefrag
  • device 
  • commit
  • degraded
  • ssd and nossd
  • discard and nodiscard
  • datacow and no datacow

Creating snapshots on Btrfs:

For the purpose of creating snapshots, we will be using Timeshift:

sudo apt install timeshift    

 

Now that Timeshift is installed, open it from the applications, and an authorization window will appear, enter your password.

 

Select the snapshot type, the disk on which the snapshot is to be taken, and some other settings.

 

 

After performing the above settings click on finish and the timeshift is ready to use. Click on create, and a snapshot will be created.

 

Creating Btrfs Subvolumes:

Let us first see the list of subvolumes available on the system, use the below command:

sudo btrfs subvolume list

 

To create a new subvolume type the below command, we will create a sample subvolume named /opt/sample.

sudo btrfs subvolume create /opt/sample

 

To see whether the subvolume has been created or not, type the same command as we did earlier;

sudo btrfs subvolume list

The subvolume that we just created can be seen.

To delete the subvolume use:

sudo btrfs subvolume delete /opt/sample

 

Conclusion

So in this article, we learned about the BTRFS file system, its features, and its installation. Along with that we also used some of its features such as snapshotting and subvolumes which make a very reliable alternative to traditional filesystems.  Hope you liked the article and gained good knowledge about this filesystem.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads