How to Convert LV or MD RAID1 and 0 Into RAID5 Without Losing Data
Tags: linux, testing, datascience, beginners
Published: 2021-07-08 20:51:29

In this tutorial, you will learn how to switch from RAID 0 or 1 to RAID 5. How to convert (on Linux, with mdadm or LVM) a software RAID1 (0) system into RAID5 while keeping all the data it stores.

![How to Convert LV or MD RAID1 and 0 Into RAID5 Without Losing Data](https://hetmanrecovery.com/pic/blog/a255/how-to-convert-lv-or-md-raid1-and-0-to-raid5-without-data-loss.jpg)

RAID 0 disk arrays are known for relatively high data access time, and that’s why people choose to use them primarily because of the considerable boost in the speed of read and write operations. However, their reliability and fault-tolerance leave much to be desired. Even if one of the drives is out of order, all the data in such a disk array is going to be lost. Recovering the information from this array type is quite a challenge, but even then, all the data you may be able to extract will certainly be damaged. In fact, this type is not much of a disk array because it offers no redundancy at all.

YouTube: {% youtube GMc2pCbbn1E %} 

On the contrary, RAID 1 is a very reliable and fault-tolerant solution, as all the data is duplicated to the other disk, creating a mirrored copy of the information on the other drive. Still, this approach affects the read and write speeds of the whole array, so it appears to be slower in accessing the data, if compared with RAID 0 systems.

If you have a spare drive, you can upgrade the existing RAID 1 array to RAID 5. This way, you will enjoy better reliability than RAID 0 could offer, and feel an impressive read/write speed boost in comparison with RAID 1. In the end, you get a RAID 5 system, which is both reliable, fault-tolerant and powerful enough to process your data faster than before.

##How to convert RAID 1 (mirroring) into RAID 5 (parity)
All right, we’ve got a software md_RAID1 system created on a Linux operating system. This array consists of two drives.

![How to convert RAID 1 (mirroring) into RAID 5 (parity)](https://hetmanrecovery.com/pic/blog/a255/01-en-raid1.jpg)

We need to convert it into RAID 5 and then add one more drive, and while doing that we have to make sure that all the data from the disk array doesn’t disappear. There are several files there: photos, videos, documents, and so on.

![into RAID 5 and then add one more drive](https://hetmanrecovery.com/pic/blog/a255/02-en-raid-files.jpg)

Before you start the conversion, it is recommended to back up important information because if you make a mistake while typing commands, important data can be erased.

Open the terminal, and run this command to make sure that all the operations are performed with superuser rights:
```
sudo -i
```
and type the password.

![sudo -i](https://hetmanrecovery.com/pic/blog/a255/03-en-root.jpg)

If the disk array is mounted, you need to unmount it with this command:
```
umount /dev/md0
```
or
```
umount /mnt
```
![umount /mnt](https://hetmanrecovery.com/pic/blog/a255/04-en-unmounting.jpg)

If the operating system tells you the target is busy, try to force this action:
```
umount -f /dev/md0
```
![umount -f /dev/md0](https://hetmanrecovery.com/pic/blog/a255/05-en-unmounting-linux.jpg)

After that, you have to stop your RAID 1 system, with the following command:
```
mdadm --stop /dev/md0
```

(whether it is md0 or another name depends on the path, which needs to be checked)

where md0 is the identifier for your RAID system.

![mdadm --stop /dev/md0](https://hetmanrecovery.com/pic/blog/a255/06-en-stopping-the-array.jpg)

Of course, you can’t stop it if this is the RAID system you boot from, and in this case, you’ll have to use a LiveCD.

Now you need to overwrite the metadata of the old RAID 1 system; to do it, create a RAID 5 system with the same drives that were included in the RAID 1 system - sdb and sdc - by typing this command:
```
mdadm --create /dev/md0 -a yes -l 5 -n 2 /dev/sdb1 /dev/sdc1
```
where l5 sets the array level,

n2 sets the number of drives it includes, and then these drives are specified.

![mdadm --create /dev/md0](https://hetmanrecovery.com/pic/blog/a255/07-en-reloading-the-metadata.jpg)

When the command is performed, the program will warn you that these drives are already used in a RAID 1 system, so hit Enter to continue.

To check if the conversion was successful, type this command:
```
cat /proc/mdstat
```
![cat /proc/mdstat](https://hetmanrecovery.com/pic/blog/a255/07-en-rebuild-process.jpg)

Now all you have to do is to wait until RAID 5 is built: the status of this process is shown here, in percent.

When it is over, the command **cat /proc/mdstat** will display the following data.

![cat /proc/mdstat](https://hetmanrecovery.com/pic/blog/a255/08-en-array-rebuilt.jpg)

After that, you can add a new drive to the array, and make it a full-featured RAID 5 system. This new drive will be added as a spare one. Use the following command for this step:
```
mdadm --add /dev/md0 /dev/sdd1
```
![mdadm --add](https://hetmanrecovery.com/pic/blog/a255/09-en-add-a-third-drive.jpg)

After that, extend the RAID system to the three active drives (now including the spare drive) with this command:
```
mdadm --grow /dev/md0 --raid-disks=3
```

Check the result with the command cat /proc/mdstat:
```
cat /proc/mdstat
```
![cat /proc/mdstat](https://hetmanrecovery.com/pic/blog/a255/10-en-expand-the-raid.jpg)

Now you can see that the RAID system is using three drives - sdb, sdc, sdd and the process of conversion (reshape) has begun.

Its progress is shown in percent; wait until it is complete.

In the end, check the disk array for errors with the following command:
```
e2fsck -f /dev/md0
```

and use another command to extend it:
```
resize2fs -p /dev/md0
```
![resize2fs -p /dev/md0](https://hetmanrecovery.com/pic/blog/a255/11-en-сhecking-for-errors.jpg)

Finally, the last command updates the configuration file:
```
mdadm --examine --scan >> /etc/mdadm.conf
```
![mdadm --examine --scan](https://hetmanrecovery.com/pic/blog/a255/12-en-updating-the-configuration-file.jpg)

If there are no errors in the course of performing these commands, all the data existing on your RAID 1 system will be transferred onto the new RAID 5.

Mount it and check what’s going on: as you can see, all the files are still here, and the array type has changed to RAID 5.

![Mount it and check what’s going on](https://hetmanrecovery.com/pic/blog/a255/13-en-all-files-remained-intact.jpg)

##How to convert RAID 0 (striping) into RAID 5 (parity)
However, you can’t use this method to change from RAID 0 to RAID 5 without formatting and losing all the data. Instead, there is a way to convert it quickly with a single command.

Migration from RAID 0 to RAID 5 is only possible when there are two drives - then RAID 0 coincides with RAID 5 in terms of structure. All the changes will affect the array level only, while the information on the drives remains intact.

Here is the command that transforms a RAID 0 consisting of two drives into a RAID 5 including three drives.
```
mdadm --grow /dev/md0 --level=5 --raid-devices=3 --add /dev/sdd --backup-file=/tmp/grow_md0.bak
```

Before you run it, unmount the array.

Check the result with the command:
```
cat /proc/mdstat
```

Wait until the reshape operation is complete.

![Wait until the reshape operation is complete](https://hetmanrecovery.com/pic/blog/a255/14-en-converting-zero-raid-into-fifth.jpg)

After changing the array type to RAID 5, you can add more drives with the command I have already mentioned in this tutorial:
```
mdadm --add /dev/md0 /dev/sdc1
```
![mdadm --add](https://hetmanrecovery.com/pic/blog/a255/15-en-adding-drives-to-the-raid.jpg)

After restarting, the array name will change to RAID 5.

If the RAID 0 system contains more than two drives, you need to reduce their number, and only then you’ll be able to convert the array to RAID 5.

##How to convert LVRAID1 into LVRAID5
If you used LVM features to create a RAID system, then you can convert it from one array type to another with the command lvconvert:
```
lvconvert
```

Run the command
```
pvdisplay
```
to view the information on this group of disks.

![view the information on this group of disks](https://hetmanrecovery.com/pic/blog/a255/15-en-see-information-about-the-carriers-included-in-the-raid.jpg)

If you want to know more about creating MD and LV RAID systems, check one of the previous videos by following the link below.

YouTube: {% youtube O3Iq9hx8V7U %}