### BTRFS RAID-1 (data mirror) quickstart ###
I used to rely on mdadm for Linux RAID-1 (mirror) setups - until the day I lost
data because one of the disks in the RAID started burping corrupted bytes and
mdadm didn't realized there was an issue.
Nowadays I prefer to use the RAID facility embedded in BTRFS. It might not be
as fast as mdadm, but I found it to be more reliable, and also less troublesome
to setup, esp. in configurations where the RAID is used as boot device.
One thing to keep in mind: BTRFS is a filesystem, hence the RAID is not a RAID
of disks, but a RAID of filesystem containers (typically: partitions). While it
is possible to write a BTRFS filesystem directly on a block device, there is no
advantage to do it, only limitations.
Let's assume we have two partitions that we want to bind together in a mirror:
/dev/sda1 and /dev/sdb1, and the filesystem is mounted at /srv.
Here below are the commands you should know.
Wipe the filesystem of a device so it can be reformatted with btrfs:
# wipefs -a /dev/sda1
Creating the mirror RAID:
# mkfs.btrfs -m raid1 -d raid1 /dev/sda1 /dev/sdb1
Note: you may use more than two devices in a BTRFS RAID-1, and the devices do
not necessarily have to be of equal size. For instance you could have a
RAID-1 with 3 partitions of 100G, 100G and 200G. In such situation BTRFS
will provide you with a RAID-1 of 200G. This works because every block of
data is always copied to two devices, even if there is more disks.
Add a new device to an already existing RAID:
# btrfs device add /dev/sdb1 /srv
Remove a device from the RAID:
# btrfs device remove /dev/sda1 /srv
See the devices that are part of the RAID:
# btrfs fi show /srv
Convert an existing BTRFS filesystem to RAID-1 by adding a second device:
# btrfs device add /dev/sdb1 /srv
# btrfs balance start -dconvert=raid1 -mconvert=raid1 /srv
See the status of an ongoing RAID conversion:
# btrfs balance status /srv
Perform an extensive sanity check of the data (validates data checksums):
# btrfs scrub start /srv
See I/O error counters for devices in the RAID:
# btrfs device stats /srv
! IMPORTANT NOTE !
By default, Linux will fail to mount a BTRFS filesystem if any of the devices
is missing. This means that should one of your RAID disks fail, your system
will become unbootable.
To make Linux mount a BTRFS RAID-1 filesystem even if one of its devices is
missing, you have to pass the 'degraded' mount option. Here an fstab example:
UUID=588b054f-c57e-4d6e-936a-4e67e6d2a075 /srv btrfs defaults,degraded 0 0