Create a RAID mirror on your Raspberry Pi

In a recent article we discussed how you might Recover a Modicum of your Privacy using NextCloud to bring your secrets back in house, under your auspices. Should you decide to do so, you’re going to want to mirror your data to ensure it’s safe. This post will guide you through the process and share some links if you’de like a deeper understanding of the underlying technologies.

Prerequisites

  1. Two identical SSD drives
  2. Initialzed drives with GPartd

Inexpensive storage and USB3 controller

USB3 Dual bay docking station

Inland 120 GB Solid State Drive

Run the blkid command to verify drives are accessible. They should show up as /dev/sda1 and /dev/sdb1

$ sudo blkid

/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="4BBD-D3E7" TYPE="vfat" PARTUUID="738a4d67-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="45e99191-771b-4e12-a526-0779148892cb" TYPE="ext4" PARTUUID="738a4d67-02"
/dev/sda1: UUID="095b9486-fc9d-49df-9ff5-6ee5283c3305" TYPE="ext4" PARTUUID="3a26c570-c21b-4d01-8f4e-9de916a98793"
/dev/sdb1: UUID="968faf3b-80aa-4fa9-95ac-fedcad5f4148" TYPE="ext4" PARTUUID="d3339526-4fe1-4d0c-8062-843b36c26d9f"

Create a redundant RAID-1 mirrored array using the mdadm command below. You may have to first install the command if you haven’t already.

# You may need to install mdadm
$ sudo apt install mdadm -y

# Define the RAID-1 configuration
$ sudo mdadm --create --verbose /dev/md/vol1 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1

# verify the raid array was created correctly
$ sudo mdadm --detail /dev/md/vol1
     
/dev/md/vol1:
           Version : 1.2
     Creation Time : Sat Dec 26 15:37:11 2020
        Raid Level : raid1
        Array Size : 117152768 (111.73 GiB 119.96 GB)
     Used Dev Size : 117152768 (111.73 GiB 119.96 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent
       
     Intent Bitmap : Internal
    
       Update Time : Sat Dec 26 15:38:12 2020
             State : clean, resyncing
    Active Devices : 2
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 0
     
Consistency Policy : bitmap
              
     Resync Status : 5% complete
            
              Name : nextcloudpi:vol1  (local to host nextcloudpi)
              UUID : 796ac3b0:a426c35a:2a2601d2:5564dc2e
            Events : 12 
       
    Number   Major   Minor   RaidDevice State
       0       8        1        0      active sync   /dev/sda1
       1       8       17        1      active sync   /dev/sdb1

Next you will preservce the RAID-1 configuration so that it can be found when you boot. Running the sudo won’t work, so you’ll have to first become the root user.

# the next command will need to be run as root
$ sudo -i

# preserve the RAID-1 config
$ sudo mdadm --detail --scan >> /etc/mdadm/mdadm.conf

# return to normal user using ^D or exit
$ exit

Create the file system

Now it’s time to create the file system, we’ll name it /dev/md/vol1, if you decide to change the name be sure to replace it in the commands that follow.

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

mke2fs 1.44.5 (15-Dec-2018)
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
7323648 inodes, 29288192 blocks
29288 blocks (0.10%) reserved for the super user
First data block=0
Maximum filesystem blocks=2176843776
894 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Filesystem UUID: 04d7502e-dc5d-4c6e-ba1b-a2128f1baabf
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done

At this point you should be able to mount your new file system.

# mount filesystem to access with blkid
$ sudo mount /dev/md/vol1 /mnt

$ ls /mnt

To use your new RAID-1 array you’ll need to add a mount point in /etc/fstab assigning the UUID. Let’s get the ID using blkid. Mine shows up as /dev/md127, yours may be the same of show up as /dev/md/vol1 or another if you renamed it.

$ sudo blkid

/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="4BBD-D3E7" TYPE="vfat" PARTUUID="738a4d67-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="45e99191-771b-4e12-a526-0779148892cb" TYPE="ext4" PARTUUID="738a4d67-02"
/dev/sda1: UUID="796ac3b0-a426-c35a-2a26-01d25564dc2e" UUID_SUB="5b1e44b2-08e2-6fd3-ef02-09ec39c94a4a" LABEL="nextcloudpi:vol1" TYPE="linux_raid_member" PARTUUID="3a26c570-c21b-4d01-8f4e-9de916a98793"
/dev/sdb1: UUID="796ac3b0-a426-c35a-2a26-01d25564dc2e" UUID_SUB="19aef3eb-a8b9-8a46-e94e-0abfce9f50cb" LABEL="nextcloudpi:vol1" TYPE="linux_raid_member" PARTUUID="d3339526-4fe1-4d0c-8062-843b36c26d9f"
/dev/mmcblk0: PTUUID="738a4d67" PTTYPE="dos"
/dev/md127: UUID="04d7502e-dc5d-4c6e-ba1b-a2128f1baabf" TYPE="ext4"

Making your changes permanent

With the UUID you can add the new mount point into /etc/fstab. We’ll backup the file first as a good practice.

# backup original fstab
$ sudo cp /etc/fstab /etc/fstab.bak
$ sudo vi /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):

# i.e. UUID=your_uuid /mnt ext4 defaults 0 0
# My fstab entry would look like this, use your UUID from blkid:
UUID=394fd8f2-7b2a-474f-8e58-48b81a6ca8fb /mnt ext4 defaults 0 0

That’s all there is to it!

References

Standard RAID levels described

Raspberry Pi RAID NAS Server

Nitty gritty command details

Format and partition new drive

Mitch enjoys tinkering with tech across a wide range of disciplines. He loves learning new things and sharing his interests. His work interests run the gamut of: application integration, scalable secure clusters, embedded systems, and user interfaces. After hours you might find him dabbling in the hobby space with Raspberry Pi's, drones, photography, home wine making and other ferments.

Published by Mitch Dresdner

Mitch enjoys tinkering with tech across a wide range of disciplines. He loves learning new things and sharing his interests. His work interests run the gamut of: application integration, scalable secure clusters, embedded systems, and user interfaces. After hours you might find him dabbling in the hobby space with Raspberry Pi's, drones, photography, home wine making and other ferments.

Leave a comment

You can comment using your social media account

%d bloggers like this: