Skip to content

Xen to KVM (or physical, VMWare) Migration

by daimon on Dezember 21st, 2010

I will describe a way to migrate a XEN machine to kvm. XEN typically stores an image containing a single partition without kernel and bootloader, while KVM images are more resembling to physical machines, using a hard disk image with a partition table and an operating system with kernel and bootloader.

So what we have to do is add a kernel and grub to the XEN image and prepend the image with a partition table and working bootloader.

First, install grub and kernel into the running virtual machine. I recommend first taking a snapshot or making a backup copy of the XEN image.

# apt-get install grub linux-image

You can now shutdown the XEN Machine. Then, we create an empty disk image, about 50MB bigger than the original image:

# dd if=/dev/zero of=newimage.raw bs=512 count=XXXX (add about 100.000 to the sector count of the original image)

Now, you have to create a partition in this image, using losetup, fdisk and kpartx (more information in this post)

# losetup -fv newimage.raw
# fdisk -cu /dev/loop0

Create a new Partition(“n”), maximizing disk usage, and mark it as bootable (“a”). Save the changes and quit (“w”). Use kpartx to create a device mapping for the new partition:

# kpartx -a /dev/loop0

You can now copy the contents of the original XEN image file/logical volume to the newly created partition. Run a full filesystem check and expand the partition to the boundaries of the new image to maximize usage.

# dd if=<xenimage> of=/dev/mapper/loop0p1
# e2fsck -f /dev/mapper/loop0p1
# resize2fs /dev/mapper/loop0p1

For the remaining steps, mount the new filesystem locally and chroot into it. Important: Make the raw image file available within the chroot shell, because grub fucks up when using the loopback device. We will instead advise grub to install itself into the raw image.

# mount /dev/mapper/loop0p1 /mnt
# for i in dev sys proc; do mount -o bind /$i /mnt/$i; done
# mount -o bind <directory which contains raw image> /mnt/mnt
# chroot /mnt

Make the new hard drive bootable by installing the bootloader into the bootsector.

# mkdir -p /boot/grub
# cp /usr/lib/grub/x86_64-pc/* /boot/grub/
# echo "(hd0) /dev/vda" > /boot/grub/devices.map
# grub --no-floppy
grub> device (hd0) /mnt/<imagefile>.raw
grub> root (hd0,0)
grub> setup (hd0)
grub> quit
# update-grub

Edit /boot/grub/menu.lst and set the options kopt(i.e. root=vda1) and groot (i.e. (hd0,0)). Then rerun update-grub.

# vim /boot/grub/menu.lst
# update-grub

Change /etc/fstab to reflect the new device paths of your root filesystem and swap and other filesystems, if they exist.

# vim /etc/fstab

Change the getty line for the XEN  console to default tty input:

# vim /etc/inittab

#1:2345:respawn:/sbin/getty 38400 hvc0
1:2345:respawn:/sbin/getty 38400 tty1

You’re done! Now you may exit the chroot shell, unmount the filesystem, remove the partition mappings and the loopback device.

# exit
# for i in dev sys proc mnt; do umount /mnt/$i; done; umount /mnt
# kpartx -d /dev/loop0
# losetup -d /dev/loop0

Transfer the newimage.raw to the KVM host and use it at your discretion. The Image should now boot successfully. You may also compress the raw image with qemu-img convert to save space.

More Information:

http://sirping.kafit.se/wp/index.php/category/kvm

http://sativouf.blogspot.com/2010/02/xen-to-kvm-migration.html

http://ebroder.net/2009/08/04/installing-grub-onto-a-disk-image

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS