What Is Btrfs? A Plain Explanation
In this article
Btrfs (usually pronounced "butter FS" or "bee-tree-ef-ess") is a copy-on-write file system for Linux that offers snapshots, checksums, compression, and built-in RAID-like features. Unlike older file systems such as ext4, Btrfs is designed to handle large storage pools and to protect data integrity. It is not a word or slang term; it is a technical acronym that stands for "B-Tree File System." The name comes from the B-tree data structure it uses to organize files and metadata. You might use it in a sentence like: "I set up Btrfs on my home server so I can take snapshots before updates."
Background and Origin
Btrfs was started in 2007 by Chris Mason, who was then working at Oracle. It was created to address limitations in existing Linux file systems, particularly the lack of pooled storage, snapshots, and checksumming. The project was inspired by Sun's ZFS, which had many of these features but could not be integrated into Linux due to licensing. Btrfs was designed to be a modern, scalable file system that could eventually replace ext4. It has been part of the Linux kernel since 2009, but it has taken years to reach a stable, production-ready state. Today, it is the default file system on some Linux distributions, such as openSUSE and Fedora (for certain installations), though many others still default to ext4 or XFS.
How Btrfs Works
Btrfs uses a copy-on-write (CoW) mechanism. When a file is modified, the new data is written to a new location on disk, and the metadata is updated to point to the new version. The old version is kept until it is no longer needed. This allows snapshots to be created instantly: a snapshot is just a read-only or writable copy of the metadata that references the same data blocks. Because of CoW, snapshots do not duplicate data; they only store the changes made after the snapshot was taken.
Btrfs also checksums data and metadata using CRC-32C. When data is read, the checksum is verified, and if a mismatch is found, Btrfs can attempt to repair it if redundancy is available (e.g., with RAID 1 or RAID 5/6). This is a key advantage over ext4, which does not verify data integrity.
Key Features and Use Cases
Btrfs offers several features that are useful for both home users and enterprise environments:
- Snapshots: Instant, low-cost snapshots of subvolumes. You can roll back to a previous state if something goes wrong.
- Subvolumes: Separate, independently mountable parts of the file system. They are often used for system root, home directories, and Docker storage.
- Compression: Transparent compression (zstd, lzo, zlib) can save disk space and reduce I/O, especially for text files and logs.
- RAID support: Btrfs can manage multiple disks with RAID 0, 1, 10, 5, and 6, though RAID 5/6 are considered unstable for production use.
- Self-healing: With redundancy, corrupted blocks can be automatically repaired when detected.
- Send/receive: Efficiently send incremental snapshots to another Btrfs file system, useful for backups.
Common use cases include home NAS servers, desktop Linux installations (especially with Timeshift or Snapper for system snapshots), and Docker storage (via the btrfs storage driver).
How to Choose Between Btrfs and Other File Systems
The choice between Btrfs and alternatives like ext4, XFS, or ZFS depends on your needs. Here are some guidelines:
- If you want snapshots and data checksums on a single disk, Btrfs is a good choice.
- If you need a battle-tested, simple file system for a basic Linux install, ext4 is still the safest bet.
- If you have a large storage pool with multiple disks and want advanced features like compression and RAIDZ, ZFS (via OpenZFS) is more mature, but it is not in the mainline kernel and requires extra setup.
- If you need high performance for large files (e.g., media servers), XFS is often recommended.
For most home users, Btrfs is a reasonable choice if you understand its quirks and keep backups. For mission-critical servers, many administrators still prefer ext4 or XFS due to Btrfs's history of bugs and slower development.
Common Problems and Misconceptions
One common misconception is that Btrfs is unreliable or "not production-ready." While it had serious bugs in the past (e.g., data loss with RAID 5/6), the core functionality is now stable for single-disk and RAID 1/10 setups. However, RAID 5/6 are still not recommended for production because of known issues with write holes and recovery.
Another issue is the "ENOSPC" error (No space left on device) even when there is free space. This can happen because Btrfs reserves space for metadata and CoW operations. It is often caused by running out of metadata space, not data space. You can mitigate this by leaving some free space (e.g., 10-20%) on the file system.
Also, snapshots are not backups. If the disk fails, all snapshots are lost. Snapshots only protect against accidental file deletion or modification, not hardware failures.
How to Get Started with Btrfs
If you want to try Btrfs, here are the basic steps for creating and using a Btrfs file system on a Linux system:
- Install the
btrfs-progspackage (most distributions have it). - Format a partition or disk with
mkfs.btrfs /dev/sdX. - Mount it with
mount /dev/sdX /mnt. - Create a subvolume:
btrfs subvolume create /mnt/@home. - Take a snapshot:
btrfs subvolume snapshot /mnt/@home /mnt/@home-backup. - To roll back, you can replace the original subvolume with the snapshot.
Note that the exact commands may vary by distribution and version. Always check the man pages (man btrfs) for details.
Safety and Maintenance
Btrfs includes a balance and scrub operation to maintain health. A scrub reads all data and metadata, checking checksums and repairing any errors if possible. It is recommended to run a scrub periodically (e.g., monthly) on systems with redundancy. A balance rewrites data to better distribute it across the disk, which can free up space and improve performance. Both operations can be run while the file system is mounted.
Be careful when using Btrfs with RAID 5/6; they are not recommended for production. Also, do not use Btrfs on a disk that is also used by another operating system like Windows, because Windows cannot read Btrfs without third-party drivers. Always keep a separate backup of important data, as no file system is immune to hardware failure or user error.
Frequently Asked Questions
Is Btrfs safe for production use?
Yes, for single-disk and RAID 1/10 configurations, Btrfs is considered stable and is used by many Linux distributions as the default. RAID 5/6 are not recommended due to known issues. Always test on a non-critical system first.
Can I convert my ext4 file system to Btrfs?
Yes, there is a tool called btrfs-convert that can convert an ext4 file system to Btrfs in-place. However, the process is not reversible and can be risky. It is safer to back up your data, format the disk as Btrfs, and restore.
Does Btrfs work on Windows or macOS?
No, Btrfs is a Linux file system. Windows and macOS cannot natively read or write it. There are third-party drivers for Windows (e.g., WinBtrfs) but they are not officially supported and may be unstable.
How do I check if my Btrfs file system is healthy?
Run btrfs scrub start /mount/point to start a scrub, then btrfs scrub status /mount/point to see the progress and any errors. Also, check the system logs for Btrfs errors.
What is the difference between a snapshot and a backup?
A snapshot is a point-in-time copy of a file system that lives on the same disk. It protects against accidental changes but not against disk failure. A backup is a copy stored on a separate device or location, protecting against hardware failure and disasters.
Can I use Btrfs for the root file system on my Linux install?
Yes, many distributions support Btrfs as the root file system. It is especially useful with tools like Snapper to create automatic snapshots before system updates, allowing easy rollback if something breaks.