Blog
Conversion of Xen VM images to Sun VirtualBox

Conversion of Xen VM images to Sun VirtualBox Virtual Machines.

This year I have been using Xen on Ubuntu with a debian kernel. This works with Intrepid and Jaunty, however I am expecting problems with the upcoming released of Karmic Koala. Once more Canonical have not included a Xen kernel in the distution, choosing instead to support KVM. For this reason I have been looking at other Virtualization Solutions as my machines doesn't VT-X style techinology required by KVM. I am still extremely keen on Xen, and will likely switch back when I can work out how to make it work reliably on Karmic. In the mean time I have beeen looking and the Sun VirtualBox solution. This is  available in the Ubuntu Repositories. When porting to virtual box there are a number of problems need to be resolved.I chose this over Vmware, firstly because its open source, and secondly it just seems lighter. The VMware server 2.0 web interface is very comprehensive but it desperately slow on my hardware. Another nice thing about VirtualBox is that you can script almost every aspect of the creation and management of virtual machines.


  1. The image files I am using in Xen are raw sparse loopback files, they just contain a filesystem (ext3), and no partition table for example. VirtualBox expects a disk image in its own format.
  2. The images files dont contain grub or kernel images. The kernel stored outside the xen image and loaded by xen (I use one kernel image in fact for all the vms)


Creating an image in the right format

Firstly create an empty image file

dd if=/dev/zero of=amandas3.raw bs=1 count=1 seek=384G 

If you make this large, the final step of converting the image with the VirtualBox tool which take much longer, but it will probably be easier to resize partitions in your final running machine

Then create an populate partition table inside the empty image file. The partitions you create should be a little bigger than the images you create in order to be on the safe side.

parted amandas3.raw mklabel msdos
parted amandas3.raw mkpartfs primary ext2 0 10G
parted amandas3.raw mkpartfs primary ext2 10G 14G
#repead for the number of partitions you need
parted amandas3.raw print all #just check what you have done

Then map the partitions in the image into the device mapper

sudo kpartx -a amandas3.raw 

kpartx is quite a useful too, if your image file contains LVM volumnes, it will even map these into the device mapper.


Copy in your image file into the partition. Repeat this for each image you have into its respective partition. Check using ls - /dev/mapper to see the names of the parititions.

sudo dd if=domains/amandas3/root.img of=/dev/mapper/loop1p1


Resize the file system to fit the destination partition. Repeat as needed for each partition.

sudo e2fsck -f /dev/mapper/loop1p1
sudo resize2fs /dev/mapper/loop1p1


Unmount the file from the loopback device and remove the mappings.

sudo kpartx -d amandas3.raw 

Convert the raw image file into the Virtual Box internal format.

VBoxManage convertdd ./amandas3.raw ../virtualbox/amandas3.vdi


Installing Grub and the kernels

These instructions are more Ubuntu specific, and may need to be tailored if you use a different distribution. However try the following:

Create a VM using the image you created, and attach and ISO bootable image to the CDROM. Boot into the cd inside Virtualbox.

Open a terminal window, and create a chroot environment

sudo su - 
mkdir /mnt/tmp
mount -t auto /dev/sda1 /mnt/tmp
mount --bind /dev /mnt/tmp/dev
cp /etc/resolv.conf /mnt/tmp/etc/
chroot /mnt/tmp /bin/bash
mount -t proc proc proc

Install the linux image. This should also download and install grub (though not yet make it bootable)

apt-get update
apt-get install linux-image

Using nano or some similar tool, check and fix the contents of /etc/mtab and /etc/fstab. Typicall you will want to change things from /dev/sda1 instead of /dev/xda2 and similar

Install Grub into the bootsector and create the bootmenu.

grub-install --recheck /dev/sda
update-grub
depmod -a

Shutdown the virtual machine amd then detach the iso image. restart the VM, it should now boot.

If you don't get a network conection, just theck the file /etc//udev/rules.d/70-persistent-net.rules and make sure it is clean.

Posted By: admin on Oct 07, 2009 01:51PM Add Comment