16

I have a linux software RAID using md. I want to list all of my RAID arrays and each hard drive attached to them. Is there an easy way to do that?

Katherine Villyard
  • 18,510
  • 4
  • 36
  • 59
fatal_error
  • 1,102
  • 1
  • 11
  • 18

3 Answers3

10

cat /proc/mdstat will give you the output you need, relatively easy to parse, because the mapped device is on the same line as its members, e.g.:

$ cat /proc/mdstat 

Personalities : [raid0] [raid1] [raid10] [raid6] [raid5] [raid4] [linear] [multipath] 
md0 : active raid1 sdf2[1] sde2[0]
      81854336 blocks super 1.2 [2/2] [UU]

md127 : active raid0 sdf3[1] sde3[0]
      286718976 blocks super 1.2 512k chunks

unused devices: <none>
sysconfig
  • 201
  • 1
  • 4
9

Method #1 - using mdadm's details

You can use the mdadm commands verbose switch, -v, to get the list of devices from the --detail --scan switches output in a form that's pretty easy to parse into a comma separated form.

$ mdadm -v --detail --scan /dev/md/raid1 | awk -F= '/^[ ]+devices/ {print $2}'
/dev/sda1,/dev/sdb1,/dev/sdc1,/dev/sde1

This can be further refined into 1 per line.

$ mdadm -v --detail --scan /dev/md/raid1 | awk -F= '/^[ ]+devices/ {print $2}' | tr , '\n'
/dev/sda1
/dev/sdb1
/dev/sdc1
/dev/sde1

Things can of course be shortened up with the short switches to mdadm.

$ mdadm -vDs /dev/md/raid1 | awk -F= '/^[ ]+devices/ {print $2}' | tr , '\n'
/dev/sda1
/dev/sdb1
/dev/sdc1
/dev/sde1

Method #2 - using mdadm's query

You can use the query (-Q) & details (-D) along with verbos (-v) to do something similar:

$ mdadm -vQD /dev/md/raid1 | grep -o '/dev/s.*'
/dev/sdb1
/dev/sda1
/dev/sdc1
/dev/sde1

Method #3 - using /proc/mdstat

You can also parse the list of HDD members from the /proc/mdstat output like so:

$ grep 'md' /proc/mdstat | tr ' ' '\n' | sed -n 's/\[.*//p'
sde1
sdc1
sdb1
sda1

These will be missing the /dev portion but you can easily add that in manually like so:

$ grep 'md' /proc/mdstat | tr ' ' '\n' | sed -n 's|^|/dev/|;s/\[.*//p'
/dev/sde1
/dev/sdc1
/dev/sdb1
/dev/sda1
slm
  • 7,355
  • 16
  • 54
  • 72
3

If you have three software RAID arrays attached to the system (md0, md1, md2), the following simple one-liner will display the drives attached to each (change the ..2 to your total number of arrays):

sudo mdadm --query --detail /dev/md{0..2} | grep dev

/dev/md0:
    0    8    18     0    active sync   /dev/sdb2
/dev/md1:
    0    8    19     0    active sync   /dev/sdb3
/dev/md2:
    0    8    20     0    active sync   /dev/sdb4
    1    8    36     1    active sync   /dev/sdc4

Note that UUID's aren't needed to track which drives are in which arrays, since the RAID superblock will handle that.

fatal_error
  • 1,102
  • 1
  • 11
  • 18