LinuxCommandLibrary

mkfs.btrfs

Create a Btrfs filesystem

TLDR

Create a btrfs filesystem on a single device

$ sudo mkfs.btrfs [[-m|--metadata]] single [[-d|--data]] single [/dev/sdX]
copy

Create a btrfs filesystem on multiple devices with raid1
$ sudo mkfs.btrfs [[-m|--metadata]] raid1 [[-d|--data]] raid1 [/dev/sdX /dev/sdY /dev/sdZ ...]
copy

Set a label for the filesystem
$ sudo mkfs.btrfs [[-L|--label]] "[label]" [/dev/sdX /dev/sdY ...]
copy

SYNOPSIS

mkfs.btrfs [options] [devices...]

PARAMETERS

-f
    Force overwrite if a filesystem is detected on the device.

-L
    Specify a filesystem label.

-d
    Specify the data RAID profile (raid0, raid1, raid5, raid6, raid10, single, dup). Default is single for single-device, raid1 for multiple devices.

-m
    Specify the metadata RAID profile (raid0, raid1, raid5, raid6, raid10, single, dup). Default is single for single-device, raid1 for multiple devices.

-n
    Specify the nodesize (in bytes). Must be a power of 2, page size or larger (minimum 4096).

-s
    Specify the sectorsize (in bytes). Must be a power of 2, page size or larger (minimum 4096).

-K
    Do not perform discard/TRIM operation.

[devices...]
    The device or devices to create the filesystem on.

DESCRIPTION

The mkfs.btrfs command is used to create a Btrfs filesystem on a specified device or devices.

Btrfs (B-tree file system) is a modern copy-on-write (CoW) filesystem for Linux aimed at implementing advanced features while focusing on fault tolerance, repair and easy administration. It supports features like snapshots, checksumming, compression, subvolumes, and online resizing.

mkfs.btrfs initializes the metadata structures on the specified block devices to create an empty Btrfs filesystem.
It can create single-device filesystems as well as RAID configurations spanning multiple devices, allowing for data redundancy and improved performance. The command can be used to format whole disks, partitions, or loopback images. Careful consideration should be given to RAID levels selected, as the right raid level will ensure that data is properly stored and mirrored.

CAVEATS

Creating a Btrfs filesystem will erase all existing data on the specified device(s). It's crucial to back up important data before running mkfs.btrfs. Choosing incorrect RAID levels can lead to data loss if devices fail.

RAID PROFILES

The -d and -m options are used to specify the RAID profile for data and metadata respectively. Common profiles include:
single: No redundancy. Data loss occurs if the device fails.
raid0: Striping. Improves performance but offers no redundancy. Data loss if one device fails.
raid1: Mirroring. Provides redundancy.
raid5/6: More advanced RAID levels requiring multiple disks.

SPACE ALLOCATION

Btrfs uses extent-based allocation, which means that files are stored in contiguous regions of the disk. This can lead to better performance than traditional block-based filesystems. It also supports online defragmentation to improve performance over time.

HISTORY

Btrfs development began in 2007 at Oracle. It was designed as a next-generation filesystem to address the limitations of ext4. The mkfs.btrfs command is the primary tool for initializing Btrfs filesystems. Over time, it has been refined with improvements to RAID support, performance optimizations, and feature enhancements.

SEE ALSO

Copied to clipboard