Linux mdadm Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux mdadm command to create, manage, and monitor software RAID arrays. Software RAID is a cost-effective and flexible alternative to hardware RAID, allowing you to configure various RAID levels without the need for specialized hardware. You will first understand the concept of software RAID and how it differs from hardware RAID. Then, you will create a RAID 5 array using the mdadm command and virtual block devices. Finally, you will learn how to manage and monitor the software RAID array.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/sudo -.-> lab-422794{{"`Linux mdadm Command with Practical Examples`"}} linux/dd -.-> lab-422794{{"`Linux mdadm Command with Practical Examples`"}} end

Understand the Concept of Software RAID

In this step, you will learn about the concept of software RAID (Redundant Array of Independent Disks) and how it differs from hardware RAID. Software RAID is a feature provided by the operating system that allows you to create and manage RAID arrays using software instead of dedicated RAID controller hardware.

The main advantage of software RAID is that it is more cost-effective and flexible than hardware RAID. It can be implemented on any system with multiple storage devices, without the need for specialized RAID hardware. However, software RAID may have slightly lower performance compared to hardware RAID, as the CPU is responsible for managing the RAID operations.

There are several RAID levels that you can configure using software RAID, such as RAID 0 (striping), RAID 1 (mirroring), RAID 5 (striping with distributed parity), and RAID 6 (striping with dual distributed parity). Each RAID level offers different trade-offs between storage capacity, performance, and data redundancy.

In the next step, you will learn how to create a software RAID array using the mdadm command in Linux.

Create a Software RAID Array Using mdadm

In this step, you will learn how to create a software RAID array using the mdadm command in Linux.

First, let's create four virtual block devices that will be used as the underlying storage for our RAID array:

sudo dd if=/dev/zero of=~/project/disk1.img bs=1M count=100
sudo dd if=/dev/zero of=~/project/disk2.img bs=1M count=100
sudo dd if=/dev/zero of=~/project/disk3.img bs=1M count=100
sudo dd if=/dev/zero of=~/project/disk4.img bs=1M count=100

Example output:

100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0730474 s, 1.4 GB/s

Next, let's create a RAID 5 array using these four virtual disks:

sudo mdadm --create /dev/md0 --level=5 --raid-devices=4 ~/project/disk1.img ~/project/disk2.img ~/project/disk3.img ~/project/disk4.img

Example output:

mdadm: chunk size defaults to 512K
mdadm: array /dev/md0 started.

The mdadm command creates a new RAID array named /dev/md0 with a RAID level of 5 and 4 underlying devices.

Now, let's check the status of the RAID array:

sudo mdadm --detail /dev/md0

Example output:

/dev/md0:
           Version : 1.2
     Creation Time : Tue Apr 25 15:25:35 2023
        Raid Level : raid5
        Array Size : 307200 (300.00 MiB 314.43 MB)
     Used Dev Size : 100000 (97.66 MiB 102.40 MB)
      Raid Devices : 4
     Total Devices : 4
       Persistence : Superblock is persistent

       Update Time : Tue Apr 25 15:25:35 2023
             State : clean
    Active Devices : 4
   Working Devices : 4
    Failed Devices : 0
     Spare Devices : 0

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 6d2d9c1c:b4d2f9c4:8d6f0e8f:e0b0d4d5
            Events : 0

    Number   Major   Minor   RaidDevice State
       0       0        0        0      active sync   /home/labex/project/disk1.img
       1       0        1        1      active sync   /home/labex/project/disk2.img
       2       0        2        2      active sync   /home/labex/project/disk3.img
       3       0        3        3      active sync   /home/labex/project/disk4.img

The output shows that the RAID 5 array has been successfully created and is in a clean, active state.

In the next step, you will learn how to manage and monitor the software RAID array.

Manage and Monitor the Software RAID Array

In this final step, you will learn how to manage and monitor the software RAID array you created in the previous step.

First, let's add a new disk to the RAID 5 array:

sudo dd if=/dev/zero of=~/project/disk5.img bs=1M count=100
sudo mdadm /dev/md0 --add ~/project/disk5.img

Example output:

mdadm: added /home/labex/project/disk5.img

Now, let's check the status of the RAID array again:

sudo mdadm --detail /dev/md0

Example output:

/dev/md0:
           Version : 1.2
     Creation Time : Tue Apr 25 15:25:35 2023
        Raid Level : raid5
        Array Size : 407200 (397.66 MiB 417.23 MB)
     Used Dev Size : 100000 (97.66 MiB 102.40 MB)
      Raid Devices : 5
     Total Devices : 5
       Persistence : Superblock is persistent

       Update Time : Tue Apr 25 15:26:16 2023
             State : clean, degraded, recovering
    Active Devices : 4
   Working Devices : 5
    Failed Devices : 0
     Spare Devices : 1

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 6d2d9c1c:b4d2f9c4:8d6f0e8f:e0b0d4d5
            Events : 6

    Number   Major   Minor   RaidDevice State
       0       0        0        0      active sync   /home/labex/project/disk1.img
       1       0        1        1      active sync   /home/labex/project/disk2.img
       2       0        2        2      active sync   /home/labex/project/disk3.img
       3       0        3        3      active sync   /home/labex/project/disk4.img
       4       0        4        -      spare   /home/labex/project/disk5.img

The output shows that the new disk /home/labex/project/disk5.img has been added as a spare device to the RAID 5 array.

Next, let's simulate a disk failure by removing one of the devices from the RAID array:

sudo mdadm /dev/md0 --fail /home/labex/project/disk1.img
sudo mdadm /dev/md0 --remove /home/labex/project/disk1.img

Example output:

mdadm: set /home/labex/project/disk1.img faulty in /dev/md0
mdadm: hot removed /home/labex/project/disk1.img from /dev/md0

Now, let's check the status of the RAID array again:

sudo mdadm --detail /dev/md0

Example output:

/dev/md0:
           Version : 1.2
     Creation Time : Tue Apr 25 15:25:35 2023
        Raid Level : raid5
        Array Size : 407200 (397.66 MiB 417.23 MB)
     Used Dev Size : 100000 (97.66 MiB 102.40 MB)
      Raid Devices : 5
     Total Devices : 4
       Persistence : Superblock is persistent

       Update Time : Tue Apr 25 15:26:56 2023
             State : clean, degraded
    Active Devices : 4
   Working Devices : 4
    Failed Devices : 1
     Spare Devices : 1

            Layout : left-symmetric
        Chunk Size : 512K

Consistency Policy : resync

              Name : localhost.localdomain:0  (local to host localhost.localdomain)
              UUID : 6d2d9c1c:b4d2f9c4:8d6f0e8f:e0b0d4d5
            Events : 8

    Number   Major   Minor   RaidDevice State
       1       0        1        1      active sync   /home/labex/project/disk2.img
       2       0        2        2      active sync   /home/labex/project/disk3.img
       3       0        3        3      active sync   /home/labex/project/disk4.img
       4       0        4        4      spare   /home/labex/project/disk5.img
       0       0        0        -      removed

The output shows that the RAID 5 array is now in a degraded state, with one failed device and one spare device available.

In this step, you have learned how to manage and monitor a software RAID array using the mdadm command, including adding a new disk, simulating a disk failure, and observing the RAID array's state.

Summary

In this lab, you first learned about the concept of software RAID, which allows you to create and manage RAID arrays using software instead of dedicated hardware. You explored the advantages of software RAID, such as cost-effectiveness and flexibility, as well as the different RAID levels available. Then, you learned how to create a software RAID array using the mdadm command in Linux. You created four virtual block devices and used them to set up a RAID 5 array, which provides data redundancy and improved performance.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like