Written August 10, 2008 in Tutorial

The following instructions explain how to convert a VMWare (CentOS 5) virtual machine, into a Xen DomU guest machine. The basis for this tutorial was this post by David Nalley.

With VMWare machine powered up

Change /etc/modprobe.conf

/etc/modprobe.conf on a VMWare guest will look something like this:

[sourcecode language=bash]alias eth0 pcnet32
alias scsi_hostadapter mptbase
alias scsi_hostadapter1 mptspi[/sourcecode]

Change it to:

[sourcecode language=bash]alias eth0 xennet
alias scsi_hostadapter xenblk[/sourcecode]

Change /etc/inittab

Edit /etc/inittab, and find the following:

[sourcecode language=bash]# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6[/sourcecode]

Add a line for the console, and comment out the “mingetty” lines:

[sourcecode language=bash]# Run gettys in standard runlevels
co:2345:respawn:/sbin/agetty xvc0 9600 vt100-nav
#1:2345:respawn:/sbin/mingetty tty1
#2:2345:respawn:/sbin/mingetty tty2
#3:2345:respawn:/sbin/mingetty tty3
#4:2345:respawn:/sbin/mingetty tty4
#5:2345:respawn:/sbin/mingetty tty5
#6:2345:respawn:/sbin/mingetty tty6[/sourcecode]

Shutdown VMWare Machine

Now shutdown your VMware machine.

With VMWare machine powered off, on the VMWARE host

“Flatten” your VMDK file

VMWare splits its VMs images over multiple 2GB files. We need to create one image file to convert.

Log into the VM host, change to the directory where your VMWare image files are stored:

[sourcecode language=bash][root@vmwarehost myvm]# ls -la
total 10496388
drwxr-xr-x  2 root root       4096 Aug  8 11:27 .
drwxrwxrwt 11 root root       4096 Aug  4 17:13 ..
-rw-------  1 root root 2147221504 Aug  8 11:27 myvm-f001.vmdk
-rw-------  1 root root 2147221504 Aug  8 11:27 myvm-f002.vmdk
-rw-------  1 root root 2147221504 Aug  8 11:27 myvm-f003.vmdk
-rw-------  1 root root 2147221504 Aug  8 04:02 myvm-f004.vmdk
-rw-------  1 root root 2147221504 Aug  8 11:27 myvm-f005.vmdk
-rw-------  1 root root    1310720 Jan 15  2008 myvm-f006.vmdk
-rw-------  1 root root        533 Jun  3 15:47 myvm.vmdk
-rw-r--r--  1 root root      65152 Aug  8 11:27 vmware.log
[root@vmwarehost myvm]#[/sourcecode]

Your primary image is the one without “-fxxx” after it. Convert it to a flat file, by running:

vmware-vdiskmanager -r vmware_image.vmdk -t 0 temporary_image.vmdk

In real life:

[sourcecode language=bash][root@vmwarehost myvm]# vmware-vdiskmanager -r myvm.vmdk -t 0 temporary_image.vmdk
Using log file /tmp/vmware-root/vdiskmanager.log
Creating a monolithic growable disk 'temporary_image.vmdk'
  Convert: 100% done.
Virtual disk conversion successful.
[root@vmwarehost myvm]#[/sourcecode]

Convert VMDK to Raw Image File

Now we use qemu to convert our flat file to a Xen-readable format:

[sourcecode language=bash][root@vmwarehost myvm]# qemu-img convert -f vmdk temporary_image.vmdk -O raw myvm.img
[root@vmwarehost myvm]#[/sourcecode]

A directory listing will confirm that our image has been created. Note that the Xen image is not a “growable” image, it’s at its full size:

[sourcecode language=bash][root@vmwarehost myvm]# ls -la
total 15324708
drwxr-xr-x  2 root root        4096 Aug  8 11:46 .
drwxrwxrwt 11 root root        4096 Aug  4 17:13 ..
-rw-------  1 root root 2147221504 Aug  8 11:27 myvm-f001.vmdk
-rw-------  1 root root 2147221504 Aug  8 11:27 myvm-f002.vmdk
-rw-------  1 root root 2147221504 Aug  8 11:27 myvm-f003.vmdk
-rw-------  1 root root 2147221504 Aug  8 04:02 myvm-f004.vmdk
-rw-------  1 root root 2147221504 Aug  8 11:27 myvm-f005.vmdk
-rw-------  1 root root    1310720 Jan 15  2008 myvm-f006.vmdk
-rw-r--r--  1 root root 10737418240 Aug  8 11:54 myvm.img
-rw-------  1 root root        533 Jun  3 15:47 myvm.vmdk
-rw-------  1 root root  2483683328 Aug  8 11:40 temporary_image.vmdk
-rw-r--r--  1 root root       65152 Aug  8 11:27 vmware.log
[root@vmwarehost myvm]#[/sourcecode]

Transfer image to Xen host

Now that you have a Xen-compatible image, transfer it to the Xen host. (Unless you’re converting your VMWare host to a Xen host)

With Xen machine powered off, on the Xen host

Mount the image file

We need to make a few more tweaks, so make a mountpoint (/mnt/tmp), and mount your image there using “lomount”, like this:

