10+ mdadm command examples in Linux [Cheat Sheet]


CheatSheet

Reviewer: Deepak Prasad

Introduction to mdadm command

RAID is the short form for Redundant Array of Independent Disks. RAID is used to store data across multiple devices. It helps to prevent data loss if a drive has failed.

mdadm command is used for building, managing, and monitoring Linux md devices (aka RAID arrays). RAID devices are made up of multiple storage devices that are arranged in a specific way to increase performance and, in some cases, fault tolerance.

 

How to install mdadm in Linux

mdadm is not pre-installed in the Linux system. You can use the following command to install mdadm according to your Linux distributions.

To install mdadm on CentOS, Fedora and RHEL

$ sudo yum install mdadm

To install mdadm on Ubuntu and Debian

$ sudo apt install mdadm

 

Modes of mdadm operation:

mdadm command has the following modes of operation.

  • Assemble: To assemble the components of a pre-existing array into an active array.
  • Build: To build an array that doesn't have per-device metadata (superblocks).
  • Create: To create a new array with per-device metadata (superblocks).
  • Follow or Monitor: To monitor one or more md devices and act on any state changes.
  • Grow: To grow (or shrink) an array, or otherwise reshape it in some way.
  • Incremental Assembly: To add a single device to an appropriate array.
  • Manage: This is for doing things to specific components of an array such as adding new spares and removing faulty devices.
  • Misc: This is an 'everything else' mode that supports operations on active arrays, operations on component devices such as erasing old superblocks, and information gathering operations.
  • Auto-detect: This mode does not act on a specific device or array, but rather it requests the Linux Kernel to activate any auto-detected arrays.

 

Syntax for mdadm command

The syntax for using the mdadm command is as follows:

$ mdadm [mode] <raiddevice> [options] <component-devices>

The mode-specific option is used before the non-mode-specific option. The options that are not mode-specific are:

  • -h, --help: Displays the help message. If used after the mode-specific option, it displays the help message of that mode.
  • -v, --verbose: Be more verbose about what is happening. It can be used twice to be extra-verbose.
  • -q, --quiet: Don't print unnecessary messages in the output.
  • -V, --version: Print version information for mdadm.
  • -s, --scan: Scan the config file or /proc/mdstat for missing information. When using --detail or --stop, --scan tells mdadm to get a list of array devices from /proc/mdstat.

 

Configure Software RAID in Linux

Below are certain steps that you must follow before creating a software RAID device on your Linux node. Since I have already performed those steps in my older article, I will share the hyperlinks here.
Important Rules of Partitioning
Partitioning with fdisk

Now we have our partitions available with us which we can validate using lsblk.

root@ubuntu-PC:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   60G  0 disk 
├─sda2   8:2    0    1K  0 part 
└─sda5   8:5    0 59.5G  0 part /
sdb      8:16   0   10G  0 disk 
└─sdb1   8:17   0   10G  0 part 
sdc      8:32   0   10G  0 disk 
└─sdc1   8:33   0   10G  0 part 
sr0     11:0    1 58.4M  0 rom  /media/golinux/VBox_GAs_6.1.32

 

Different examples to use mdadm command

1. Create RAID 0 array using mdadm command

RAID 0 is usually referred to as "striping". In the RAID 0 array, data is separated into blocks that are scattered across the storage devices like hard disks. This means that each disk contains a portion of data and several disks will be referenced to access the data. The RAID 0 array requires a minimum of 2 storage devices.

You can create an md array using the mdadm --create command. You will need to specify the md name, raid level, number of devices, and their names.

For example, the following command creates a RAID 0 array md0 using two storage devices /dev/sdb1 and /dev/sdc1.

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

Sample Output:

root@ubuntu-PC:~# mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sdb1 /dev/sdc1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

You can verify whether the RAID array was successfully created by checking the /proc/mdstat file.

root@ubuntu-PC:~# cat /proc/mdstat
Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid0 sdc1[1] sdb1[0]
      20951040 blocks super 1.2 512k chunks
      
unused devices: <none>

