The btrfs command manages and displays information about the Btrfs file system. The Btrfs always takes a subcommand followed by other command arguments. Without any subcommands, it prints out the usage followed by all the subcommands as shown below.
Let’s go a bit deeper and explore some of the utilities that Btrfs provides to manage its filesystem.
Creating a Btrfs filesystem
We need to create a btrfs filesystem to leverage the btrfs commands and utilities from the onset. In this example, I will create a btrfs filesystem from my removable USB medium on the /dev/sdb1 filesystem, which is mounted on the /media/winnie/DATA mount point.
The lsblk command lists all the block devices on your system and is a nifty tool for listing every block device attached to your system.
To start off, we will unmount the file system using the umount command.
Once unmounted, we will format the volume to the btrfs filesystem using the mkfs command as shown.
It would be best if you got an output that is similar to the one we have below. The command prints to standard out information such as the File system Label, UUID, node size, sector size, filesystem size, among other details.
After that, we will proceed and create a new mount point in which we will mount the Btrfs device.
Then we will mount the btrfs filesystem to the mount point.
To verify that the filesystem has been properly mounted, invoke the df command shown.
Creating a subvolume
A subvolume in Btrfs filesystem is a subset of the filesystem that bears its own entirely independent directory structure. You can create multiple subvolumes in a Btrfs filesystem using the create argument.
We will create sales, marketing & IT subvolumes in the newly created /Reports btrfs filesystem.
For the sales subvolume:
For the marketing subvolume:
For the IT subvolume:
Listing the subvolumes
To list the subvolumes in the Btrfs filesystem, use the list argument as shown.
This displays the subvolumes created, which are basically directories within the larger Btrfs filesystem. You can view the directory hierarchy using the good old tree command shown:
Creating a snapshot of the subvolumes
Additionally, you can use the snapshot argument to create a read and write a snapshot of your subvolume as follows. Here, we are creating a read & write snapshot called marketing-snap of the marketing subvolume.
Additionally, you can create a read-only snapshot using the -r flag as shown. The marketing-rosnap is a read-only snapshot of the marketing subvolume
Check disk space usage using the ‘df’ utility
In Btrfs filesystems, checking disk space utilization using the df command can be misleading, especially when a filesystem is mounted and files copied into it.
To attain more accurate information or output, use the df command as demonstrated.
Display filesystem structure using the ‘show’ utility
The show option allows you to probe the file structure of the filesystem or subvolume.
For instance, to display the file structure of the /Reports Btrfs filesystem, run the command:
To check the file structure of the marketing subvolume, execute:
Force filesystem sync using the ‘sync’ utility
To force filesystem sync, invoke the sync option as shown. Take note that the filesystem should already be mounted for the sync process to proceed successfully.
Manage devices using the ‘device’ utility
You can add another device on the mounted filesystem as follows. Here the /dev/sdc is another removable device that we are adding on the /Reports mounted filesystem.
After adding the device, use the balance command to balance groups of blocks or chunks in a btrfs filesystem.
To delete the device from the filesystem, use the device delete command as shown.
Scrubbing of the filesystem using the ‘scrub’ directive
Scrubbing is an exercise that attempts to repair or correct damaged blocks on the btrfs file system by verifying checksums and metadata. The scrub tool runs quietly in the background without impacting other operations.
To launch scrub on all the devices located on the file system, use the scrub start command as shown.
To probe the status of a scrub, use the scrub status command with the -dR option.
This will provide a very detailed report, including the time and date of the start of the scrub, duration, and any errors encountered (if any).
To cancel scrub execution, use the scrub cancel command.
If the scrub executed successfully without any issues and exited, you will get an error indicating that the cancellation failed since the scrub operating is not running.
To resume or carry on with a previously interrupted scrub, run the scrub cancel command.
Again, If the scrub command is executed successfully without interruptions, you will get the notification that there is nothing to resume.
That was a general overview of some of the btrfs commands and utilities that can be used to manage and probe the status of a btrfs filesystem. We trust you have a firm grasp on managing the btrfs filesystem using the myriad of command options and utilities it offers.