2020-05-26: This guide is now updated to reflect some bug fixes and changes in mdadm and Samba.
Today I’ll show you how to build a Raspberry Pi 3/4 RAID NAS server using USB flash drives and the Linux native RAID application mdadm, along with SAMBA so the drive will show up as a normal network folder on Windows PC’s. It’s an intermediate tutorial and shows you how to create a Linux RAID array which is a good skill to have.
You can use Raspberry Pi’s from version 2 onward, so 2B, 2B+, RPi 3/4 are fine, but I recommend the latest Raspberry Pi 4 with 4GB RAM. You’ll also need a good quality micro-USB power pack suitable for the Raspberry Pi you’re using, if in doubt, just buy a combo pack that comes with case, power pack and other goodies.
We will be using USB2.0 flash drives for our RAID array, however you can purchase USB3.0 drives if you’re using a Raspberry Pi 4 (which has 2 USB3.0 ports) and RAID levels 0 or 1. 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.
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 your total capacity, so 4x 64GB drives will give you 128GB storage (more about RAID levels further below).
The number of drives you have with determine what RAID level you’ll 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, 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).
|Number of Drives||RAID Level Availability|
Once you’ve chosen a desired RAID level, let’s move onto Install/Update Raspbian and mdadm.
Now we need to prepare our MicroSD card, install Raspbian, update the system and install mdadm Linux RAID. You will need a MicroSD card of 8GB or more and the application Etcher to transfer the .img file to it (Etcher supports Windows, Linux and MAC).
Now we need to do some initial setup of Raspbian, so go ahead and hook up a screen, keyboard and power up your Raspberry Pi and remember, on first boot your RasPi will automatically expand the file system to fit your MicroSD card size, then it will reboot. Once rebooted, login with
username: pi password: raspberry
Now we’ll do some basic setup options for our RasPi NAS server, so enter the following and use raspberry for the password prompt:
Then change the following:
Step-by-step screenshots of the above process:
Now we just need to update Raspbian and install the mdadm RAID package:
sudo apt-get update sudo apt-get upgrade -y sudo apt-get install mdadm -y
Just to make things easier for identifying our USB drives, we need to format and label each one sequentially:
Now it’s time to insert your USB drives into your Raspberry Pi, it doesn’t matter which drive goes where. We need to find out the mount points for each drive using the following command:
Which should produce something like the following:
pi@RASPINAS:~ $ sudo blkid /dev/mmcblk0p1: LABEL="boot" UUID="CDD4-B453" TYPE="vfat" PARTUUID="498dcd43-01" /dev/mmcblk0p2: LABEL="rootfs" UUID="72bfc10d-73ec-4d9e-a54a-1cc507ee7ed2" TYPE="ext4" PARTUUID="498dcd43-02" /dev/sda1: LABEL="USB01" UUID="8A44B0F244B0E1DD" TYPE="ntfs" PARTUUID="01411281-01" /dev/sdb1: LABEL="USB02" UUID="2CF48901F488CE90" TYPE="ntfs" PARTUUID="bd5d9029-01" /dev/sdc1: LABEL="USB03" UUID="A8CE3E46CE3E0D56" TYPE="ntfs" PARTUUID="0fb30d0e-01" /dev/sdd1: LABEL="USB04" UUID="3AC2CC7DC2CC3F3F" TYPE="ntfs" PARTUUID="0fb30d2b-01" /dev/mmcblk0: PTUUID="498dcd43" PTTYPE="dos" pi@RASPINAS:~ $
We need to confirm the mount points of our USB drives, most of the time it will be /dev/sda1, /dev/sdb1, /dev/sdc1 and /dev/sdd1
/dev/sda1: LABEL="USB01" /dev/sdb1: LABEL="USB02" /dev/sdc1: LABEL="USB03" /dev/sdd1: LABEL="USB04"
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, choose your RAID level and follow the instructions for your chosen level below.
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:
(Note for RAID-0 arrays: If you get error: “mdadm: RUN_ARRAY failed: Unknown error 524” this is a bug due to drives not being exactly the same size for RAID-0 which has been fixed but Raspbian hasn’t updated yet).
sudo mdadm --create --verbose /dev/md/vol1 --level=0 --raid-devices=2 /dev/sda1 /dev/sdb1
If I were using 4 drives in my RAID-0 array the code would be as follows:
sudo mdadm --create --verbose /dev/md/vol1 --level=0 --raid-devices=4 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1
To setup a redundant RAID-1 array using two drives, enter the following code:
sudo mdadm --create --verbose /dev/md/vol1 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
To setup a redundant RAID-4, RAID-5 with three drives, or RAID-6 array with four drives, use the following code, replacing
--level=X with the RAID level you want:
sudo mdadm --create --verbose /dev/md/vol1 --level=4 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1
If I wanted a RAID-5 array using three drives and a hot spare, I would use the following code:
sudo mdadm --create --verbose /dev/md/vol1 --level=5 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1 --spare-devices=1 /dev/sdd1
To setup a fast, redundant RAID-10 (aka Nested RAID-1+0) use the following code:
sudo mdadm --create --verbose /dev/md/vol1 --level=10 --raid-devices=4 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1
For more info on complex RAID arrays, see the mdadm Wiki.
Now let’s confirm all went well creating your RAID array with the following command:
sudo mdadm --detail /dev/md/vol1
Which should produce something like this:
pi@raspberrypi:~ $ sudo mdadm --detail /dev/md/vol1 /dev/md/vol1: Version : 1.2 Creation Time : Mon May 25 06:19:08 2020 Raid Level : raid1 Array Size : 7808000 (7.45 GiB 8.00 GB) Used Dev Size : 7808000 (7.45 GiB 8.00 GB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Update Time : Mon May 25 06:23:01 2020 State : clean, resyncing Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 Consistency Policy : resync Resync Status : 14% complete Name : raspberrypi:vol1 (local to host raspberrypi) UUID : 9ca39f48:7c165518:5b186d64:c1ca774e Events : 2 Number Major Minor RaidDevice State 0 8 1 0 active sync /dev/sda1 1 8 17 1 active sync /dev/sdb1
Raspbian won’t let us save the RAID array as user:pi even using sudo, so we need to temporarily change to root account, enter the following:
Now we can save our RAID array by entering the following code:
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
It’s also good to confirm it was saved by viewing the config file
nano /etc/mdadm/mdadm.conf and look for something like this at the bottom of the file:
# This configuration was auto-generated on Fri, 19 Jan 2018 08:14:18 +0000 by mkconf ARRAY /dev/md/vol1 metadata=1.2 name=RASPINAS:0 UUID=0d7c55bd:ae5f8fbc:0e048a15:79de5064
Then exit from root user:
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 (replace
/dev/md/vol1 with your RAID array name):
sudo mkfs.ext4 -v -m .1 -b 4096 -E stride=32,stripe-width=64 /dev/md/vol1
If successful, you should see the following output (accounting information can take a minute or two):
pi@RASPINAS:~ $ sudo mkfs.ext4 -v -m .1 -b 4096 -E stride=32,stripe-width=64 /dev/md/vol1 mke2fs 1.43.4 (31-Jan-2017) fs_types for mke2fs.conf resolution: 'ext4' Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=32 blocks, Stripe width=64 blocks 958464 inodes, 3829760 blocks 3829 blocks (0.10%) reserved for the super user First data block=0 Maximum filesystem blocks=2151677952 117 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Filesystem UUID: 394fd8f2-7b2a-474f-8e58-48b81a6ca8fb Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done pi@RASPINAS:~ $
Now mount the file system so we can access it using the following command:
sudo mount /dev/md/vol1 /mnt
We need to automatically mount the file system at boot, so let’s find out the UUID of our mounted file system by entering the following command:
Which should produce something like the following:
pi@RASPINAS:~ $ sudo blkid /dev/mmcblk0p1: LABEL="boot" UUID="CDD4-B453" TYPE="vfat" PARTUUID="498dcd43-01" /dev/mmcblk0p2: LABEL="rootfs" UUID="72bfc10d-73ec-4d9e-a54a-1cc507ee7ed2" TYPE="ext4" PARTUUID="498dcd43-02" /dev/mmcblk0: PTUUID="498dcd43" PTTYPE="dos" /dev/sda1: UUID="0d7c55bd-ae5f-8fbc-0e04-8a1579de5064" UUID_SUB="f954ff33-87db-4e26-8be9-e978aa0ef815" LABEL="RASPINAS:0" TYPE="linux_raid_member" PARTUUID="bd5d9029-01" /dev/sdb1: UUID="0d7c55bd-ae5f-8fbc-0e04-8a1579de5064" UUID_SUB="eea15556-4e0a-1985-163a-064670df9db2" LABEL="RASPINAS:0" TYPE="linux_raid_member" PARTUUID="01411281-01" /dev/md/vol1: UUID="394fd8f2-7b2a-474f-8e58-48b81a6ca8fb" TYPE="ext4" /dev/sdc1: UUID="0d7c55bd-ae5f-8fbc-0e04-8a1579de5064" UUID_SUB="2a8456d7-6fea-eba2-7728-ebd20aa238e5" LABEL="RASPINAS:0" TYPE="linux_raid_member" PARTUUID="0fb30d2b-01" /dev/sdd1: UUID="0d7c55bd-ae5f-8fbc-0e04-8a1579de5064" UUID_SUB="51019b58-995f-cf35-f6c3-33d61a104d76" LABEL="RASPINAS:0" TYPE="linux_raid_member" PARTUUID="0fb30d0e-01"
The item we’re after is the UUID of
/dev/md/vol1: (if your volume name doesn’t show, it’ll be called “md127” or similar, this is a bug in mdadm, but continue the guide using the name you gave your array) which in my case is
394fd8f2-7b2a-474f-8e58-48b81a6ca8fb but yours will be different. Highlight and copy the UUID and paste into a text editor, then 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.
By default, root user owns the
/mnt file system with no read or write permissions, so we need to change that as follows:
sudo chown -R pi:pi /mnt sudo chmod -R 0777 /mnt
SAMBA is a Windows file sharing system that runs on Linux, works flawlessly, and is easy to setup too. Let’s install SAMBA and then configure it with the following command (choose Yes to window prompt during installation):
sudo apt-get install samba samba-common-bin -y
After SAMBA finishes installing, let’s add user:pi and share password by entering the following command (enter a password at the prompts):
sudo smbpasswd -a pi
Which should produce the following result if successful:
pi@RASPINAS:~ $ sudo smbpasswd -a pi New SMB password: [ENTER PASSWORD] Retype new SMB password: [CONFIRM PASSWORD] Added user pi. pi@RASPINAS:~ $
Before we start altering the SAMBA config file, let’s make a backup:
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
Now we edit the smb.conf file:
sudo nano /etc/samba/smb.conf
Scroll down to the bottom of the file using your arrow key, then add the following code to create your NAS file share:
# NAS Share [NAS] path = /mnt comment = RasPi NAS Share valid users = pi writable = yes browsable = yes create mask = 0777 directory mask = 0777
The save Ctrl+O and exit Ctrl+X. Now let’s make sure our changes work, enter the following:
Which should product the following (if you want a full output, press Enter at the prompt):
pi@RASPINAS:~ $ testparm Load smb config files from /etc/samba/smb.conf rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384) WARNING: The "syslog" option is deprecated Processing section "[homes]" Processing section "[printers]" Processing section "[print$]" Processing section "[NAS]" Loaded services file OK. Server role: ROLE_STANDALONE Press enter to see a dump of your service definitions
If the line reads “Loaded services file OK.” then you’re good to go.
Finally, we need to restart the SAMBA service by with the following command:
sudo systemctl restart smbd
(Please Note: Windows is a p.i.t.a when it comes to accessing your NAS if you get it wrong the first time, so please make sure you get it right first time. If you get any errors like “…Disconnect all previous connections to the server or shared resource and try again.” see this guide.)
Now let’s connect to your NAS share and make it automatically show up in your Network drive. Open File Explorer then click on the Network item on left-side window pane. Click inside the address bar, then type
\\RASPINAS (or whatever you chose for a host-name during Debian setup, if you’re unsure enter the
hostname command) and hit Enter
You can make your NAS a permanent drive if you like. Open File Manager and go to Computer/This PC, click the Computer tab, then Map Network Drive. Enter the host-name of your NAS followed by the shared folder name:
\\RASPINAS\NAS and choose a drive letter. If successful you should be redirected to the NAS shared folder. You can now go to My Computer/This PC and see your network drive there permanently.
Well I hope this guide has shown you some cool RPi and Linux stuff and that you had fun 🙂 Something to think about is adding a USB hub to expand the drive number capacity instead of forking out big $$ for large capacity USB drives. You can also add normal 2.5″ or 3.5″ SATA drives via a USB adapter, see this article. If you have any questions or comments, leave them below.