How to Configure KVM Virtualization in CentOS 7

Kernel-based Virtual Machine (KVM) is a virtualization infrastructure for the Linux kernel that turns it into a hypervisor. It was merged into the Linux kernel mainline in kernel version 2.6.20, which was released on February 5, 2007. KVM requires a processor with hardware virtualization extension. KVM has also been ported to FreeBSD and illumos in the form of loadable kernel modules.

KVM originally supported x86 processors and has been ported to S/390, PowerPC, and IA-64. An ARM port was merged during the 3.9 kernel merge window.
KVM uses QEMU for I/O hardware emulation. QEMU is a user-space emulator that can emulate a variety of guest processors on host processors with decent performance. Using the KVM kernel module allows it to approach native speeds. KVM is managed via the libvirt API and tools. Some libvirt tools used in this article include virsh, virt-install and virt-clone.

Step 1: Check if your hardware processor can support the KVM virtualization by running the commnad below the svm is for AMD processor CPU and vmxis for intel processor CPU

# grep -E ‘svm|vmx‘ /proc/cpuinfo
and if you like to run in individual command you can use this and see the below query
[[email protected] ~]# grep --color vmx /proc/cpuinfo
flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts 
acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good
aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm tpr_shadow vnmi flexpriority
----output truncated----
[[email protected] ~]# grep --color svm /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts
acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good
aperfmperf pni dtes64 monitor ds_cpl svm est tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm tpr_shadow vnmi flexpriority
----output truncated----
Step 2: Assuming you have the latest patches and updates in your CentOS 7 and if not you may want to run the “yum -y update && yum -y upgrade && reboot” then command the reboot to restart the CentOS. After its done you can proceed to install the required packages to run the KVM and in your terminal run the yum install and the packages in below list
# yum install kvm qemu-kvm python-virtinst libvirt libvirt-python virt-manager virt-viewer  libguestfs-tools virt-install wget openssl-devel kernel-devel
AND
# yum groupinstall “Virtualisation Tools” “Virtualization Platform”
# yum groupinstall “Development Tools”
kvm_03
Step 3: Enable to autorun the libvirtd and then start the service
# systemctl enable libvirtd
# systemctl start libvirtd

and verify if the libvirtd service is running by issuing the command “service libvirtd status”

# service libvirtd status
libvirtd (pid  31128) is running...
# virsh -c qemu:///system list
 Id    Name                           State
-----------------------------------------------

*NOTE: As this is running in Home Lab environment and to not have any issues I disabled my SELINUX cofiguration from enforcing to disabled unless you already know what your doing then leave this alone
# vim /etc/selinux/config
kvm_02
Step 4: Configure the Network to share in your KVM by default VMs will only have network access to other VMs within the server privately. If you want the VMs to have access to your Home lab LAN or WAN, then you must create a network bridge on the host that is connected to the NIC that connects to your LAN but before that install the bridge-utils rpm to support the bridge configuration
#  yum install bridge-utils

We will create a bridge named ‘br0’ and add to your network controller configuration file by editing the ifcfg-em0 or your ifcfg-eth0 file

PRIVATE NIC LAN
# vim /etc/sysconfig/network-scripts/ifcfg-em0′ and then append BRIDGE=br0
DEVICE=em0
ONBOOT=yes
HWADDR=01:2A:48:A2:3A:12
BRIDGE=br0
PUBLIC NIC LAN
 # vim /etc/sysconfig/network-scripts/ifcfg-em1 and then append the BRIDGE=br1
DEVICE=em1
ONBOOT=yes
HWADDR=01:2A:48:A2:3A:15
BRIDGE=br1

 Create/edit the /etc/sysconfig/network-scripts/ifcfg-br0 file to setup private/lan ip address for br0:

# vim /etc/sysconfig/network-scripts/ifcfg-br0
 DEVICE=br0
 TYPE=Bridge
 BOOTPROTO=static
 ONBOOT=yes
 ## LAN/VLAN ips as per your needs ##
 IPADDR=192.168.10.20
 NETMASK=255.255.255.0
 DELAY=0
 Create/edit the /etc/sysconfig/network-scripts/ifcfg-br1 file to setup public/wan/internet ip address for br1:
 # vim /etc/sysconfig/network-scripts/ifcfg-br1
 DEVICE=br1
 TYPE=Bridge
 BOOTPROTO=static
 ONBOOT=yes
 ## WAN/INTERNET ips as per your needs ##
 IPADDR=202.xx.xx.xx
 NETMASK=255.255.255.248
 GATEWAY=202.xx.xx.xx
 DELAY=0
 And verify the network setting in your terminal console
# brctl show
# ip addr show br0
# ip addr show br1
# ip route
Step 5: Create route for the kvm bridge network to go out all traffic to your lan by creating or editing the file /etc/sysconfig/network-scripts/route-br0 and add this below line the file.
# vim /etc/sysconfig/network-scripts/route-br0
192.168.0.0/24 via 192.168.10.1
Step 6: Edit the file /etc/sysctl.conf and enable network forwarding then add ‘net.ipv4.ip_forward = 1’
# vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

and then run the below command to apply the changes that was made followed by restarting the Network Manager

# sysctl -p /etc/sysctl.conf
# systemctl restart NetworkManager

Step 7: Optional: Create the disk storage that will be use in the VM and we will name it as ‘CentOS-VM1_disk0.img’

# dd if=/dev/zero of=/vm-images/CentOS-VM1_disk0.img bs=1M count=15360

Step 8: Create KVM guest VM’s by using the ‘virt-install’ tools. This tool can run in interactive or non-interactive mode now lets use the virt-install in non-interactive mode to create CentOS 7 x86 VM and will be naming it as ‘CentOS-VM1’ with 1 vCPU, 1GB Memory and 10GB of virtual disk to be use by OS.

# virt-install \
    --name CentOS-VM1 \
    --description "Test CentOS VM1 for WebApp" \
    --ram=1024 \
    --vcpus=1 \
    --disk path=/vm-images/CentOS-VM1_disk0.img,bus=virtio,size=10 \
    --cpu host \
    --cpuset=auto \
    --os-type linux \
    --os-variant centos7 \
    --hvm \
    --graphics vnc \
    --cdrom /mnt/CentOS7.iso \
    --network bridge:br0,model=virtio

virt-install options

  • –name CentOS-vm1 : The name of the guest VM
  • –description “Test CentOS VM1 for WebApp”: Description of the VM or what app is running in it
  • –ram=1024 : 1GB is the amount of memory allocated to the VM in MegaByte
  • –vcpus=1 : The number of virtual CPU(s) for the VM.
  • –cpu host : Optimize CPU properties of the VM.
  • –os-variant=centos7: This is the VM OS type or to view variant OS’s use this command #osinfo-query os
  • –hvm : Request the use of full virtualization stands for Hardware-assisted Virtual Machine. Para-virtualization (PV) An OS or Kernel called Hypervisor is installed on the hardware. Dom0 is called the “privileged domain” which can issue commands to the hypervisor
  • –cdrom /mnt/cento7.iso : Install CentOS 7 from .iso file that was save in your local drive.
  • –network bridge:br0,model=virtio : Create a network bridge using br0 in your lan
  • –graphics none : we use option ‘none’ of if you want to use VNC to access installation using vnc server/viewer from your local Linux/Windows desktop change the ‘none’ to ‘vnc’.
  • –disk path=/vm-images/CentOS-VM1.img,bus=virtio,size=10 : This is is the image file for the VM, the size is specified in GBs and forcing vio driver. The virtio driver provides support service for paravirtualized devices using the VirtIO protocol.

Step 9: Login in in your VNC Client first we need to create tunnel from your Desktop if your using Linux you need to do this

# ssh -L 5900:localhost:5900 -N -f -l your-user Remote-system-with-VNC-server

OR

# ssh -L 5900:127.0.0.1:5900 -N -f -l admin 192.168.10.150

  • -L 5900:127.0.0.1:5900 : Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side. Here you are using port 5901 on the localhost to be forward to sshserver.mydomain.com on the 5900 port.
  • -N : Do not execute a remote command i.e. just forward ports.
  • -f : Requests ssh to go to background just before command execution. Requests ssh to go to background just before command execution. Once password supplied it will go to background and you can use prompt for type commands on local system.
  • -l admin : rocky is the user to log in as on the remote machine (192.168.10.150).
  • 192.168.10.150: Remote system with VNC server

In your localhost VNC client use 127.0.0.1:5900 for connection. Make sure you use appropriate port i.e. 5900 (VNC server running on display 1). This tunnel will provide nice enhanced security. you can use this alternative tightVNC if you do not have the VNC.

And if you if your using Windows Client to login in your KVM Server you can follow this below instruction using your putty.exe application or you can download copy from here

Source port: 5900
Destination: 127.0.0.1:5900
Choose -> Local and Auto

