Wednesday, February 8, 2012

Virtualization With Xen On CentOS 6.2 (x86_64) (Paravirtualization & Hardware Virtualization)


This tutorial provides step-by-step instructions on how to install Xen (version 4.1.2) on a CentOS 6.2 (x86_64) system.
Xen lets you create guest operating systems (*nix operating systems like Linux and FreeBSD), so called "virtual machines" or domUs, under a host operating system (dom0). Using Xen you can separate your applications into different virtual machines that are totally independent from each other (e.g. a virtual machine for a mail server, a virtual machine for a high-traffic web site, another virtual machine that serves your customers' web sites, a virtual machine for DNS, etc.), but still use the same hardware. This saves money, and what is even more important, it's more secure. If the virtual machine of your DNS server gets hacked, it has no effect on your other virtual machines. Plus, you can move virtual machines from one Xen server to the next one.
I will use CentOS 6.2 (x86_64) for both the host OS (dom0) and the guest OS (domU).
This howto is meant as a practical guide; it does not cover the theoretical backgrounds. They are treated in a lot of other documents in the web.
This document comes without warranty of any kind! I want to say that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

1 Preliminary Note

This guide will explain how to set up image-based virtual machines and also LVM-based virtual machines.
Make sure that SELinux is disabled or permissive:
vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted
If you had to modify /etc/sysconfig/selinux, please reboot the system:
reboot

2 Creating A Network Bridge

We need to set up a network bridge on our server so that our virtual machines can be accessed from other hosts as if they were physical systems in the network.
To do this, we install the package bridge-utils...
yum install bridge-utils
... and configure a bridge. Create the file /etc/sysconfig/network-scripts/ifcfg-br0 (please use the IPADDR, PREFIX, GATEWAY, DNS1 and DNS2 values from the /etc/sysconfig/network-scripts/ifcfg-eth0 file); make sure you use TYPE=Bridge, not TYPE=Ethernet:
vi /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
NM_CONTROLLED="yes"
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=none
IPADDR=192.168.0.100
PREFIX=24
GATEWAY=192.168.0.1
DNS1=8.8.8.8
DNS2=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System br0"
Modify /etc/sysconfig/network-scripts/ifcfg-eth0 as follows (comment out BOOTPROTO, IPADDR, PREFIX, GATEWAY, DNS1, and DNS2 and add BRIDGE=br0):
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=00:1E:90:F3:F0:02
TYPE=Ethernet
#BOOTPROTO=none
#IPADDR=192.168.0.100
#PREFIX=24
#GATEWAY=192.168.0.1
#DNS1=8.8.8.8
#DNS2=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
BRIDGE=br0
Restart the network...
/etc/init.d/network restart
... and run
ifconfig
It should now show the network bridge (br0):
[root@server1 ~]# ifconfig
br0       Link encap:Ethernet  HWaddr 00:1E:90:F3:F0:02
          inet addr:192.168.0.100  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::21e:90ff:fef3:f002/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:17 errors:0 dropped:0 overruns:0 frame:0
          TX packets:29 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1196 (1.1 KiB)  TX bytes:2794 (2.7 KiB)

eth0      Link encap:Ethernet  HWaddr 00:1E:90:F3:F0:02
          inet6 addr: fe80::21e:90ff:fef3:f002/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4554 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3020 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6249612 (5.9 MiB)  TX bytes:254928 (248.9 KiB)
          Interrupt:25 Base address:0x6000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1304 (1.2 KiB)  TX bytes:1304 (1.2 KiB)

[root@server1 ~]#
 

3 Installing Xen

First check if your CPU supports hardware virtualization - if this is the case, the command
egrep '(vmx|svm)' --color=always /proc/cpuinfo
should display something, e.g. like this:
[root@server1 ~]# egrep '(vmx|svm)' --color=always /proc/cpuinfo
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall
 nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy misalignsse
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall
 nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy misalignsse
[root@server1 ~]#
If nothing is displayed, then your processor doesn't support hardware virtualization. This means you can use only paravirtualization with Xen, but not hardware virtualization.
As CentOS 6 is based on RedHat 6, and RedHat has dropped support for Xen in version 6, we need to get Xen from a third-party repository. We can enable the repo as follows:
yum install wget
cd /etc/yum.repos.d/
wget http://www.crc.id.au/repo/kernel-xen.repo
To install Xen, we now simply run
yum install kernel-xen xen
This installs Xen and a Xen kernel on our CentOS system.
Before we can boot the system with the Xen kernel, please check your GRUB bootloader configuration. We open /boot/grub/menu.lst:
vi /boot/grub/menu.lst
The first listed kernel should be the Xen kernel that you've just installed:
[...]
title CentOS (2.6.32.54-1.el6xen.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32.54-1.el6xen.x86_64 ro root=/dev/mapper/VolGroup00-LogVol00 rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=de rd_LVM_LV=VolGroup00/LogVol01 rd_LVM_LV=VolGroup00/LogVol00 rd_NO_DM
        initrd /initramfs-2.6.32.54-1.el6xen.x86_64.img
[...]
We need to modify that section so that the Xen hypervisor gets loaded first. In the kernel /vmlinuz... line, replace the first word kernel with module. Do the same in the next line - replace the first word initrd with module in the initrd /initramfs... line. Then add the line kernel /xen.gz dom0_mem=1024M cpufreq=xen dom0_max_vcpus=1 dom0_vcpus_pin after the root line and before the first module line (if you have more than one CPU core, you can specify another number than 1 for dom0_max_vcpus). The final kernel section should look like this:
[...]
title CentOS (2.6.32.54-1.el6xen.x86_64)
        root (hd0,0)
        kernel /xen.gz dom0_mem=1024M cpufreq=xen dom0_max_vcpus=1 dom0_vcpus_pin
        module /vmlinuz-2.6.32.54-1.el6xen.x86_64 ro root=/dev/mapper/VolGroup00-LogVol00 rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=de rd_LVM_LV=VolGroup00/LogVol01 rd_LVM_LV=VolGroup00/LogVol00 rd_NO_DM
        module /initramfs-2.6.32.54-1.el6xen.x86_64.img
[...]
Change the value of default to 0 (so that the first kernel (the Xen kernel) will be booted by default):
[...]
default=0
[...]
The complete /boot/grub/menu.lst should look something like this:
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes 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/mapper/VolGroup00-LogVol00
#          initrd /initrd-[generic-]version.img
#boot=/dev/sde
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32.54-1.el6xen.x86_64)
        root (hd0,0)
        kernel /xen.gz dom0_mem=1024M cpufreq=xen dom0_max_vcpus=1 dom0_vcpus_pin
        module /vmlinuz-2.6.32.54-1.el6xen.x86_64 ro root=/dev/mapper/VolGroup00-LogVol00 rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=de rd_LVM_LV=VolGroup00/LogVol01 rd_LVM_LV=VolGroup00/LogVol00 rd_NO_DM
        module /initramfs-2.6.32.54-1.el6xen.x86_64.img
title CentOS (2.6.32-220.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-220.el6.x86_64 ro root=/dev/mapper/VolGroup00-LogVol00 rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=de rd_LVM_LV=VolGroup00/LogVol01 rd_LVM_LV=VolGroup00/LogVol00 rd_NO_DM
        initrd /initramfs-2.6.32-220.el6.x86_64.img
Before we reboot, we install the libvirt and python-virtinst (which contains the virt-install tool which we will use later on to install Xen VMs) packages:
yum install libvirt python-virtinst
Because the libvirt package from CentOS 6/RedHat 6 has no support for Xen, we must rebuild it with Xen support. To do this, we install a few prerequisites now:
yum groupinstall 'Development Tools'
yum install python-devel xen-devel libxml2-devel xhtml1-dtds readline-devel ncurses-devel libtasn1-devel gnutls-devel augeas libudev-devel libpciaccess-devel yajl-devel sanlock-devel libpcap-devel libnl-devel avahi-devel libselinux-devel cyrus-sasl-devel parted-devel device-mapper-devel numactl-devel libcap-ng-devel netcf-devel libcurl-devel audit-libs-devel systemtap-sdt-devel
Let's find out our libvirt version:
rpm -qa | grep libvirt
[root@server1 ~]# rpm -qa | grep libvirt
libvirt-client-0.9.4-23.el6_2.4.x86_64
libvirt-0.9.4-23.el6_2.4.x86_64
libvirt-python-0.9.4-23.el6_2.4.x86_64
[root@server1 ~]#
It's 0.9.4, so we download the appropriate src.rpm package into /root/src and install it:
mkdir /root/src
cd /root/src
wget http://vault.centos.org/6.2/os/Source/SPackages/libvirt-0.9.4-23.el6.src.rpm
rpm -i libvirt-0.9.4-23.el6.src.rpm
The last command will show some warnings that you can ignore:
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
Next we patch Xen support into the libvirt sources:
wget http://pasik.reaktio.net/xen/patches/libvirt-spec-rhel6-enable-xen.patch
cd /root/rpmbuild/SPECS
cp -a libvirt.spec libvirt.spec.orig
patch -p0 < ~/src/libvirt-spec-rhel6-enable-xen.patch
Now we build a new libvirt package:
rpmbuild -bb libvirt.spec
At the end of the build process you should see something like this:
Wrote: /root/rpmbuild/RPMS/x86_64/libvirt-0.9.4-23.el6.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/libvirt-client-0.9.4-23.el6.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/libvirt-devel-0.9.4-23.el6.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/libvirt-lock-sanlock-0.9.4-23.el6.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/libvirt-python-0.9.4-23.el6.x86_64.rpm
Wrote: /root/rpmbuild/RPMS/x86_64/libvirt-debuginfo-0.9.4-23.el6.x86_64.rpm
Go to the directory where the new packages have been created (/root/rpmbuild/RPMS/x86_64/ in this case)...
cd /root/rpmbuild/RPMS/x86_64/
... and install the new libvirt packages (with Xen support) as follows:
rpm -Uvh --force libvirt-0.9.4-23.el6.x86_64.rpm libvirt-client-0.9.4-23.el6.x86_64.rpm libvirt-python-0.9.4-23.el6.x86_64.rpm
Afterwards, we reboot the system:
reboot
The system should now automatically boot the new Xen kernel. After the system has booted, we can check that by running
uname -r
[root@server1 ~]# uname -r
2.6.32.54-1.el6xen.x86_64
[root@server1 ~]#
So it's really using the new Xen kernel!
We can now run
xm list
to check if Xen has started. It should list Domain-0 (dom0):
[root@server1 ~]# xm list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1024     1     r-----     18.9
[root@server1 ~]#
Instead of using the xm command, I will from now on use the virsh command to manage Xen VMs. This is the preferred way as we are using libvirt.
virsh list
should show this:
[root@server1 ~]# virsh list
 Id Name                 State
----------------------------------
  0 Domain-0             running

[root@server1 ~]#

4 Paravirtualization

4.1 Creating An Image-Based Virtual Machine

 
We will create our image-based virtual machines in the directory /var/lib/libvirt/images/ which was created automatically when we installed libvirt.
CentOS has a nice tool called virt-install with which we can create virtual machines for Xen. To start it, we simply run
virt-install --prompt --network bridge=br0 --virt-type=xen
(We must specify our network bridge with --network bridge=br0 so that the virtual machine uses our network bridge which we created in chapter2.)
The tools asks a few questions before it creates a virtual machine. I want to call my first virtual machine vm01, with 1024MB RAM and a disk size of 10GB. I want to store it in the file /var/lib/libvirt/images/vm01.img:
Would you like a fully virtualized guest (yes or no)? This will allow you to run unmodified operating systems.
<-- no
What is the name of your virtual machine?
<-- vm01
How much RAM should be allocated (in megabytes)?
<-- 1024
What would you like to use as the disk (file path)?
<-- /var/lib/libvirt/images/vm01.img
How large would you like the disk (/vm/vm01.img) to be (in gigabytes)?
<-- 10
What is the install URL?
<-- http://ftp.tu-chemnitz.de/pub/linux/centos/6.2/os/x86_64
In this chapter I want to create a paravirtualized guest, not a fully (hardware) virtualized guest, that's why I answer the first question with no.
As install URL, you should specify a mirror close to you where the installer can download all files needed for the installation of CentOS 6.2 in our virtual machine. You can find a list of CentOS mirrors here: http://www.centos.org/modules/tinycontent/index.php?id=13
After we have answered all questions, virt-install starts the normal CentOS 6.2 installer (in text mode) in our vm01 virtual machine. After a few moments the installer gives you the choice to use VNC or to continue with the text mode installer. Using text mode is no problem if you are familiar with the CentOS installer, but if you like a little more comfort, select Start VNC here:


On the next screen you can provide a password for the VNC connection (recommended). IF you don't want to use a password, hit No password:


Afterwards, the installer tells you how you can connect to the installer via VNC (192.168.0.15:1 in this case):


Now open a VNC client (like TightVNC) and type in the connection details (192.168.0.15:1 in this case) and click on Connect:

If you have specified a password for the VNC connection, you must now provide it:

Afterwards you can use the CentOS installer in your VNC client:


After the installation, we stay at the vm01 console. To leave it, type CTRL+] if you are at the console, or CTRL+5 if you're using PuTTY. You will then be back at the dom0 console.
Run
virsh console vm01
to log in on that virtual machine again (type CTRL+] if you are at the console, or CTRL+5 if you're using PuTTY to go back to dom0), or use an SSH client to connect to it.
To get a list of running virtual machines, type
virsh list
The output should look like this:
[root@server1 ~]# virsh list
 Id Name                 State
----------------------------------
  0 Domain-0             running
  2 vm01                 idle

[root@server1 ~]#
To shut down vm01, do this:
virsh shutdown vm01
To start vm01 again, run
virsh start vm01
Here are the most important Xen commands:
virsh start - Start a virtual machine.
virsh shutdown - Stop a virtual machine.
virsh destroy - Stop a virtual machine immediately without shutting it down. It's as if you switch off the power button.
virsh list - List all running systems.
virsh console - Log in on a virtual machine.
virsh help - List of all commands.

4.2 Creating An LVM-Based Virtual Machine (Paravirtualization)

This chapter explains how you can set up LVM-based virtual machines instead of virtual machines that use disk images. Virtual machines that use disk images are very slow and heavy on disk IO.
In this example I'm using a CentOS 6.2 host with the LVM volume group /dev/VolGroup00 that has about 465GB of space. /dev/VolGroup00 contains two logical volumes, /dev/VolGroup00/LogVol00 and /dev/VolGroup00/LogVol01 that consume about 104GB of space - the rest is not allocated and can be used to create logical volumes for our virtual machines:
vgdisplay
[root@server1 ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup00
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               465.26 GiB
  PE Size               4.00 MiB
  Total PE              119106
  Alloc PE / Size       26250 / 102.54 GiB
  Free  PE / Size       92856 / 362.72 GiB
  VG UUID               LCdZgy-jVrp-S92Q-RQeI-H87G-jiko-4fmaHF

[root@server1 ~]#
lvdisplay
[root@server1 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                n5remv-KYEE-O0Ry-g2r6-OrUe-rjyV-x65rHu
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                4.88 GiB
  Current LE             1250
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                CnvnGP-sDL8-OtBU-OzTT-VjZQ-ZIUo-yzISkX
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                97.66 GiB
  Current LE             25000
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1

[root@server1 ~]#
I want to create the virtual machine vm02 now which uses the logical volume /dev/VolGroup00/vm02. I want the virtual machine to have a disk space of 10GB, so I create the logical volume /dev/VolGroup00/vm02 as follows:
lvcreate -L10G -n vm02 VolGroup00
Afterwards we can run
virt-install --prompt --network bridge=br0 --virt-type=xen
again:
Would you like a fully virtualized guest (yes or no)? This will allow you to run unmodified operating systems.
<-- no
What is the name of your virtual machine?
<-- vm02
How much RAM should be allocated (in megabytes)?
<-- 1024
What would you like to use as the disk (file path)?
<-- /dev/VolGroup00/vm02
What is the install URL?
<-- http://ftp.tu-chemnitz.de/pub/linux/centos/6.2/os/x86_64
As the disk file path, we specify our new volume group /dev/VolGroup00/vm02. Please note that virt-install doesn't ask for the disk space anymore because the disk space is determined by the size of the logical volume (10GB).

4.3 Using virt-manager To Create & Manage Virtual Machines

 
Instead of using VNC or a text-based installer, we can use virt-manager on a Linux desktop to create and manage virtual machines. In this example, I'm using a Fedora 16 desktop.
Initialize a new VM on the CentOS 6.2 host (in this example, it's vm06):
virt-install --prompt --network bridge=br0 --virt-type=xen --vnc --noautoconsole
The --vnc --noautoconsole parameters are important - they allow us to connect to the installer with virt-manager.
[root@server1 ~]# virt-install --prompt --network bridge=br0 --virt-type=xen --vnc --noautoconsole
Would you like a fully virtualized guest (yes or no)? This will allow you to run unmodified operating systems.

<-- no
What is the name of your virtual machine?
<-- vm06
How much RAM should be allocated (in megabytes)?
<-- 1024
What would you like to use as the disk (file path)?
<-- /var/lib/libvirt/images/vm06.img
How large would you like the disk (/var/lib/libvirt/images/vm06.img) to be (in gigabytes)?
<-- 10
What is the install URL?
<-- http://ftp.tu-chemnitz.de/pub/linux/centos/6.2/os/x86_64
Starting install...
Retrieving file .treeinfo... | 798 B 00:00 ...
Retrieving file vmlinuz... | 7.5 MB 00:01 ...
Retrieving file initrd.img... | 59 MB 00:10 ...
Allocating 'vm06.img' | 10 GB 00:00
Creating domain... | 0 B 00:01
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
[root@server1 ~]#
As you see the command returns to the prompt, so we must use virt-manager now to complete the VM installation.
You can install virt-manager as follows on a Fedora 16 desktop - open a terminal and become root:
su
Then install virt-manager:
yum install virt-manager openssh-askpass
Go to Applications > System Tools > Virtual Machine Manager to start virt-manager:

When you start virt-manager for the first time, you will most likely see the following message (Packages required for KVM usage). You can ignore this because we don't want to connect to the local libvirt daemon, but to the one on our CentOS 6.2 Xen host. Click on No...


... and go to File > Add Connection... to connect to our CentOS 6.2 Xen host:


Select Xen as Hypervisor, then check Connect to remote host, select SSH in the Method drop-down menu, type in root as the Username and the hostname (server1.example.com) or IP address (192.168.0.100) of the CentOS 6.2 Xen host in the Hostname field. Then click on Connect:


If this is the first connection to the remote Xen server, you must type in yes and click on OK:


Afterwards type in the root password of the CentOS 6.2 Xen host:


You should see vm06 as running. Mark that guest and click on the Open button to open the graphical console of the guest:


Type in the root password of the Xen host again:


You should now be connected to the graphical console of the guest and see the CentOS installer:




Now install CentOS as you would normally do on a physical system. Please note that at the end of the installation, the CentOS guest needs a reboot. The guest will then stop, so you need to start it again, either with virt-manager or like this on our CentOS 6.2 Xen host command line:
CentOS 6.2 Xen Host:
virsh start vm06
Afterwards, you can connect to the guest again with virt-manager and configure the guest. If you install OpenSSH in the guest, you can connect to it with an SSH client (such as PuTTY).

5 Full Virtualization (Hardware Virtualization)

 
The process for creating a fully virtualized guest is not that much different from that for creating a paravirtualized guest, except that for full virtualization, you need virt-manager (installation cannot be completed on the command line) and installation media (CD/DVD or an .iso image).

5.1 Creating A Fully Virtualized Guest On The Command Line

Because we need installation media, you can now either put your CentOS 6.2 DVD (the first one) into your DVD drive, or you download the CentOS 6.2 iso file and put it in the /var/lib/libvirt/images/ directory (it must be in the /var/lib/libvirt/images/ directory because later on I will show how to create virtual machines through virt-manager from your Fedora desktop, and virt-manager will look for ISO images in the /var/lib/libvirt/images/ directory). IT is also possible to create an iso file from the DVD as follows:
dd if=/dev/cdrom of=/var/lib/libvirt/images/CentOS-6.2-x86_64-bin-DVD1.iso
The guest installation can be started as follows:
virt-install --prompt --network bridge=br0 --virt-type=xen --vnc --noautoconsole
[root@server1 ~]# virt-install --prompt --network bridge=br0 --virt-type=xen --vnc --noautoconsole
Would you like a fully virtualized guest (yes or no)? This will allow you to run unmodified operating systems.

<-- yes
What is the name of your virtual machine?
<-- vm03
How much RAM should be allocated (in megabytes)?
<-- 1024
What would you like to use as the disk (file path)?
<-- /var/lib/libvirt/images/vm03.img (you can as well specify a logical volume here, see chapter 4.2)
How large would you like the disk (/var/lib/libvirt/images/vm03.img) to be (in gigabytes)?
<-- 10
What is the install CD-ROM/ISO or URL?
<-- /dev/cdrom (you can also specify an iso file here, e.g. /var/lib/libvirt/images/CentOS-6.2-x86_64-bin-DVD1.iso)
Starting install...
Creating domain... | 0 B 00:01
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
[root@server1 ~]#
That's it! Now you must open virt-manager and complete the guest installation, as shown in chapter 4.3.

5.2 Creating A CentOS 6.2 Guest (Image-Based) From The Desktop With virt-manager

Instead of creating a virtual machine from the command line, you can as well create it from the Fedora desktop using virt-manager (of course, the virtual machine will be created on the CentOS 6.2 Xen host - in case you ask yourself if virt-manager is able to create virtual machines on remote systems).
(I have tried this for a paravirtualized guest as well, but got an error after initrd.img got loaded - therefore this chapter is only for fully-virtualized guests.)
To do this, click on the following button:

The New VM dialogue comes up. Fill in a name for the VM (e.g. vm07), select Local install media (ISO image or CDROM), and click on Forward:


Next check Use ISO image and click on the Browse... button:


Select the CentOS-6.2-x86_64-bin-DVD1.iso image and click on Choose Volume:


Now select Linux in the OS type drop-down menu and RedHat Enterprise Linux 6 in the Version drop-down menu, then click on Forward:


Assign memory and the number of CPUs to the virtual machine and click on Forward:


Now we come to the storage. Check Enable storage for this virtual machine, select Create a disk image on the computer's hard drive, specify the size of the hard drive (e.g. 10GB), and check Allocate entire disk now. Then click on Forward:


Now we come to the last step of the New VM dialogue. Go to the Advanced options section. Select Specify shared device name and then specify br0 in the Bridge name field; that is the name of the bridge which we created in chapter 2. Click on Finish afterwards:


The disk image for the VM is now being created:


Afterwards, the VM will start. Type in the root password of the CentOS 6.2 Xen host:


You should now be connected to the graphical console of the guest and see the CentOS installer:


Now install CentOS as you would normally do on a physical system.

6 Managing A Xen Guest From The Command Line

 
Xen guests can be managed through virsh, the "virtual shell". To connect to the virtual shell, run
virsh
This is how the virtual shell looks:
[root@server1 ~]# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh #
You can now type in commands on the virtual shell to manage your guests. Run
help
to get a list of available commands:
virsh # help
Grouped commands:

 Domain Management (help keyword 'domain'):
    attach-device                  attach device from an XML file
    attach-disk                    attach disk device
    attach-interface               attach network interface
    autostart                      autostart a domain
    blkiotune                      Get or set blkio parameters
    blockpull                      Populate a disk from its backing image.
    blockjob                       Manage active block operations.
    console                        connect to the guest console
    cpu-baseline                   compute baseline CPU
    cpu-compare                    compare host CPU with a CPU described by an XML file
    create                         create a domain from an XML file
    define                         define (but don't start) a domain from an XML file
    destroy                        destroy (stop) a domain
    detach-device                  detach device from an XML file
    detach-disk                    detach disk device
    detach-interface               detach network interface
    domid                          convert a domain name or UUID to domain id
    domjobabort                    abort active domain job
    domjobinfo                     domain job information
    domname                        convert a domain id or UUID to domain name
    domuuid                        convert a domain name or id to domain UUID
    domxml-from-native             Convert native config to domain XML
    domxml-to-native               Convert domain XML to native config
    dump                           dump the core of a domain to a file for analysis
    dumpxml                        domain information in XML
    edit                           edit XML configuration for a domain
    inject-nmi                     Inject NMI to the guest
    send-key                       Send keycodes to the guest
    managedsave                    managed save of a domain state
    managedsave-remove             Remove managed save of a domain
    maxvcpus                       connection vcpu maximum
    memtune                        Get or set memory parameters
    migrate                        migrate domain to another host
    migrate-setmaxdowntime         set maximum tolerable downtime
    migrate-setspeed               Set the maximum migration bandwidth
    reboot                         reboot a domain
    restore                        restore a domain from a saved state in a file
    resume                         resume a domain
    save                           save a domain state to a file
    save-image-define              redefine the XML for a domain's saved state file
    save-image-dumpxml             saved state domain information in XML
    save-image-edit                edit XML for a domain's saved state file
    schedinfo                      show/set scheduler parameters
    screenshot                     take a screenshot of a current domain console and store it into a file
    setmaxmem                      change maximum memory limit
    setmem                         change memory allocation
    setvcpus                       change number of virtual CPUs
    shutdown                       gracefully shutdown a domain
    start                          start a (previously defined) inactive domain
    suspend                        suspend a domain
    ttyconsole                     tty console
    undefine                       undefine an inactive domain
    update-device                  update device from an XML file
    vcpucount                      domain vcpu counts
    vcpuinfo                       detailed domain vcpu information
    vcpupin                        control or query domain vcpu affinity
    version                        show version
    vncdisplay                     vnc display

 Domain Monitoring (help keyword 'monitor'):
    domblkinfo                     domain block device size information
    domblklist                     list all domain blocks
    domblkstat                     get device block stats for a domain
    domcontrol                     domain control interface state
    domifstat                      get network interface stats for a domain
    dominfo                        domain information
    dommemstat                     get memory statistics for a domain
    domstate                       domain state
    list                           list domains

 Host and Hypervisor (help keyword 'host'):
    capabilities                   capabilities
    connect                        (re)connect to hypervisor
    freecell                       NUMA free memory
    hostname                       print the hypervisor hostname
    nodecpustats                   Prints cpu stats of the node.
    nodeinfo                       node information
    nodememstats                   Prints memory stats of the node.
    qemu-attach                    QEMU Attach
    qemu-monitor-command           QEMU Monitor Command
    sysinfo                        print the hypervisor sysinfo
    uri                            print the hypervisor canonical URI

 Interface (help keyword 'interface'):
    iface-begin                    create a snapshot of current interfaces settings, which can be later commited (iface-commit) or restored (iface-rollback)
    iface-commit                   commit changes made since iface-begin and free restore point
    iface-define                   define (but don't start) a physical host interface from an XML file
    iface-destroy                  destroy a physical host interface (disable it / "if-down")
    iface-dumpxml                  interface information in XML
    iface-edit                     edit XML configuration for a physical host interface
    iface-list                     list physical host interfaces
    iface-mac                      convert an interface name to interface MAC address
    iface-name                     convert an interface MAC address to interface name
    iface-rollback                 rollback to previous saved configuration created via iface-begin
    iface-start                    start a physical host interface (enable it / "if-up")
    iface-undefine                 undefine a physical host interface (remove it from configuration)

 Network Filter (help keyword 'filter'):
    nwfilter-define                define or update a network filter from an XML file
    nwfilter-dumpxml               network filter information in XML
    nwfilter-edit                  edit XML configuration for a network filter
    nwfilter-list                  list network filters
    nwfilter-undefine              undefine a network filter

 Networking (help keyword 'network'):
    net-autostart                  autostart a network
    net-create                     create a network from an XML file
    net-define                     define (but don't start) a network from an XML file
    net-destroy                    destroy (stop) a network
    net-dumpxml                    network information in XML
    net-edit                       edit XML configuration for a network
    net-info                       network information
    net-list                       list networks
    net-name                       convert a network UUID to network name
    net-start                      start a (previously defined) inactive network
    net-undefine                   undefine an inactive network
    net-uuid                       convert a network name to network UUID

 Node Device (help keyword 'nodedev'):
    nodedev-create                 create a device defined by an XML file on the node
    nodedev-destroy                destroy (stop) a device on the node
    nodedev-dettach                dettach node device from its device driver
    nodedev-dumpxml                node device details in XML
    nodedev-list                   enumerate devices on this host
    nodedev-reattach               reattach node device to its device driver
    nodedev-reset                  reset node device

 Secret (help keyword 'secret'):
    secret-define                  define or modify a secret from an XML file
    secret-dumpxml                 secret attributes in XML
    secret-get-value               Output a secret value
    secret-list                    list secrets
    secret-set-value               set a secret value
    secret-undefine                undefine a secret

 Snapshot (help keyword 'snapshot'):
    snapshot-create                Create a snapshot from XML
    snapshot-create-as             Create a snapshot from a set of args
    snapshot-current               Get or set the current snapshot
    snapshot-delete                Delete a domain snapshot
    snapshot-dumpxml               Dump XML for a domain snapshot
    snapshot-edit                  edit XML for a snapshot
    snapshot-list                  List snapshots for a domain
    snapshot-parent                Get the name of the parent of a snapshot
    snapshot-revert                Revert a domain to a snapshot

 Storage Pool (help keyword 'pool'):
    find-storage-pool-sources-as   find potential storage pool sources
    find-storage-pool-sources      discover potential storage pool sources
    pool-autostart                 autostart a pool
    pool-build                     build a pool
    pool-create-as                 create a pool from a set of args
    pool-create                    create a pool from an XML file
    pool-define-as                 define a pool from a set of args
    pool-define                    define (but don't start) a pool from an XML file
    pool-delete                    delete a pool
    pool-destroy                   destroy (stop) a pool
    pool-dumpxml                   pool information in XML
    pool-edit                      edit XML configuration for a storage pool
    pool-info                      storage pool information
    pool-list                      list pools
    pool-name                      convert a pool UUID to pool name
    pool-refresh                   refresh a pool
    pool-start                     start a (previously defined) inactive pool
    pool-undefine                  undefine an inactive pool
    pool-uuid                      convert a pool name to pool UUID

 Storage Volume (help keyword 'volume'):
    vol-clone                      clone a volume.
    vol-create-as                  create a volume from a set of args
    vol-create                     create a vol from an XML file
    vol-create-from                create a vol, using another volume as input
    vol-delete                     delete a vol
    vol-download                   Download a volume to a file
    vol-dumpxml                    vol information in XML
    vol-info                       storage vol information
    vol-key                        returns the volume key for a given volume name or path
    vol-list                       list vols
    vol-name                       returns the volume name for a given volume key or path
    vol-path                       returns the volume path for a given volume name or key
    vol-pool                       returns the storage pool for a given volume key or path
    vol-upload                     upload a file into a volume
    vol-wipe                       wipe a vol

 Virsh itself (help keyword 'virsh'):
    cd                             change the current directory
    echo                           echo arguments
    exit                           quit this interactive terminal
    help                           print help
    pwd                            print the current directory
    quit                           quit this interactive terminal


virsh #
list
shows all running guests;
list --all
shows all guests, running and inactive:
virsh # list --all
 Id Name                 State
----------------------------------
  0 Domain-0             running
  - vm01                 shut off

virsh #
To start a stopped guest, run:
start vm10
To stop a guest, run
shutdown vm10
To immediately stop it (i.e., pull the power plug), run
destroy vm10
Suspend a guest:
suspend vm10
Resume a guest:
resume vm10
These are the most important commands.
Type
quit
to leave the virtual shell.
 

7 Links



No comments:

Post a Comment