Convert Xen guest to KVM guest using Centos 5.5 in the same machine

Convert Xen guest to KVM guest using Centos 5.5 in the same machine

Suddenly have chance to do the migration…  and virt-v2v version 0.4.0 and 0.4.10 for CentOS do not work.
The key to manual convert is to include virtio_blk into initrd, otherwise, you will see the errors:
Volume group “VolGroup00” not found
Unable to access resume device ( /dev/VolGroup00/LogVol01 )
Mount: could not find filesystem /dev/root

In all guests (DomU) install the normal kernel
yum install kernel
set the normal kernel as the default boot kernel, by editing /boot/grub/menu.lst
set default=X, where X is an integer of the position of normal kernel
edit /boot/grub/devices.map
change /dev/hda  to /dev/vda
use mkinitrd to add virtio_blk to initrd
cd /boot
mv initrd-2.6.18-194.26.1.el5.img initrd-2.6.18-194.26.1.el5.img.bak
mkinitrd -f –with=virtio_blk –with=virtio_pci –builtin=xenblk initrd-2.6.18-194.26.1.el5.img 2.6.18-194.26.1.el5
( you have to match your kernel version number)
copy down your mac address in the file /etc/sysconfig/network-script/ifcfg-eth0
add virtio_blk to /etc/modprobe.conf
alias scsi_hostadapter1 virtio_blk
Power off all guests (DomU)
You need not to boot it from Xen anymore.

In the host (Dom0), install the normal kernel and kvm
yum install kernel kvm kvm-tools kmod-kvm kvm-qemu-img bridge-utils
Stop xend and xendomains
chkconfig – -levels 2345 xend off
chkconfig – -levels 2345 xendomains off
set the kernel as the default kernel, by editing /boot/grub/menu.lst
//add bridge network interface br0
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0
ifcfg-eth0 need only 4 lines
DEVICE=eth0
HWADDR=0a:1b:2c:3d:4e:5f
BRIDGE=br0
ONBOOT=yes

Ifcfg-br0 need the following lines. Replace with your IP.
DEVICE=br0
BOOTPROTO=static
BROADCAST=192.168.x.255
IPADDR=192.168.x.y
NETMASK=255.255.255.0
NETWORK=192.168.x.0
ONBOOT=yes
TYPE=Bridge

service network restart

reboot the host (Dom0)
start virtual machine manager (virt-manager)
click file -> add connection
choose qemu as hypervisor, local as connection then click connect
You will now have a new tree of virtual machines.
Add a new virtual machine in the qemu tree
Choose whatever ISO file as the install media (will not actually install anything…)
Choose a very small harddisk image. By default, it will be in /var/lib/libvirt/images/your_image_name.img
Choose a suitable network, br0 in our case.
If you have recorded your guest mac address, you can paste here, otherwise, we will do it later by editing the config file /etc/libvirt/qemu/your_guest_name.xml .
Force off the newly created guest.
Copy your Xen harddisk file to replace the newly created one. By default, you can find it in /var/lib/xen/images/your_xen_DomU_guest_name.img
cp /var/lib/xen/images/Xen_hdd_file.img /var/lib/libvirt/images/your_image_name.img

Boot into guest.
Then we need to re-enable eth0 on the guest
mv /etc/sysconfig/network-scripts/ifcfg-eth0.bak /etc/sysconfig/network-scripts/ifcfg-eth0
service network restart
We are done.

For some case, if you forgot do something in the guest, you can use kpartx to help you instead of booting back to Xen kernel.
//mount the hdd image file
cd /var/lib/libvirt/images
kpartx -a your_image_name.img
vgchange –ay VolGroup00
cd /mnt
mkdir ddd
mount /dev/mapper/VolGroup00-LogVol00 /mnt/ddd
mount /dev/mapper/loop0p1 /mnt/ddd/boot
mount –bind /sys /mnt/ddd/sys
mount –bind /dev /mnt/ddd/dev
mount –bind /proc /mnt/ddd/proc
chroot /mnt/ddd
cd boot
mv initrd-2.6.18-194.26.1.el5.img initrd-2.6.18-194.26.1.el5.img.bak
mkinitrd -f –with=virtio_blk –with=virtio_pci –builtin=xenblk initrd-2.6.18-194.26.1.el5.img 2.6.18-194.26.1.el5
( you have to match your kernel version number)
exit chroot mode
exit
umount /mnt/ddd/sys
umount /mnt/ddd/dev
umount /mnt/ddd/proc
umount /mnt/ddd/boot
umount /mnt/ddd
vgchange –an VolGroup00
cd /var/lib/libvirt/images
kpartx -d your_image_name.img