Btrfs - Setup a RAID1 mirror

By xngo on February 3, 2020

RAID 1 is also called a mirror. It consists of an exact copy of 1 disk to another 1. The benefit of this setup is redundancy and the read speed might improve since it can fetch the data concurrently from 2 disks. If 1 disk failed, then you can still access the data from the other disk.

Note: For clarity and simplicity, I used the device name(e.g. /dev/sda) instead of the device ID(e.g. /dev/disk/by-id/ata-VBOX_HARDDISK_VB21ee09e7-8e7e6c43). In practice, you should use the device ID. Otherwise, any disk plugs in the 1st connector is labelled /dev/sda.

Create a RAID1 mirror

Create a RAID 1 mirror called MyMirrorDisks with devices: /dev/sdg and /dev/sdh.

mkfs.btrfs -L MyMirrorDisks -m raid1 -d raid1 /dev/sdg /dev/sdh
    # -L: Label
    # -m: Mirror metadata.
    # -d: Mirror data.

Verify that it is indeed set to RAID 1.

btrfs filesystem df /media/MyMirrorDisks/
    Data, RAID1: total=256.00MiB, used=192.00KiB
    Data, single: total=256.00MiB, used=0.00B
    System, RAID1: total=8.00MiB, used=16.00KiB
    System, single: total=32.00MiB, used=0.00B
    Metadata, RAID1: total=256.00MiB, used=176.00KiB
    Metadata, single: total=256.00MiB, used=0.00B
    GlobalReserve, single: total=3.25MiB, used=0.00B

Degraded mode

In case that 1 of the disks failed, then mount the remaining device in degraded mode.

Check which device is still alive.

btrfs filesystem show
    Label: 'MyMirrorDisks'  uuid: f656414a-3e5c-4ae7-8987-db0eefb6a295
        Total devices 2 FS bytes used 384.00KiB
        devid    2 size 2.50GiB used 1.04GiB path /dev/sdg
        *** Some devices missing

Mount in degraded mode the available device, /dev/sdg.

mkdir /tmp/kkk
mount -o degraded /dev/sdg /tmp/kkk/mountpoint/

Note: You still can read and write while on the degraded mode.

Replace faulty disk

Add new device, /dev/sdj.

btrfs device add /dev/sdj /tmp/kkk/mountpoint/

Check that the new device is added.

btrfs filesystem show
    Label: 'MyMirrorDisks'  uuid: f656414a-3e5c-4ae7-8987-db0eefb6a295
        Total devices 3 FS bytes used 448.00KiB
        devid    2 size 2.50GiB used 1.04GiB path /dev/sdg
        devid    3 size 2.50GiB used 0.00B path /dev/sdj
        *** Some devices missing

Remove missing device.

btrfs device delete missing /tmp/kkk/mountpoint/

Balance data

At this point we have a filesystem with two devices, but all of the metadata and data are still stored on the original device. The filesystem must be balanced to spread the exact same data to the new device.

btrfs filesystem balance /tmp/kkk/mountpoint/

Check mirroring is completed. The used space size numbers(e.g. 800.00MiB) should be the same on both devices.

btrfs filesystem show
    Label: 'MyMirrorDisks'  uuid: f656414a-3e5c-4ae7-8987-db0eefb6a295
        Total devices 2 FS bytes used 384.00KiB
        devid    2 size 2.50GiB used 800.00MiB path /dev/sdg
        devid    3 size 2.50GiB used 800.00MiB path /dev/sdj

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.