Converting to RAID - An Idiots Guide
(note: I'd have called this Raid for Dummies, but IDG publishers would take exception.)

by David A. Bandel (david at gmail dot com)
Revised: Oct 2006

This document is designed to update the fine RAID write-ups already on the Linux SXS site.  As such, I make no attempt to duplicate that information here.

Following is a summary of the steps I took to convert several running (read production) systems to RAID.  While I chose to convert to RAID 1, the process for RAID 4, 5, 6, or 10 is identical, it just requires more disks.

Preparing for the conversion.

Converting a production system means, above all else, protecting the data you already have.  So while not necessary, a verified backup is always a good idea.  And if you're comfortable with that backup and can spare the downtime, then it might be easier to just set your system up with RAID from scratch.

First, building and testing a new kernel with the proper RAID support built-in is absolutely essential.  As of this writing, I am using a Debian 2.6.17 kernel with all support built as modules and am using an initrd.

The important point for this kernel is that you either build in the RAID support or have it in your initrd.img. Debian's kernel and initrd.img will boot correctly out of the box, I imagine most other modern distros can as well.  If you choose to build in RAID support (the safe way to go), in your choice of configuration menus select "Device Drivers" --> "Multi-device support (RAID and LVM)" and ensure that RAID support and at least the RAID level you want to use is built in, not a module.  You might also want to choose Multipath I/O support if your system can make use of it (choose Yes if unsure).

Build and boot from your new kernel.  If you're using an initrd.img, you may have to modprobe your raid module this time. You should be able to cat /proc/mdstat.

Second, you'll need a second hard disk at least as large as the one you want to RAID (a third for RAID 4 or 5, a fourth for RAID 6).  Identical disks will make this task much easier.  Assuming you have your current disk installed as /dev/hda, install the second disk as /dev/hdc (and if a third, as /dev/hde, a fourth as /dev/hdg).  Using one disk per controller (even to the point of buying new controllers) is well worth the extra effort for the increase in speed.  This document will assume you've installed as per this paragraph. SATA and SCSI do not suffer from the shared IRQ problem so how they are connected does not matter.

Once your new disk is installed and running, open fdisk /dev/hda, option p and note your partition sizes and locations.  Then run df and note your partition sizes and mount points.  One of my systems looked like this:

DeviceBootStartEndBlocksIdSystem
/dev/hda1*1540131fdLinux
/dev/hda263502771212+fdLinux
/dev/hda3351247517069062+fdLinux
/dev/hda42476249516065082Linux swap

The above corresponded to a 20Mb disk that originally held the non-RAIDed system:

/dev/hda1/boot
/dev/hda2/
/dev/hda3/home
/dev/hda4swap

So I installed an 80Mb disk (no 20Mb disks were to be had) and partitioned it as follows:
DeviceBootStartEndBlocksIdSystem
/dev/hdc116855502231fdLinux raid autodetect
/dev/hdc268613705502262+fdLinux raid autodetect
/dev/hdc3137120505462100fdLinux raid autodetect
/dev/hdc42051972961681567+85Linux extended
/dev/hdc52051417517069031fdLinux raid autodetect
/dev/hdc6417645202771181fdLinux raid autodetect
/dev/hdc79651972963453682Linux swap
/dev/hdc84521452540131fdLinux raid autodetect

The above partitions correspond to:
/dev/hdc8/boot
/dev/hdc6/
/dev/hdc5/home

Other partitions correspond to partitions on /dev/hde and were RAIDED as well.

The important points to pay attention to are:
1.  The new RAID partition must be the same size or _smaller_ than the corresponding non-RAIDED partition it will mirror.
2.  The new partitions must use type fd - Linux raid autodetect.
3.  Your kernel or initrd.img must have RAID support.
4.  Ensure you have the RAID device files.  If you're using udev, you should see them in /dev/md/, otherwise you might have to create them.  You can create them with this command:

for i in 0 1 2 3 4 5 6 7 8 9 10; do mknod /dev/md$i b 9 $i; done


Ensure you have installed the latest mdadm utility.  You will _NOT_ need raidtools2 (and particularly not raidtools version 1).  We will not need to build /etc/raidtab.

Once your new RAID disk has been partitioned and all but the swap partition are type Linux raid autodetect (fd), the hard part is done.  N.B. I chose not to RAID swap.  However you may if you like.  I kept two separate swap partitions, one on each disk, for extra speed.  Your choice, RAID if you like, but you'll take a slight performance hit.

Setting up the RAID

Now, the easy part.  Activate the RAID partitions.  For this step we'll activate a degraded RAID array.  That is, it won't be a full RAID yet.  Our friend is mdadm:

mdadm --create /dev/md0 -c 128 -l 1 -n 2 /dev/hdc8 missing

The above will create the RAID array /dev/md0 using a chunk size of 128Mb, level 1 (RAID 1), with 2 devices.  The first device is /dev/hdc8, the other device is missing (we'll add it later).

Once the device is created, we need to format it with our favorite filesystem.  My filesystem of choice is XFS, but you get to pick your own poison:
mkfs.xfs -f /dev/md0
Mount the device and copy the appropriate information to it:
mount /dev/md0 /foo
tar c /boot | tar xvC /foo
Do the same for each of the other partitions, creating the degraded RAID, formatting the device, and copying the information you want to each new device.

Some notes about the copying process:
Some directories are static.  They can be copied at anytime.  Others are not.  To handle the ones that are not (notably /home and /var) I took the following precautions:
before copying /home, I ran `touch /etc/nologin` then booted everyone from the system
before copying /var, I went to runlevel 1

Once all was copied over (and I was in runlevel 1), I removed /etc/nologin, then modified etc/fstab on the new RAID device to reflect the RAID device mounts.

I reboot the system and at the boot: prompt gave the new kernel name and root=/dev/md2 (where my root filesystem was located).

Finishing up

Congratulations, you're now running on a RAID system (albeit degraded).  So let's fix that part.  You can `cat /proc/mdstat` at any time to see your progress.  You can only add one device at a time, then you'll have to wait for the kernel to bring that device fully on-line before you can go on to the next device.  Not to worry, the kernel will handle the scheduling part for you.

First, open /dev/hda with fdisk and change all the partittions you want to have added to the array to type fd - linux raid autodetection.

Then, for each degraded array, add the appropriate non-array device to it:
mdadm /dev/md0 -a /dev/hda1

Once each device has been rebuilt, you can see in /proc/mdstat that all is well.

Note:  If you get an error message to the effect that a device cannot be added because it doesn't have enough space, that's because you're trying to add a device that's smaller than the current RAID device.  Fix that and all will be well.  This is why I emphasized that the RAID devices needed to be the same size or smaller (not larger) than your current non-RAIDed devices in the first part above.

The final step comes in making sure you can boot from your raid array.  I'm sorry for all those folks using GRUB, but last I heard (please correct this document if I'm wrong), only LILO will boot from a raid partition.  So dump GRUB and install LILO.

Ensure your /etc/lilo.conf has the correct setup:
boot=/dev/md0
root=/dev/md2
raid-extra-boot=mbr-only

LILO will write boot information to each of the individual raid devices boot sectors, so if either /boot or your root partition are on failed disks, you'll still be able to boot.

Enjoy your newly raided system (but continue to make backups).