[sourcecode language=bash]# usageĀ : lomount -diskimage path-to-image -partition (normally 1 but if it fails increment it) mountpoint
lomount -diskimage /xen/myvm/myvm.img -partition 1 /mnt/tmp/[/sourcecode]

Then, because we’re going to work with yum “inside” the image, mount /proc and /sys inside the image (so that we have a friendly chroot environment)

[sourcecode language=bash]mount -o bind /proc/ /mnt/tmp/proc/
mount -o bind /sys/ /mnt/tmp/sys[/sourcecode]

Change into your mount directory, and type “chroot” to enter the chroot environment:

[sourcecode language=bash]cd /mnt/tmp
chroot[/sourcecode]

Now you’re “inside” your VM’s filesystem, which is where you need to be to install a Xen guest kernel. (If you install the kernel earlier, yum auto-installs a Xen Dom0 kernel instead)

Confirm fstab entries

Check /etc/fstab. If your fstab file is standard, it’ll be using labels instead of devices, like below:

[sourcecode language=bash]LABEL=/                 /                       ext3    defaults        1 1
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
sysfs                   /sys                    sysfs   defaults        0 0
LABEL=SWAP-hda2         swap                    swap    defaults        0 0[/sourcecode]

If this is the case, you don’t have to change anything.

If your fstab refers to physical devices (/dev/hda1), you’ll need to adjust these, by changing “hda” to “xvda”, so that “/dev/hda3″ becomes “/dev/xvda3″

Install Xen kernel

Install the Xen guest kernel, by running “yum install kernel-xen”:

[sourcecode language=bash][root@xenhost /]# yum install kernel-xen
Loading "installonlyn" plugin
Repository base is listed more than once in the configuration
Setting up Install Process
Setting up repositories
Reading repository metadata in from local files
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Package kernel-xen.i686 0:2.6.18-92.1.10.el5 set to be installed
--> Running transaction check

Dependencies Resolved

=============================================================================
 Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
 kernel-xen              i686       2.6.18-92.1.10.el5  updates            15 M

Transaction Summary
=============================================================================
Install      1 Package(s)
Update       0 Package(s)
Remove       0 Package(s)         

Total download size: 15 M
Is this ok [y/N]: y
Downloading Packages:
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: kernel-xen                   ######################### [1/1]
grubby fatal error: unable to find a suitable template

Installed: kernel-xen.i686 0:2.6.18-92.1.10.el5
Complete!
[root@xenhost /]#[/sourcecode]

Note the error after installation. The next section explains why that happens, and what to do.

Manually create entry in /etc/grub/grub.conf

Because you installed the kernel inside a chroot, and there’s no entry for the current running kernel (on your host) in the chroot’s /boot/grub/grub.conf, yum wasn’t able to automatically create new kernel entries in grub.conf for you. So you’ll have to do it yourself.

Take note of the kernel version installed above, and then create an entry at the top of grub.conf like this:

[sourcecode language=bash]title CentOS (2.6.18-92.1.10.el5xen)
        root (hd0,0)
        kernel /boot/vmlinuz-2.6.18-92.1.10.el5xen ro root=LABEL=/ console=xvc0
        initrd /boot/initrd-2.6.18-92.1.10.el5xen.img[/sourcecode]

You may as well delete all the old grub configs from the file – they won’t work anyway.

Make sure that grub will default to your new config, by adding the “default” line before your “title” line:

[sourcecode language=bash]default = 0
title CentOS (2.6.18-92.1.10.el5xen)
        root (hd0,0)
[...][/sourcecode]

Exit the chroot

Exit the chroot, and unmount the mounted filesystems

[sourcecode language=bash][root@xenhost ~]# exit
[root@xenhost mnt]# umount /mnt/tmp/sys/
[root@xenhost mnt]# umount /mnt/tmp/proc/
[root@xenhost mnt]# umount /mnt/tmp/
[root@xenhost mnt]#[/sourcecode]

Setup Xen configuration

Create a Xen config file to point to your image file. It should look something like this:

[sourcecode language=bash]extra = "3"
name = "myvm"
memory = "200"
disk = [ 'tap:aio:/xen/myvm/myvm.img,xvda,w', ]
vif = [ 'bridge=xenbr0', ]
bootloader="/usr/bin/pygrub"
vcpus=1
on_reboot = 'restart'
on_crash = 'restart'[/sourcecode]

Startup your Xen machine

You’re done. Start your machine with “xm create name.conf”, or use “xm create -c name.conf” to auto-connect to the console on startup.

Related posts:

  1. How to convert a .BIN file to .TRX for OpenWRT / DD-WRT
  2. No network on your CentOS Xen Host? Got 2 NICs?
  3. How to make VMWare vSphere Client work on Windows 7
  4. Serial Port (ttyS0) pass-thru to Xen domU on CentOS 5

2 Comments

  • At 2009.02.04 02:26, geekyB said:

    I have put up a document to convert Windows guest OS that is running on vmware server to Xen guest. Check out the vmware to xen conversion guide

    • At 2009.12.15 14:37, Michael Barto said:

      In following your instructions, I cannot get chroot to work. It fails with command not found. in all cases. Is the command broken in Centos? I am running Centos 5.3 latest update with Xen installed and I am trying to more an img from VMware Server.

      (Required)
      (Required, will not be published)
      CommentLuv Enabled