# Raspberry Pi RAID NAS Server Setup

## Prerequisite

* Raspberry Pi 4 (4+GB RAM)
    
* Micro SD card 64GB
    
* 2x Integral USB 3.1 flash drives 128GB Or 2x USB 3.1 HDDs 1024GB
    
* **OS:** - Raspbian Bullseye
    

## RAID Setup

#### **FORMAT DRIVES**

1. Insert drive and list existing partition tables :
    
    ```bash
    sudo fdisk -l
    ```
    
2. Unmount drive (if needed) :
    
    ```bash
    sudo unmount /media/pi/<HARD-DRIVE-LABEL>
    ```
    
3. Partitioning :
    
    ```bash
    sudo fdisk /dev/sda
    ```
    
4. Press the following Keys when prompted :
    
    \[m\] for help  
    \[o\]  
    \[n\]  
    \[p\]  
    \[1\]  
    \[Enter\]  
    \[Enter\]  
    \[w\]
    
5. Formatting :
    
    ```bash
    sudo mkfs.ext4 /dev/sda1
    ```
    
    Press \[y\] when prompted.
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">DO NOT MOUNT THE DRIVE</div>
    </div>
    
6. Repeat steps 1-5 above for other drives.
    

---

#### **CREATE RAID ARRAY**

* Update the system and install 'mdadm' RAID package :
    
    ```bash
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install mdadm
    ```
    
* Find out the mount points for each drive :
    
    ```bash
    blkid
    ```
    
    OR
    
    ```bash
    lsblk
    ```
    
* Create RAID volume/array :
    
    ### **Linear Mode**
    
    ```bash
    sudo mdadm --create --verbose /dev/md0 --level=linear --raid-devices=2 /dev/sda1 /dev/sdb1
    ```
    
    ### **RAID-0 (Stripe Mode)**
    
    ```bash
    sudo mdadm --create --verbose /dev/md0 --level=stripe --raid-devices=2 /dev/sda1 /dev/sdb1
    ```
    
    **OR**
    
    ```bash
    sudo mdadm -Cv /dev/md0 -l0 -n2 /dev/sd[ab]1
    ```
    
    ### **RAID-1 (Mirror Mode)**
    
    ```bash
    sudo mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 /dev/sda1 /dev/sdb1
    ```
    
    **OR**
    
    ```bash
    sudo mdadm -Cv /dev/md0 -l1 -n2 /dev/sd[ab]1
    ```
    
    ### **RAID-4/5/6**
    
    ```bash
    sudo mdadm --create --verbose /dev/md0 --level=4 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1
    ```
    
    ```bash
    sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1 --spare-devices=1 /dev/sde1
    ```
    
    ```bash
    sudo mdadm --create --verbose /dev/md0 --level=6 --raid-devices=4 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
    ```
    
    ### **RAID-10**
    
    ```bash
    sudo mdadm --create --verbose /dev/md0 --level=10 --raid-devices=4 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1
    ```
    
* Confirm RAID array :
    
    ```bash
    cat /proc/mdstat
    ```
    
* Save RAID array :
    
    ```bash
    sudo -i  
    mdadm --detail --scan >> /etc/mdadm/mdadm.conf  
    less /etc/mdadm/mdadm.conf  
    exit
    ```
    
* Create file system :
    
    ```bash
    sudo mkfs.ext4 -v -m .1 -b 4096 -E stride=32,stripe-width=64 /dev/md0
    ```
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">NOTE: To change advanced parameters after creation if needed :</div>
    </div>
    
    ```bash
    tune2fs -E stride=n,stripe-width=m /dev/md0
    ```
    
* Create a mount point and mount file system :
    
    ```bash
    sudo mkdir /mnt/raidx  
    sudo mount /dev/md0 /mnt/raidx
    ```
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">NOTE: To change the owner of the mount point to pi :</div>
    </div>
    
    ```bash
    sudo chown pi:pi /mnt/raidx
    ```
    
