How to set up software RAID for Raspberry Pi

In my previous article, I explained how to add an USB HDD (in my case, it is 314GB WD PiDrive) to Raspberry Pi. Today, I am going to go over the steps to create a software RAID to make the file system more reliable. Ideally, there should be at least two HDD in the system to prevent data loss in the event of drive failure. However, I only have one HDD in my Raspberry Pi, so, I created two partitions /dev/sda2  and /dev/sda3 to mirror each other using RAID1.

Installing RAID software

The software that I am going to use is ‘mdadm’. It is widely used in Linux for software RAID. To install it, run the following command.

sudo apt-get install mdadm

Create RAID1 Drive

There are many different RAID levels. In my case, I am using RAID1 to mirror two partitions on the same drive. By running the following command, it creates a RAID1 drive /dev/md0.

sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sda2 /dev/sda3
Raspberry Pi 3 create software RAID with mdadm
Raspberry Pi 3 create software RAID with mdadm

Once the RAID drive is created, mdadm automatically synchronize the two partitions in a background process. To check the status, view the file /proc/mdstat

It shows that the sync process is 5% done and will finish in 162.6 minutes.

Raspberry Pi 3 software RAID1 /proc/mdstat
Raspberry Pi 3 software RAID1 /proc/mdstat

Format and mount the RAID disk

The raid disk needs to be formated before mounting. Run the following commands to format and create a directory /home/disks/raid for mounting:

sudo mkfs.ext4 /dev/md0
cd /home/disks
mkdir raid
chmod 777 raid

Add the following line to /etc/fstab to mount the disk

/dev/md0     /home/disks/raid    ext4    defaults   1   2

mount the disk

sudo mount -a

Below is a screenshot showing the file system statistics after the RAID disk is mounted

RAID1 disk mounted on Ubuntu
RAID1 disk mounted on Ubuntu

Next steps

The RAID disk will be the place for me to store critical data and programs. In the future, I will install GIT server to store the code the I develop. I will post an article on how to do so.

  • Richard Houts

    I try this and i get some kind of error

    mdadm: unrecognized option ‘–vebose’
    Usage: mdadm –help
    for help

    Any help would be great

    • it may be due to the difference in the version.
      when I typed ‘mdadm -V’, it returned the followings:
      mdadm – v3.3 – 3rd September 2013

      Can you type ‘mdadm –help-options’ ?
      ‘–create’ is same as ‘-C’
      ‘–verbose’ is same as ‘-v’
      ‘–build’ is same as ‘-B’


      • Richard Houts

        My ‘mdadm -V’ is mdadm – v3.3.2 – 21st August 2014

        When I type ‘sudo mdadm -C -v /dev/md0 –level=1 –raid-devices=2 /dev/sda2 /dev/sda3’ it now says:

        mdadm: super1.x cannot open /dev/sda2: Device or resource busy
        mdmon: ddf: Cannot use /dev/sda2: Device or resource busy
        mdmon: Cannot use /dev/sda2: It is busy
        mdadm: cannot open /dev/sda2: Device or resource busy

        What do I do to not make it busy?

      • It is probably because the partition is mounted. You need to unmount it before running mdadm command.

        Refer to this thread:

      • Richard Houts

        I am not sure how to unmount.

      • sudo umount /dev/sda2

      • Richard Houts

        It’s causing problems. May have to format the HD and start over with ‘How to install a USB Hard Disk’ and move on to ‘How to set up software RAID’

    • Ryan Richardson

      It’s because it’s supposed to be “verbose”. The command “sudo mdadm –create –vebose /dev/md0 –level=1 –raid-devices=2 /dev/sda2 /dev/sda3” has a typo.

  • Richard Houts

    Should i wait until the sync process is 100% before going onto “Format and mount the RAID disk”?