Btrfs – creating, resizing, and monitoring – Creating and Managing Single-Instance Filesystems

Share this post on:

Btrfs – creating, resizing, and monitoring

In this recipe, we will create a new RAIDed Btrfs volume and filesystem, using multiple disks for fault-tolerant storage. We will then add a new LUN, growing the filesystem. We will wrap up by modifying the filesystem to compress the data!

Getting ready

To get started, I added five 10 GB drives to the OS. These will be used to build a new RAID1C4 volume. I can see these new devices by using the fdisk -l command, grepping for GiB using the following command:


fdisk -l | grep GiB

The output is seen in the following figure:

Figure 4.5 – fdisk output

Here, we can see that the 10 GB devices are sdb, sdc, sbd, sbe, and sbf. We will need this info to make the Btrfs volume.

How to do it…

Now that we know the devices, let’s manually create a RAID1C3 volume. We will use all five devices in a RAID1C3 configuration and name the volume data.

We will then use the following command to make the volume:


mkfs.btrfs -L data -d raid1c3 -m raid1c3 /dev/sd[bcdef]

Please refer to the following figure to view the output:

Figure 4.6 – mkfs.btrfs output

Note

When mounting a Btrfs volume, you normally use the first device in the volume or the UUID. The UUID is reported by mkfs.btrfs when the volume is created.

Next, let’s mount this in /data. Make the /data directory, then mount it with the following commands:


mkdir /data
mount /dev/sdb /data

Optional, though highly recommended, is to add this to the fstab file. With this example, we are using the UUID of the volume, and since data has no subvolume, the subvol parameter is defined but left blank:

Figure 4.7 – Sample fstab using UUID

How it works…

Now that we have a mounted volume, let’s do a few things with it! First, we can use the btrfs command to check several things. The first is to check the device’s health, which is useful to see whether the RAID has any failing devices. The btrfs stats /$DEVICE command is used to show the status. Don’t forget to replace $DEVICE with the actual Btrfs device you are checking:

Figure 4.8 – Healthy devices

When a device starts to fail, you should start to see errors in this report.

Next up, we will add a few more devices to the volume. Four more 10 GB disks were added: sdg, sdh, sdi, and sdj.

Before we add the device, we can see that /data has 50 GB of usable raw space. This is seen using the following command:


btrfs filesystem usage /data

The output from the command is shown in the following screenshot:

Figure 4.9 – Btrfs filesystem usage

Here we can see the stats, mainly the 50 GB of free raw space, as well as the other metrics, including the space allocated to each device in the volume and which devices have the metadata.

Next, let’s add the four new devices. This is done with the btrfs device add command:


btrfs device add /dev/sdg /data

Do this for each device being added to the volume, or bulk add them with /dev/sd[a-z], replacing a and z with the appropriate range. When done, you can check using the usage option, as seen in the following sample:

Figure 4.10 – Btrfs devices added

You will now see the device at 90 GB. Now, as space in /data gets consumed, you should start to see the available space go down, as well as the distribution of data against the individual disks:

Figure 4.11 – Btrfs space used

For the last example, we will be removing a device to free up space, and then rebalancing the data. To delete a physical device, use the btrfs device delete command, passing the device mountpoint:


btrfs device delete /dev/sdj /data

This will remove the device:

Figure 4.12 – Device removal

Once the device is removed, rerun the usage report. What you will now see is the remaining devices, and which data is on which device:

Figure 4.13 – Unbalanced usage

While it may appear to be a minor issue, it has the potential to cause complications down the line. Fortunately, the solution is simple – a system rebalance. This is done using the balance option in the btrfs command. The following command will be used to balance the /data filesystem:


btrfs filesystem balance /data

This command will then rebalance the data chunks, and when done, the usage will show the data balanced across the disks:

Figure 4.14 – Balanced usage

Note

To ensure a well-balanced distribution of data, it is recommended to always balance your system when adding or removing devices, even though there may be slight variations. This practice is essential for maintaining an optimally performing filesystem. The btrfsmaintenance package in the ol8_developer repo is a great tool for automating all the required Btrfs maintenance tasks.

Share this post on:

Leave a Reply

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