How to Create and Query a BTRFS File System

The Basics

The btrfs file system is designed to meet the expanding scalability requirements of large storage subsystems. As the btrfs file system uses B-trees in its implementation, its name derives from the name of those data structures, although it is not a true acronym. A B-tree is a tree-like data structure that enables file systems and databases to efficiently access and update large blocks of data no matter how large the tree grows.

The btrfs file system provides the following important features:

  • Copy-on-write functionality allows you to create both readable and writable snapshots, and to roll back a file system to a previous state, even after you have converted it from an ext3 or ext4 file system.
  • Checksum functionality ensures data integrity.
  • Transparent compression saves disk space.
  • Transparent defragmentation improves performance.
  • Integrated logical volume management allows you to implement RAID 0, RAID 1, or RAID 10 configurations, and to dynamically add and remove storage capacity.

Starting with CentOS/RHEL 6 Update 3, the boot ISO allows you to configure a btrfs root file system. Prior to CentOS/RHEL 6 Update 3, you could not create a btrfs root file system during installation.

With UEK R3, btrfs supports the following additional features:

  • The send/receive feature allows you to record the differences between two subvolumes, which can either be snapshots of the same subvolume or parent and child subvolumes.
  • Quota groups (qgroups) allow you to set different size limits for a volume and its subvolumes.
  • You can replace devices without unmounting or otherwise disrupting access to the file system.

This document is to explain how to create a BTRFS file system in a single device on CentOS/RHEL system. The following steps were performed on a virtual machine running CentOS/RHEL 7 with a virtual disk of 5GB size.

Create a BTRFS file system

Add the desired disk where btrfs filesystem is going to be configured and make sure the system recognizes the disk:

# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 20G 0 disk
├─xvda1 202:1 0 1G 0 part /boot
└─xvda2 202:2 0 19G 0 part
├─ol-root 251:0 0 17G 0 lvm /
└─ol-swap 251:1 0 2G 0 lvm [SWAP]
xvdb 202:16 0 5G 0 disk

In above output, our disk is going to be “xvdb”. The devices can be simple disk partitions, loopback devices (that is, disk images in memory), multipath devices, or LUNs that implement RAID in hardware.

1. Install btrfs-progs package if not already installed on your system:

# yum install btrfs-progs

2. Create the Btrfs file system using the mkfs.brtfs command:

# mkfs.btrfs /dev/xvdb
btrfs-progs v4.9.1
See http://btrfs.wiki.kernel.org for more information.

Detected a SSD, turning off metadata duplication. Mkfs with -m dup if you want to force metadata duplication.
Label: (null)
UUID: c7d1687d-fe91-4837-b14a-4870466d1a3f
Node size: 16384
Sector size: 4096
Filesystem size: 5.00GiB
Block group profiles:
Data: single 8.00MiB
Metadata: single 8.00MiB
System: single 4.00MiB
SSD detected: yes
Incompat features: extref
Number of devices: 1
Devices:
ID SIZE PATH
1 5.00GiB /dev/xvdb

3. Use the btrfs filesystem show command to verify that the file system was created on the device:

# btrfs filesystem show
Label: none uuid: c7d1687d-fe91-4837-b14a-4870466d1a3f
Total devices 1 FS bytes used 112.00KiB
devid 1 size 5.00GiB used 20.00MiB path /dev/xvdb

Above command displays all btrfs file systems in the system

or

# btrfs filesystem show /dev/xvdb
Label: none uuid: c7d1687d-fe91-4837-b14a-4870466d1a3f
Total devices 1 FS bytes used 112.00KiB
devid 1 size 5.00GiB used 20.00MiB path /dev/xvdb

Above command displays information about the btrfs file system only on the specified device needed (/dev/xvdb)

4. Mount the corresponding file system:

# mount /dev/xvdb /btrfsTest

5. Make sure that the btrfs file system is now mounted:

# mount | grep btrfs
/dev/xvdb on /btrfsTest type btrfs (rw,relatime,seclabel,ssd,space_cache)

Query a BTRFS file system

Run the btrfs filesystem df command to display more accurate information about the space used by a btrfs file system.

# btrfs filesystem df /btrfsTest/
Data, single: total=8.00MiB, used=64.00KiB
System, single: total=4.00MiB, used=16.00KiB
Metadata, single: total=264.00MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B

We can use regular df command to query the file system but the displayed information may not be accurate.

# df -h /btrfsTest/
Filesystem Size Used Avail Use% Mounted on
/dev/xvdb 5.0G 17M 4.8G 1% /btrfsTest

More information and options can be found at the manual page of mkfs.btrfs:

# man mkfs.btrfs
Related Post