USB Raid

Alex, December 7, 2015 08:57
At home I have a NAS to store photos, videos, documents and stuff like that. It runs on a VIA motherboard and has 4 1TB drives in a RAID5 configuration. These disks spin down after a while to reduce power consumption. As a side effect it takes 30+ seconds to spin up before a write. That’s why I added a USB memory stick for quick and temporary writes. A couple of days ago this device went into read only mode.
lost page write due to I/O error on sdg1
I was able to copy all data and now it has completely died on me.

USB RAID1 Stick

To prevent this from happening again, I am now setting up 2 USB memory sticks in a RAID-1 configuration. If one would fail, I would simply replace it and let the data sync again. To start I added a USB hub and inserted two 8GB USB memory sticks from two different vendors. When creating a new partition table I noticed the two drives are both labeled 8GB but have small difference in total available sectors. To be on the safe side I made both partitions the size of the smallest USB stick minus 1 megabytes in case a new USB stick would be even smaller.
DeviceBootStartEndSectorsSizeIdType
/dev/sdg1204815500000154979537.4G83Linux
/dev/sdh1204815500000154979537.4G83Linux

Creating a new RAID device

We will be creating a (--level) RAID 1 device, using (--raid-devices) 2 devices and we will use this raid device as /dev/md1 (/dev/md0 already in use as the main NAS storage mentioned above)
# mdadm --create --verbose /dev/md1 --level=1 --raid-devices=2 /dev/sdg1 /dev/sdh1
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
mdadm: size set to 7744832K
Continue creating array? Y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.

Encryption

Encryption is still not the default. I want encryption on my storage and will use LUKS for this /dev/md1 device.
# cryptsetup luksFormat  /dev/md1
WARNING!
========
This will overwrite data on /dev/md1 irrevocably.
Are you sure? (Type uppercase yes): YES
Enter passphrase: ***********************
Verify passphrase: ***********************
# cryptsetup luksOpen /dev/md1 usb

Using encrypted USB RAID device

# mkfs.ext4 /dev/mapper/usb
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 1935696 4k blocks and 484800 inodes
Filesystem UUID: 56fff3c3-a1f1-4171-b399-2cd81654f422
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

Everyday use

Now we have set up everything, we can use this device with the following commands. This is of course required after every reboot.
cryptsetup luksOpen /dev/md1 usb
mount /dev/mapper/usb /mnt/usb

Future proof

Now when one USB device fails, I can remove it, replace it with a new one, rebuild the array and I should be good to go.