Sometimes you like to just quickly create a linux raid but don’t care too much about the fine tuning. One of such use cases might be when you try to run performance tests on a number of possible raid setups but don’t want to go through the motions every time manually. The following script does exactly do that.
#!/bin/bash
DEVICES="/dev/sdb /dev/sdc /dev/sdd /dev/sde"
NO_OF_DEVICES=4
LEVEL="10"
NO_OF_DATA_DEVICES=2
ARRAY=/dev/md0
CHUNKS="512"
MOUNT=/tmpraid
echo "==============================="
echo "* RAID $LEVEL - Chunk $CHUNKS"
echo "* about to unmount existing file system at $MOUNT"
umount $MOUNT >> /dev/null 2>&1
echo "* about to stop array $ARRAY"
mdadm -S $ARRAY >> /dev/null 2>&1
echo "* about to zero all MDADM devices $DEVICES"
mdadm --zero $DEVICES >> /dev/null 2>&1
echo "* about to create new array $ARRAY"
yes | mdadm --create $ARRAY --assume-clean --level $LEVEL --chunk $CHUNKS --name myRaid${LEVEL}-${CHUNKS}k --raid-devices $NO_OF_DEVICES $DEVICES >> /dev/null 2>&1
echo "* about to create xfs file system on array $ARRAY"
mkfs.xfs $ARRAY -f -d su=${CHUNKS}k -d sw=$NO_OF_DATA_DEVICES 2>&1 >> /dev/null
echo "* about to mount file system at $MOUNT"
mount $ARRAY $MOUNT
echo "===============================\n"
mdadm --query --detail $ARRAY
Running the script requires superuser privileges. At the top of the script there are a number of variables that can be used for configuration. Important to note here is that depending on the raid level the number of data devices will vary. In the example above the raid 10 configuration has 4 devices. Given how raid 10 works (half the devices are data devices), there are 2 data devices in that case. However, in a raid 5 setup there is one less data device than the number of devices. In case of setting up a raid 5 with 4 devices the configuration ought to look like the following.
NO_OF_DEVICES=4 LEVEL="5" NO_OF_DATA_DEVICES=3
