JRehkemper.de

MDADM - Convert existing Drive to RAID 1 without dataloss

In this article I will describe, how you can create a RAID 1 in MDADM by adding a second drive to an existing one.

If you want to store your data reliably, you should probably use some kind of RAID for redundancy. The easies form is a RAID 1. RAID 1 typicaly consists out of two drives and all data is mirrored on both drives. If one fails, all data is saved on the second one as well. Most tutorials assume, that you have three drives in total. The current one and two new ones that will form the RAID. But in that case the original drive isn’t needed after the migration. But there is a way around that, if you only have two drives.

Disclaimer

First of all MAKE SURE YOU HAVE A BACKUP. If you are coping large amounts of data, there is always a possibility for failure. In that case you want a backup to restore you data!

Requirements

You will need two drives with identical capacity. Since one of them is already in use, try to get a matching one. If you can’t match them excatly, try to get a bigger one. The smallest drive in the array will dictate the size of the array. If the second on is bigger, you will not be able to use all the capacity. If the second drive is smaller that the original, you will not be able to create the array.
You will need to install mdadm. It should be available on all major distibutions. So it should be irrelevant which distribution you use.

Starting Point

We have three drives connected to our System:
/dev/sda - 512 GB SATA SSD for the OS
/dev/sdb - 2 TB HDD currently mountet at /opt. This is the drive with our data that should be converted to a RAID1.
/dev/sdc - 2 TB HDD without any data and not yet mounted. This will be the second drive in the array.

Prepare the new drive

The sdc needs to have a partition with the same specs as sdb. We should also creat the same Filesystem. In my case XFS.

[tux@server]$ sudo mkfs.xfs /dev/sdc

Create the Array

Now we create the RAID1 Array. I will show you the command first and explain afterwards.

[tux@server]$ sudo mdadm --create /dev/md0 --level 1 --raid-devices=2 missing /dev/sdc1

--create /dev/md0 - creates an array with the name /dev/md0
--level 1 - defines the RAID Level. In our case RAID 1.
--raid-devices=2 - defines that our array will have two devies. This important, since we will start with only one drive of the two. More on that later.
missing - means we will create a degraded array with missing drives. Normaly MDADM would not allow to create a RAID 1 with only one drive.
/dev/sdc1 - the new drive to create the array with.

How does it work

The idea behind all this is to create a degraded array. Normaly a degraded array means, that one of the drive failed and the array waits for you to replace it and restore all the data to the new one.
In our case the creating of the degraded array gives us the possibilty to copy the data from the original drive to the array before wiping the original disk to include it in the array. Once we include it in the array, the data will get restored to the now new drive and in the end we have a working RAID 1 Array.

Copy the data

Next we need to mount the array and copy the data. I will mount it at /mnt but you can choose a different mount-point if you like. Just rember to use the right path while coping.

[tux@server]$ sudo mount /dev/md0 /mnt

Now we need to copy the data from our sdb mountet at /opt to our new array md0 mountet at /mnt. I will use rsync for that.

# -a - copy all data including hidden files
# -v - show file names while coping
# --progress - draw a progressbar
[tux@server]$ sudo rsync -a -v --progress /opt /mnt

Depending on your amount of data, this might take a while…

Restoring the Array

Before you proceed make sure that all data is copied successfuly! Otherwise it will be lost!

Next we add sdb to the array. While doing this, all data on sdb will be wiped! There is no way around that.

[tux@server]$ sudo mdadm --add /dev/md0 /dev/sdb1

Once the drive is added, mdadm will start to rebuild the array and copy all the data to the “new” drive sdb.
You can check the state of the array like so:

[tux@server]$ sudo mdadm -D /dev/md0

The progress of the rebuild can be displayed like this:

[tux@server]$ sudo cat /proc/mdstat

After a few hours the array will be rebuilt and fully functional.

A Word of advice

This way of creating an array is by no means suitable for production! If you need this data available at all time, buy another disk and have it as a cold spare.
But for home use this might be an alternative.
BUT!
You have to keep in mind that a drive is most likely to fail when you copy large amount of data. In this procedure this happens twice (copy to the array and rebuild the array). So make sure you have an external backup beside these two drives in the array.
Nethertheless for a homelab this might be a viable option since you dont by a large hard disk that is lying in the shelf afterwards.

profile picture of the author

Jannik Rehkemper

I'm an professional Linux Administrator and Hobby Programmer. My training as an IT-Professional started in 2019 and ended in 2022. Since 2023 I'm working as an Linux Administrator.