Build A Raspberry Pi RAID NAS Server - Complete DIY Guide - Ricmedia
Build A Raspberry Pi RAID NAS Server - Complete DIY Guide - Ricmedia
You are here: Home / Linux / Build a Raspberry Pi RAID NAS Server –
Complete DIY Guide
Introduction
Today I’ll show you how to build a Raspberry Pi 3 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.
Hardware Requirements
Raspberry Pi
You can use Raspberry Pi’s from version 2 onward, so 2B, 2B+ and RPi 3’s
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.
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).
RAID Levels
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
RAID LEVEL AVAILABILITY
DRIVES
redundancy)
3
RAID-5 Block-level striping with distributed parity
redundancy)
RAID 10 (nested RAID 1+0) (Excellent speed and
redundancy)
Once you’ve chosen a desired RAID level, let’s move onto Install/Update
Raspbian and mdadm.
Installing Raspbian
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).
raspi-config Options
Now we’ll do some basic setup options for our RasPi NAS server, so enter
the following and use raspberry for the password prompt:
bash
sudo raspi-config
Note* Use the Arrow keys to move up and down, use Enter key to
select/confirm, use Tab key to select Cancel or Finish
Select “Change User Password” and change user:pi’s password
Select Hostname, then type RASPINAS for host-name (or anything you
want) hit Ok
Select Interfacing Options > SSH, then Yes to enable
On Home Screen, hit Tab key until the <Finish> item is selected, hit
Enter to reboot
You Raspberry Pi is now configured
1. MAC OS X
2. Insert one of your USB drives (close any automatic windows)
3. Go to Finder > Applications > Utilities > Disk Utility
4. Locate the USB drive from the left hand-side and select it
5. Click on the Erase tab and choose the following options: Format:
ExFAT, Name: USB01
6. Now click on Erase button, then confirm the Erase on the alert box
7. Repeat this process for each USB drive you want to use, increment
Name: to USB02, USB03 etc
1. Linux (Ubuntu/Debian)
2. Insert one of your USB drives (close any automatic windows)
3. Open “All applications” icon bottom-left of your screen, then choose
Utilities > Disks
4. Select the USB drive from left side panel, then click the Gears icon,
select Format partition
5. Choose the following options: Erase: Don’t overwrite existing data
(quick), Type: NTFS, Name: USB01
6. Now click Format… button, then confirm by clicking Format button on
alert window
7. Repeat this process for each USB drive you want to use, increment
Name: 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. We need to find out the mount points for
each drive using the following command:
bash
sudo blkid
bash
pi@RASPINAS:~ $ sudo blkid
/dev/mmcblk0p1: LABEL="boot" UUID="CDD4-B453" TYPE="vfat"
/dev/mmcblk0p2: LABEL="rootfs" UUID="72bfc10d-73ec-4d9e-a54a-
/dev/sda1: LABEL="8GB01" UUID="8A44B0F244B0E1DD" TYPE="ntfs"
/dev/sdb1: LABEL="8GB02" UUID="2CF48901F488CE90" TYPE="ntfs"
/dev/sdc1: LABEL="8GB03" UUID="A8CE3E46CE3E0D56" TYPE="ntfs"
/dev/sdd1: LABEL="8GB04" UUID="3AC2CC7DC2CC3F3F" TYPE="ntfs"
/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
bash
/dev/sda1: LABEL="8GB01"
/dev/sdb1: LABEL="8GB02"
/dev/sdc1: LABEL="8GB03"
/dev/sdd1: LABEL="8GB04"
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.
bash
sudo mdadm --create --verbose /dev/md0 --level=stripe --raid
bash
sudo mdadm --create --verbose /dev/md0 --level=stripe --raid
bash
sudo mdadm --create --verbose /dev/md0 --level=mirror --raid
bash
sudo mdadm --create --verbose /dev/md0 --level=4 --raid-devi
If I wanted a RAID-5 array using three drives and a hot spare, I would use the
following code:
bash
sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devi
bash
sudo mdadm --create --verbose /dev/md0 --level=10 --raid-dev
bash
cat /proc/mdstat
Which should produce something like this (replace raid10 with whatever
RAID level you chose):
markup
pi@RASPINAS:~ $ cat /proc/mdstat
Personalities : [raid10]
md0 : active raid10 sdd1[3] sdc1[2] sdb1[1] sda1[0]
15319040 blocks super 1.2 512K chunks 2 near-copies [4/4] [UU
[>....................] resync = 0.4% (61504/15319040) finish
unused devices: <none>
Now we can save our RAID array by entering the following code:
bash
mdadm --detail --scan >> /etc/mdadm/mdadm.conf
It’s also good to confirm it was saved by viewing the mdadm.conf file and
look for something like this at the bottom of the file:
bash
# This configuration was auto-generated on Fri, 19 Jan 2018
ARRAY /dev/md0 metadata=1.2 name=RASPINAS:0 UUID=0d7c55bd:ae5
bash
exit
bash
mkfs.ext4 -v -m .1 -b 4096 -E stride=32,stripe-width=64 /dev
bash
pi@RASPINAS:~ $ sudo mkfs.ext4 -v -m .1 -b 4096 -E stride
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, 38297
First data block=0 Maximum filesystem blocks=2151677952 117 b
32768 fragments per group 8192 inodes per group Filesystem UU
Superblock backups stored on blocks: 32768, 98304, 163840, 22
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information:
pi@RASPINAS:~ $
We also need to mount the file system so we can access it using the
following command:
bash
sudo mount /dev/md0 /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:
bash
sudo blkid
bash
pi@RASPINAS:~ $ sudo blkid
/dev/mmcblk0p1: LABEL="boot" UUID="CDD4-B453" TYPE="vfat"
/dev/mmcblk0p2: LABEL="rootfs" UUID="72bfc10d-73ec-4d9e-a54a-
/dev/mmcblk0: PTUUID="498dcd43" PTTYPE="dos"
/dev/sda1: UUID="0d7c55bd-ae5f-8fbc-0e04-8a1579de5064" UUID_S
/dev/sdb1: UUID="0d7c55bd-ae5f-8fbc-0e04-8a1579de5064" UUID_S
/dev/md0: UUID="394fd8f2-7b2a-474f-8e58-48b81a6ca8fb" TYPE
/dev/sdc1: UUID="0d7c55bd-ae5f-8fbc-0e04-8a1579de5064" UUID_S
/dev/sdd1: UUID="0d7c55bd-ae5f-8fbc-0e04-8a1579de5064" UUID_S
bash
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):
bash
UUID=[your_uuid] /mnt ext4 defaults 0 0
bash
UUID=394fd8f2-7b2a-474f-8e58-48b81a6ca8fb /mnt ext4 defaults
bash
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):
bash
sudo smbpasswd -a pi
bash
pi@RASPINAS:~ $ sudo smbpasswd -a pi
New SMB password:
Retype new SMB password:
Added user pi.
pi@RASPINAS:~ $
Before we start altering the SAMBA config file, let’s make a backup:
bash
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
bash
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:
bash
# NAS Share
[NAS]
path = /mnt
comment = RasPi NAS Share
valid users = pi
writable = yes
browsable = yes
The save Ctrl + O and exit Ctrl + X . Now let’s make sure our changes
work, enter the following:
bash
testparm
Which should product the following (if you want a full output, press Enter
at the prompt):
bash
pi@RASPINAS:~ $ testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows l
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
If the line reads “Loaded services file OK.” then you’re good to go.
bash
sudo /etc/init.d/samba restart
Raspberry Pi (Official)
Linux RAID Wiki
9 commands to check hard disk partitions and disk space on Linux
RAID Pi – Raspberry Pi as a RAID file server
How to Build a Raspberry Pi File Server
Comments
Reply
Richie says
August 14, 2019 at 2:35 pm
Howdy, yes there are a few good tutorials on troubleshooting
RAID arrays with mdadm:
Reply
Nick says
August 27, 2019 at 5:34 am
Reply
Richie says
August 27, 2019 at 4:49 pm
Hi Nick, no worries glad you liked the guide. When you got to
the “Connect to your NAS” step, were you prompted to enter
username and password on Windows? And did you check the
“Remember Credentials” checkbox?
Cheers!
Richie
Reply
Hi!
In the “Setup Drives and RAID Volume” section of the guide, why
choose NTFS as the filesystem to place on the individual drives
when performing the step in Linux? Is there any disadvantage to
choosing ext4?
Reply
Richie says
August 29, 2019 at 10:40 am
cheers!
Rchie
Reply
How is the response time with USB 2.0? Are there any performance
bottlenecks?
Reply
Richie says
September 27, 2019 at 10:17 am
cheers!
Richie
Reply
Lance says
September 16, 2019 at 2:13 am
Reply
djkelly99 says
November 30, 2019 at 11:53 am
Reply
Nathan says
September 27, 2019 at 1:35 am
I’m excited to give this a shot in my own home as I’ve never built a
RAID before. With that said, how would I rebuild this raid in the
event one drive crashes? Is it as easy as plugging in a new blank
drive and the software does it for me? or would I have to rebuild the
project?
Reply
Richie says
September 27, 2019 at 10:14 am
Cheers!
Riche
Reply
Andy says
October 21, 2019 at 9:26 am
Reply
Andy says
October 21, 2019 at 9:27 am
Reply
Richie says
October 21, 2019 at 9:45 am
cheers!
Richie
Reply
Dan says
November 5, 2019 at 6:46 am
Reply
Richie says
November 5, 2019 at 10:42 am
Cheers!
Richie
Reply
Reply
Richie says
November 12, 2019 at 9:46 pm
cheers!
Richie
Reply
Richie says
November 14, 2019 at 1:05 pm
cheers!
Richie
Dan Martin says
November 15, 2019 at 2:01 am
Reply
One further question though … I see you referenced the mount “fail
on boot.” Are USB drives not hot-mountable on the Pi?
Reply
Reply
What I actually did was to find the vendor and product IDs of
the drives by using lsusb. In my case the Seagate drives were
0bc2:2343. So then I used nano to edit the file
/boot/cmdline.txt to add the string in front of what was
already there, save and reboot. Presto! smartctl can now query
the drives.
Reply
Richie says
November 24, 2019 at 12:29 pm
Cheers!
Richie
Reply
Reply
Reply
Dan Martin says
November 20, 2019 at 7:43 am
Reply
Regards,
John
Reply
Reply
Reply
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I
comment.
POST COMMENT