ThorneLabs

md and mdadm Software RAID Usage Scenarios

• Updated March 17, 2019


md, which stands for multiple device driver, is used to enable software RAID on Linux distributions. I personally used it many years ago, and it even saved my data once. It is managed using the mdadm command.

The following post describes various scenarios I have used md for.

It should go without saying that many of the commands in this post are destructive and could result in data loss. It is your responsibility to make sure your data is backed up.

Rename md Devices

Renaming md arrays can be useful when moving an already existing md array to a system which already has md arrays with the same name.

Rename /dev/md0 (comprised of /dev/sda1 and /dev/sdb1) to /dev/md6:

umount /dev/md0

mdadm --stop /dev/md0

mdadm --assemble /dev/md6 --name=md6 --update=name /dev/sdb1 /dev/sdc1

If you reboot and find that your md array is still being named different from what you specified, you might need to run update-initramfs -u.

Create a md RAID0 Array

Create a md RAID0 array, named /dev/md0, with 8 devices:

mdadm --create --verbose --raid-devices=8 --level=raid0 --chunk=64 /dev/md0 /dev/nvme0n1 /dev/nvme0n2 /dev/nvme0n3 /dev/nvme0n4 /dev/nvme0n5 /dev/nvme0n6 /dev/nvme0n7 /dev/nvme0n8

Format the new RAID0 array:

mkfs.ext4 -m 0 /dev/md0

Create a md Linear Array and Add a Disk

Create a md linear array comprised of two disks with different sizes. Then, add a third disk, also with a different size from the other two disks.

Partition the first disk:

parted /dev/sdb mklabel gpt
parted /dev/sdb mkpart primary 0% 100%

Partition the second disk:

parted /dev/sdc mklabel gpt
parted /dev/sdc mkpart primary 0% 100%

Create a md linear array:

mdadm --create --verbose --level=linear /dev/md0 --raid-devices=2 /dev/sdb1 /dev/sdc1

Format the new md linear array:

mkfs.ext4 -m 0 /dev/md0

Partition the third disk:

parted /dev/sdd mklabel gpt
parted /dev/sdd mkpart primary 0% 100%

Add the third disk to the md linear array:

mdadm --grow /dev/md0 --add /dev/sdd1

Grow the filesystem on the linear md array to use the new space:

resize2fs /dev/md0

Output md’s configuration, and store in mdadm.conf:

mdadm --examine --scan >> /etc/mdadm.conf

Create a md RAID1 Array with a Missing Disk

RAID1 arrays typically consist of an even number of disks. However, if you have data on an existing disk that you need copied to the new RAID1 array but also need to use that same disk in the RAID1 array and do not have another disk to temporarily move the data to, you can create a new RAID1 array with a missing disk, copy the data, and then add the second disk to the RAID0 array.

Start by partitioning the first disk:

parted /dev/sdb mklabel gpt
parted /dev/sdb mkpart primary 0% 100%

Create the md RAID1 array with a missing disk:

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 /dev/sdb1 missing

Once the RAID1 array is created, mount the array:

mount /dev/md0 /mnt/raid1

Copy data from second disk to newly created RAID1 array:

cp -r /mnt/second-disk /mnt/raid1/

Once the data finishes copying, unmount the second disk:

umount /mnt/second-disk

Partition the second disk:

parted /dev/sdc mklabel gpt
parted /dev/sdc mkpart primary 0% 100%

Add the second disk to the RAID1 array:

mdadm --manage /dev/md0 --add /dev/sdc1

md will begin replicating data from the first disk to the second disk, and, depending on the size of the disks, you will eventually have a healthy RAID1 array.

Dismantle a md Software RAID Array

To dismantle a healthy RAID array, use the following steps to erase all traces of the RAID array from each disk.

Figure out the md device ID and what disks are part of the RAID array using the following command:

cat /proc/mdstat

Figure out what directory the RAID array is mounted to using the df -h or mount commands.

If mounted, unmount the RAID array:

umount /mnt/raid

Remove the RAID array’s mount entry in /etc/fstab.

Remove the RAID array’s entry in /etc/mdadm.conf.

Stop the RAID Array (the md device ID will probably be different for you):

mdadm --stop /dev/md127

Zero the superblocks of each disk in the RAID array (there may be more disks to zero depending on the type of RAID array):

mdadm --zero-superblock /dev/sdb1

mdadm --zero-superblock /dev/sdc1

Now you can repartition, format, and use each disk independently.

References

If you found this post useful and would like to help support this site - and get something for yourself - sign up for any of the services listed below through the provided affiliate links. I will receive a referral payment from any of the services you sign-up for.

Get faster shipping and more with Amazon Prime: About to order something from Amazon but want to get more value out of the money you would normally pay for shipping? Sign-up for a free 30-day trial of Amazon Prime to get free two-day shipping, access to thousands of movies and TV shows, and more.

Thanks for reading and take care.