A Linux Playground

My doodles and what I get up to.

How to Create a Raid 1 Array With One Disk Already Containing Data

The Problem

I recently decided to upgrade my computer system by making the system resilient to a single hard drive failure. After obtaining a second hard drive to create a mirrored array, to do so I found you should create the array then add the filesystem and then data. This caused me an issue as the current drive contained lots of data already. Although I had backups of everything important, I didn’t have enough space for everything. I wanted to keep all of the data if possible.

How I resolved the problem

After playing around with the raid software mdadm and using the friend that is Google. I found that it was possible to create an array in a degraded state (not all devices working or present). This allowed me to create the array using one disk and add the second later.

To create the array I first partitioned the new disk in to one large volume leaving a 200Mb space at the end. This is recommended if using different disk manufactures as they do not always have the exact same size due to formatting and manufacturing errors. This is easily accomplished using fdisk or gparted (for non CLI). I will use disks sdb and sdc for my examples.

To create the raid array I ran

1
mdadm --create /dev/md0 -l raid1 -f -n 1 /dev/sdb1

/dev/md0 is the place to interact with the raid block device. As we are creating an array with a missing disk the “-f” must be used. The “-n 1” tells the software that only one disk is currently available.

To confirm that the raid is working run:

1
2
3
cat /proc/mdstat
    md0 : active (auto-read-only) raid1 sdb1[0]
    4881700 blocks super 1.2 [1 1] [U]

The next step was to create a filesystem on the raid device

1
mkfs.ext4 /dev/md0

I then copied all the data from the original disk on to the raid disk. Once copied, I then reformatted the original disk to be the same size as the raid using fdisk. Lastly I expanded the array and added the second disk.

1
2
mdadm --grow  /dev/md0 -n 2
mdadm --manage  /dev/md0 --add  /dev/sdc1
1
2
3
4
5
cat  /proc/mdstat
    Personalities : [raid1]
    md0 : active raid1 sdb1[1] sda1[0]
    4016528792 blocks super 1.2 [2 1] [U_]
    [>....................] recovery = 1.7% (700422 4016528792) finish=102.5min speed=62000K secA