RAID-1 to RAID-5 using mdadm

As per subject.

When converting from RAID-1 to RAID-5 using mdadm, why must a RAID 1 array contain 2 devices and not more than 2 devices? I don't understand RAID strong enough to pinpoint a reason.

Background: My three RAID-1 arrays contain 3 devices each, my goal is to remove one device from two of the arrays, and attach the spare devices to the remaining RAID-1 array. That will leave two RAID-1 arrays with 2 devices each, and the remaining RAID array with 4 devices. The goal is take the 4 device RAID-1 array and convert that to RAID-5. This is not a discussion about which RAID architectures are better or worse, just the process to change a RAID-1 array to a RAID-5 array.


There is no migration path from RAID-1 to RAID-5, except for the special case with two disks, where RAID-1 conveniently is the same as RAID-4 and RAID-5 (because the parity of a single bit is the bit itself), so the migration code just changes the RAID level without touching the data.

After converting to RAID-5, you can add more disks to the array — this migration path exists.

So your migration plan would be:

  1. Run a consistency check on all devices (/usr/share/mdadm/checkarray …)
  2. Reduce all arrays to two disks¹
  3. Convert the array you want to switch to RAID-5 (--grow … -l5)
  4. Add the extra disks to the RAID-5 as spares (--grow … --add …)
  5. Set the new number of disks (--grow … -n4).

¹ That is tricky, because there is no good way to reduce the number of disks. You can, from a rescue system, overwrite the RAID superblock and use the --assume-clean option to avoid a rebuild, but you need to use the same superblock version as before (use mdadm --examine … on one of the component devices to find out).


Upgrading an N-disk RAID1 to an N-disk RAID5 is possible, though a bit tedious:

  • Remove all but two disks from the RAID1. To do so, repeatedly execute:

    mdadm ARRAY --fail DEVICE
    mdadm ARRAY --remove DEVICE
    

    Where ARRAY is the ID of the array like /dev/md2 and DEVICE is a partition of that array like sdc2 or sdd2.

  • Change the size of the RAID1 to two disks:

    mdadm --grow ARRAY -n 2
    
  • Change the type of array from RAID1 to RAID5:

    mdadm --grow ARRAY -l 5
    
  • Now add all the disks again, that were removed in the first step:

    mdadm ARRAY --add-spare DEVICE DEVICE ...
    

    Please note, that unlike for --fail and --remove, you need to specify the full pathnames of the DEVICEs.

  • Grow the RAID5 to the number of available DISKS:

    mdadm --grow ARRAY -n DISKS
    

    This is the only slow operation, as it needs to move all data on the disks and generate parity. However, while it is in progress, you can still work on the machine, but you should expect degraded disk performance.

  • After the grow operation has finished (use cat /proc/mdstat to see its progress), you can resize the file system. Assuming, that you use ext4, this goes via:

    resize2fs ARRAY
    

Because of the lengthy re-arrangement of the data, it is often faster to delete the RAID1, then create a fresh RAID5 (which still needs to create parity, but which is an easier operation than to re-shuffle all data) and restore the data from a backup.


As per mdadm documentation, you can 'upgrade' a mirror raid (raid1) to a degraded parity raid (raid5) and then add a new disk. Your raid will mostly not survive a single disk failure during rebuild, so you should have a recent backup, just in case.