Get Started With the Btrfs File System on Oracle Linux
Introduction
The Btrfs file system provides the capacity to handle pooling, snapshots, checksums, and multi-device spanning in existing Linux file systems. For an overview of the Btrfs file system and its features, see Oracle Linux: Managing Local File Systems .
This tutorial describes setting up a directory on external storage on your Oracle Linux system to use the Btrfs file system.
Objectives
In this tutorial, you will learn how to:
- Create a Btrfs file system
- Run basic Btrfs commands to perform tasks such as:
- Checking file system usage
- Extend the size of the file system
- Recover a missing device
- Test and see how the file system works
Prerequisites
Minimum of a single Oracle Linux system running the UEK kernel
Each system should have Oracle Linux installed and configured with:
- A non-root user account with sudo access
- Access to the Internet
Deploy Oracle Linux
Note: If running in your own tenancy, read the linux-virt-labs GitHub project README.md and complete the prerequisites before deploying the lab environment.
Open a terminal on the Luna Desktop.
Clone the
linux-virt-labsGitHub project.git clone https://github.com/oracle-devrel/linux-virt-labs.gitChange into the working directory.
cd linux-virt-labs/olInstall the required collections.
ansible-galaxy collection install -r requirements.ymlDeploy the lab environment.
ansible-playbook create_instance.yml -e localhost_python_interpreter="/usr/bin/python3.6" -e add_block_storage=true -e block_count=4The free lab environment requires the extra variable
local_python_interpreter, which setsansible_python_interpreterfor plays running on localhost. This variable is needed because the environment installs the RPM package for the Oracle Cloud Infrastructure SDK for Python, located under the python3.6 modules.The default deployment shape uses the AMD CPU and Oracle Linux 8. To use an Intel CPU or Oracle Linux 9, add
-e instance_shape="VM.Standard3.Flex"or-e os_version="9"to the deployment command.Important: Wait for the playbook to run successfully and reach the pause task. At this stage of the playbook, the installation of Oracle Linux is complete, and the instances are ready. Take note of the previous play, which prints the public and private IP addresses of the nodes it deploys and any other deployment information needed while running the lab.
Check the Installation of the Btrfs Packages
Open a terminal and connect via SSH to the ol-node-01 instance.
ssh oracle@<ip_address_of_instance>Check whether the system contains the
btrfs-progspackage.dnf list installed | grep btrfs-progsIf the package is not in the system, install it and its dependent packages.
sudo dnf install -y btrfs-progs
Create the Btrfs File System
Check the devices that you would use for the file system.
lsblkYour lab instance includes the following additional block volumes:
- /dev/sdb
- /dev/sdc
- /dev/sdd
- /dev/sde
Run the following command to create the Btrfs file system.
sudo mkfs.btrfs /dev/sdb /dev/sdcIn the output, the
Devicessection lists the two devices used to create the file system. By default, the command configures the file system with Raid 0 to stripe the data and Raid 1 to mirror the metadata.Create a directory on which to mount the file system.
sudo mkdir /dataMount the file system.
sudo mount /dev/sdb /dataVerify the file system configuration.
sudo btrfs filesystem usage /dataAlternatively, you can also type:
sudo btrfs filesystem df /dataMake the file system mount automatically on reboot.
Get the Btrfs UUID.
sudo btrfs filesystem showNote that
/dev/sdband/dev/sdchave identical UIDs. You can further verify their identical UIDs with either of the following commands:lsblk -o NAME,UUID,MOUNTPOINTsudo blkid -s UUID -o value <device-name>wherecan be either /dev/sdbor/dev/sdc.
Add the following line to the
/etc/fstabfile:<UUID> /data btrfs defaults 0 0Note: You can add the line by using the
teecommand as follows:echo "UUID=$(sudo blkid -s UUID -o value /dev/sdc) /data btrfs defaults 0 0" | sudo tee -a /etc/fstab
Test the New File System
Create a file in
/data.sudo fallocate -l 5G /data/test1.imgCheck the file system usage due to the new file.
sudo btrfs filesystem usage /dataPart of the output indicates that the file in
/datais now striped across the two devices:Data,single: Size:5.01GiB, Used:5.00GiB (99.87%)/dev/sdb 2.01Gib/dev/sdc 3.00Gib
Extend the Btrfs File System
Add the
/dev/sdddevice to the file system.sudo btrfs device add /dev/sdd /dataView the new file system configuration.
sudo btrfs filesystem usage /dataThe
Unallocatedsection of the output includes the new device you just added.Unallocated:/dev/sdb 46.98GiB/dev/sdc 45.99GiB/dev/sdd 50.00GiBReconfigure
/datato use the new file system configuration.While the file system has expanded,
/datais still distributed based on the previous configuration. This step adjusts the directory to the new configuration.sudo btrfs balance start --bg /dataThe
--bgoption causes the process to run in the background.To monitor the progress of the operation, issue one of the following commands:
Note: The balance operation might take about 7 minutes to complete. Only proceed to the next step once monitoring indicates the completion of the process.
To check the progress at a point in time in the process, type:
sudo btrfs balance status /dataThe output specifies the percentage of completion. If the operation completes, then the command output shows
No balance found on /data.To run continuous monitoring, type:
sudo watch -t -n5 btrfs balance status /dataThe screen periodically displays the percentage of completion of the process, for example,
2 out of about 8 chunks balanced (3 considered), 75% left.At the end of the process, the output reports
No balance found on /data.Press Ctrl+C to exit real-time monitoring.
Check the results of the balance process.
sudo btrfs filesystem usage /dataPart of the output indicates that
/datais now striped across the three devices:Data,single: Size:11.00GiB, Used:10.00GiB (90.91%)/dev/sdb 2.00Gib/dev/sdc 1.00Gib/dev/sdd 3.00Gib
Recover the File System After a Device Loss
To complete the exercise in this section, a scenario in which a device is lost due to a hardware failure is created.
Simulate a Failure
Detach the
/dev/sdddevice with the following command:echo 1 | sudo tee /sys/block/sdd/device/deleteCheck the status of the file system.
sudo btrfs filesystem showThe output reports some missing devices.
Determine which device is missing.
sudo btrfs filesystem usage /dataBy comparing the command output with the previous
filesystem showcommand, you can identify/dev/sddas the missing device.
Perform a Recovery Operation
Remount the file system in degraded mode.
sudo mount -o remount,rw,degraded /dataReplace the missing device.
sudo btrfs replace start -r 3 /dev/sde /dataYou specify
-r 3based on the original configuration of 3 devices.Check the status of the replacement process.
sudo btrfs replace status /dataThe command periodically displays the percentage of process completion, such as
2.0 done, 0 write errs, 0 uncorr. read errs.The process takes about 5 minutes to complete. At its completion, a notification similar to the following displays:
Started on 30.Sep 16:32:17, finished on 30.Sep 16:40:06, 0 write errs, 0 uncorr. read errsCheck the file system status.
sudo btrfs filesystem showThe list of devices now includes
/dev/sde.Reconfigure
/datato use the new file system configuration.sudo btrfs balance start --bg /dataTo monitor the progress of the balancing operation, issue one of the following commands:
Note: The balance operation might take about 7 minutes to complete. Only proceed to the next step once monitoring indicates the completion of the process.
To check the progress at a point in time in the process, type:
sudo btrfs balance status /dataThe output specifies the percentage of completion. If the operation completes, then the command output shows
No balance found on /data.To run continuous monitoring, type:
sudo watch -t -n5 btrfs balance status /dataThe screen periodically displays the percentage of completion of the process, for example,
2 out of about 8 chunks balanced (3 considered), 75% left.At the end of the process, the output reports
No balance found on /data.Press Ctrl+C to exit real-time monitoring.
Check the results of the balance process.
sudo btrfs filesystem usage /dataThe output shows that
/datais now striped across/dev/sdb,/dev/sdc, and/dev/sde.
Next Steps
That completes our introduction to using Btrfs and the capabilities of using and extending the filesystem. Check out our other Btrfs content available on the Oracle Linux Training Station.