How to Create and Manage Btrfs File System in Linux

Btrfs or B-tree file system is a GPL-licensed copy-on-write (COW) was developed by multiple companies as follows Oracle, Redhat, Fujitsu, Intel, Facebook, Linux Foundation, Suse, etc. Brtfs will support maximum up-to 16 exbibyte and filesize can be maximum up-to 8 exbibyte, due to limitation of kernel.

Create Btrfs Filesystem in Linux
Create Btrfs Filesystem in Linux

Files can be created in any characters except “/” and NULL. Btrfs has self-healing features and have the capability of spanning multiple volumes. In Btrfs we can shrink, grow the file-system, add or remove block device in online mode.

It also provides subvolumes, Subvolumes are not a separate block devices, we can create snapshots and restore the snapshot for those subvolumes. Instead of using LVM we can use btrfs. Btrfs file-system are still under testing not yet included in production, If we have any important data, currently advised not to use btrfs in Production environments.

Btrfs released it’s 3.18 version by last month Dec 2014 with several new features.

Features of Btrfs

This new version of btrfs filled with many new features as follows:

  1. By default mkfs skinny-metadata feature is available from kernel 3.10.
  2. To repair the severely corrupted file-systems with care.
  3. Added convert option to show progress.
  4. Ability to link lost files to lost+found. This is a fix for a recent kernel Bug.
  5. To see the overview of file-system usage rather than df.
  6. And many more bug-fixes with and improved documentation.
  7. Subvolumes for file-system.
My Environment Setup
Hostname	:	btrfs.tecmintlocal.com
IP addrress 	:	192.168.0.120
Disk Size Used	:	8GB [/dev/sdb]
Btrfs Setup
Btrfs Setup

Step 1: Installing and Creating Btrfs Filesystem

1. In most of the today’s latest Linux distributions, btrfs package comes as pre-installed. If not, install btrfs package using the following command.

# yum install btrfs-progs -y		[On RedHat based Distro's]
# sudo apt-get install btrfs-tools -y	[On Debian based Distro's]

2. After btrfs package has been installed on the system, now we need to enable the Kernel module for btrfs using below command.

# modprobe btrfs

3. Here, we’ve used only one disk (i.e. /dev/sdb) in this disk, we’re going to setup logical volumes and create the btrfs file-system. Before creating them, let’s first verify the disk attached to the system.

# ls -l /dev | grep sd
Verify Disk
Verify Disk

4. Once you’ve confirmed that the disk is properly attached to the system, now it’s time to create partition for LVM. We will use ‘fdisk‘ command to create partitions on the /dev/sdb disk. Follow the instructions as explained below to create new partition on the drive.

# fdisk -c /dev/sdb
  1. Press ‘n‘ for creating new partition.
  2. Then choose ‘P‘ for Primary partition.
  3. Next choose the partition number as 1.
  4. Define the default value by just pressing two times Enter key.
  5. Next press ‘P‘ to print the defined partition.
  6. Press ‘L‘ to list all available types.
  7. Type ‘t‘ to choose the partitions.
  8. Choose ‘8e‘ for Linux LVM and press Enter to apply.
  9. Then again use ‘p‘ to print the changes what we have made.
  10. Use ‘w‘ to write the changes.
Create Partition for LVM
Create Partition for LVM

5. Once you’ve created partition successfully, you need to update the partition table changes to kernel for that let us run the partprobe command to add the disk information to kernel and after that list the partition as shown below.

# partprobe -s
# ls -l /dev | grep sd
Update Partition Table
Update Partition Table

Setup Logical Volume

6. Create Physical volume and volume group on /dev/sdb1 disk using pvcreate and vgcreate command.

# pvcreate /dev/sdb1
# vgcreate tecmint_vg /dev/sdb1

7. Create Logical volume in the volume group. Here I have created two logical volumes.

# lvcreate -L +2G -n tecmint_lv1 tecmint_vg
# lvcreate -L +2G -n tecmint_lv2 tecmint_vg

8. List the created Physical volume, Volume group and logical volumes.

# pvs && vgs && lvs
Create Logical Volumes
Create Logical Volumes

Creating Btrfs Filesystem

9. Let us create the file-system now for our logical volumes.

# mkfs.btrfs /dev/tecmint_vg/tecmint_lv1
Create Btrfs Filesystem
Create Btrfs Filesystem

10. Next, create a mount point and mount the file-system.

# mkdir /mnt/tecmint_btrfs1
# mount /dev/tecmint_vg/tecmint_lv1 /mnt/tecmint_btrfs1/
Mount Btrfs Filesystem
Mount Btrfs Filesystem

11. Verify the mount point with the help of df command.

# df -h

Here available size was 2 GB

Verify Btrfs Filesystem
Verify Btrfs Filesystem
If you read this far, tweet to the author to show them you care. Tweet a thanks
Babin Lonston
I'm Working as a System Administrator for last 10 year's with 4 years experience with Linux Distributions, fall in love with text based operating systems.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

7 thoughts on “How to Create and Manage Btrfs File System in Linux”

  1. It is very very sad this is the number one search result for creating a btrfs volume. While there might be a reason to use LVM with BTRFS, it is a very niche condition. 99% of users should not be using LVM if they are using btrfs, it is just adding complexity and another point of failure.

    Reply
  2. As LVM supports caching, you can set up your btrfs logical volumes with an SSD cache. This also open’s up all the LVM tools to you. So, for some, there might be perfectly legitimate reasons to use LVM.

    Reply
  3. Please revise this guide and remove the LVM parts. It’s so unnecessary!

    You wouldn’t put ZFS on LVM, now would you?

    BTFS is easy:

    Install btrfs-progs and make sure kernel module is loaded…

    mkfs.btrfs [-L <label of disk if wanted] /dev/
    mount /dev/ 
    tail -n1 /etc/mtab >> /etc/fstab
    
    Reply
  4. I seen lvm and thought What ?

    I made the mistake of using gpt partition then putting btrfs on that and ouch lost my data later.

    Now I make sure to wipe all the partitions from the drives first. Then I create my btrfs partitions.

    This seems to work best starting with blank drives.

    Reply
  5. As the 2 previous posters. No need for LVM with BTRFS and NEVER use “df“, it will give you the wrong information, use “btrfs fylesystem df” instead.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.