Skip to content

Instantly share code, notes, and snippets.

@leandrofilipe
Last active March 31, 2024 23:26
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save leandrofilipe/f9636be272f97d414652ce1f21e6b1f4 to your computer and use it in GitHub Desktop.
Save leandrofilipe/f9636be272f97d414652ce1f21e6b1f4 to your computer and use it in GitHub Desktop.
Raspberry Pi RAID NAS Server Setup

※ Raspberry Pi RAID NAS Server Setup ※

Hardware:

  • Raspberry Pi 4 (4GB RAM)
  • Micro SD card 32GB
  • 2x Integral USB 3.1 flash drives 128GB

OS:

  • Raspbian Buster

FORMAT DRIVES

  1. Insert drive and list existing partition tables:

    $ sudo fdisk -l
  2. Unmount drive (if needed):

    $ sudo unmount /media/pi/<HARD-DRIVE-LABEL>
  3. Partitioning:

    $ sudo fdisk /dev/sda

    [m] for help
    [o]
    [n]
    [p]
    [1]
    [Enter]
    [Enter]
    [w]

  4. Formatting:

    $ sudo mkfs.ext4 /dev/sda1

    [y]

    *** DO NOT MOUNT THE DRIVE ***

  5. Repeat steps 1-4 above for other drives.


CREATE RAID ARRAY

  1. Update system and install 'mdadm' RAID package:

    $ sudo apt-get update
    $ sudo apt-get upgrade
    $ sudo apt-get install mdadm
  2. Find out the mount points for each drive:

    $ blkid

    -or-

    $ lsblk
  3. Create RAID volume/array:

    Linear Mode

    $ sudo mdadm --create --verbose /dev/md0 --level=linear --raid-devices=2 /dev/sda1 /dev/sdb1

    RAID-0 (Stripe Mode)

    $ sudo mdadm --create --verbose /dev/md0 --level=stripe --raid-devices=2 /dev/sda1 /dev/sdb1

    -or-

    $ sudo mdadm -Cv /dev/md0 -l0 -n2 /dev/sd[ab]1

    RAID-1 (Mirror Mode)

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

    -or-

    $ sudo mdadm -Cv /dev/md0 -l1 -n2 /dev/sd[ab]1

    RAID-4/5/6

    $ sudo mdadm --create --verbose /dev/md0 --level=4 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1
    $ sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1 --spare-devices=1 /dev/sde1
    $ sudo mdadm --create --verbose /dev/md0 --level=6 --raid-devices=4 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1

    RAID-10

    $ sudo mdadm --create --verbose /dev/md0 --level=10 --raid-devices=4 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1
  4. Confirm RAID array:

    $ cat /proc/mdstat
  5. Save RAID array:

    $ sudo -i  
    $ mdadm --detail --scan >> /etc/mdadm/mdadm.conf  
    $ less /etc/mdadm/mdadm.conf  
    $ exit
  6. Create file system:

    $ sudo mkfs.ext4 -v -m .1 -b 4096 -E stride=32,stripe-width=64 /dev/md0

    NOTE: To change advanced parameters after creation if needed:

    $ tune2fs -E stride=n,stripe-width=m /dev/md0
  7. Create mount point and mount file system:

    $ sudo mkdir /mnt/raidx  
    $ sudo mount /dev/md0 /mnt/raidx

    NOTE: To change the owner of the mount point to pi:

    $ sudo chown pi:pi /mnt/raidx
  8. Check content of the mounted file system:

    $ ls -la /mnt/raidx
  9. Confirm its capacity:

    $ df -h -x devtmpfs -x tmpfs
  10. Update the initial file system (Raspberry Pi uses a RAM disk image when booting up and we want to include our array):

    $ sudo update-initramfs -u
  11. Check UUID of mounted file system:

    $ blkid

    -or-

    $ ls -l /dev/disk/by-uuid
  12. Add to fstab (make the drive permanent and auto mount drive at boot):

    $ sudo nano /etc/fstab

    Enter new line before the bottom comments and add:

    UUID=(my_uuid) /mnt/raidx ext4 defaults,noatime 0 0  
    

    Save and exit:
    [Ctrl+O]
    [Ctrl+X]

  13. Retrieve drive parameters and test speeds (optional):

    $ sudo hdparm -I /dev/md0
    $ sudo hdparm -tT --direct /dev/md0
  14. Reboot:

    $ sudo reboot
  15. Useful commands/Manage Mode:

    $ cat /proc/mdstat : show status of all RAID devices
    $ mdadm --detail /dev/md0 : detailed information about RAID md0 (mdadm -D)
    $ mdadm --detail --brief /dev/md0 : for shorthened/brief details (mdadm -Db)
    $ mdadm --query /dev/md0 : quick human-readable summary of RAID md0 (mdadm -Q)
    $ mdadm --examine /dev/sdx : information about RAID component device sdx (mdadm -E)
    $ mdadm --stop /dev/md0 : stop RAID device md0
    $ mdadm --assemble --scan : restart/assemble RAID device


