Linux RAID mirror from existing

This tech article will address building a Linux RAID mirror from an existing, unraided disk. Some assumptions are made:

+There are three drives.
+The existing drive (with the original data to mirror) is /dev/sdc.
+The other two (/dev/sda and /dev/sdb) are identical make and model.
+/dev/sdb will receive the installation from /dev/sdc first. The '/' partition will be sdb3.
+/dev/sda will then receive the installation via the mirror process.

Additionally, for this exercise, CentOS 5.5 was the optical boot media. Feasibly, just about any distribution should work.

Put all three drives online, and boot to the CentOS 5.5 media. After the media has booted to the colon prompt, enter:

Code:

#linux rescue

The boot process continues. Choose the appropriate language and keyboard. There is no need to start the network, and there is no need to search for existing installations. Just choose "Skip" at the end to go straight to the rescue prompt.

Now we will duplicate the original disk's partition structure. For this example, partition 3 is '/'. But, we want all partitions anyway, so at the rescue prompt:

Code:

#sfdisk -d /dev/sdc | sfdisk /dev/sdb

This command will warn, and also output some rather dire appearing language. It's bark is worse than it's bite!

Now we want to create the RAID:

Code:

#mdadm --create /dev/md0 --level=mirror --raid-devices=2 /dev/sdb3 missing

Remember, sda and sdb will be in the RAID. sdc will NOT. So, we have just setup the RAID to include sdb. sda will come later.

Now you should be able to have a look at the RAID with the following command:

Code:

#mdadm --detail /dev/md0

The output should show one drive online/working/active (sdb) out of a total of two. The state will be 'clean, degraded'.

Now to create the filesystem on the RAID:

Code:

#mkfs.ext4 /dev/md0

This will take some processing but should finish with the output:

Code:

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Now, we want to get our ducks in a row. Referencing the "assumptions" portion at the top, enter the following:

Code:

#cd /
#cd mnt
#mkdir raid1
#mkdir original
#mount -t ext4 /dev/md0 raid1
#mount -t ext4 /dev/sdc3 original

The 'raid1' mountpoint implicitly mounts sdb3, added to the RAID just prior. The 'original' mountpoint contains our precious installation. Next we will clone that installation into the degraded RAID:

Code:

#rsync -av /mnt/original/ /mnt/raid1

NOTE!!! the trailing '/' after "original". Be sure to include this or you will find yourself reworking this recipe.

The entire partition should copy from the original to the raid. When it's done we will unmount the original:

Code:

#umount /mnt/original

Now we want to add the second drive to the RAID mirror:

Code:

#mdadm /dev/md0 --add /dev/sda3

Mirroring should begin immediately. To check it's progress:

Code:

#cat /proc/mdstat

Also, be sure to edit fstab to include /dev/md0 as '/'. The original installation was not to this device.