kvm_06

Once your login to your server and the guest VM starts you can double check by doing this below

kvm_11

by typing the below command it will show you the correct port to connect. this will query for the port starting with ’59’

# netstat -tln | grep 59

and in your TightVNC application you type in the  below IP address then hit “Connect”

Remote Host: 127.0.0.1

kvm_08

make sure you start the guest VM first before doing the VNC connection

virsh list -all : To show all the current VM in the list for me I only have the ‘CentOS-VM1’ and currently its in shut off State

virsh start CentOS-VM : This command will run the guest VM once its running you can connect to your VNC

kvm_07

 

Step 10: And if you want to install using the Console text file you can use this template and make sure you presented/available the .ISO file to browse

# virt-install \
--name CentOS-VM2 \
--description "Test CentOS VM2 for WebApp" \
--ram=1024 \
--vcpus=2 \
--disk path=/home/kvm-images/CentOS-VM2/CentOS-VM2_disk0.img,bus=virtio,size=15 \
--cpu host \
--cpuset=auto \
--os-type linux \
--os-variant centos7.0 \
--hvm \
--graphics none \
--location=http://192.168.58.200/iso/ \
--network bridge:virbr0,model=virtio \
--extra-args='console=tty0 console=ttyS0,115200n8 serial'

kvm_12

Step 11: This is the window you should be seeing once your connected using the VNC after the installation of your CentOS 7

kvm_09

And this is for the text console installation that you should expect when installing

# virsh console CentOS-VM2

Step 12: And if you will install other distro of OS’es you can list all the variant by typing the command below. Use the ‘Short ID’ name

# osinfo-query os

And to show the VM information

# virsh dominfo CentOS-VM1

Show vCPU/Memory usage of all the VMs

# virt-top

show VM disk partitions

# virt-df CentOS-VM1

Stop the VM (Shutdown the OS), Destroy & Undefine the VM to decommission

# virsh shutdown CentOS-VM1

 

# virsh destroy CentOS-VM1

 

# virsh undefine CentOS-VM1

To List the path of the virtual disk VM file image

# virsh vol-list CentOS-VM1

# virsh vol-delete –pool CentOS-VM1 CentOS-VM1_disk0.img

Start the VM

# virsh start CentOS-VM1

And to mark the VM for autostart (This will autostart the VM if the hosts reboots)

# virsh autostart CentOS-VM1

And to mark the autostart to be manual start

# virsh autostart –disable CentOS-VM1

And to console to the guest VM server

# virsh console CentOS-VM1

*Note: make sure on the VM configuration you include this below string else it wont work

console=tty0 console=ttyS0,115200

To edit the VM configuration such as memory and cpu

# virsh edit CentOS-VM1

To restart the VM configuration after changing the .xml file

# virsh create /etc/libvirt/qemu/CentOS-VM1.xml

To view the Memory that allocated to the guest VM or see the virtual hardware that was assigned to the VM

# virsh dominfo CentOS-VM1 | grep memory

# virsh dominfo CentOS-VM1

To dynamically add memory into the guest VM to 4GB

# virsh setmem CentOS-VM1 4096000

To delete/destroy the guest VM

# virsh shutdown CentOS-VM1 : first shutdown the VM

# virsh destroy CentOS-VM1 : if can not shutdown you can force it

# virsh undefine CentOS-VM1 : undefine the CentOS-VM1

# rm -rf /vm-image/CentOS-VM1.img : permamently delete the image file

To view all the available virtual network or to view remote system

# virsh net-list –all

# virsh -c qemu:///system list –all

 

Misc:

If you want to manually configure your network lan card that you configured for Bonding+Bridge+VLAN you can use this commands below

nmcli connection show 

nmcli connection add con-name virbr10 ifname virbr10 type bridge stp no
nmcli connection down virbr10
nmcli connection edit virbr10
set ipv4.method disabled
set ipv6.method ignore
save
quit
nmcli connection up virbr10

 





nmcli  connection  add  con-name  vlan10  ifname  vlan10 type vlan dev <DEVICE_BONDNAMEHERE> id 10
nmcli connection down vlan10
nmcli connection edit vlan10
set connection.master virbr10
set connection.slave-type bridge
verify fix
save
quit
nmcli connection up vlan10

similarly for virbr20+vlan20, virbr30+vlan30, virbr40+vlan40

KVM_Network_VLAN

Be the first to comment on "How to Configure KVM Virtualization in CentOS 7"

Leave a comment

Your email address will not be published.