Installing KVM Virtualization on CentOS

From Techotopia
Revision as of 12:23, 18 July 2010 by Neil (Talk | contribs)

Jump to: navigation, search
PreviousTable of ContentsNext
Managing Xen on CentOS using the xm and virsh Command-line ToolsCreating a CentOS KVM Virtual Machine


<google>BUY_CENTOS</google>


As discussed in previous chapters of this book, virtualization is the ability to run multiple operating systems simultaneously on a single computer system, and a number of different commercial and open source solutions are available. In the preceding chapters we looked in detail at configuring and maintaining Xen based virtualization. In this and subsequent chapters we will look at KVM based virtualization hosted on a CentOS system.

The goal of this chapter is to cover the installation of KVM on CentOS and configure a network bridge to allow guest operating systems to share the physical network connection of the host computer. The next chapter will cover Creating a CentOS KVM Virtual Machine.


Contents


KVM Hardware Requirements

Before proceeding with this chapter we need to take a moment to discuss the hardware requirements for running virtual machines within a KVM environment. Firstly, KVM virtualization is only available on certain processor types. These processors include either Intel-VT or AMD-V technology.

To check for Intel-VT support, run the following command in a terminal window to check for the presence of vmx:

# grep vmx /proc/cpuinfo 
flags           : fpu tsc msr pae mce cx8 apic mtrr mca cmov pat pse36 clflush dts 
acpi mmx fxsr sse sse2 ss ht tm syscall nx lm constant_tsc pni monitor ds_cpl vmx est tm2 cx16 xtpr lahf_lm

To check for AMD-V support, run the following command which checks for the presence of svm:

# grep svm /proc/cpuinfo
flags           : fpu tsc msr pae mce cx8 apic mtrr 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 cr8_legacy

Note that while the above commands only report whether the processor supports the respective feature, it does not indicate whether the feature is currently enabled in the BIOS. In practice virtualization support is typically disabled in the BIOS of most systems. It is recommended, therefore, that you check your BIOS settings to ensure the appropriate virtualization technology is enabled before proceeding with this tutorial.

If your processor does not have Intel-VT or AMD-V support you will still be able to install guest operating systems though this will be implemented using the QEMU environment instead of KVM virtualization. QEMU uses emulation to run guest operating systems and as such results in slower performance than the hardware assisted virtualization offered by KVM.

Unlike a dual booting environment a virtualized environment involves the running of two or more complete operating systems concurrently on a single computer system. This means that the system must have enough physical memory, disk space and CPU processing power to comfortably accommodate all these systems in parallel. Before beginning the configuration and installation process check on the minimum system requirements for both CentOS and your chosen guest operating systems and verify that your system has sufficient resources to handle the requirements of both systems.

32-bit Support for KVM on CentOS

Whilst previous versions of CentOS supported KVM on both 32-bit and 64-bit systems, as of CentOS 5.4 support for KVM virtualization is only available for 64-bit versions of the operating system.


Preparing CentOS for KVM Virtualization

Unlike Xen, it is not necessary to run a special version of the kernel in order to support KVM. As a result KVM support is already available for use with the standard kernel negating the need to install and boot from a special kernel.

To avoid conflicts, however, if a Xen enabled kernel is currently running on the system, reboot the system and select a non-Xen kernel from the boot menu before proceeding with the remainder of this chapter.

The tools required to setup and maintain a KVM based virtualized system are not installed by default unless specifically selected during the CentOS operating system installation process. To install KVM, launch the Package Manager by selecting the Applications -> Add/Remove Software menu from the desktop menu system. Enter the root password if prompted to do so and scroll down the list of categories, selecting Virtualization when it becomes visible.

In the right hand column make sure that Virtualization is not selected. If it is selected, deselect it and click on the Apply button. Once these packages have been removed, select KVM and click on Apply once again to install the KVM package and any additional dependencies. If the installation fails with a message similar to the following then it is likely that you are attempting to install KVM on a 32-bit system, or a 64-bit system running a 32-bit version of CentOS:

Error: libvirt conflicts with kvm

Once the installation of KVM is complete, close any applications that are running and reboot the system.

Verifying the KVM Installation

Once the system has restarted after the reboot it is worthwhile checking that the installation worked correctly before moving forward. When KVM is installed and running two modules will have been loaded into the kernel. The presence or otherwise of these modules can be verified in a terminal window by running the following command:

su -
lsmod | grep kvm 

Assuming that the installation was successful the above command should generate output similar to the following:

lsmod | grep kvm
kvm_intel              86920  1
kvm                   226208  2 ksm,kvm_intel

Note that if the system contains an AMD processor the kvm module will likely read kvm_amd rather than kvm_intel. The installation process should also have configured the libvirtd daemon to run in the background. Once again using a terminal window with super user privileges, run the following command to ensure libvirtd is running:

/sbin/service libvirtd status
libvirtd (pid  2958) is running...


If the process is not running, it may be started as follows:

 /sbin/service libvirtd start

Finally, run the virt-manager tool by selecting the Applications -> System Tools -> Virtual Machine Manager menu. When loaded, the manager should appear as illustrated in the following figure:


The virt-manager on a KVM enabled CentOS system


If the manager is not currently connected to the virtualization processes, right click on the only entry listed and select Connect from the popup menu.

Virtual Networks and Network Bridges

A KVM virtual machine running on CentOS has two options in terms of networking connectivity. One option is for it to be connected to a virtual network running within the operating system of the host computer. In this configuration any virtual machines on the virtual network can see each other but access to the external network is provided by Network Address Translation (NAT). When using the virtual network and NAT, each virtual machine is represented on the external network (the network to which the host is connected) using the IP address of the host system. This is the default behavior for KVM virtualization and requires no additional configuration, other than selecting the Virtual network option on the Network screen of the virt-manager new virtual machine wizard.

