Home » Virtualization » How to configure KVM virtual server on Centos

How to configure KVM virtual server on Centos

KVM (Kernel-based Virtual Machine) is a virtualization infrastructure for the Linux kernel which turns it into a hypervisor. KVM requires a processor with hardware virtualization extension.

This article explains you that how to configure KVM virtual server for creating and running virtual machines on a CentOS server.
Check Hardware Virtualization Supoort
KVM requires hardware virtualization support for instance Intel VT or AMD’s AMD-V, which are teaching set extensions for hardware-assisted virtualization. Check if hardware virtualization support is available on CentOS host machine:
#egrep -i ‘vmx|svm’ –color=always /proc/cpuinfo
If CPU flags contain “vmx” or “svm”, it means hardware virtualization support is available.
Disable SELinux

We need to disable selinux before installing KVM SELinux booleans that can affect the behavior of KVM and libvirt.

To disable SELinux on CentOS:

# vi /etc/selinux/config

SELINUX=disabled
Reboot the machine for the change to take effect.
Install KVM, QEMU and user-space tools
Install KVM and virtinst (a tool to create VMs) as follows:

# yum install kvm libvirt python-virtinst qemu-kvm

Start libvirtd daemon, and set it to auto-start:

# service libvirtd start
# chkconfig libvirtd on

Check if KVM has successfully been installed. You should see no error as follows.

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

Configure Linux Bridge for VM Networking
Installing KVM alone does not allow VMs to communicate with each other or access external networks. You need to configure VM networking separately. So we are going to set up “bridged networking” via Linux bridge.
Install bridge-utils to configure Bridge.

# yum install bridge-utils

and now configure a bridge, By Creating the file /etc/sysconfig/network-scripts/ifcfg-br0 and disable Ethernet card /etc/sysconfig/network-scripts/ifcfg-eth0 file); make sure you use TYPE=Bridge, not TYPE=Ethernet

You need to comment out or remove the following lines as below.
#BOOTPROTO=none
#IPADDR
#GATEWAY
#NETMASK
And append a line in the bottom of the file.

BRIDGE=br0

It looks like as

# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=9C:8E:99:C4:64:3A
TYPE=Ethernet
UUID=eef82508-3959-4baf-b7c7-908728379e38
ONBOOT=yes
NM_CONTROLLED=yes
#BOOTPROTO=none
#IPADDR=192.168.1.33
#GATEWAY=192.168.1.1
#NETMASK=255.255.254.0
BRIDGE=br0

To configure bridge br0 with a static IP address:

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

Append the following lines

DEVICE=”br0″
NM_CONTROLLED=”yes”
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=none
IPADDR=192.168.1.33
PREFIX=23
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME=”System br0″

Note :: Please replace the ip address with your own ip address.

# service network restart

And the ifconfig command to verify the new eth settings

# ifconfig
br0 Link encap:Ethernet HWaddr D4:85:64:78:01:DC
inet addr:192.168.1.33 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::d685:64ff:fe78:1dc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:182 errors:0 dropped:0 overruns:0 frame:0
TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:15434 (15.0 KiB) TX bytes:6648 (6.4 KiB)

eth0 Link encap:Ethernet HWaddr D4:85:64:78:01:DC
inet6 addr: fe80::d685:64ff:fe78:1dc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:188 errors:0 dropped:0 overruns:0 frame:0
TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:21424 (20.9 KiB) TX bytes:6408 (6.2 KiB)

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: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)

virbr0 Link encap:Ethernet HWaddr 52:54:00:59:A3:88
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)

Install Virt Manager install a desktop User Interface
The final step is to install virt manager

# yum install virt-manager libvirt qemu-system-x86 openssh-askpass libcanberra-devel

1. Launch Virt Manager Locally
If you are using CentOS desktop then run the below command

# virt-manager

2. Launch VirtManager Remotely Xming X11 server
If you are using CentOS server without desktop User Interface, follow these steps to launch VirtManager.
Enable X11 forwarding on SSH server:

# yum install xauth
#vi /etc/ssh/sshd_config
X11Forwarding yes
# service sshd restart

Install Xming X11 server on your local Windows machine and setup your PuTTy connection to the server and under the SSH options enable X11 Forwarding.
Create a following executable “wrapper” script for virt-manager.

#vi /usr/bin/vm
#! /bin/bash
xauth list | while read line; do
sudo -i xauth add $line
done
sudo -i virt-manager
# chmod +x /usr/bin/vm

Then connect to your CentOS server from a separate desktop machine, and run the wrapper script vm to launch VirtManager remotely.

#ssh -X user@centos_server

Troubleshooting KVM and VirtManager setup
1. If you getting the error when attempting to launch VirtManager remotely, make sure that you use the wrapper script (vm) to launch it, as described above.

X11 connection rejected because of wrong authentication.
Traceback (most recent call last):
File “/usr/share/virt-manager/virt-manager.py”, line 383, in
main()
File “/usr/share/virt-manager/virt-manager.py”, line 286, in main
raise gtk_error
RuntimeError: could not open display

2. If you see the following D-Bus error:

D-Bus library appears to be incorrectly set up; failed to read machine
uuid: UUID file ‘/var/lib/dbus/machine-id’

Then run the command below and reboot the host machine.

# sh -c ‘dbus-uuidgen > /var/lib/dbus/machine-id’

3. If you have font issue while running VirtManager, install the following font, and relaunch it.

# yum install dejavu-lgc-sans-fonts

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry from last 7 years.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud