setup KVM on centos 6 / 7


KVM requires a linux kernel module to support full virtualization. The linux module consists of three files:
1) kvm.ko,
2) kvm_intel.ko (for Intel processors),
3) kvm_amd.ko (for AMD processors).
the 2.6.20 kernel version (and higher) includes these kernel modules as part of the mainline kernel.

To disable SELinux on CentOS:
$ sudo -e /etc/selinux/config

SELINUX=permissive


modprobe is command used to add / remove module from linux kernel (check)
modprobe -l | grep kvm

kernel/arch/x86/kvm/kvm.ko
kernel/arch/x86/kvm/kvm-intel.ko
kernel/arch/x86/kvm/kvm-amd.ko

if kvm modules are properly loaded you will see following two sets of modules
lsmod | grep kvm

kvm_intel              54285  0
kvm                   333172  1 kvm_intel

OR

kvm_amd     35678
kvm         333172  kvm_amd

Load modules and its dependent modules if not loaded with modprobe command:
modprobe  kvm

yum groupinstall "Virtualization Tools" "Virtualization Platform"
yum install python-virtinst

OR

yum install kvm qemu-kvm python-virtinst libvirt libvirt-python virt-manager libguestfs-tools

yum install policycoreutils-python   //for selinux commands like semanage

yum install virt-viewer  //for viewing vms during install

//at client side install vnc with #yum install vnc -y

#chkconfig libvirtd on
The libvirtd program is the server side daemon component of the libvirt virtualization management system.
#service libvirtd start

check service status with:
service libvirtd status
or
virsh -c qemu:///system list

Sample setup

                     +-------------+
                     | CentOS/RHEL |             |-----|
     -->ISP router---+ Box Host w/ +--- eth0 -->-+ br0 +  Private IP
                     | KVM         |             |     |
                     +-------------+--- eth1 -->-+ br1 | Public IP
                                                 |-----|
                                                    |
                                                vm1-+---vm2----vm3---vm4
*** NOTE: each VMs will have two interface i.e. Private and Public *****

Where,
    eth0 - KVM servers' LAN interface.
    eth1 - KVM servers' WAN/Internet interface.
    br0 - KVM servers' bridge interface to give VM direct access to all the resources on the LAN.
    br1 - KVM servers' bridge interface to give VM direct access to all the resources on the WAN/Internet. Users can ssh into VMs or you can host HTTPD/SMTPD using this interface.
    obsd-vm1 (vm1) - OpenBSD VM name.
    rhel-vm1 (vm2) - RHEL VM name.

Install and configure a network bridge

All VMs will only have network access to host and other VMs on same physical server via private network. You need to crate a network bridge so that the VMs can access your LAN and possible the Internet/WAN from outside. Type the following yum command to install bridge-utils package:
# yum install bridge-utils

Setup a default gateway

Edit /etc/sysconfig/network as follows
# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=kvm-42.cyberciti.biz
## I am routing internet traffic via br1 ##
GATEWAYDEV=br1

Configure bridging

Update /etc/sysconfig/network-scripts/ifcfg-eth0 (private) as follows:
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
HWADDR=00:30:48:C6:0A:D8
BRIDGE=br0

Update /etc/sysconfig/network-scripts/ifcfg-eth1 (public) as follows:
# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
HWADDR=00:30:48:C6:0A:D9
BRIDGE=br1

Create/edit the /etc/sysconfig/network-scripts/ifcfg-br0 file to setup private/lan ip address for br0:
# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
ONBOOT=yes
## setup LAN/VLAN ips as per your needs ##
IPADDR=10.10.29.66
NETMASK=255.255.255.192
DELAY=0


Create/edit the /etc/sysconfig/network-scripts/ifcfg-br1 file to setup public/wan/internet ip address for br1:
# cat /etc/sysconfig/network-scripts/ifcfg-br1
DEVICE=br1
TYPE=Bridge
BOOTPROTO=static
ONBOOT=yes
## setup INTERNET ips as per your needs ##
IPADDR=74.ww.xx.yy
NETMASK=255.255.255.248
GATEWAY=74.ww.xx.yy
DELAY=0


