MDADM: Full usefull ‘Cheat Sheet’

Mdadm is the modern tool most Linux distributions use these days to manage software RAID arrays; in the past raidtools was the tool we have used for this. This cheat sheet will show the most common usages of mdadm to manage software raid arrays; it assumes you have a good understanding of software RAID and Linux in general, and it will just explain the commands line usage of mdadm. The examples bellow use RAID1, but they can be adapted for any RAID level the Linux kernel driver supports.

1. Create a new RAID array

Create (mdadm –create) is used to create a new array:

# mdadm --create --verbose /dev/md0 --level=1  --raid-devices=2 /dev/sdX1 /dev/sdY2

or using the compact notation:

# mdadm -Cv /dev/md0 -l1 -n2 /dev/sd[XY]1

2. Save of configuration – mdadm.conf

mdadm.conf -is the main configuration file for mdadm. After we create our RAID arrays we add them to this file using:

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

3. Remove a disk from an array

We can’t remove a disk directly from the array, unless it is failed, so we first have to fail it (if the drive it is failed this is normally already in failed state and this step is not needed):

Мы не можем удалить диск непосредственно из массива, если только он не уже отказал, поэтому сначала нам нужно его отключить (если диск не работает, обычно он уже находится в состоянии отказа, и этот шаг не требуется):

# mdadm --fail /dev/md0 /dev/sdX1

and now we can remove it:

# mdadm --remove /dev/md0 /dev/sdX1

This can be done in a single step using:

# mdadm /dev/md0 --fail /dev/sdX1 --remove /dev/sdX1

4. Add a disk to an existing array

We can add a new disk to an array (replacing a failed one probably):

# mdadm --add /dev/md0 /dev/sdX1

5. Seeking of arrays

The result of the section research by the command ‘mdadm –examine’ “

# mdadm --examine /dev/sda1
          Magic : a92b4efc
        Version : 0.90.00
           UUID : 6d1679c1:31d4919b:a3bab061:9112c3c7 (local to host RIPLinuX)
  Creation Time : Thu Jan 30 10:47:24 2014
     Raid Level : raid1
  Used Dev Size : 524224 (512.02 MiB 536.81 MB)
     Array Size : 524224 (512.02 MiB 536.81 MB)
   Raid Devices : 2
  Total Devices : 2
Preferred Minor : 0

    Update Time : Wed Jan 10 14:30:11 2018
          State : clean
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0
       Checksum : 2a2c227f - correct
         Events : 309

      Number   Major   Minor   RaidDevice State
this     0       8        1        0      active sync   /dev/sda1

   0     0       8        1        0      active sync   /dev/sda1
   1     1       8       17        1      active sync   /dev/sdb1

6. Auto assembly

When –assemble is used with –scan and no devices are listed, mdadm will first attempt to assemble all the arrays listed in the config file. If no arrays are listed in the config (other than those marked ) it will look through the available devices for possible arrays and will try to assemble anything that it finds. Arrays which are tagged as belonging to the given homehost will be assembled and started normally. Arrays which do not obviously belong to this host are given names that are expected not to conflict with anything local, and are started “read-auto” so that nothing is written to any device until the array is written to. i.e. automatic resync etc is delayed.

If mdadm finds a consistent set of devices that look like they should comprise an array, and if the superblock is tagged as belonging to the given home
host, it will automatically choose a device name and try to assemble the array. If the array uses version-0.90 metadata, then the minor number as recorded
in the superblock is used to create a name in /dev/md/ so for example /dev/md/3. If the array uses version-1 metadata, then the name from the superblock is
used to similarly create a name in /dev/md/ (the name will have any ‘host’ prefix stripped first).

# mdadm --assemble --scan
mdadm: /dev/md/2 has been started with 2 drives.
mdadm: /dev/md/1 has been started with 2 drives.
mdadm: /dev/md/0 has been started with 2 drives.

This behaviour can be modified by the AUTO line in the mdadm.conf configuration file. This line can indicate that specific metadata type should, or should
not, be automatically assembled. If an array is found which is not listed in mdadm.conf and has a metadata format that is denied by the AUTO line, then it
will not be assembled. The AUTO line can also request that all arrays identified as being for this homehost should be assembled regardless of their meta-
data type. See mdadm.conf(5) for further details.

Note: Auto assembly cannot be used for assembling and activating some arrays which are undergoing reshape. In particular as the backup-file cannot be
given, any reshape which requires a backup-file to continue cannot be started by auto assembly. An array which is growing to more devices and has passed
the critical section can be assembled using auto-assembly.

7. Verifying the status of the RAID arrays

We can check the status of the arrays on the system with:

# watch -n 5 cat /proc/mdstat


# mdadm --detail /dev/md0

8. Stop and delete a RAID array

If we want to completely remove a raid array we have to stop if first and then remove it:

# mdadm --stop /dev/md0
# mdadm --remove /dev/md0

and finally we can even delete the superblock from the individual drives:

# mdadm --zero-superblock /dev/sdX

Finally in using RAID1 arrays, where we create identical partitions on both drives this can be useful to copy the partitions from sda to sdb:

# sfdisk -d /dev/sdX | sfdisk /dev/sdY

(this will dump the partition table of sda, removing completely the existing partitions on sdb, so be sure you want this before running this command, as it will not warn you at all).

9. If you got – Auto-read-only status

If we in process of creation or after of booting got status of array – active (auto-read-only), then, probably need check status of some member:

md127 : active (auto-read-only) raid1 sdg2[0]
      16777216 blocks super 1.2 [2/1] [U_]

what is wrong with sdh2?

and finally we can try command with key —-readwrite:

# mdadm --readwrite /dev/md127

When an array is initially assembled, it is placed in “auto-read-only” mode. But not alwyas in different versions.

However, auto-read-only isn’t an error, nor is it anything to worry about. The basic idea behind it is to make –assemble (and, apparently now, even –create) safer: Nothing is written to the disks until the array goes read-write. The array will automatically switch from auto-read-only to read-write when it receives its first write. So, if you went ahead and created a filesystem on the device, or a LVM physical volume, or whatever, it would have switched to read-write, and started the sync. The only reason you’d need to run mdadm –readwrite on it is if you want it to sync before you perform any writes.

Scroll to top