Xen to KVM disk image migration

Latest improvements in the EMOTIVE Cloud development have been focused in expanding the platform to new virtualization technologies such as KVM or others thanks to Libvirt.

During the research and deployment of this new features, we’ve found several difficulties in order to migrate disk images. The platform was using a single raw .img image, the one used by Xen and our goal was to maintain this structure, but it seemed that KVM was only working with partitioned images and a boot loader.

For this reason, KVM virtual machines base system had to be created, partitioned, formatted, and have a grub-enabled boot record. It was not possible to easily migrate our previous images into that new structure as well as it would add a new complexity level. Here is attached that creation method:

  • KVM image creation:

qemu-img create -f raw $IMAGENAME $TOTALSIZEKB
  • Label KVM image:
parted --script $IMAGENAME mklabel msdos
  • Create Partitions
parted --script $IMAGENAME mkpartfs primary ext2 $START $END
parted --script $IMAGENAME mkpartfs primary linux-swap $START $END
  • Map the partitions
losetup -s -f $IMAGENAME
kpartx -a /dev/loop0
  • Format and mount the partitions
mkfs.ext3 -q /dev/loop0/p1
mkswap /dev/loop0/p2
mount /dev/loop0/p1 $MOUNT
  • Debootstrap the new image and GRUB setup
debootstrap --arch $ARCH --unpack-tarball $POOL/sources.tgz --include=$EXTRA lenny $MOUNT $MIRROR
mkdir $MOUNT/boot/grub
cp $MOUNT/usr/lib/grub/x86_64-pc/stage[12] $MOUNT/boot/grub
cp $MOUNT/usr/lib/grub/x86_64-pc/*stage1_5 $MOUNT/boot/grub
echo "(hd0) $IMAGENAME" > device.map
$MOUNT/usr/sbin/grub --device-map=device.map --batch <<EOT
root (hd0,0)
setup (hd0)
echo "(hd0) $UUID" > $MOUNT/boot/grub/device.map
  • XML Libvirt Domain definition
 <type arch='x86_64' machine='pc'>hvm</type>
 <boot dev='hd'/>

Finally, we’ve found the solution: use the same Xen VM’s images but including the kernel image during the boot process. So now, it is as easy as include the kernel image name and “boot=/dev/vda”, as an argument passed to the kernel, in the XML Libvirt Domain definition. We’re assuming that we have a compiled and installed kernel into $MOUNT/boot. Consequently, no more boot loaders or image partitioning is required. Here’s attached the method for creating a new VM:

  • KVM image creation:

dd if=/dev/zero of=debianbase.img bs=1024k count=$IMAGESIZE
  • Format and mount the partitions
mkfs.ext3 -F debianbase.img
mount -o loop debianbase.img $MOUNT
  • Debootstrap the new image
debootstrap --arch $ARCH --unpack-tarball sources.tgz --include=$EXTRA lenny $MOUNT $MIRROR
  • Extract Kernel out of the disk image
  • XML Libvirt Domain definition
<domain type='kvm'>
 <type arch='x86_64' machine='pc'>hvm</type>
 <boot dev='hd'/>
<clock offset='utc'/>
 <disk type='file' device='disk'>
 <source file='debianbase.img'/>
 <target dev='vda' bus='virtio'/>
 <disk type='file' device='disk'>
 <source file='home.img'/>
 <target dev='vdb' bus='virtio'/>
 <disk type='file' device='disk'>
 <source file='swap.img'/>
 <target dev='vdc' bus='virtio'/>
<interface type='bridge'>
 <mac address='52:54:83:ea:4a:cb'/>

<serial type='pty'>
 <target port='0'/>
<console type='pty'>
 <target port='0'/>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' autoport='yes' keymap='es'/>

In this way,  you can migrate your Xen platform to KVM in an easy way.

One Response to “Xen to KVM disk image migration”

  1. Acceder a una imagen de disco KVM ubicada en un volumen lógico « Desde lo alto del Cerro said:

    Dec 14, 09 at 13:00

    [...] Xen to KVM disk image migration [...]