For more information you can read Step-by-Step Tutorial: Configure Software RAID 0 in Linux

 

2. mdadm command to query an md device

The -Q or --query flags of mdadm command examine a device to check if it is an md device or a component of an md array.

# mdadm -Q /dev/md0

OR

# mdadm --query /dev/md0

Sample Output:

root@ubuntu-PC:~# mdadm --query /dev/md0
/dev/md0: 19.98GiB raid0 2 devices, 0 spares. Use mdadm --detail for more detail.

 

3. Print detail of md devices

The -D or --detail options print the detailed description of the array. The device should be an active md device.

# mdadm -D /dev/md0

OR

# mdadm --detail /dev/md0

Sample Output:

root@ubuntu-PC:~# mdadm --detail /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Mon Apr  4 22:21:40 2022
        Raid Level : raid0
        Array Size : 20951040 (19.98 GiB 21.45 GB)
      Raid Devices : 2
     Total Devices : 2
       Persistence : Superblock is persistent

       Update Time : Mon Apr  4 22:21:40 2022
             State : clean 
    Active Devices : 2
   Working Devices : 2
    Failed Devices : 0
     Spare Devices : 0

            Layout : -unknown-
        Chunk Size : 512K

Consistency Policy : none

              Name : ubuntu-PC:0  (local to host ubuntu-PC)
              UUID : 6bab41ea:41b69079:b4b7fa0e:59873349
            Events : 0

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       33        1      active sync   /dev/sdc1

 

4. Add RAID to mdadm configuration file

/etc/mdadm.conf is the configuration file for management of Software RAID. The following command adds the RAID arrays to the configuration file.

# mdadm -D -s > /etc/mdadm.conf

OR

# mdadm --detail --scan > /etc/mdadm.conf

Sample Output:

root@ubuntu-PC:~# mdadm --detail --scan > /etc/mdadm.conf
root@ubuntu-PC:~# cat /etc/mdadm.conf
ARRAY /dev/md0 metadata=1.2 name=ubuntu-PC:0 UUID=6bab41ea:41b69079:b4b7fa0e:59873349
  • Array: Actual arrays are identified by the ARRAY lines. The second word on the line is the name of the device where the array is normally assembled, such as /dev/md1 or /dev/md/backup.
  • metadata: Specify the metadata format that the array has.
  • name: This is a simple textual name given to mdadm when the array was created.
  • uuid: The value should be a 128-bit uuid in hexadecimal, with punctuation interspersed if desired. It must match the uuid stored in the superblock.

 

5. Create a file system on a RAID drive

It is required to build a filesystem on the RAID device before using it. The following mkfs command creates an ext3 filesystem on the /dev/md0.

# mkfs.ext4 /dev/md0

Sample Output:

root@ubuntu-PC:~# mkfs.ext4 /dev/md0
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 5237760 4k blocks and 1310720 inodes
Filesystem UUID: 283d9eb9-93a4-48e6-9139-59578966a120
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

 

6. Mount the RAID device

Now, let's create a mount point for the RAID device.

# mkdir /mnt/raid

Then mount the RAID device using the mount command.

# mount /dev/md0 /mnt/raid

Sample Output:

root@ubuntu-PC:~# mkdir /mnt/raid
root@ubuntu-PC:~# 
root@ubuntu-PC:~# mount /dev/md0 /mnt/raid
root@ubuntu-PC:~# 
root@ubuntu-PC:~# df -h /mnt/raid
Filesystem      Size  Used Avail Use% Mounted on
/dev/md0         20G   45M   19G   1% /mnt/raid

The RAID device is ready to be used now.

 

7. Deactivate or delete a RAID array

The -S or --stop options deactivate or shut down the given array. If the raid array is mounted, you have to unmount it first.

# mdadm -S /dev/md0

OR

# mdadm --stop /dev/md0

Sample Output:

root@ubuntu-PC:~# umount /dev/md0
root@ubuntu-PC:~# mdadm --stop /dev/md0
mdadm: stopped /dev/md0

