I'm working on a script that automatically detects new drives that have been added so they can be formatted and turned into a RAID-5 array. I've been able to detect the drives and format them with little trouble, but am having issues with creating (and later destroying) the RAID device.

Once detected and formatted, I pass the detected drives to the following commands like so:

mdadm --create ${RAID_DEVICE} --level=5 --raid-devices=${COUNT} ${DEVICES}
mkfs.ext4 ${RAID_DEVICE}
mdadm --detail --scan >> /etc/mdadm.conf

UUID=$(cat /etc/mdadm.conf | grep ${RAID_DEVICE} | awk '{print $6}')
UUID=$(echo ${UUID:5})

echo "UUID=${UUID} ${MOUNT_LOCATION} ext4 defaults 0 0" >> /etc/fstab

mount -a

Now, for testing purposes, because I don't currently have the SATA drives I will be using for this project yet, I've set it to detect USB devices specifically, and am using 3 USB devices. The script correctly identifies the UUID of the device from mdadm.conf, but when running mount -a from the script, it says that the UUID is not recognized as a device. However, when I replace the UUID with ${RAID_DEVICE}, usually /dev/md0, it can correctly mount the device.

Also, the install script (after testing the script several times and manually deconstructing and uninstalling the RAID device), says that the USB devices are already part of a RAID device. I manually tried to undo the device between tests by running the following commands:

mdadm ${RAID_DEVICE} --fail ${DEVICES}
mdadm ${RAID_DEVICE} --remove ${DEVICES}
mdadm --stop ${RAID_DEVICE}
mdadm --zero-superblock ${DEVICES}

However, when recreating the RAID device, it seems to still see the devices as part of the md0 RAID device. Did I miss a step? Trying to call --remove on /dev/md0 after stopping it results in it saying there's no such directory.

So why does the UUID not work correctly as an identifier for the RAID device? And why can I not seem to properly remove the RAID information from the drives afterwards? Does it have anything to do with them being USB devices?

I appreciate any guidance towards the proper usage of the mdadm tool.


The UUID that you want to put in fstab is not the UUID of the array, but the UUID of the filesystem you created on the array. You can use the blkid utility to find its value. If you've got the same udev rules I have, you may find that there's an entry in /dev/disk/by-id that you can indirect through using the array's uuid, like:

$ sudo blkid /dev/disk/by-id/md-uuid-${UUID}
/dev/disk/by-id/md-uuid-c6a65df0:6df343e6:bd3d6cfc:1349c37c: UUID="901e8907-8f74-4300-8410-654a9410b236" TYPE="xfs"

Otherwise, you can just run blkid ${RAID_DEVICE}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.