I need to route all lan traffic (subnet 10.0.0.0/8) via 10.10.29.65 gateway. Create/edit file /etc/sysconfig/network-scripts/route-br0 as follows:
# cat /etc/sysconfig/network-scripts/route-br0
10.0.0.0/8 via 10.10.29.65

#check the selinux context for newly created network config files(check the other files)
ls -lz /etc/sysconfig/network-scripts/route-br0

-rw-r--r--. root root system_u:object_r:net_conf_t:s0  route-br0

Warning: Restarting network service over the ssh session may result into total loss of the connectivity to the server. So make sure br0 and br1 configuration including routing set correctly.

cd /var/lib/libvirt/    #checkout selinux context
[root@v1 libvirt]# ls -lZ
drwx--x--x. root root system_u:object_r:virt_content_t:s0 boot
drwxr-xr-x. root root system_u:object_r:virt_var_lib_t:s0 dnsmasq
drwx--x--x. root root system_u:object_r:virt_var_lib_t:s0 filesystems
drwxr-xr-x. root root system_u:object_r:virt_image_t:s0 images
drwx------. root root system_u:object_r:virt_var_lib_t:s0 lxc
drwx------. root root system_u:object_r:virt_var_lib_t:s0 network
drwxr-x---. qemu qemu system_u:object_r:qemu_var_run_t:s0 qemu

[root@v1 libvirt]# ls -lZ boot/     #we have our centos 6.4 iso inside boot
-rw-r--r--. qemu qemu system_u:object_r:virt_content_t:s0 CentOS-6.4-x86_64-minimal.iso

Use below for restoring default SELinux contexts
# restorecon -Rv /etc/sysconfig/
If you are going to reboot the SELinux enabled server, make sure you type the following command:
# touch /.autorelabel
# reboot
####Now restart Network and verify br0/br1 settings
service network restart
brctl  show
ip address
ping youtube.com

###########CREATING VMS

cd /var/lib/libvirt/boot/
put your iso file there with proper selinux
do #chcon --reference  source   isofile

virt-install -n vm201 --description "demoserver centos6.4 64bit"  --ram=512 --vcpus=1 --cpu host  --cdrom /var/lib/libvirt/boot/CentOS-6.4-x86_64-minimal.iso  --network bridge:br0,model=virtio --network bridge:br1,model=virtio --graphics vnc  --disk path=/var/lib/libvirt/images/vm201.img,bus=virtio,size=10 --hvm

go to your REAL HOST MACHINE if you are using putty:-
In boot option edit boot machine and add below it:---->>>>

console=ttyS0,115200

install .................
guest centos 6.5 machine gets rebooted
now, you are on main Host machine now(No putty)

virsh console vm201

####Now set  ip address
[root@localhost network-scripts]# cat ifcfg-eth0
DEVICE=eth0
HWADDR=52:54:00:D2:CB:F0
TYPE=Ethernet
UUID=309c7798-762f-424a-a84f-d74036039a7e
ONBOOT=YES
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.10.10.100
NETMASK=255.255.255.0
GATEWAY=10.10.10.2

[root@localhost network-scripts]# cat ifcfg-eth1
DEVICE=eth1
HWADDR=52:54:00:8E:CE:8C
TYPE=Ethernet
UUID=986d56c6-1aee-4fbe-bbf9-e12b55be520e
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.213.201
NETMASK=255.255.255.0
GATEWAY=192.168.213.2

#service network restart

You can access your GUEST vm with VNC

on your NON-kvm  linux laptop
do ssh portforwarding
ssh -L 5900:127.0.0.1:5900  root@KVM-Server-IP-Here
OR
ssh -L 5900:127.0.0.1:5900 -N -f -l root


hit tab and enter

console=ttyS0,115200

No comments:

Post a Comment