Create a Xen Virtual Machine

This tutorial shows how to create a VM (virtual machine) on Xen and install CentOS 6.0.

1. Prepare Virtual Disk Image

Xen supports to run VM on physical device or virtual disk image. Here I am going to use virtual disk image in raw format. The below creates a 8G virtual disk for root partition, and a 512M virtual disk for swap:

# dd if=/dev/zero of=centos6.img bs=4K count=0 seek=2048K
# dd if=/dev/zero of=centos6.swp bs=4K count=0 seek=128K

Or, you can use qemu-img utility to replace dd:

# qemu-img create -f raw centos6.img 8G
# qemu-img create -f raw centos6.swp 512M

2. Prepare Configuration for Xen VM installation

Xen needs three files to install a VM: kernel, ramdisk, and a configuration file.

You can always find appropriate kernel and ramdisk from your Linux distribution’s website, for example, CentOS 6.0:

# wget http://mirror.centos.org/centos/6.0/os/x86_64/isolinux/vmlinuz
# wget http://mirror.centos.org/centos/6.0/os/x86_64/isolinux/initrd.img

And create a Xen configuration file:

# vi centos6-x64-domu

kernel = "/data/vm/vmlinuz"
ramdisk = "/data/vm/initrd.img"
name = "xc6"
memory = "512"
disk = [ "file:/data/vm/centos6.img,xvda,w","file:/data/vm/centos6.swp,xvdb,w" ]
vif = [ "bridge=br0" ]
vcpus = 1
on_reboot = "destroy"
on_crash = "destroy"

Notes:

file: uses dom0 kernel page cache, and thus might give better performance than phy: or tap:aio:, but it’s also more insecure because of caching. tap:aio: uses direct IO, so it bypasses dom0 kernel cache, and works like phy: in that sense. Refer to blktap on Xen’s wiki to understand more.

Please be aware:
1, your dom0 must have loaded blktap modules before you can use tap:aio:. Not every Linux distribution has built everything in.
2, if your blktap is version 2 (i.e., blktap2), for example, Debian Wheezy, you need to use tap2:tapdisk:aio: etc.

3. Start Installing

Now it’s time to create the VM and start installing CentOS 6.0 in it:

# xm create centos6-x64-domu

Then you can connect to the virtual console, and do regular OS installation:

# xm console xc6

4. Change Configuration for Xen VM Normal Operation

After the OS is installed, the VM is destroyed. You need to edit the configuration from Xen VM installation to normal operation.

# vi centos6-x64-domu

name = "xc6"
memory = "512"
disk = [ "file:/data/vm/centos6.img,xvda,w","file:/data/vm/centos6.swp,xvdb,w" ]
vif = [ "bridge=br0" ]
bootloader = "/usr/lib/xen-4.1/bin/pygrub"
vcpus = 1
on_reboot = "restart"
on_crash = "destroy"

The new configuration uses pygrub as a bootloader that acts grub and uses grub.conf from inside the VM. Of course, you can continue using the separated kernel and ramdisk, but this means you need to maitain them by yourselves.

Then you can start your VM by typing:

# xm create centos6-x64-domu

Have fun!

This entry was posted in Cloud Computing and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">