Raspberry Pi – RAID Storage

I want to extend my storage but it also has to be redundant, so I don’t loss any data that’s why I’m going to build a Raspberry Pi® 3 with RAID Storage using Some old 2.5″ HD drives, the Linux native RAID application MDADM. It’s pretty easy and shows you how to create a Linux RAID array which is a good skill to have.

Raid Storage Requirements

Raspberry Pi®

You can use Raspberry Pi’s from version 2 onwards, so 2B, 2B+ and RPi 3’s are fine, but I recommend the latest Raspberry Pi® 3. You’ll also need a good quality micro-USB power pack suitable for the Raspberry Pi® you’re using.

Recommended is a 5V ~ 2.5A power supply

2.5″ HDD Drives

We will be using some old 2.5″HDD drives for our RAID array. The drives should all be the same size and preferably the same manufacturer for a good RAID array however, if you have an odd one out, just make sure it’s the same size or larger than the smallest drive. USB 2.0 delivers up to 500 mA (at 5 V) and that’s enough for most 2.5″ HDD’s.

Note: Do not use HDD’s witch use more power, this can harm your Raspberry Pi®.

In terms of drive size, your budget will be the determining factor, so just get what you can afford. Remember that with RAID-0 you get the total amount of all drives put together, so 2x 64GB drives will give you 128GB. RAID-1 will halve your total capacity, so 2x 64GB drives will give you 64GB, and RAID-5/6 will give you roughly 2/3 of total capacity, so 3x 64GB drives will give you 128GB of storage. RAID-10 will halve you total capacity, so 4x 64GB drives will give you 128GB storage.

Note: You can all so use USB sticks!

RAID Levels

The number of drives you have with determine what RAID level you will be able to create. I suggest 3 (or more) drives which is the minimum required for a RAID-4/5 array that offers a good balance between redundancy and speed. If you only have 2 drives, I suggest using RAID-0, and even though there is no redundancy, RAID-0 rarely fails on solid state media. I have had my workstation running on SATA-3 SSD RAID-0 for years and it never skips a beat, but If you’re storing important files, definitely go with RAID-1.

If you’re confident in your Linux skills you can create a RAID-10 array with 4 drives, which is 2 sets of RAID-1, then each set is used to form a RAID-0 array giving you the benefit of speed and redundancy (I will show you how to create all types of Linux RAID arrays).

Installing Raspbian 9.x “Stretch”

In this blog we therefore we use a Raspberry Pi® and do a Raspbian Stretch Lite, a Minimal image based on Debian™ Stretch, installation, where you make a complete web server upon. This means that we will install Apache2, a database server MySQL and PHP support from the ground up.

SD Formatter

Visit the SD Association’s website and download SD Formatter 4.0 for either Windows or Mac. Follow the instructions to install the software. Insert your SD card into the computer or laptop’s SD card reader, select the drive letter for your SD card and format it.

Download etcher

Visit etcher.io and download and install the Etcher SD card image utility. Run Etcher and select the Raspbian image you unzipped on your computer or laptop. Select the SD card drive. Note that the software may have already selected the right drive. Finally, click Burn to transfer Raspbian to the SD card. You’ll see a progress bar that tells you how much is left to do. Once complete, the utility will automatically eject/unmount the SD card so it’s safe to remove it from the computer.

Take your SD card out of your computer and place it back so it can be mounted again.

Enable ssh

For security reasons, ssh is no longer enabled by default. Open the Terminal app. Click the Finder icon in your dock. Then click Go. and click Utilities. Double-click Terminal. To enable it, run this command:

touch /Volumes/boot/ssh

Eject the micro SD card

Issue an eject command:

diskutil eject /dev/disk2

The command won’t physically eject the disk/card. It’s more of a logical eject: meaning it’s safe to remove the card now. You can also do this by right-clicking on the card on your desktop.

Boot the raspberry Pi®

Put the SD card into the Raspberry Pi®. Plug a Micro-USB power cable into the power port (the one closest to the end of the board). Give the Raspberry Pi® plenty of time to boot up (it can take as much as 90 seconds — or more)

Login to you raspberry Pi®

This part assumes that ssh is enabled for your image and that the default user is pi with a password of raspberry. Open the Terminal app. Click the Finder icon in your dock. then click Go. and click Utilities. Double-click Terminal. Run the following commands:

ssh pi@raspberrypi.local

If prompted with this warning:

The authenticity of host 'raspberrypi.local (xxxxxxxxxxxxxxxxxxxxxxxxxxxxx)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)?

just hit enter to accept the default (Yes). Type in the password — by default this is raspberry. Congratulations! You can now access your Raspberry Pi® over wifi! If you can’t connect, see the troubleshooting blog here.

Change your Hostname and Password

Because your pi is now on the network, you should immediately change the host name and password. At the command line type:

sudo raspi-config

Select the options for changing the host name and password. On a new image, I would also recommend expanding the file system (now under the Advanced options) and set the locale settings. Once the changes are made you need to, reboot. To reboot your Raspberry Pi®, type:

sudo reboot

Remember that once you reboot, you will need to use the new host name to login over ssh. For example, if your new host name is server you would connect like this:

ssh pi@server.local

Fix the locale issue

Last week I was trying to make my Raspberry Pi® work again, and while reinstalling and upgrading Raspbian OS packages I run into an issue where a perl warning is displayed.

