Convert Single disk to RAID on Live Linux Server

Warning!  If you don’t have a full backup of the data on your server, DO NOT ATTEMPT THIS!  If you’re not familiar with basic disk utilities (fdisk, grub, ect) DO NOT ATTEMPT THIS!  If you don’t have KVM access in case something goes wrong, DO NOT ATTEMPT THIS!  Although I have checked my instructions for consistency, chances are you are human and are going to mess this up.  If downtime costs you $$$, skip this how-to get a new server with a real (hardware) RAID.

In this example, /dev/hda is our primary existing disk and /dev/hdd is the new disk we want to use to form a RAID mirror.  First, we examine the main disk’s partition table so we can recreate it on the secondary disk.

fdisk -l /dev/hda

Then, you’ll need to create the partition layout on /dev/hdd exactly how it is layed out on /dev/hda.  All partition types should be set to FD (Linux Raid).  I won’t describe this part in great detail since every disk geometry is different, but you’ll need to run this command:

fdisk /dev/hdd

Once you have applied your partition layout, you need to create a degraded RAID member on the new disk.  In this example, we have three main partitions we want to mirror  (swap, /boot, and /).

mdadm –create /dev/md0 -c 128 -l 1 -n 2 /dev/hdd1 missing
mdadm –create /dev/md1 -c 128 -l 1 -n 2 /dev/hdd2 missing
mdadm –create /dev/md2 -c 128 -l 1 -n 2 /dev/hdd3 missing

Now, we create the filesystems

mkfs.ext3 /dev/md0
mkswap /dev/md1
mkfs.ext3 /dev/md2

Mount the new RAID disk members:

mkdir /mnt/tmp
mount /dev/md2 /mnt/tmp
mkdir /mnt/tmp/boot
mount /dev/md0 /mnt/tmp/boot

Copy data from your original, live disk:

cd /mnt/tmp
tar -C / -clspf – . | tar -xlspvf –
cd /mnt/tmp/boot
tar -C /boot -clspf – . | tar -xlspvf –

Assuming you’re using the Grub bootloader (who uses LILO anymore?), install the bootloader on the new disk so it is bootable:

grub /dev/hdd
root (hd1,0)
setup (hd1)
exit

Now we need to modify the /mnt/tmp/etc/fstab file on the new disk so the Kernel can find your new RAID partitions next time you boot.  Below I have shown what mine looks like AFTER editing it:

### vi /mnt/tmp/etc/fstab

/dev/md2                /                       ext3    defaults        1 1
/dev/md0                /boot                   ext3    defaults        1 2
none                    /dev/pts                devpts  gid=5,mode=620  0 0
none                    /dev/shm                tmpfs   defaults        0 0
none                    /proc                   proc    defaults        0 0
none                    /sys                    sysfs   defaults        0 0
/dev/md1                swap                    swap    defaults        0 0
/dev/hdc                /media/cdrom            auto    pamconsole,exec,noauto,managed 0 0

Now modify grub.conf on the new boot partition so Grub can find the Kernel.  Below, you will see what mine looks like AFTER editing it.  The primary change is the root= directive.  I have also duplicated the original entry so that you have the option to boot from either disk, in case one is missing (Grub isn’t aware of Linux RAID).  The last entry is a fail-safe that allows you to boot from the original disk should the first reboot fail.

### vi /mnt/tmp/boot/grub/grub.conf

default=0
fallback=1
timeout=5

## Option 0 default
title CentOS (2.6.9-55.0.2.EL) (disc0)
root (hd0,0)
kernel /vmlinuz-2.6.9-55.0.2.EL ro root=/dev/md2
initrd /initrd-2.6.9-55.0.2.EL.img

## Option 1 in case disk0 fails
title CentOS (2.6.9-55.0.2.EL) (disk1)
root (hd1,0)
kernel /vmlinuz-2.6.9-55.0.2.EL ro root=/dev/md2
initrd /initrd-2.6.9-55.0.2.EL.img

## Option 2 in case new RAID won’t boot during conversion, boot original disk
title CentOS (2.6.9-55.0.2.EL) (old)
root (hd0,0)
kernel /vmlinuz-2.6.9-55.0.2.EL ro root=/dev/hda3
initrd /initrd-2.6.9-55.0.2.EL.img

Now, reboot the server.  Since the default in Grub is entry 0, you should automatically boot from the new single (degraded) RAID disk you created.

All good?  Next, fdisk /dev/hda and change the partition types on old disk to FD (Linux RAID).  Again, I’m not going to teach you how to use fdisk but here’s the command:

fdisk /dev/hda

Now, complete the array and start the RAID build process (this will wipe your original disk with a mirror copy of your new RAID disk):

mdadm /dev/md0 -a /dev/hdd1
mdadm /dev/md1 -a /dev/hdd2
mdadm /dev/md2 -a /dev/hdd3

Now we can monitor the build process.  Wait until it is complete before continuing.

watch cat /proc/mdstat

Now, reinstall the MBR on the old disk so it’s bootable:

grub
root (hd0,0)
setup (hd0)
exit

And now for a final drumroll….test your work:

reboot

Categories: Linux. Bookmark the permalink. Comments Off on Convert Single disk to RAID on Live Linux Server

Comments are closed.