Xen to KVM the manual way

19 Jan 2009

Make a directory for the kvm disk image[s]

mkdir /virt/<hostname>

Make a qcow2 image to copy the data to

cd /virt/<hostname>
qemu-img create -f qcow2 disk.qcow2 8G

Note: make the image a similar size to that of the disk.img from the Xen image

Create a config for the new VM

create-xml.sh <hostname>

Edit the config and add the old disk image and the live cd

use this page Libvirt_XML_Config to add a live cd to the config and the Xen disk image

The final disk configs should look similar to this

<disk type='file' device='cdrom'>
      <source file='/virt/Hardy32.iso'/>
      <target dev='hdc'/>
    <disk type='file' device='disk'>
      <source file='/virt/pxe.msu.montana.edu/disk.qcow2'/>
      <target dev='hda'/>
    <disk type='file' device='disk'>
      <source file='/virt/pxe.msu.montana.edu/disk.img'/>
      <target dev='hdb'/>
    <disk type='block' device='disk'>
     <source dev='/dev/sda1'/>
     <target dev='hdd'/>

Start the vm and connect using vnc

Start the vm

virsh create /etc/libvirt/qemu/<hostname>

Get the vnc display number:

virsh vncdisplay <hostname>

On your local machine

vncviewer virtual1-acg.msu.montana.edu:<display number from above>

Select English and then select the Option to use the live cd

Partition, mount, and copy data

In the vnc vm display open a terminal

sudo su
cd /mnt
mkdir xen
mkdir kvm
mkdir grub

You need to determine which /dev is which. I just mount /dev/sdb to something and if it works thats the xen image since it has a filesystem on it and can mount. If it complains about there not being a filesystem then mount /dev/sda to verify that it is the disk.


Figure out which /dev is the qcow2 image and run this on it

sfdisk table

# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=       63, size= 13687317, Id=83
/dev/sda2 : start= 13687380, size=  3084480, Id=83
/dev/sda3 : start=        0, size=        0, Id= 0
/dev/sda4 : start=        0, size=        0, Id= 0

copy that text into a file called parts and then run

sfdisk < parts
mkfs.ext3 /dev/sda1
mkswap /dev/sda2


mount /dev/sda1 /mnt/kvm
mount /dev/sdb /mnt/xen
mount /dev/sdc /mnt/grub

Note: The dev names may be different for you

Copy Data, Install Kernel

You will need to setup the live cd network.

  1. In the upper right there is a network icon.
  2. Left click it an go to settings/manual configuration
  3. Select the wired connection and click properties
  4. Uncheck Enable Roaming mode
    • Configuration: Static Ip address
    • Ip Address: Ip of the vm you are transfering
    • Subnet mask:
    • Gateway address:
    • Set the DNS to
  5. Click ok
  6. Close the Network Settings box you now have network
cp -a /mnt/xen/* /mnt/kvm/
cp -a /mnt/grub/grub /mnt/kvm/boot/
chroot /mnt/kvm
apt-get update
aptitude install -r grub linux-image-<kernel #>-server linux-image-server linux-ubuntu-modules-2.6.22-15-server
rm /boot/grub/menu.lst

Note: Change the to the kernel for the dist in the image(gutsy[2.6.22-15]/heron[2.6.24-16]/intrepid)

Edit the vm network settings(kvm/etc/network/interfaces)

For some reason you need to use eth1 instead of eth0


auto eth0
interface eth0 inet static


auto eth1
interface eth1 inet static

Gather the virtual disk UUID

udevinfo -q env -n /dev/sd[a-z]1

__Note: sd[a-z]1 is the device that you partitioned above

Copy the UUID

#udevinfo -q env -n /dev/sda1

Edit the /mnt/kvm/boot/grub/menu.lst

change all the kernel lines to reflect the UUID instead of whatever disk it is trying to use

kernel     /boot/vmlinuz-2.6.22-15-virtual root=UUID=<UUID FROM ABOVE> ro quiet splash

Note: This is the point where my browser froze and I lost some stuff so there may be a few minor steps missing

Shutdown the VM

Edit the xml file

  • Remove the /dev/sda1 and disk.img disk declarations
  • Change the boot device to cdrom
    <boot dev='cdrom'/>
  • Boot back into the live cd

Install Grub

In the live cd open a terminal again

sudo su
cd /mnt
mkdir root
mount /dev/sda1 /mnt/root
mount -t proc none /mnt/root/proc
mount -o bind /dev /mnt/root/dev
chroot /mnt/root /bin/bash

Now we open a grub prompt by typing grub and then executing the following

root (hd0,0)
setup (hd0)

Shutdown the vm

Set the newly setup disk to boot

  • Edit the xml file and remove the iso disk declaration
  • Change the boot device back to hd
    <boot dev='hd'/>
  • Start the vm

Setup the NIC in the vm

For some reason the nic doesn’t get setup so we have to manually do it

  • Find the nic model(most likely RTL-8139)

    Using lspci will show you the Ethernet Controller

«« Previous Post Next Post »»