mdadm Command: Tutorial & Examples

Managing RAID Devices

mdadm stands for Multiple Device Administration. It is a powerful command-line tool used in Linux to manage software RAID arrays. RAID, which stands for Redundant Array of Independent Disks, is a technique used to combine multiple physical storage devices into a logical unit for improved performance, data redundancy, or both. With mdadm , you can create, monitor, and manage RAID arrays, ensuring data integrity and availability.

Understanding RAID Arrays

Before we dive into mdadm, let's briefly understand RAID arrays. A RAID array consists of two or more physical disks combined together to form a single logical unit. The data is distributed across these disks using different strategies known as RAID levels, such as RAID 0, RAID 1, RAID 5, RAID 6, and more.

  • RAID 0: Provides increased performance by striping data across multiple disks, but offers no redundancy.
  • RAID 1: Offers data redundancy by mirroring the data on multiple disks, providing fault tolerance.
  • RAID 5: Combines striping and parity for enhanced performance and redundancy.
  • RAID 6: Similar to RAID 5 but with double parity, offering increased fault tolerance.

Why is mdadm Important?

mdadm is essential for managing and maintaining RAID arrays in Linux. It allows you to create, assemble, and monitor RAID configurations, ensuring the stability and reliability of your storage infrastructure. Whether you're setting up a file server, a database server, or a web server, mdadm comes to the rescue when it comes to managing your RAID arrays efficiently.

With mdadm, you can perform various operations, including:

  • Creating new RAID arrays
  • Adding or removing disks from an existing array
  • Monitoring the status and health of RAID devices
  • Rebuilding failed or replaced disks
  • Reshaping the array for capacity or performance changes
  • Handling RAID failover and recovery
  • And much more!

Usage Examples

Now, let's explore some practical examples to understand how to use mdadm effectively.

Example 1: Creating a RAID 1 Array

To create a RAID 1 array, which provides data redundancy by mirroring, you can use the following command:

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

In this example, we are creating a new RAID 1 array named /dev/md0 with two devices (/dev/sdb1 and /dev/sdc1).

Example 2: Monitoring RAID Devices

You can use mdadm to monitor the status and health of your RAID devices. Use the following command to display the detailed information of all RAID arrays:

mdadm --detail --scan

This command provides a comprehensive overview of your RAID devices, including their current status, disk health, and any failures or inconsistencies.

Example 3: Rebuilding a Failed Disk

When a disk in your RAID array fails, you need to replace it and rebuild the array. Assuming /dev/sdb1 has failed and has been replaced with a new disk, you can rebuild it using the following command:

mdadm --manage /dev/md0 --add /dev/sdb1

This command instructs mdadm to add /dev/sdb1 back to the RAID array /dev/md0 for the rebuilding process.

Example 4: Scan Devices and Start RAID

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

mdadm --assemble --scan

Example 5: Resizing a RAID array and adding a partition

This only works when the RAID is not in use. You need to start your server or VM in recovery mode to perform these steps. First, 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 disks resyncing:

cat /proc/mdstat

Create a new raid 1:

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

Example 6: Delete Raid volume

umount /mnt/raidvolume
mdadm --stop /dev/md0
mdadm --zero-superblock /dev/sda4

After that you can remove /mnt/raidvolume in /etc/fstab. Also remove raidvolume in /etc/mdadm/mdadm.conf.

Conclusion

mdadm is a powerful command-line tool for managing software RAID arrays in Linux. It allows you to create, monitor, and maintain RAID configurations, providing improved performance, fault tolerance, and data redundancy. By mastering mdadm, you can ensure the stability and reliability of your storage infrastructure, protecting your valuable data from loss and maximizing the efficiency of your server environment.

Remember, RAID arrays are an essential part of building robust server setups, and mdadm is here to make managing them a breeze.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA