INTRODUCTION

kvmbanner-logo2

This guide explains how you can install and use KVM for creating and running virtual machines on a CentOS 6.0 server. I will show how to create image-based virtual machines . KVM is short for Kernel-based Virtual Machine and makes use of hardware virtualization, i.e., you need a CPU that supports hardware virtualization, e.g. Intel VT or AMD-V.

 

 Note

I’m using a CentOS 6.0 server with the hostname server1.sathish.com and the IP address 192.168.31.100 here as my KVM host.

I had SELinux disabled on my CentOS 6.0 system. I didn’t test with SELinux on; it might work, but if not, you better switch off SELinux as well:

#vi /etc/selinux/config

SELINUX=disabled

 

#reboot

 

 

Installing KVM

First check if your CPU supports hardware virtualization – if this is the case, the command

#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

If nothing is displayed, then your processor doesn’t support hardware virtualization, and you must stop here.

Now we import the GPG keys for software packages:

#rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY*

 

To install KVM and virtinst (a tool to create virtual machines), we run


#yum groupinstall ‘Virtualization’

#yum install  kvm libvirt  python-virtinst   qemu-kvm  virt-manager  virt-viewer

 

Then start the libvirt daemon:

#/etc/init.d/libvirtd start

#/etc/init.d/messagebus start

#/etc/init.d/avahi-daemon start

#chkconfig libvirtd on

#chkconfig messagebus on

#chkconfig  avahi-daemon on

To check if KVM has successfully been installed, run

#virsh -c qemu:///system list
Id Name                 State
———————————-

If it displays an error instead, then something went wrong.

 

Next 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

 

#vi /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=”br0″

NM_CONTROLLED=”no”

ONBOOT=yes

TYPE=Bridge

BOOTPROTO=none

IPADDR=192.168.31.1

PREFIX=24

GATEWAY=192.168.31.1

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=”no”

ONBOOT=yes

HWADDR=44:37:E6:26:E6:47

TYPE=Ethernet

#BOOTPROTO=none

#IPADDR=192.168.31.1

#PREFIX=24

#GATEWAY=192.168.31.1

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

 

#ifconfig
br0       Link encap:Ethernet  HWaddr 44:37:E6:26:E6:4F
inet addr:192.168.31.1  Bcast:192.168.31.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:103 errors:0 dropped:0 overruns:0 frame:0
TX packets:79 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:7434 (7.2 KiB)  TX bytes:21398 (20.8 KiB)

eth0      Link encap:Ethernet  HWaddr 44:37:E6:26:E6:47
inet6 addr: fe80::21e:90ff:fef3:f002/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
RX packets:19511 errors:0 dropped:0 overruns:0 frame:0
TX packets:11592 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25980124 (24.7 MiB)  TX bytes:1104371 (1.0 MiB)
Interrupt:28 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:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3608 (3.5 KiB)  TX bytes:3608 (3.5 KiB)

virbr0    Link encap:Ethernet  HWaddr 6A:12:69:18:2B:05
inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

 

We need to create new LVM volumes for the new virtual machine. I’m assuming LVM is already being used, that the volume group is called ‘vg_www’, and that there is sufficient free space available in the vg_www group for the new volumes. If your volume group has a different name then just modify the instructions below accordingly. Change the volume sizes to suit your requirements:

 

#lvcreate  -L 10G  -n  vm1   vg_www

This command installs CentOS on the guest and The guest is called ‘vm1’, it has a dedicated physical CPU core (core number 1) and 1 GB of RAM allocated to it,using ftp server as a installation source. you can use any other type such as CD-ROM  , ISO images, HTTP  etc.

 

#virt-install –-name=vm1  -–cpuset=1   –-ram=1024   –network bridge=br0  –disk=/dev/mapper/vg_www-vm1  –extra-args=’console=tty0 console=ttyS0,115200n8 serial  –location=ftp://19.168.31.1

 

1