Setting Up and Administering a Btrfs File System
This section describes procedures to create a Btrfs file system as well as administering it for a more efficient use.
Creating a Btrfs File System
Note:
Note that the /sbin/mkfs.* tool is not
provided by the same package as the kernel modules that are
required by the btrfs command. Use the
modinfo btrfs command to check whether
the btrfs module is available in the kernel that is booted.
Also, if the btrfs-progs
package is not
installed on your system, you will need to install it.
You can use the mkfs.btrfs command to create a Btrfs file system that is laid out across one or more block devices. The default configuration is to mirror the filesystem metadata across the devices. If you specify a single device, the metadata is duplicated on that device, unless you specify to use only one copy of the metadata. The devices can be whole block device(s), simple disk partitions, files, loopback devices (that is, disk images in memory), multipath devices, or LUNs that implement RAID in hardware.
See the mkfs.btrfs(8)
manual page for more
detailed information about the mkfs.btrfs
command the various Btrfs configurations that you can create.
When you want to mount the file system, you can specify it by any of its component devices, for example:
sudo mkfs.btrfs -d raid10 -m raid10 /dev/sd[fghijk]
sudo mount /dev/sdf mountpoint
The mkfs.btrfs command automatically scans the new Btrfs devices into the kernel. To scan and assemble all of the relevant devices of the volume in the kernel you need to run the btrfs device scan command. You can undo this action by using the btrfs device scan --forget command. To assemble all of the relevant devices of the volume, you might need to run the btrfs device scan command.
You can obtain the RAID configuration for a mounted
btrfs
file system as follows:
sudo btrfs filesystem df mountpoint
Note that the btrfs filesystem df command displays more accurate information about the space that is used by a Btrfs file system than by using the df command.
You can also use the btrfs filesystem usage
command to display information about all of the
btrfs
file systems that are on a system, for
example:
sudo btrfs filesystem usage /btrfs
Overall: Device size: 1.95TiB Device allocated: 5.03GiB Device unallocated: 1.95TiB Device missing: 0.00B Used: 256.00KiB Free (estimated): 999.48GiB (min: 999.48GiB) Data ratio: 2.00 Metadata ratio: 2.00 Global reserve: 3.25MiB (used: 0.00B) Data,RAID10: Size:2.00GiB, Used:0.00B (0.00%) /dev/nvme0n1p6 1.00GiB /dev/nvme0n1p7 1.00GiB /dev/nvme0n1p8 1.00GiB /dev/nvme0n1p9 1.00GiB Metadata,RAID10: Size:512.00MiB, Used:112.00KiB (0.02%) /dev/nvme0n1p6 256.00MiB /dev/nvme0n1p7 256.00MiB /dev/nvme0n1p8 256.00MiB /dev/nvme0n1p9 256.00MiB System,RAID10: Size:16.00MiB, Used:16.00KiB (0.10%) /dev/nvme0n1p6 8.00MiB /dev/nvme0n1p7 8.00MiB /dev/nvme0n1p8 8.00MiB /dev/nvme0n1p9 8.00MiB Unallocated: /dev/nvme0n1p6 498.74GiB /dev/nvme0n1p7 498.74GiB /dev/nvme0n1p8 498.74GiB /dev/nvme0n1p9 498.74GiB
Modifying a Btrfs File System
You can use the btrfs command to add or remove devices and rebalance the layout of the file system data and metadata across devices. The following table describes each of the commands that can use to perform these tasks.
Command | Description |
---|---|
btrfs device add device mountpoint |
Add a device to the file system that is mounted on the specified mount point, for example: btrfs device add /dev/sdd /myfs |
btrfs device delete device mountpoint |
Remove a device from a mounted file system, for example: btrfs device delete /dev/sde /myfs |
btrfs device delete missing mountpoint |
Remove a failed device from the file system that is mounted in degraded mode, for example: btrfs device remove missing /myfs To mount a file system in degraded mode, specify the -o degraded option to the mount command. For a RAID configuration, if the number of devices would fall below the minimum number that are required, you must add the replacement device before removing the failed device. |
btrfs filesystem balance mountpoint |
After adding or removing devices, redistribute the file system data and metadata across the available devices. |
Compressing and Defragmenting a Btrfs File System
You can compress a Btrfs file system to increase its effective capacity, as well as defragment it to increase I/O performance.
The following three compression types are supported:
-
zlib
-
lzo
-
zstd
There are three ways in which you can enable compression:
-
By using the subvolume property, for example:
sudo btrfs subvolume create /btrfs/sv1
sudo btrfs property set /btrfs/sv1 compression zstd
-
By using the mount option in one of the following ways:
Use the
lzo
type to compress the file data on the whole file system:sudo mount -o compress=lzo /dev/sdb /btrfs
Use the
zstd
type to compress a subvolume and then mount it at the/btrfs1
mount point:sudo mount -o compress=zstd,subvolume=sv1 /devsdb /btrfs1
-
By using defragment, for example:
sudo btrfs filesystem defragment -czlib /btrfs1/akzo
You can enable compression at any point and only the new writes are compressed or defragmentation is run.
You can compress a Btrfs file system at the same time that you defragment it.
To defragment a Btrfs file system, use the following command:
sudo btrfs filesystem defragment filesystem_name
To defragment a Btrfs file system and compress it at the same time, use the following command:
sudo btrfs filesystem defragment -c filesystem_name
You can use the following command to defragment and optionally compress individual file system objects such as directories and files within a Btrfs file system:
sudo btrfs filesystem defragment [-c] file_name ...
To set up automatic defragmentation, specify the autodefrag option when you mount the file system. However, note that automatic defragmentation is not recommended for large databases or for images of virtual machines.
Note:
Defragmenting a file or a subvolume with a copy-on-write copy breaks the link between the file and its copy. For example, if you defragment a subvolume that has a snapshot, the disk usage by the subvolume and its snapshot will increase because the snapshot is no longer a copy-on-write image of the subvolume.
Resizing a Btrfs File System
You can use the btrfs command to increase the size of a mounted Btrfs file system as long as there is space on the underlying devices to accommodate the change. You also use the btrfs command to decrease its size, if the file system has sufficient available free space. Note that running the command does not have any effect on the layout or size of the underlying devices.
You would increase the size of /mybtrfs1
by 2
GB as follows:
sudo btrfs filesystem resize +2g /mybtrfs1
The following example shows how to set the size of
/mybtrfs3
to 20 GB:
sudo btrfs filesystem resize 20g /mybtrfs3
The following command decreases the size of
/mybtrfs2
by 4 GB:
sudo btrfs filesystem resize -4g /mybtrfs2