HOWTO: Migrating a CentOS server with software RAID1 to a paravirtualized Xen

by Iván-Benjamín García Torà and Ian Blanes

This document briefly explains the process of migrating a machine running CentOS 5.7 with a level 1 RAID with two disks, to a paravirtualized machine with Xen, while reusing the original RAID disks. We spent more time than needed trying to do a relatively simple thing, so we wrote this guide in the hopes that it might be useful to somebody.


WARNING: This is an advanced guide for people who know what they do. USE AT YOUR OWN RISK.


On the original machine do the following steps:

Step 1: install the Xen domU enabled kernel

As simple as executing:

yum install kernel-xen

This will install and configure a Linux kernel image with xen support for a paravirtualized domU (in our case, 2.6.18-274.7.1.el5xen).

Step 2: adjust device mappings

Using your favorite text editor, modify the entries in /etc/fstab as follows:

From

/dev/md0                /                       ext3    defaults        1 1
/dev/md1                swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

To

/dev/sda1               /                       ext3    defaults        1 1
/dev/sda2               swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

In addition, edit the Grub configuration file at /etc/grub.conf changing any mention of /dev/md0 to /dev/sda1.

Step 3: adjust the initrd image

One of the peculiarities of CentOS (and, therefore, of Fedora, Red Hat, and probably others) is having to customize or recreate the initrd image (a small image that is loaded along the kernel by the bootloader that contains a few basic scripts, whose function is to make some modules available at boot time, needed to boot the system, that are not inlaid in the kernel image) when the root device is changed. This is so because those scripts contain hardcoded information about which device is to be used and, therefore, It is essential that this information is correct for the system to boot.

In our case we will, besides configuring the root device, include the basic modules for Xen domU support. To do that, we will move the default initrd image and we will create a new one with the necessary parameters:

# We change wordking directory to /boot
cd /boot

# We move the original image
mv initrd-2.6.18-274.7.1.el5xen.img initrd-2.6.18-274.7.1.el5xen.img.backup

# We create the image with blkbk,xenblk modules included and preloaded at boot time.
mkinitrd --preload=xenblk --preload=blkbk --with=xenblk --with=blkbk --rootdev=/dev/sda1 -v initrd-2.6.18-274.7.1.el5xen.img 2.6.18-274.7.1.el5xen

In case of working with 2.6.18-274.7.1.el5xen version of the kernel.

Step 4: configure the network

In this step we will simply make sure there isn't any mention about the card's physical address (MAC).

Typically those files are located under:

# vim /etc/sysconfig/networking/{devices,profiles}/ifcfg-* 

Additionally we will simplify the configuration to one network interface, in case we were using a bonding system, since it will not be necessary anymore.

For other more complex configurations (i.e. with more than one network interface) please refer to the CentOS and Xen documentation.

Finally, shut down the machine and we will move Its RAID disks to the Xen host machine.


Next, the steps to be completed in the Dom0 machine:

Step 1: configure RAID

Using the mdadm tool we will activate the RAID disks. In our case, one for the root device and another one for the swap:

# We assemble the first and the second RAID, in case they weren't already active ("cat /proc/mdstat" to check that).
mdadm --assemble /dev/md0 /dev/sda1 /dev/sdb1
mdadm --assemble /dev/md1 /dev/sda2 /dev/sdb2

# We add the configuration to the mdadm configuration file.
mdadm -Es >> /etc/mdadm/mdadm.conf

# Optionally, edit it to avoid duplicates, etc.
vim /etc/mdadm/mdadm.conf

Step 2: create Xen domU configuration file

Now that we have migrated the disks and the guest configuration, the only remaining step is to create the Xen configuration to boot the paravirtualized machine.

Here are the key elements of a sample configuration file, continuing with our example disk setup (two RAID 1 disks with two partitions, one for the system root and another one for swap):

memory = 2048
name = "centos"
vif = [ 'ip=192.168.1.2, mac=00:16:3e:00:00:01' ]
disk = [ 'phy:/dev/md0,sda1,w', 'phy:/dev/md1,sda2,w' ]

In this example we have assigned a system memory of 2GB, the domain's name is "centos", with the IP "192.168.1.2" and MAC address "00:16:3e:00:00:01" (in the range that is usually used for Xen domains).

Running

xm create -c config_file.cfg

should be enough to bring the new virtualized CentOS up.

Step 3: final tweaking

As we have paravirtualized our machine, some of the services we were possibly using make no sense anymore. To disable them, just run

ntsysv

from the domU (centos) domain. The services that can be removed are:


And that's all. In case something goes wrong, there is always the option of mounting and editing the filesystem in the dom0 machine (make sure to destroy the domain before mounting the filesystem and unmount the filesystem before creating the domain again):

# Destroy centos domain instance, just in case it was running.
xm destroy centos

# Create a mount target directory
mkdir -p /mnt/centos

# Mount the filesystem
mount /dev/md0 /mnt/centos

# Miscellaneous mounts
mount --bind /dev /mnt/centos/dev
mount --bind /proc /mnt/centos/proc
mount --bind /sys /mnt/centos/sys

# chroot and su - to centos system
chroot /mnt/centos
su -

Try to fix something.

To test it again with xen:

# Exit (twice, one for the su -, one for the initial chroot)
exit
exit

# umount all filesystems
umount /mnt/centos/dev
umount /mnt/centos/proc
umount /mnt/centos/sys
umount /mnt/centos

Always be sure to umount the filesystem before running it in xen.

Good luck !