Creating and Mounting a Btrfs File System

Use the mkfs.btrfs command to create a Btrfs file system that's laid out across one or more block devices.

Btrfs can be used on a single block device, or you can use the built-in RAID functionality to format the file system across more than one block device.

When formatting more than one block device, Btrfs defaults to mirror the file system 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), disk partitions, files, loopback devices, 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.

  1. Identify the target device, partition, or file that you want to format with Btrfs.

    Typically, you might use the lsblk command to list block devices and partitions that are available to the system.

    Note that formatting a file system is a destructive operation and erases any data on the target device. Ensure that the following steps use the correct target device or file paths.

  2. Use the mkfs.btrfs command to format the device or devices.
    sudo mkfs.btrfs [options] <device> [<device>...]

    To format a single block device, /dev/sdb, with Btrfs, you can run:

    sudo mkfs.btrfs /dev/sdb

    To format several block devices, in a RAID10 configuration, you can run:

    sudo mkfs.btrfs -d raid10 -m raid10 /dev/sdb /dev/sdc /dev/sdb /dev/sde

    Note that in the example, RAID10 requires a minimum of four block devices and stripes data across mirrored pairs. The appropriate number of devices required to achieve the specified RAID configuration must be used when formatting the file system.

    The mkfs.btrfs command automatically scans the new Btrfs devices into the kernel. To scan and assemble all 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 the relevant devices of the volume, you might need to run the btrfs device scan command.
  3. Mount the file system.

    For example, you can run:

    sudo mount /dev/sdb /mnt

    If you have formatted the file system across several devices, you can specify any of the component devices to perform the mount.

    Note that the default subvolume is mounted unless an alternative subvolume ID or path is specified. See Mounting Btrfs Subvolumes for more information.

  4. View the disk space information for the mounted Btrfs file system, by using the btrfs filesystem df command.
    For example, you can run:
    btrfs filesystem df /mnt

    The btrfs filesystem df command displays more exact information about the space that's used by a Btrfs file system than the df command.

    For more detailed information about the file system, use the btrfs filesystem usage command. For example:

    btrfs filesystem usage /mnt
    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