Saturday, August 14, 2010

Xen Virtualization - Installation & Configuration HOWTO


Installing and Configuring Xen
Xen is a server virtualisation solution that works on the principle of Paravirtualisation. 
Paravirtualization requires modifying the operating system to run on Xen. Paravirtualized operating systems "know" that they are virtualized. 
This is achieved by modifying the kernel. Xen is a virtual machine hypervisor.
Xen Hypervisor Requirements

  • A pre-existing Linux or BSD installation as kernel needs to be modified, preferably something running Kernel 2.6 and having Grub as bootloader.
  • x86-compatible system with at least 256MB RAM. More is better.
  • 5GB of spare disk space additional to the disk space you need to install your favorite distribution.
  • Hardware based Xen virtualisation requires Intel VT or AMD-V ( Pacifica ) Processor. To check for Intel VT support look for the 'vmx' flag, or for AMD-V support check for 'svm' flag.
Checking if the processor supports hardware virtualisation.
For Intel
cat /proc/cpuinfo|grep vmx
For AMD
cat /proc/cpuinfo|grep vmx
Installing Xen
There are 3 ways of installing Xen. But installing using yum is the fastest and easiest option. Following are the 3 methods by which Xen can be installed.


1. Using Source code.
2. Using RPM.
3. Using YUM.
Installing using RPM
Download the xen and xen-kernal RPM's according to your OS from and the Xen version you require
http://xen.xensource.com/download/

xen-3.1.0-1.i386.rpm

kernel-xen-2.6.18-3.1.0.i386.rpm

Install each of the the RPM's as shown below.  


# rpm -ivh xen-3.1.0-1.i386.rpm
# rpm -ivh kernel-xen-2.6.18-3.1.0.i386.rpm
After this you need to edit your system's grub config and add a stanza for the Xen hypervisor.


An example of a correctly configured /etc/grub.conf file is given below.
=====================================================================
timeout=5
default=0
splashimage=(hd0,0)/boot/grub/splash.xpm.gz
title Fedora Core (2.6.18-1.2798.fc6xen)
root (hd0,0)
kernel /boot/xen.gz-2.6.18-1.2798.fc6
module /boot/vmlinuz-2.6.18-1.2798.fc6xen ro root=LABEL=/
module /boot/initrd-2.6.18-1.2798.fc6xen.img
title Fedora Core (2.6.18-1.2798.fc6)
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-1.2798.fc6 ro root=LABEL=/
initrd /boot/initrd-2.6.18-1.2798.fc6.img
=====================================================
reboot the machine into the xen kernel. Once the system is booted into the Xen kernel, check to verify the kernel and that Xen is running:
# uname -r
2.6.18-1.2798.fc6xen ( This shows the Xen kernall is running ).
# xm list
Name ID Mem(MiB) VCPUs State Time(s)
Domain-0 0 492 1 r—– 1041.7 (Shows the setting of Dom-0)
Building a Guest System on Xen
There are many methods of installing the Guest OS like using virt-install , virt-manager ( GUI option) or using images of OS's. Using the images of OS's is the easiest option. 
We can download the images from sites like http://jailtime.org and use it as a template any number of times. This is also the fastest as we need not install the OS's.
Building a Guest System using `virt-install`
Start the interactive install process by running the virt-install program:
# /usr/sbin/virt-install
The following questions about the new guest OS will be presented. This information can also be passed as command line options; run with an argument of –help for more details. 
Lets us consider the case of installing Fedora Core 6 as the Guest OS.
1.What is the name of your virtual machine? This is the label that will identify the guest OS. This label will be used for various xm commands and also appear in virt-manager the Gnome-panel Xen applet. In addition, it will be the name of the /etc/xen/ configuration file that stores the guest's configuration information.
2. How much RAM should be allocated (in megabytes)? This is the amount of RAM to be allocated for the guest instance in megabytes (eg, 256). Installation requires atleast 256 MB of RAM.
3. What would you like to use as the disk (path)? The local path and file name of the file to serve as the disk image for the guest (eg, /home/nitin/xenfc). This will be exported as a full disk to your guest.
4. How large would you like the disk to be (in gigabytes)? The size of the virtual disk for the guest (only appears if the file specified above does not already exist). 4.0 gigabytes is a reasonable size for a "default" install
5. Would you like to enable graphics support (yes or no): Should the graphical installer be used?
6. What is the install location? This is the path to a Fedora Core 6 installation tree in the format used by anaconda. NFS, FTP, and HTTP locations are all supported. Examples include:
nfs:my.nfs.server.com:/path/to/test2/tree/ *
http://my.http.server.com/path/to/tree/ *
ftp://my.ftp.server.com/path/to/tree
The installation will then commence. If graphics were enabled, a VNC window will open and present the graphical installer. If graphics were not enabled, the standard text installer will appear. Proceed as normal with the installation.
Building a Guest System using Images of OS's
In this process we download the images of root file system of required OS, swap . We then create an an initrd image for the DomU and xm (Xend manager ) to mount it as Root file system. Swap and Initrd of the DomU we are creating. We will consider the case of building a Centos DomU now.
1. Download the DomU image of required OS from http://jailtime.org/ or any other site providing OS images for virtualisation in a to a convenient location on the machine.
2. Untar the downloaded file. The tar file from jailtime.org normally contains the following files.

  1. centos.5-0.img (Image of OS file system )
  2. centos.swap (Swap of OS)
  3. centos.cfg ( Xen configuration file for OS)