SET UP NAS

  1. Install SAMBA:

    $ sudo apt-get install samba samba-common-bin
  2. Set up SAMBA password (for user pi):

    $ sudo smbpasswd -a pi
  3. Edit SAMBA config file:

    $ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak  
    $ sudo nano /etc/samba/smb.conf

    Scroll all the way down to the bottom of the file and add the following code to create NAS file share:

    # NAS Share Block  
    [NAS]  
    path = /mnt/raidx  
    comment = RPI4 RAID0 NAS Server  
    volume = NAS-Server  
    valid users = pi  
    read only = NO  
    guest ok = NO  
    public = NO  
    writable = YES  
    browsable = YES  
    ### -rwxr--r--  
    create mask = 0744  
    ### -rwxr-xr-x  
    directory mask = 0755  
    ### All hosts on the 192.168.142 subnet allowed:  
    hosts allow = 192.168.142.  
    

    Save and exit:
    [Ctrl+O]
    [Ctrl+X]

  4. Check configuration file for internal correctness:

    $ testparm
  5. Restart SAMBA service:

    $ sudo /etc/init.d/samba restart

    -or-

    $ sudo service smbd restart
  6. Reboot ?

    $ sudo reboot

USAGE ON MAC

  1. Open Finder

  2. Menu "Go" ⟶ "Connect to Server..."

    Address: [smb://rpi4/NAS]

  3. [Connect]

  4. Connect As:

    • Registered User

    Name: pi
    Password: ●●●●●●●●

  5. [Connect]


APPENDIX

Commands to check

$ rsync options source destination : remote/local file-copying tool
$ rsync -ahv /mnt/u1/ /mnt/u2/ : example of the above

RAID Levels

Number of Drives /
RAID Level Availability:

2 /
RAID-0 Stripe (Fastest, but no redundancy)
RAID-1 Mirror (Excellent redundancy, good speed)

3 /
RAID-0 Stripe (Fastest, but no redundancy)
RAID-4 Dedicated parity disk (Good speed & redundancy)
RAID-5 Block-level striping with distributed parity (Excellent speed & redundancy)

4 /
RAID-6 Block-level striping with two parity blocks distributed across all member disks (Excellent speed & redundancy)
RAID 10 (nested RAID 1+0) (Excellent speed and redundancy)

References

mdadm(8) - Linux man page
How To Manage RAID Arrays with mdadm
Advantages and disadvantages of various RAID levels
RAID setup
A guide to mdadm
Characteristics of Linux RAID levels
Build your own Raspberry Pi NAS
How to Setup a Raspberry Pi Samba Server
Build a Raspberry Pi RAID NAS Server – Complete DIY Guide
Partitioning, Formatting, and Mounting a Hard Drive in Linux

@gerardo-junior
Copy link

sometimes the raspberry (especially the old models) restarts the usb controllers and degrading the disks. what he decided for me was to create a cronjob every 30 minutes to add the disks again

*/30 * * * * bash -c 'ls /dev/ | grep sd | while read s; do mdadm -a /dev/md0 /dev/$s; done'

@DiomedesDominguez
Copy link

sometimes the raspberry (especially the old models) restarts the usb controllers and degrading the disks. what he decided for me was to create a cronjob every 30 minutes to add the disks again

*/30 * * * * bash -c 'ls /dev/ | grep sd | while read s; do mdadm -a /dev/md0 /dev/$s; done'

does this behavior continues in RPI4?

@mpkonmbk
Copy link

mpkonmbk commented May 7, 2022

@DiomedesDominguez FWIW I haven't observed such behaviour on my RPi4

@WallbrownF
Copy link

I am currently trying to utilize this walkthrough to setup a raid 5 array for Open Media Vault and I don't know if this helpful, but when I go to partition my disks (section: FORMAT DRIVES; subsection 3.) and I'm using fdisk... I notice that when I use the command "sudo fdisk /dev/sdX" (where "X" is the appropriate drive letter), I get the message:

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x########.

Now, this step is to create a new empty DOS partition table, right? But, it is already creating a new one automatically for me (and I'm assuming others) when it doesn't recognize a DOS partition table, correct? So, then individuals don't actually need to do this step if it is a new drive?

I feel like an expansion of the these steps to elaborate what each command is and the optional steps (if they are optional), would be appropriate here, assuming I am understanding what is going on here.

Either way, thank you for the walkthrough and I appreciate your work to help people with this...

@36thchambersoftware
Copy link

How do you get around the usb drives changing from /dev/sdx to /dev/sdy on reboots?

@DiomedesDominguez
Copy link

DiomedesDominguez commented Mar 19, 2024

How do you get around the usb drives changing from /dev/sdx to /dev/sdy on reboots?

Steps 11 & 12. Also, try not to change the drives from ports, for example, I left this setup for 3 months inside a desk drawer without problems drives changing letters, even though I had several power outage.

@36thchambersoftware
Copy link

Steps 11 & 12

Hm. Strange. I had one power outage and a couple drives moved, even though I added the raid10 to the fstab like your instructions show. I didn't unplug anything, that's just how they changed at bootup. I ended up with /dev/sde and /dev/sdf. Then I rebooted and they went back to the correct letters but the raid was broken.

@DiomedesDominguez
Copy link

Steps 11 & 12

Hm. Strange. I had one power outage and a couple drives moved, even though I added the raid10 to the fstab like your instructions show. I didn't unplug anything, that's just how they changed at bootup. I ended up with /dev/sde and /dev/sdf. Then I rebooted and they went back to the correct letters but the raid was broken.

Have you used the UUID or the direct reference to /dev/md0? I had a similar case and it was that I used /dev/md0 instead of the UUID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment