All Articles

Setting up RAID1 with mdadm

I bought myself a relatively cheap USB Sata docking station to replace an dead external USB disk. Since I have some old SATA harddrives lying around I figured I could reuse them and get a lot more storage while at the same time being a lot cheaper. I purposefully choose an offline device instead of a NAS, because I’m paranoid about exposing data on the network. This way I physically control when the device is on or off, and there’s no networking going to a device that most likely has a few security issues.

The device itself fits two sata harddrives, so as per the title this post is about how I set up the RAID-1 array for my Linux to mount this.

When plugging it in to my system I see two new block devices using udiskctl.

$ udisksctl status
MODEL                     REVISION  SERIAL               DEVICE
--------------------------------------------------------------------------
SAMSUNG MZ7LN256HCHP-000L7 EMT03L6Q  S20HNXBG791614       sda     
External USB13.0          0103      201503310007C        sdc     
External USB03.0          0103      201503310007C        sdb

So it seems to just work out of the box. As these /dev/sdb and /dev/sdc allocations can change, it’s better to identify the devices by their ID, which I believe udev assigns them. Again udiskctl is helpful as it can be used to get this symlink:

$ udisksctl info -b /dev/sdb
/org/freedesktop/UDisks2/block_devices/sdb:
  org.freedesktop.UDisks2.Block:
    Configuration:              []
    CryptoBackingDevice:        '/'
    Device:                     /dev/sdb
    DeviceNumber:               2064
    Drive:                      '/org/freedesktop/UDisks2/drives/External_USB03_2e0_201503310007C'
    HintAuto:                   true
    HintIconName:               
    HintIgnore:                 false
    HintName:                   
    HintPartitionable:          true
    HintSymbolicIconName:       
    HintSystem:                 false
    Id:                         by-id-usb-External_USB03.0_201503310007C-0:0
    IdLabel:                    mimikyu:ext
    IdType:                     linux_raid_member
    IdUUID:                     fbccb684-69f0-c9e4-7df7-f51ae9a5a377
    IdUsage:                    raid
    IdVersion:                  1.2
    MDRaid:                     '/'
    MDRaidMember:               '/org/freedesktop/UDisks2/mdraid/fbccb684_69f0c9e4_7df7f51a_e9a5a377'
    PreferredDevice:            /dev/sdb
    ReadOnly:                   false
    Size:                       3000592982016
    Symlinks:                   /dev/disk/by-id/usb-External_USB03.0_201503310007C-0:0
                                /dev/disk/by-path/pci-0000:00:14.0-usb-0:3.1:1.0-scsi-0:0:0:0

To create the RAID-1 array I run the following command. This will create a new virtual device at /dev/md/ext and start to sync the two devices, to ensure they are mirrored.

mdadm --create /dev/md/ext /dev/disk/by-id/usb-External_USB03.0_201503310007C-0:0 /dev/disk/by-id/usb-External_USB13.0_201503310007C-0:1 --level=1 --raid-devices=2

The sync between the two drives will continue happening automatically in the background.
While the sync was happening I formatted the new /dev/md* device in gparted using a just standard ext4 partition.

showing gparted's ui

To check the process of the sync I started, I use mdadm --detail

$ sudo mdadm --detail /dev/md127
/dev/md127:
           Version : 1.2
     Creation Time : Thu May  7 13:00:16 2020
        Raid Level : raid1
        Array Size : 2930134464 (2794.39 GiB 3000.46 GB)
     Used Dev Size : 2930134464 (2794.39 GiB 3000.46 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent

     Intent Bitmap : Internal

       Update Time : Thu May  7 13:24:15 2020
             State : clean, resyncing 
    Active Devices : 2
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 0

Consistency Policy : bitmap

     Resync Status : 6% complete

              Name : mimikyu:ext  (local to host mimikyu)
              UUID : fbccb684:69f0c9e4:7df7f51a:e9a5a377
            Events : 456

    Number   Major   Minor   RaidDevice State
       0       8       16        0      active sync   /dev/sdb
       1       8       32        1      active sync   /dev/sdc

Many hours later, the disks should be ready for use. For my 3TB drives, this took well over 6 hours.

Mounting/Unmounting

To mount the drive I create the following script as mount-external. This reassambles the RAID-1 array and then mounts it to /mnt/external.

#!/bin/bash
sudo mdadm --assemble /dev/md/ext /dev/disk/by-id/usb-External_USB13.0_201503310007C-0:1 /dev/disk/by-id/usb-External_USB03.0_201503310007C-0:0
sudo mkdir -p /mnt/external
while [ ! -e /dev/md/ext1 ]; do sleep 1; done
sudo mount /dev/md/ext1 /mnt/external

Once I’m done working with the device, I use the following unmount-external script to safely disconnect it.

#!/bin/bash
sudo umount /mnt/external
sudo mdadm --manage --stop /dev/md/ext
sudo udisksctl power-off -b /dev/disk/by-id/usb-External_USB13.0_201503310007C-0:1

Troubleshooting

Seeing what disks are connected

udisksctl status

Checking the status of the array (as seen above)

sudo mdadm --detail /dev/md127

To stop an array manually, in case it’s mounted in a weird state:

sudo mdadm --manage /dev/md127 -S

References