Hi, I’m Eric. I’m technical director @enginsightcom with deep interest in it security.
Eric Range

Creating a Btrfs RAID5 Array on Raspberry Pi Using Disk IDs

Eric Range
Eric Range
Nov 10, 2024
Hi, I’m Eric. I’m technical director @enginsightcom with deep interest in it security.

Table of Contents

  1. Introduction
  2. Why Use Disk IDs on Raspberry Pi
  3. Prerequisites
  4. Identifying Your Disks Using Disk IDs
  5. Preparing the Disks
  6. Creating the Btrfs RAID5 Array
  7. Mounting the RAID5 Array
  8. Configuring Automatic Mounting at Boot
  9. Verifying the RAID5 Setup
  10. Conclusion
  11. Additional Resources

Introduction

Setting up a RAID5 array on a Raspberry Pi can enhance both the performance and reliability of your storage system. However, one common challenge is that the device names (e.g., /dev/sda, /dev/sdb) assigned to USB disks can change between reboots. This can cause issues when your RAID configuration relies on these device names.

In this article, we’ll walk through creating a Btrfs RAID5 array using persistent disk IDs, ensuring that your RAID configuration remains stable across reboots.


Why Use Disk IDs on Raspberry Pi

On a Raspberry Pi, especially when using multiple USB drives, the kernel may assign different device names to your disks after each reboot. This happens because the order in which the disks are detected can vary.

For example:

  • Today, your disks might be /dev/sda, /dev/sdb, /dev/sdc, etc.
  • After a reboot, they might be assigned /dev/sdc, /dev/sda, /dev/sdb, etc.

This variability can break your RAID setup if it’s configured using device names. To avoid this, we use disk IDs, which are persistent identifiers that remain the same across reboots. Disk IDs are found under /dev/disk/by-id/ and are based on the hardware properties of the disks, such as serial numbers.


Prerequisites

  • A Raspberry Pi running a Linux distribution with Btrfs support.
  • Five USB disks to be used in the RAID5 array.
  • Root or sudo access to the Raspberry Pi.
  • Familiarity with basic Linux commands.

Warning: The steps below will erase all data on the disks involved. Ensure you have backups of any important data before proceeding.


Identifying Your Disks Using Disk IDs

First, we need to identify the disk IDs of the USB disks we want to include in the RAID5 array.

List Disk IDs

Run the following command to list all disk IDs:

ls -l /dev/disk/by-id/

Sample Output:

lrwxrwxrwx 1 root root 13 Nov 10 16:29 mmc-SC32G_0xd43a18de -> ../../mmcblk0
lrwxrwxrwx 1 root root 15 Nov 10 16:29 mmc-SC32G_0xd43a18de-part1 -> ../../mmcblk0p1
lrwxrwxrwx 1 root root 15 Nov 10 16:29 mmc-SC32G_0xd43a18de-part2 -> ../../mmcblk0p2
lrwxrwxrwx 1 root root  9 Nov 10 16:29 usb-External_USB3.0_DISK00_20170331000DA-0:0 -> ../../sdc
lrwxrwxrwx 1 root root  9 Nov 10 16:29 usb-External_USB3.0_DISK01_20170331000DA-0:1 -> ../../sdd
lrwxrwxrwx 1 root root  9 Nov 10 16:29 usb-External_USB3.0_DISK02_20170331000DA-0:2 -> ../../sde
lrwxrwxrwx 1 root root  9 Nov 10 16:29 usb-External_USB3.0_DISK03_20170331000DA-0:3 -> ../../sdf
lrwxrwxrwx 1 root root  9 Nov 10 16:29 usb-External_USB3.0_DISK04_20170331000DA-0:4 -> ../../sdg
lrwxrwxrwx 1 root root  9 Nov 10 16:29 usb-WD_Elements_2620_575837324131304434304A36-0:0 -> ../../sdb
lrwxrwxrwx 1 root root  9 Nov 10 16:29 usb-WD_Elements_2621_575837314139393237364431-0:0 -> ../../sda

Identify the Disks

From the output, identify the disk IDs of the USB disks you want to include in the RAID5 array. In this example, we’ll use the following disks:

  • /dev/disk/by-id/usb-External_USB3.0_DISK00_20170331000DA-0:0
  • /dev/disk/by-id/usb-External_USB3.0_DISK01_20170331000DA-0:1
  • /dev/disk/by-id/usb-External_USB3.0_DISK02_20170331000DA-0:2
  • /dev/disk/by-id/usb-External_USB3.0_DISK03_20170331000DA-0:3
  • /dev/disk/by-id/usb-External_USB3.0_DISK04_20170331000DA-0:4

These disk IDs correspond to the physical disks and remain constant across reboots.


Preparing the Disks

Warning: The following steps will erase all data on the specified disks.

Wipe Existing Filesystems

Use the wipefs command to remove any existing filesystems or partition tables on the disks:

sudo wipefs -a /dev/disk/by-id/usb-External_USB3.0_DISK00_20170331000DA-0:0
sudo wipefs -a /dev/disk/by-id/usb-External_USB3.0_DISK01_20170331000DA-0:1
sudo wipefs -a /dev/disk/by-id/usb-External_USB3.0_DISK02_20170331000DA-0:2
sudo wipefs -a /dev/disk/by-id/usb-External_USB3.0_DISK03_20170331000DA-0:3
sudo wipefs -a /dev/disk/by-id/usb-External_USB3.0_DISK04_20170331000DA-0:4

Explanation:

  • The wipefs -a command removes all filesystem signatures from the specified disk.
  • Ensure you run this command only on the disks intended for the RAID array.

Creating the Btrfs RAID5 Array

With the disks prepared, we can now create the Btrfs RAID5 array using the disk IDs.

Create the RAID5 Filesystem

Run the following command:

sudo mkfs.btrfs -m raid5 -d raid5 -L "raid5" \
    /dev/disk/by-id/usb-External_USB3.0_DISK00_20170331000DA-0:0 \
    /dev/disk/by-id/usb-External_USB3.0_DISK01_20170331000DA-0:1 \
    /dev/disk/by-id/usb-External_USB3.0_DISK02_20170331000DA-0:2 \
    /dev/disk/by-id/usb-External_USB3.0_DISK03_20170331000DA-0:3 \
    /dev/disk/by-id/usb-External_USB3.0_DISK04_20170331000DA-0:4

Explanation:

  • sudo mkfs.btrfs: Invokes the Btrfs filesystem creation utility.
  • -m raid5: Sets the metadata profile to RAID5.
  • -d raid5: Sets the data profile to RAID5.
  • -L "raid5": Assigns the label “raid5” to the filesystem.
  • The disk IDs are specified at the end, ensuring persistent identification.

Note: Btrfs RAID5 is considered experimental in some distributions. Be aware of potential risks and consider using RAID10 for critical data.


Mounting the RAID5 Array

Create a Mount Point

Create the directory where you want to mount the RAID array:

sudo mkdir -p /mnt/raid5

Mount the Filesystem

Mount the RAID5 array using the label:

sudo mount -L raid5 /mnt/raid5

Explanation:

  • -L raid5: Mounts the filesystem labeled “raid5”.
  • /mnt/raid5: The mount point where the filesystem will be accessible.

Configuring Automatic Mounting at Boot

To ensure the RAID5 array is mounted automatically at boot, add an entry to /etc/fstab.

Edit /etc/fstab

Open the file with a text editor:

sudo nano /etc/fstab

Add the Mount Entry

Add the following line at the end of the file:

LABEL=raid5   /mnt/raid5   btrfs   defaults   0   0

Explanation:

  • LABEL=raid5: Refers to the filesystem labeled “raid5”.
  • /mnt/raid5: The mount point.
  • btrfs: Filesystem type.
  • defaults: Mount options.
  • 0 0: Disable dump and fsck.

Save and Close the File

  • If using nano, press Ctrl + O to save and Ctrl + X to exit.

Test the /etc/fstab Entry

Before rebooting, test the new /etc/fstab entry:

sudo umount /mnt/raid5
sudo mount -a
  • Ensure no errors occur and the filesystem is mounted correctly.

Verifying the RAID5 Setup

Check Mounted Filesystems

Run:

df -h | grep /mnt/raid5

You should see an entry like:

/dev/sdc        3.6T  100M  3.6T   1% /mnt/raid5

Note: The device name (/dev/sdc) may vary but is not significant due to our use of disk IDs.

Check Btrfs Filesystem Details

Run:

sudo btrfs filesystem show /mnt/raid5

Sample Output:

Label: 'raid5'  uuid: abcdefgh-1234-5678-90ab-cdefghijklmn
    Total devices 5 FS bytes used 100.00MiB
    devid    1 size 1.00TiB used 50.03GiB path /dev/sdc
    devid    2 size 1.00TiB used 50.03GiB path /dev/sdd
    devid    3 size 1.00TiB used 50.03GiB path /dev/sde
    devid    4 size 1.00TiB used 50.03GiB path /dev/sdf
    devid    5 size 1.00TiB used 50.03GiB path /dev/sdg

This output confirms that all five disks are part of the RAID5 array.


Conclusion

By using disk IDs to create your Btrfs RAID5 array on a Raspberry Pi, you ensure that your RAID configuration remains consistent across reboots, even if the device names change. This approach enhances the reliability of your storage setup and prevents potential issues associated with dynamic device naming.


Additional Resources


Disclaimer: While Btrfs offers advanced features like RAID5, it’s important to note that RAID5 and RAID6 implementations in Btrfs have had stability issues in the past. It’s recommended to keep backups of critical data and consider using other RAID levels like RAID1 or RAID10 for production environments.