Create a software RAID 1 with one device

diskmdadmraid

I have a single disk that I want to create a mirror of; let's call this disk sda. I have just bought another identically-sized disk, which we can call sdb. sda and sdb have one partition called sda1 and sdb1 respectively.

When creating a raid, I don't want to wipe my sda clean and start again, I just want it to start mirroring with sdb. My train of thought was to do:

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=1 /dev/sda1

… to create the array without sdb disk, then run something like (I'm thinking the following command out loud, because I am not sure how to achieve this step)

mdadm /dev/md0 --add /dev/sdb1

Note sdb1 is assumed to be formatted similarly to sda1

Is this possible?

Best Answer

The simple answer to the question in the title is "Yes". But what you really want to do is the next step, which is getting the existing data mirrored.

It's possible to convert the existing disk, but it's risky, as mentioned, due the the metadata location. Much better to create an empty (broken) mirror with the new disk and copy the existing data onto it. Then, if it doesn't work, you just boot back to the un-mirrored original.

First, initialize /dev/sdb1 as the new /dev/md0 with a missing drive and initialize the filesystem (I'm assuming ext3, but the choice is yours)

mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 /dev/sdb1 missing
mkfs -text3 /dev/md0

Now, /dev/sda1 is most likely your root file system (/) so for safety you should do the next step from a live CD, rescue disk or other bootable system which can access both /dev/sda1 and /dev/md0 although I have successfully done this by dropping to single user mode.

Copy the entire contents of the filesystem on /dev/sda1 to /dev/md0. For example:

mount /dev/sda1 /mnt/a       # only do this if /dev/sda1 isn't mounted as root
mount /dev/md0 /mnt/b
cd /mnt/a                    # or "cd /" if it's the root filesystem
cp -dpRxv . /mnt/b

Edit /etc/fstab or otherwise ensure that on the next boot, /dev/md0 is mounted instead of /dev/sda1. Your system is probably set to boot from /dev/sda1 and the boot parameters probably specify this as the root device, so when rebooting you should manually change this so that the root is /dev/md0 (assuming /dev/sda1 was root). After reboot, check that/dev/md0 is now mounted (df) and that it is running as a degraded mirror (cat /proc/mdstat). Add /dev/sda1 to the array:

mdadm /dev/md0 --add /dev/sda1

Since the rebuild will overwrite /dev/sda1, which metadata version you use is irrelevant. As always when making major changes, take a full backup (if possible) or at least ensure that anything which can't be recreated is safe.

You will need to regenerate your boot config to use /dev/md0 as root (if /dev/sda1 was root) and probably need to regenerate mdadm.conf to ensure /dev/md0 is always started.