3. Create the initrd for DomU using mkinitrd command specifying the kernel version of Dom0.
# mkinitrd /boot/initrd-centos-5.img 2.6.20-1.3001.fc6xen
Some times the initrd images created using above command may not be able to mount the root file system causing Kernel panic. To avoid this create the initrd specifying the following options.
# mkinitrd –with=xennet –preload=xenblk /boot/initrd-centos-5.img 2.6.20-1.3001.fc6xen
When installing kernel-xenU, the initrd is built without xenblk, rendering the system unbootable under Xen using that kernel.
4. In most cases tty will not be enabled which we require for using the console of guest OS (DomU). To enable console we will have to edit the etc/initab file of DomU. For this we mount the image of guest OS ( here centos.5-0.img) as a Loop back device.
# mount -o loop=/dev/loop1 /home/CentOS/centos.5-0.img /home/anydir/
We can now access the files in the image. Open the inittab file and add the following line to it
co:2345:respawn:/sbin/mingetty console
and comment the following lines as shown below.
#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
If that does not work try replacing tty with console to the line specifying getty or mingetty like that is given below.
5:2345:respawn:/sbin/mingetty console
After editing the file unmount the image.
5. Now we need to create the configuration file for DomU. Copy the configuration file provided with the image centos.cfg to preferably /etc/xen directory (Default configuration directory of Xen).
Sample configuration file
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
kernel = "/boot/vmlinuz-2.6.20-1.3001.fc6xen" #Xen Image ( location of kernel of Dom0)
ramdisk = "/boot/initrd-centos-5.img" #DomU's initrd
memory = 256 # DomU memory
name = "centos.5-0″ # Name of DomU
vif = [ '' ]
disk = ['file:/home/CentOS/centos.5-0.img,sda1,w' , 'file:/home/CentOS/centos.swap,sda2,w'] # Location of DomU root filesystem & Swap
ip = "192.168.1.116″ # DomU IP address
gateway = "192.168.1.1″
netmask = "255.255.255.0″
root = "/dev/sda1 ro"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Sometimes the network of DomU may not function as xenbr0 ( xen virtual bridge) may not get connected to virtual interface vif. In such case add the following line to xen configuration file.
vif = [ 'bridge=xenbr0', ]
6. Create DomU and mount the file system using following command.
# xm create /etc/xen/centos.cfg -c
( Providing the full path to configuration file or just configuration filename if it inside /etc/xen/ )
The Xend manager (xm) creates the DomU reading the configurations provided in config file. The -c option lets us see all the steps involved in creating and booting into the DomU kernel.
7. We can now check and verify if the DomU is running using the xm list command.
# xm list
Name ID Mem(MiB) VCPUs State Time(s)
Domain-0 0 610 1 r—– 12492.1


centos 1 256 1 r—– 256.7


States of domains

  • r – Running
  • b- Blocked
  • c -crashed
  • s -Shutdown
  • p -Paused
We can now enter the console of guest domain using following command.
# xm console
Managing Domains with xm
1. Connecting to a Domain
You can use xm to connect to a domain or virtual machine:
# xm console domain-id
This causes the console to attach to the domain-id's text console.
2. Creating a Domain
You can use xm to make a domain:
# xm create domain001 [-c]
This creates a domain named domain001 with the file residing in the /etc/xen/ directory. The [-c]option aids with troubleshooting by allowing you to connect to the text console.
3. Saving a Domain
You can use xm to save a domain:
# xm save [domain-id] [statefile]
4. Terminating a Domain ID
You can use xm to terminate a domain-id:
# xm destroy [domain-id]
This instantly terminates the domain-id.
5. Shutting Down a Domain
You can use xm to shut down any domain:
# xm shutdown [domain-id]
6. Suspending a Domain
You can use xm to suspend a domain:
# xm suspend [domain-id]
7. Resuming a Domain
You can use xm to resume a previously suspended session:
# xm resume [domain-id]
8. Rebooting a Domain
You can use xm to reboot a domain:
# xm reboot [domain-id]
9. Renaming a Domain
You can use xm to assign a new name to an existing domain:
# xm rename [domain-name] [new domain-name]
Domain renaming will keep the same settings (same hard disk, same memory, etc.).
10. Pausing a Domain
You can use xm to pause a domain:
# xm pause [domain-id]
11. Unpausing a Domain
You can use xm to unpause a domain:
# xm unpause [domain-id]
This makes the domain available for scheduling by a hypervisor.
12. Configuring Memory Allocation
You can use xm to modify a domain's memory allocation:
# xm mem-set [domain-id] [count]
13. Configuring Maximum Memory
You can use xm to modify a domain's maximum memory:
# xm mem-max [domain-id] [count]
You must specify the [count] in megabytes.