Wednesday, July 28, 2010

Installing Xen on CentOS 5.0


Xen lets you create secondary operating systems, called virtual machines or domus, under the primary operating system (dom0). Using Xen we can separate applications in different virtual machines that are completely independent of one another, (eg virtual machines for mail server for a high-traffic website, one for DNS, etc.) but always the same hardware. This saves money, and most importantly, it is safer. If the DNS server virtual machine is violated by any malicious user, does not affect other virtual machines, it is also possible to move them from one server to another.
I will use CentOS 5.0 (i386) for Both the host OS (dom0) and the guest OS (domU).
We will use CentOS 5.0 (i386) for both the main machine (dom0) and virtual (domU)
1 Preliminary Note
We use the following partitions on the host machine with Centos 5.0 (dom0):
  • / Boot 150 MB (ext3)
  • 1GB swap
  • / 3GB (ext3)
  • / Vm the rest of the disk (ext3)
Create the virtual machines in the / vm, you can use some other directory that contains the remaining space and not have to create a partition for this.
mkdir / vm
[...]
title CentOS (2.6.18-8.1.4.el5xen)
root (hd0, 0)
kernel / xen.gz-2.6.18-8.1.4.el5
module / vmlinuz-2.6.18-8.1.4.el5xen ro root = / dev/VolGroup00/LogVol00
module / initrd-2.6.18-8.1.4.el5xen.img
[...]
For the file / boot / grub / menu.lst probably use:
[...]
title CentOS (2.6.18-8.1.4.el5xen)
root (hd0, 0)
kernel / boot/xen.gz-2.6.18-8.1.4.el5
module / boot/vmlinuz-2.6.18-8.1.4.el5xen ro root = / dev/VolGroup00/LogVol00
module / boot/initrd-2.6.18-8.1.4.el5xen.img
[...]


2 Installing Xen

To install xen, simply run:
yum install xen kernel-xen
This install Xen and Xen Kernel on CentOS system. Anyway, it found a new kernel in / boot /
ls-l / boot /
[Root @ server1 ~] # ls-l / boot /
Total 16 327
-Rw-r - r - 1 root root 62 154 April 9 16:30 config-2.6.18-8.1.1.el5
-Rw-r - r - 1 root root 61 057 May 17 12:12 config-2.6.18-8.1.4.el5xen
-Rw-r - r - 1 root root 16 Mar 1:19 62 150 config-2.6.18-8.el5
drwxr-xr-x 2 root root 1024 June 8 00:14 grub
-Rw ------- 1 root root 2318110 April 13 17:10 initrd-2.6.18-8.1.1.el5.img
-Rw ------- 1 root root 2320081 June 8 00:14 initrd-2.6.18-8.1.4.el5xen.img
-Rw ------- 1 root root 2318126 April 13 16:18 initrd-2.6.18-8.el5.img
drwx ------ 2 root root 12 288 April 13 16:05 lost + found
-Rw-r - r - 1 root root 80 032 16:49 April 1 message
-Rw-r - r - 1 root root 83 542 16:31 April 9 symvers-2.6.18-8.1.1.el5.gz
-Rw-r - r - 1 root root 84 906 May 17 12:13 symvers-2.6.18-8.1.4.el5xen.gz
-Rw-r - r - 1 root root 16 Mar 83 542 1:20 symvers-2.6.18-8.el5.gz
-Rw-r - r - 1 root root 884 787 April 9 16:30 System.map-2.6.18-8.1.1.el5
-Rw-r - r - 1 root root 868 062 May 17 12:12 System.map-2.6.18-8.1.4.el5xen
-Rw-r - r - 1 root root 16 Mar 1:19 884 787 System.map-2.6.18-8.el5
-Rw-r - r - 1 root root 1765460 April 9 16:30 vmlinuz-2.6.18-8.1.1.el5
-Rw-r - r - 1 root root 2075341 May 17 12:12 vmlinuz-2.6.18-8.1.4.el5xen
-Rw-r - r - 1 root root 1765428 Mar 16 1:19 vmlinuz-2.6.18-8.el5
-Rw-r - r - 1 root root 274 228 May 17 9:13 xen.gz-2.6.18-8.1.4.el5
-Rwxr-xr-x 1 root root 608 568 May 17 12:28 xen-syms-2.6.18-8.1.4.el5
[Root @ server1 ~] #
Before starting the system using the Xen kernel, add the arguments for the grub, then opens the file with an editor
vi / boot / grub / menu.lst
and add the arguments over the other arguments the kernel:
[...]
title CentOS (2.6.18-8.1.4.el5xen)
root (hd0, 0)
kernel / xen.gz-2.6.18-8.1.4.el5
module / vmlinuz-2.6.18-8.1.4.el5xen ro root = / dev/VolGroup00/LogVol00
module / initrd-2.6.18-8.1.4.el5xen.img
[...]
Change the value to 0:
[...]
default = 0
[...]
The full list of / boot / grub / menu.lst should look as follows
# Grub.conf generated by anaconda
#
# Note That You Do not have to rerun grub after-Making Change to this file
# NOTICE: You have a / boot partition. This Means That
# All kernel and initrd paths are relative to / boot /, eg.
# Root (hd0, 0)
# Kernel / vmlinuz-version ro root = / dev/VolGroup00/LogVol00
# Initrd / initrd-version.img
# Boot = / dev / sda
default = 0
timeout = 5
splashimage = (hd0, 0) / grub / splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-8.1.4.el5xen)
root (hd0, 0)
kernel / xen.gz-2.6.18-8.1.4.el5
module / vmlinuz-2.6.18-8.1.4.el5xen ro root = / dev/VolGroup00/LogVol00
module / initrd-2.6.18-8.1.4.el5xen.img
title CentOS (2.6.18-8.1.1.el5)
root (hd0, 0)
kernel / vmlinuz-2.6.18-8.1.1.el5 ro root = / dev/VolGroup00/LogVol00
initrd / initrd-2.6.18-8.1.1.el5.img
title CentOS (2.6.18-8.el5)
root (hd0, 0)
kernel / vmlinuz-2.6.18-8.el5 ro root = / dev/VolGroup00/LogVol00
initrd / initrd-2.6.18-8.el5.img
Now restart the system
shutdown-r now
Automatically The system should now boot the new Xen kernel. After the system has booted, That We Can check by running
The system should now start automatically with the new Xen kernel, once started, we can see that is running
uname-r
[Root @ server1 ~] # uname-r
2.6.18-8.1.4.el5xen
[Root @ server1 ~] #
Now execute:
xm list
to see if Xen has already begun. This should show something like Domain-0 (dom0):
[Root @ server1 ~] # xm list
Name ID Mem (MiB) vcpus State Time (s)
Domain-0 0 350 1 r ----- 94.4
[Root @ server1 ~] #
 3 Creating the virtual machine