The superblock for the devices can be deleted using the --zero-superblock option. By using this option, you can re-use the partitions for creating new RAID arrays.

# mdadm --zero-superblock /dev/sdb1 /dev/sdc1

When --stop option is used with --scan option, it shut down all arrays that can be shut down (i.e. are not currently in use).

# mdadm --stop --scan

 

8. Assemble and start all arrays listed in the standard config file

The -A or --assemble options assemble a previously create array. When it is used with -S or --scan option, it will assemble and start all arrays listed in the standard config file.

# mdadm -A -s

OR

# mdadm --assemble --scan

Sample Output:

root@ubuntu-PC:~# mdadm --assemble --scan
mdadm: /dev/md/0 has been started with 2 drives.

 

9. Create RAID 1 array with mdadm command

RAID 1 is usually referred to as "mirroring". In the RAID 1 array, it is required to mirror data between two or more devices. The data is written on each device in the group. As a result, each disk has the exact copy of the data.

The RAID 1 array requires a minimum of 2 storage devices. The following command creates a RAID 1 array /dev/md1 using two disks /dev/sdb1 and /dev/sdc1.

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

Sample Output:

root@ubuntu-PC:~# mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata

Verify the changes using the following command.

root@ubuntu-PC:~# cat /proc/mdstat
Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] 
md1 : active raid1 sdc1[1] sdb1[0]
10475520 blocks super 1.2 [2/2] [UU]
[===============>.....] resync = 78.2% (8202496/10475520) finish=0.1min speed=206497K/sec

unused devices: <none>
NOTE:
The resynchronization process takes some time depending on your RAID 1 array size. After the resynchronization completes, you can start creating a filesystem on the array.
root@ubuntu-PC:~# cat /proc/mdstat
Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] 
md1 : active raid1 sdb2[1] sdb1[0]
      4876288 blocks super 1.2 [2/2] [UU]
      
unused devices: <none>

For more information you can read Step-by-Step Tutorial: Configure Software RAID 1 in Linux

 

10. Create RAID 5 array with mdadm command

In the RAID 5 array, the parity information is distributed across all drives in the array. The RAID 5 array requires a minimum of 3 storage devices. If one drive in the array fails, it will cause a loss of data.

The following command creates a RAID 5 array /dev/md5 using three disks /dev/sdb1, /dev/sdc1, and /dev/sde.

# mdadm --create /dev/md5 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sde

For more information you can read Step-by-Step Tutorial: Configure Software RAID 5 in Linux

 

11. Create RAID 10 array (striping mirror)

The RAID 10 array (also known as RIAD 1 + 0) does both works of mirror and striping. It is the most widely used, and effective, hybrid array which is the combination of RAID 0 and RAID 1. The RAID 10 array also requires a minimum of 4 storage devices.

  • RAID 0 (striping) requires at least 2 devices for splitting data.
  • RAID 1 (mirroring) also requires at least 2 devices to mirror the data between them.
mdadm --create /dev/md2 --level=10 --raid-devices=4 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1

For more information you can read Step-by-Step Tutorial: Configure Hybrid Software RAID 10 in Linux

 

Conclusion

This tutorial teaches you to manage and configure software RAID devices in Linux. We also showed how you can create various types of RAID arrays using the mdadm command. If you have any questions, let us know in the comment section below.

 

What's Next

Step-by-Step Tutorial: Configure Software RAID 5 in Linux
Step-by-Step Tutorial: Configure Hybrid Software RAID 10 in Linux

 

Further Reading

man page for mdadm command

 

Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

5 thoughts on “10+ mdadm command examples in Linux [Cheat Sheet]”

  1. Since I enjoy reading your articles, I suggest this erratum :
    “The RAID 10 array requires a minimum of 4 storage devices.”

    It’s actuallly possible to create mdadm raid1 with empty placeholders
    as an initially degraded array, but we do mean the standard functional 4-device volume.

    Reply
  2. Hi, this section has been explained in detail and very informative. Thanks for sharing. But these commands are for Ubuntu. specifically.

    Reply

Leave a Comment