The warning message appears during the installation packages(invoking Perl) and it’s due to missing locales. Type:

locale

when you see this message and see that LC_ALL = (unset), and LANG = (unset) are (unset), then proceed with the fix.

Static Ip adress

After setting up the network connection, all I have to do is configure a static IP address and my Raspberry Pi is ready for use. Here I will show you how to set a static IP address on your Raspberry Pi® on Raspbian OS.

sudo nano /etc/dhcpcd.conf

Scroll all the way to the bottom of the file and add one, or both of the following snippets. Depending on whether you want to set a static IP address for a wired connection or a wireless connection eth0 = wired, wlan0 = wireless.

You’ll need to edit the numbers in the snippet so they match your network configuration:

interface = This defines which network interface you are setting the configuration for.static ip_address = This is the IP address that you want to set your device to. (Make sure you leave the /24 at the end)static routers = This is the IP address of your gateway (probably the IP address or your router)static domain_name_servers = This is the IP address of your DNS (probably the IP address of your router). You can add multiple IP addresses here separated with a single space.

Save your changes, press CTRL + O, followed by Enter. Then press CTRL + X. Now all you need to do is reboot, and everything should be set!

sudo reboot

Get the updates and upgrade

Once connected over WiFi, the next thing you should do is run some updates:

sudo apt-get update

and then:

sudo apt-get upgrade -y

Now we are ready to install MDADM.

Update & Install MDADM

Now we just need to update Raspbian and install the MDADM RAID package:

sudo apt-get update

Then we are going to upgrade your Raspberry Pi®:

sudo apt-get upgrade -y

Now you can give the command to install MDADM:

sudo apt-get install mdadm -y

Now let’s move on to Setup RAID Volume & File System.

Setup Drives and RAID Volume

Just to make things easier for identifying our USB drives, we need to format and label each one sequentially:

MAC OS X

Insert one of your USB drives (close any automatic windows). Go to Finder > Applications > Utilities > Disk Utility. Locate the USB drive from the left hand side and select it. Click on the Erase tab and choose the following options: Format: ExFAT, Name: USB01. Now click on Erase button, then confirm the Erase on the alert box. Repeat this process for each USB drive you want to use, increment Name: to USB02, USB03 etc.

Windows

Insert one of your USB drives (close any automatic windows). (Win7,XP,Vista) Go to Start > Computer (Win8,10) search “File Explorer” and open top result. Right-click on the USB drive icon, then select Format. In the Format window, change the following options: File system: NTFS, Volume label: USB01. Now click the Start button, then Yes to alert box. Repeat this process for each USB drive you want to use, increment Volume label: to USB02, USB03 etc

Now it’s time to insert your USB drives into your Raspberry Pi®, it doesn’t matter which drive goes where. Now we need to find out the mount points for each drive using the following command:

If you have different mount points than shown here, it doesn’t really matter, you’ll just need to replace the code snippets with the mount points you have. It also doesn’t matter if the drive labels are out of order, just so long as they match up to your mount points.

Depending on how many drives you have (see RAID levels chart above) choose your RAID level and follow the instructions for your chosen level below.

Setup RAID-0 Volume

Enter the following code to setup two or more USB drives into a high performance RAID-0 array in parallel, replacing –raid-devices=X with the number of drives you’re using, and /dev/sda1 /dev/sdb1 with your mount points:

Create File System

You have a number of choices when it comes to Linux file systems, but I will be using the EXT4 file system as it’s the best choice for our RAID array. If you want to learn more about some of the flags (stride, stripe-width, block options) see this article. The following code will produce an EXT4 filesystem with a block size of 4096kb, stride of 32, a stripe-width of 64 and will leave 1% free so it won’t fill up and cause issues:

The item we’re after is the UUID of /dev/md0: which in my case is 394fd8f2-7b2a-474f-8e58-48b81a6ca8fb but yours will be different. Highlight and copy the UUID, then we need to add it to fstab and backup the original fstab just in case:

sudo cp /etc/fstab /etc/fstab.bak

sudo nano /etc/fstab

Just before the bottom comments, make a space and enter the following on a single line (replace [your_uuid] with the UUID of your file system):

UUID=[your_uuid] /mnt ext4 defaults 0 0

So my fstab entry would look like this:

UUID=394fd8f2-7b2a-474f-8e58-48b81a6ca8fb /mnt ext4 defaults 0 0

Now save Ctrl+O and exit Ctrl+X.

That’s it your done!

Now you have a Raspberry Pi® as a RAID array controller. Now it won’t be the fastest raid controller on the planet, but if you’re only doing a bit of streaming, and a bit of backup, and would like a bit of redundancy, then this solution might be for you. The performance will not compete with a real Raid controller. This because the Raspberry Pi® has USB 2.0 ports with a maximum speed of 480 Mbit/s (high speed). But it will do for your Small Office / Home Office setup.

REFERENCE

Raspberry Pi® (2019), The Raspberry Pi is a tiny and affordable computer that you can use to learn programming through fun, practical projects, https://www.raspberrypi.org

Donate

Since October 2015, StudioPieters® has been writing this blog. Help accelerate its development with a small contribution! StudioPieters® is open source, and 100% free . Please consider donating to StudioPieters® before starting the fun of building and learning. StudioPieters® is a non-profit organisation devoted to making creative use of electronics accessible to everyone.