* Check the content of the mounted file system :
    
    ```bash
    ls -la /mnt/raidx
    ```
    
* Confirm its capacity :
    
    ```bash
    df -h -x devtmpfs -x tmpfs
    ```
    

---

* Update the initial file system (Raspberry Pi uses a RAM disk image when booting up and we want to include our array) :
    
    ```bash
    sudo update-initramfs -u
    ```
    
* Check the UUID of the mounted file system :
    
    ```bash
    blkid
    ```
    
    OR
    
    ```bash
    ls -l /dev/disk/by-uuid
    ```
    
* Add to fstab (make the drive permanent and auto mount drive at boot) :
    
    ```bash
    sudo nano /etc/fstab
    ```
    
    Enter a new line before the bottom comments and add :
    
    ```bash
    UUID=(my_uuid) /mnt/raidx ext4 defaults,noatime 0 0
    ```
    
* Save and exit :
    
    \[Ctrl+O\]  
    \[Ctrl+X\]
    
* Retrieve drive parameters and test speeds (optional) :
    
    ```bash
    sudo hdparm -I /dev/md0
    sudo hdparm -tT --direct /dev/md0
    ```
    
* Reboot
    
    ```bash
    sudo reboot
    ```
    

---

### Useful commands/Manage Mode

`$ cat /proc/mdstat`: show the status of all RAID devices  
`$ mdadm --detail /dev/md0` : detailed information about RAID md0 (`mdadm -D`)  
`$ mdadm --detail --brief /dev/md0` : for shortened/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**

* Install SAMBA
    
    ```bash
    sudo apt-get install samba samba-common-bin
    ```
    
* Set up SAMBA password (for user pi) :
    
    ```bash
    sudo smbpasswd -a pi
    ```
    
* Edit SAMBA config file :
    
    ```bash
    sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak  
    sudo nano /etc/samba/smb.conf
    ```
    
    Scroll down to the bottom of the file and add the following code to create NAS file share :
    
    ```bash
    # 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\]
    

---

* Check the configuration file for internal correctness :
    
    ```bash
    testparm
    ```
    
* Restart SAMBA service :
    
    ```bash
    sudo /etc/init.d/samba restart
    ```
    
    OR
    
    ```bash
    sudo service smbd restart
    ```
    
* Reboot
    
    ```bash
    sudo reboot
    ```
    

---

## **USAGE ON MAC**

* Open Finder
    
* Menu "Go" ⟶ "Connect to Server..."
    

Address: \[smb://rpi4/NAS\]

* \[Connect\]
    
* Connect As:
    
* Registered User
    

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

* \[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](https://linux.die.net/man/8/mdadm)  
⇒ [How To Manage RAID Arrays with mdadm](https://www.digitalocean.com/community/tutorials/how-to-manage-raid-arrays-with-mdadm-on-ubuntu-16-04)  
⇒ [Advantages and disadvantages of various RAID levels](https://datapacket.com/blog/advantages-disadvantages-various-raid-levels/)  
⇒ [RAID setup](https://raid.wiki.kernel.org/index.php/RAID_setup)  
⇒ [A guide to mdadm](https://raid.wiki.kernel.org/index.php/A_guide_to_mdadm)  
⇒ [Characteristics of Linux RAID levels](https://linux.die.net/EVMSUG/characraidlvls.html)  
⇒ [Build your own Raspberry Pi NAS](https://pimylifeup.com/raspberry-pi-nas-3/)  
⇒ [How to set up a Raspberry Pi Samba Server](https://pimylifeup.com/raspberry-pi-samba/)  
⇒ [Build a Raspberry Pi RAID NAS Server – Complete DIY Guide](https://pchelp.ricmedia.com/build-raspberry-pi3-raid-nas-server/)  
⇒ [Partitioning, Formatting, and Mounting a Hard Drive in Linux](https://medium.com/@sh.tsang/partitioning-formatting-and-mounting-a-hard-drive-in-linux-ubuntu-18-04-324b7634d1e0)
