Tip

Six steps for migrating Xen virtual machines to KVM

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

    Requires Free Membership to View

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:

modprobe dm-mod

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:
qemu kvm.img

Summary
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

Sander van Vugt is an independent trainer and consultant living in the Netherlands. Van Vugt is an expert in Linux high availability, virtualization and performance and has completed several projects that implement all three. He is also the writer of various Linux-related books, such as Beginning the Linux Command Line, Beginning Ubuntu Server Administration and Pro Ubuntu Server Administration.


This was first published in August 2009

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.