If you use virtualization on Linux, chances are that you've implemented a system based on the Xen hypervisor. Because the Kernel-based Virtual Machine (KVM) has become increasingly popular on Linux, you may want to consider switching your Linux-based virtualization environment from Xen to KVM. This article provides a six-step tutorial on migrating Xen-based virtual machines (VMs) to KVM.
As of now, there are no mature management offerings to automatically convert Xen virtual machines to KVM. There is, however, a technical solution that helps convert a virtual machine that was installed in Xen to KVM. The following steps explain how to do so for a Linux virtual machine. I've written this procedure to be as distribution-independent as possible, but make sure that for specific steps, you use the tools that come with your Linux distribution.
1. Make sure that you have installed the parted and kpartx utilities in the host environment (referred to as the Dom0 environment). Parted helps in creating partitions; kpartx helps in mounting a partition that is defined within a virtual machine.
2. Open the virtual machine you want to migrate, and install the GRUB boot loader. By default, there is no boot loader installed in a Xen virtual machine because the boot loader is offered by the Xen virtual environment itself. So make sure it is installed before proceeding.
3. Shut down the Xen virtual machine and copy the entire virtual machine to an image file. Before you start, do make sure that you have lots of available disk space; we're talking multiple gigabytes here, and you need space for that. Make sure that you're using the right procedure here. The Xen virtual storage back end, for example, can be in an image file already, but it can also be installed using a device, such as a partition or a logical volume manager (LVM) volume as its storage back end. If, for instance, you've used the "/dev/images/xenvm1" LVM logical volume as the Xen storage back end, you can write that to an image file using the following command:
dd if=/dev/images/xevvm1 of=xen.img bs=1M
Note: If you use complex partitioning in your virtual machine, you'll have to create an image file for each individual partition. This procedure assumes that everything in your virtual machine is in one big root partition.
4. Create a new image file for the KVM virtual machine. This image file should be as big as the image file you've just created for the old virtual machine. In the example below, I'll assume that you're using a 10 GB Xen image; make sure to adjust this to the value that is really occupied by your virtual machine:
dd if=/dev/zero of=kvm.img bs=1M count=10240
5. Create partitions in the KVM image file you've just created. The only way to do so is by using the parted command-line utility. The steps below will create a 9 GB root partition and a 1 GB swap partition. Make sure to execute these steps with root privileges:
parted kvm.img mklabel msdos
parted kvm.img mkpart primary ext2 0 9000
parted kvm.img mkpart primary linux-swap 9000 10000
parted kvm.img set 1 boot on
6. Copy the old Xen image to the new KVM image. To do so using root permissions, issue the following commands:
losetup /dev/loop kvm.img
kpartx -a /dev/loop0
dd if=xen.img of=/dev/mapper/loop0p1 bs=1M
fsck.ext3 -f /dev/mapper/loop0p1
At this point, you've copied all files from the old image to the new image. Now it's time to test if it works. The following command will help you in doing that:
Currently, there are no tools to smoothly convert a Xen virtual machine to a KVM virtual machine. If your virtual machine uses Linux, though, you can find your way using command-line utilities. This article has shown you how to do that. But this method has limitations -- where the main hurdle is that there's support for Linux virtual machines only. Given the current switch to KVM virtualization in Linux environments, it will only be a matter of time before more adequate tools become available.
About the expert