mdadm: Tutorial & Best Practices

How to set up and configure RAID devices

mdadm is a Linux utility that allows you to manage multiple disks as a single logical storage device using RAID (Redundant Array of Independent Disks) technology. RAID allows you to improve the performance, reliability, and capacity of your storage system by combining multiple disks into a single entity.

To use mdadm to configure RAID devices in Linux, you need to have at least two disks available and the mdadm utility installed on your system. Here is an overview of the basic steps for using mdadm to configure a RAID device:

Create a partition on each disk using a partitioning tool like fdisk or gdisk:

fdisk /dev/sda
fdisk /dev/sdb

Create a RAID array using the mdadm command:

mdadm --create /dev/md0 --level=raid1 --raid-devices=2 /dev/sda1 /dev/sdb1

This creates a RAID 1 array called "md0" using the first partitions on both disks. The "--level=raid1" option specifies that the array will use mirroring, which means that data will be written to both disks for redundancy.

Format the RAID array with a filesystem:

mkfs.ext4 /dev/md0

This formats the RAID array with the ext4 filesystem. You can use a different filesystem depending on your needs.

Mount the RAID array:

mount /dev/md0 /mnt

This mounts the RAID array at the "/mnt" mount point.

Add the RAID array to the system's list of devices:

echo "/dev/md0 /mnt ext4 defaults 0 0" >> /etc/fstab

This adds an entry to the "/etc/fstab" file so that the RAID array is automatically mounted at boot time.

Scan Devices and Start Raid

This helps when not /dev/md0 has been found:

mdadm --assemble --scan

Check the device for errors:

e2fsck -f /dev/md2

Resize the file system and make it a little smaller than necessary:

resize2fs /dev/md2 25G

Resize the Raid device:

mdadm --grow /dev/md2 --size=33554432

Resize the file system to the maximum:

resize2fs /dev/md2

Check the file system again:

e2fsck -f /dev/md2

Remove one partition from the Raid:

mdadm /dev/md2 --fail /dev/sdb4

Stop the Raid array:

mdadm --stop /dev/md2

Resize the partitions using fdisk or gdisk. Run partprobe to refresh the partition table to the kernel. Add the partition to the raid array:

mdadm --zero-superblock /dev/sdb4
mdadm -a /dev/md2 /dev/sdb4

Watch the resync:

cat /proc/mdstat

Create a new raid 1:

mdadm --create --verbose /dev/md3 --level=mirror --raid-devices=2 /dev/sda5 /dev/sdb5

Delete Raid volume

umount /mnt/raidvolume
mdadm --stop /dev/md0
mdadm --zero-superblock /dev/sda4
# remove /mnt/raidvolume in /etc/fstab
# remove raidvolume in /etc/mdadm/mdadm.conf

There are many other options and parameters available with mdadm, so you can customize the configuration of your RAID array to suit your specific needs.

For more information, you can refer to the mdadm documentation or use the man mdadm command to view the manual pages.