In order for guests to appear as individual systems on the external network (i.e. with their own IP addresses), they must be configured to share a physical network connection on the host. This is achieved by configuring a network bridge on the host system to which the guests can connect. In the remainder of this chapter we will cover the steps necessary to configure a CentOS network bridge for use by KVM based guest operating systems.

Creating a Network Bridge

The creation of a network bridge involves the use of the bridge-utils package. Begin by checking if this is already installed by executing the following command:

rpm –q bridge-utils

If the package is not yet installed, install it using yum:

su –
yum install bridge-utils

With this package installed it is time to look at the current network configuration. The easiest way to do this is to run the ifconfig command. The following shows output from running ifconfig on a system on which KVM is installed, but on which a network bridge has yet to be configured:

eth1      Link encap:Ethernet  HWaddr 00:13:72:0B:14:57
          inet addr:192.168.2.18  Bcast:255.255.255.255  Mask:255.255.255.0
          inet6 addr: fe80::213:72ff:fe0b:1457/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:86 errors:0 dropped:0 overruns:0 frame:0
          TX packets:131 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:16579 (16.1 KiB)  TX bytes:17997 (17.5 KiB)
          Memory:fe3e0000-fe400000

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:1671 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1671 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2636500 (2.5 MiB)  TX bytes:2636500 (2.5 MiB)

virbr0    Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:57 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:9438 (9.2 KiB)

In the above output, the entry for eth1 represents a physical network adaptor on the host computer. This is the connection currently used by this host to access the external network. The virbr0 entry represents the virtual network to which guest operating systems will connect if configured to do so.

In order to provide the option for guest operating systems to share the eth1 connection of the host it is necessary to establish a network bridge between eth1 and the virtual machines.

Begin the bridge creation process by gaining root access and changing directory to the /etc/sysconfig/network-scripts directory:

su –
cd /etc/sysconfig/network-scripts

The next step is to create a configuration file for the network bridge (which we will name bridge0). Rather than create this script from scratch we can copy the eth1 file and then modify it:

cp ifcfg-eth1 ifcfg-bridge0

At this point, the ifcfg-bridge0 file reads as follows (keeping in mind that the file will likely differ slightly depending on the configuration of your CentOS system):

# Intel Corporation 82573L Gigabit Ethernet Controller
DEVICE=eth1
BOOTPROTO=dhcp
HWADDR=00:13:72:0b:14:57
ONBOOT=yes
DHCP_HOSTNAME=centos5
TYPE=Ethernet

The next step is to modify the file as follows:

DEVICE=bridge0
BOOTPROTO=dhcp
HWADDR=00:13:72:0b:14:57
ONBOOT=yes
DHCP_HOSTNAME=centos5
TYPE=Bridge

After making the required changes to the bridge file, edit the ifcfg-eth1 file so that the BOOTPRO, DHCP_HOSTNAME and TYPE directives are commented out. Note that the example file reflects the likely settings for a system configured to obtain a dynamic IP address from a DHCP server. If your system is configured to use a static IP address comment out the corresponding directives for this configuration. Finally, add a BRIDGE= directive referencing our new bridge0 network bridge:

# Intel Corporation 82573L Gigabit Ethernet Controller
DEVICE=eth1
#BOOTPROTO=dhcp
HWADDR=00:13:72:0b:14:57
ONBOOT=yes
#DHCP_HOSTNAME=centos5
#TYPE=Ethernet
BRIDGE=bridge0

Once the above changes have been implemented and the script files saved, reboot the system for the changes to take effect. Once the system reboots, run the ifconfig command a second time and review the output. Assuming no problems were encountered, a bridge0 entry should be now listed:

bridge0   Link encap:Ethernet  HWaddr 00:13:72:0B:14:57
          inet addr:192.168.2.18  Bcast:255.255.255.255  Mask:255.255.255.0
          inet6 addr: fe80::213:72ff:fe0b:1457/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:922 errors:0 dropped:0 overruns:0 frame:0
          TX packets:300 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:67210 (65.6 KiB)  TX bytes:48069 (46.9 KiB)

eth1      Link encap:Ethernet  HWaddr 00:13:72:0B:14:57
          inet6 addr: fe80::213:72ff:fe0b:1457/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:939 errors:0 dropped:0 overruns:0 frame:0
          TX packets:322 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:83646 (81.6 KiB)  TX bytes:52229 (51.0 KiB)
          Memory:fe3e0000-fe400000

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:2201 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2201 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3997276 (3.8 MiB)  TX bytes:3997276 (3.8 MiB)

virbr0    Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:52 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:9063 (8.8 KiB)

During the virtual machine creation process, eth1 will now be listed as eth1 (Bridge bridge0) under Shared physical device on the virt-manager network configuration screen:


Selecting a Network Bridge for a CentOS KVM virtual machine


When this option is selected for a virtual machine it will have access to the external network using the same eth1 device used by the host and operate with its own IP address which may be defined statically or obtained dynamically from a DHCP server. Assuming that KVM is now installed and a network bridge configure if NAT networking is suitable, we are ready to move on the next chapter entitled Creating a CentOS KVM Virtual Machine.


<google>BUY_CENTOS_BOTTOM</google>


PreviousTable of ContentsNext
Managing Xen on CentOS using the xm and virsh Command-line ToolsCreating a CentOS KVM Virtual Machine