Center includes a good toolmaker called virt-install, where we can create Xen virtual machines. To get started, simply run:
virt-install
The tool does a few questions before creating the virtual machine. The name of the virtual machine called vm01, with 256 of RAM and disk size of 4GB, this record will be saved in the file / vm/vm01.img:
What is the name of your virtual machine? <- Vm01
How much RAM should be Allocated (in megabytes)? <- 256 
What would you like to use as the disk (path)? <- / Vm/vm01.img
How large would you like the disk (/ vm/vm01.img) to be (in gigabytes)? <- 4 
Would you like to enable graphics support? (Yes or no) <- no
What is the install location? <- Http://wftp.tu-chemnitz.de/pub/linux/centos/5.0/os/i386
The graphic support question refers to the installer, not the virtual machine. You can start a graphical installer, but it is necessary to connect via VNC. In this case we choose the installer in text mode.
We specify a mirror close to where the installer can download the files needed for the installation of Centos 5.0 on the virtual machine, can also be through an NFS server only works with Red Hat based distributions:
Once you have answered the questions, virt-install starts the normal installation in text mode virtual machine called vm01.
After installation, we will console vm01. to abandon it, press CTRL. +].
virt-install the vm01 has created configuration file / etc/xen/vm01 for us (in dom0). It should look like this:
virt-install created the configuration file for vm01 in / etc/xen/vm01, would be of
follows:
cat / etc/xen/vm01
# Automatically generated xen config file
name = "vm01"
memory = "256"
disk = ['tap: aio: / vm/vm01.img, xvda, w',]
vif = ['mac = 00:16:3 e: 13: e4: 81, bridge = xenbr0',]

uuid = "5aafecf1-dd66-401d-69cc-151c1cb8ac9e"
bootloader = "/ usr / bin / pygrub"
vcpus = 1
on_reboot = 'restart'
on_crash = 'restart'
Run
xm console vm01
To start session again in the virtual machine, or also used an ssh client to connect
To view the list of running virtual machines, typing:
xm list
The output should display like this:
[Root @ server1 xen] # xm list
Name ID Mem (MiB) vcpus State Time (s)
Domain-0 0 259 1 r ----- 1906.6
vm01 3255 1 ------ 137.9
[Root @ server1 xen] #
To turn vm01, run:
xm shutdown vm01
To restart it, execute:
xm create / etc/xen/vm01
If you want vm01 virtual machine starts automatically when you boot the system, is typed:
ln-s / etc/xen/vm01 / etc / xen / auto
Here are the most important Xen commands:
xm create-c / path / to / config - Starts the virtual machine xm shutdown - Stops the virtual machine.xm destroy  - Stops the virtual machine abruptly. Similar to press the power buttonxm list - List all running systems
xm console - Starts the virtual machine sessionxm help - Displays help for this command