Create a RAID volume on Raspberry Pi

In this tutorial, I will go through the steps necessary to create a RAID volume in Raspbian OS on your Raspberry Pi. RAID allows redundant, quick or extended storage depending on which RAID level you go with. I’m going to assume that you’ve followed my previous tutorial on how to install and run Raspbian OS from a USB Flash Drive, although this isn’t necessary.

As we’re using a software RAID controller, we can’t run the OS from our RAID volume, but we can use to for storage. This tutorial is part one of two where we will use our RAID volume for creating our own cloud storage platform using OwnCloud. View the Installing OwnCloud on Raspberry Pi tutorial.

What is RAID?

RAID stands for Redundant Array of Independent Disks. There are various RAID levels, the most common are RAID-0, RAID-1 and RAID-5. RAID allows us to use several hard drives or USB flash drives in clever ways to add speed, redundancy or larger combined volumes.

RAID-0

RAID 0 sees blocks of data spread across two or more physical storage devices in the array. The benefits are read and writes are very quick. The trade-off is there is no redundancy. If one of the disks fails, the data is lost or damaged for good across all the devices.

RAID-1

RAID-1 is the level of RAID which this tutorial will work towards. RAID-1 mirrors the blocks of data across the storage devices in the array. If one disk fails, it can be replaced without any loss of data. There is a slight performance trade-off, but nothing noticeable under ordinary circumstances.

RAID-5

RAID-5 stripes blocks of data across a minimum of three storage devices. RAID-5 offers both performance and good redundancy. The trade-off comes if you have multiple disk failures.

My Setup

The setup I’ll be configuring is the following:

  • 1 x Toshiba 8GB Flash storage – Used for the OS. See this tutorial for booting from a USB Flash Drive
  • 2 x Toshiba 16GB Flash storage – Used for our RAID-1 volume. Larger flash drives can be used.
  • 1 x Raspberry Pi 3 – I’m not sure if this tutorial will work on a RPi Zero or Rpi 1 or 2.

I find that the Corsair Voyager and Toshiba Flash drives tend to be quick and reliable.

Step 1 – Configure storage ready for the RAID array

To begin, we need to list the block devices using the following command:

lsblk

You should see an output like this:

In my example, sda and sdb are the two 16GB Toshiba flash drives, with sdc being the 8GB boot flash drive. Yours may differ so make a note of which devices are your flash storage which you intend to put into your RAID array.

We’re going to use fdisk to configure our devices. Remember, in my example, sda and sdb are the flash devices, this may differ to yours.

Repeat the following for each device in your RAID array, switching ‘sda’ for the device being configured:

sudo fdisk /dev/sda

The interactive prompt will start. When asked for a command to run, type ‘o‘. This will create an empty DOS partition table.

After this type ‘n‘ to create a new partition. For each of the options, we’re going with the default values to hit enter for each until the interactive prompt reads ‘Command (m for help):’ again. Now type ‘w‘ to write the changes to the device. You will have seen something like this:

 

Repeat the above for all the devices in your RAID array.

Step 2 – Install the software RAID controller

To begin, we ensure we’re up-to-date:

sudo apt-get update -y

This will install any updates to installed software on our Raspberry Pi. Next we’re going to install the software RAID controller called mdadm:

sudo apt-get install mdadm -y

You should now see the package configuration interface:

Choose OK and then go with the default value of ‘all’. The installation should finish after this.

Step 3 – Create the RAID volume

Now it’s time to create our RAID-1 volume. RAID-1 will create a mirrored volume. Remember to change sda1 and sdb1 to suit your configuration:

sudo mdadm --create /dev/md0 --level=mirror --raid-devices=2 /dev/sda1 /dev/sdb1

After this, you will receive a warning about not being able to boot from it, type ‘y‘ to continue creating the array.

Next, we’re going to format our partition and mount it. Create a start point for your RAID array:

sudo mkdir -p /mnt/raid0

You don’t have to call your mount point ‘raid0’, call it whatever you want.

Now let’s format the RAID volume:

sudo mkfs.ext4 /dev/md0

And finally, mount it on the mount point we created earlier:

sudo mount /dev/md0 /mnt/raid0/

Next, we want to mount the volume on boot:

echo "/dev/md0 /mnt/raid0/ ext4 defaults,noatime 0 1" | sudo tee -a /etc/fstab

Finally, we update mdadm so that we can mount on startup:

sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf

Step 4 – Take a look at your RAID volume

That’s pretty much it. Change to your RAID volume:

cd /mnt/raid0/

List the contents, it should be empty (apart from lost+found):

ls

Conclusion

We’ve created our RAID volume which will ensure that the contents are mirrored between the two USB storage devices. We can now continue and create our OwnCloud server, using the RAID volume as the storage. View the tutorial on installing OwnCloud on Raspberry Pi.

If you’ve found this tutorial beneficial, please feedback below and click an advert. The small revenues I receive from banner adverts allows me to buy the various bits of hardware necessary to write these tutorials.

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

5 Responses

  1. Ken DeWitt says:

    Do you have a picture of how you got the 2 16 gig flash connected to the raspberry pi 3?

  2. Michael says:

    Fantastic. Great instructions. I had a failed attempt to set up a raid 1 a few weeks ago but managed to get it up and running by following this. Thanks

  3. Dmitry Rogov says:

    Thanks for the article! It really helped me, to get into the topic!)) BTW I have also found an article about making up a network storage (NAS) on Raspbian – https://eltechs.com/raspberry-pi-nas-guide/

  4. Santino says:

    Hi Ste.

    I tried to set up to external 4TB-HDDs as a raid with your tutorial and ran into some problems.
    The OS I’m using is Kodi, running on a Raspberry Pi 2 from an SD card.

    First, I had to use a GPT partition table instead of DOS because of the size of the disks. Then I had to safely remove both disks (which I did via the Kodi GUI) in order to let mdadm actually access them.
    Then everythin went pretty much like you described, but when I tried to copy files from one HDD (that’s not part of the raid, of course) to the raid, I got an unspecific error message that the files couldn’t be written.
    I thought a reboot might solve the problem, but I wasn’t able to restart the system. During booting up the raid could not be found and therefore the system booted into emergency mode.
    I had to re-edit /etc/fstab and delete the line pertaining to the mounting of the raid in order to re-enable my system to boot.

    I do not know anything about Linux and was only able to set up my Kodi Box through a lot of tutorials, so I have no clue as to what might be going wrong here. I hope you can help me, as I’d really like to have the redundancy of a raid and actually bought the mentioned two 4TB disks just to set this up. Would be a shame not to be able to use them as such.

    Best regards,
    Santino

  1. 6th August 2017

    […] volume for our storage. I’m going to assume you’ve followed my previous tutorial ‘Create a RAID Volume on Raspberry Pi‘ first, although this is not […]

Leave a Reply