Virtualization is the process of creating a software-based version of a computer system in a layer abstracted from the actual hardware. This can be the whole operating system, storage, networking, servers, or applications. Virtualization is done through a hypervisor program that pools resources from the physical server and allocates them to the virtual machines.

There are two types of hypervisors;

  • Type 1, commonly known as bare metal hypervisors, are installed directly on a system’s hardware, making them ideal in a Production environment. These include; Citrix/Xen Server, VMware ESXi, and Microsoft Hyper-V.
  • Type 2 or hosted hypervisors have a layer of a host OS which means they are installed on an OS and can be started and stopped like a normal program. These include; VMware Workstation, Oracle Solaris Zones, VMware Fusion, and Oracle VM Server. These types of hypervisors are ideal for home/test lab environments.

KVM Virtualization

KXM short for Kernel-based Virtual Machine is an open-source virtualization technology built into Linux Kernel as a module as of 2.6.20. It lets you turn a Linux machine into a hypervisor that allows you to run multiple, isolated virtual machines with each virtual machine running a private virtualized hardware: a network card, disk, graphics adapter, etc.

It is mostly classified as a Type-1 hypervisor, which turns the Linux kernel into a “bare metal” hypervisor. This provides each VM with all basic services of the physical system, including the virtual BIOS (basic input/output system) and dedicated virtual hardware, such as processor, memory, storage, and network cards. This results in the VM simulating a physical machine.

Features

  • Secure – It uses SELinux and secure virtualization (sVirt) for enhanced VM security and isolation.
  • Mature – It was created in 2006 and has continuously been developed.
  • Cost-Efficient – It is open-source and comes at zero cost.
  • Scalable- It automatically scales to match the demand load.
  • Performace – It has high performance with fast booting to achieve desired results.
  • Low Latency and high prioritization – It divides processes with long computing time into smaller ones.
  • Supports Live Migration with no service interruption.
  • It uses the regular Linux scheduler and memory management

To run KVM, you will need an x86 machine running a recent Linux kernel on an Intel processor with VT (virtualization technology) extensions, or an AMD processor with SVM extensions (also called AMD-V).

This guide will show you how to install KVM Virtualization on CentOS 9|AlmaLinux 9|RHEL 9.

Install KVM Virtualization on CentOS 9|AlmaLinux 9|RHEL 9

Check if you have KVM support on your system. If something shows up, You have virtualization enabled.

egrep -c '(vmx|svm)' /proc/cpuinfo

Alternatively, you can check if virtualization is enabled with the following command.

$ lscpu | grep Virtualization:
Virtualization:                  VT-x

Install KVM and its dependencies with the following command.

sudo dnf install -y qemu-kvm qemu-img libvirt virt-install libvirt-client virt-manager

Verify KVM installation with the following command.

$ lsmod | grep kvm
kvm_intel             376832  0
kvm                  1085440  1 kvm_intel
irqbypass              16384  1 kvm

Enable and start the libvirtd service

sudo systemctl enable --now libvirtd

Check for the libvirtd service status

$ systemctl status libvirtd
● libvirtd.service - Virtualization daemon
     Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor >
     Active: active (running) since Fri 2022-07-01 23:21:45 EAT; 18s ago
TriggeredBy: ● libvirtd.socket
             ○ libvirtd-tls.socket
             ○ libvirtd-tcp.socket
             ● libvirtd-admin.socket
             ● libvirtd-ro.socket
       Docs: man:libvirtd(8)
             https://libvirt.org
   Main PID: 34390 (libvirtd)
      Tasks: 21 (limit: 32768)
     Memory: 15.0M
        CPU: 286ms

Verify your system is ready to create VMs. All checks should return a PASS value.

$ sudo virt-host-validate
  QEMU: Checking for hardware virtualization                                 : PASS
  QEMU: Checking if device /dev/kvm exists                                   : PASS
  QEMU: Checking if device /dev/kvm is accessible                            : PASS
  QEMU: Checking if device /dev/vhost-net exists                             : PASS
  QEMU: Checking if device /dev/net/tun exists                               : PASS
  QEMU: Checking for cgroup 'cpu' controller support                         : PASS
  QEMU: Checking for cgroup 'cpuacct' controller support                     : PASS
  QEMU: Checking for cgroup 'cpuset' controller support                      : PASS
  QEMU: Checking for cgroup 'memory' controller support                      : PASS
  QEMU: Checking for cgroup 'devices' controller support                     : PASS
  QEMU: Checking for cgroup 'blkio' controller support                       : PASS
...

Configuring Network Bridge

Create a bridge interface.

$ nmcli connection add type bridge con-name bridge0 ifname bridge0
Connection 'bridge0' (1c09f87e-7787-4a73-b525-45989feb7d9f) successfully added.

List the network interfaces to note down the names of the interface to add to the bridge.

$ nmcli device status
DEVICE   TYPE      STATE                                  CONNECTION 
ens18    ethernet  connected                              ens18      
virbr0   bridge    connected (externally)                 virbr0     
bridge0  bridge    connecting (getting IP configuration)  bridge0    
lo       loopback  unmanaged 

I want to use ‘ens18’ for the bridge. Hence, assign the existing connection profile to the bridge with the following command.

nmcli connection modify ens18 master bridge0

To assign an interface that is not configured to an existing connection to the bridge, use the following command syntax.

nmcli connection add type ethernet slave-type bridge con-name bridge0 ifname ens18 master bridge0

Assign IP settings of the bridge.

nmcli connection modify bridge0 ipv4.addresses '192.168.200.41/24'
nmcli connection modify bridge0 ipv4.gateway '192.168.200.1'
nmcli connection modify bridge0 ipv4.dns '192.168.200.1'
nmcli connection modify bridge0 ipv4.method manual

Activate the Connection

$ nmcli connection up bridge0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)

Verify the ports are connected.

$ nmcli device
DEVICE   TYPE      STATE                   CONNECTION 
ens18    ethernet  connected               ens18      
bridge0  bridge    connected               bridge0    
virbr0   bridge    connected (externally)  virbr0     
lo       loopback  unmanaged

To configure ports to be activated automatically when the bridge is enabled, use the following command

nmcli connection modify bridge0 connection.autoconnect-slaves 1

Reactivate the connection

nmcli connection up bridge0

Verify the connection

$ ifconfig
bridge0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.200.41  netmask 255.255.255.0  broadcast 192.168.200.255
        ether 6a:d4:46:a6:f0:d3  txqueuelen 1000  (Ethernet)
        RX packets 381  bytes 109438 (106.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 345  bytes 55304 (54.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens18: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 6a:d4:46:a6:f0:d3  txqueuelen 1000  (Ethernet)
        RX packets 78584  bytes 585548297 (558.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 60090  bytes 9897936 (9.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
......

Using libvirtd as a non-root user

Check if a Libvirtd group exists.

$ sudo getent group | grep libvirt
libvirt:x:976:

Create a Unix group

sudo groupadd --system libvirt

Add User to the group

sudo usermod -a -G libvirt $(whoami)
newgrp libvirt

Verify the user has been added to the group.

$ id $(whoami)
uid=1000(technixleo) gid=1000(technixleo) groups=1000(technixleo),10(wheel),976(libvirt)

Edit the main configuration file.

sudo vi /etc/libvirt/libvirtd.conf

Set the UNIX domain socket group ownership to libvirt by uncommenting the following line.

unix_sock_group = "libvirt"

Set the UNIX socket permissions for the R/W socket by uncommenting the following line and setting the correct permissions.

unix_sock_rw_perms = "0770"

Save the file and restart the Libvirtd service.

sudo systemctl restart libvirtd

Create VMs on CentOS 9|AlmaLinux 9|RHEL 9

You can create a VM using the Command Line Interface and a Graphical-User Interface.

a. Create VM with CLI

To create a VM, you can use the following command with the following options;

  • The name of the new machine (–name)
  • The amount of allocated memory (–memory)
  • The number of allocated virtual CPUs (–vcpus)
  • The type and size of the allocated storage (–disk)
  • The type and location of the OS installation source (–cdrom or –location)
virt-install \
--name centos \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/centos.img,size=20 \
--os-variant generic \
--os-type linux \
--network bridge=bridge0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/' \
--extra-args 'console=ttyS0,115200n8 serial'

Follow the options on the screen to create the VM.

Once the VM is created, it can be managed by the ‘virsh’ tool.
To start a VM on a local host, use the following command.

virsh start centos

To configure a VM to start automatically when a host starts use the following command.

 virsh autostart centos

To stop a VM on a local host.

virsh shutdown centos

To force shut an unresponsive VM, use the following command. This does not delete the VM but terminates the running instance of the VM.

virsh destroy centos

To delete a VM, use the following command. Make sure you back up the necessary data and shut down the VM before deleting it.

virsh undefine centos --remove-all-storage --nvram

b. Create a VM with GUI tool (virt-manager)

To create a VM with a GUI tool like virt-manager, launched it from the console using the following command or search from the applications installed. I installed virt-manager with KVM as one of the dependencies.

virt-manager

It opens as shown below. Click on the Monitor to create a new VM.

The first step is to choose how to install the Operating system.

The next option is to select the installation media.

Choose Memory and CPU settings.

Create a disk image for the virtual machine.

A summary of the settings will be shown. You can click on ‘Back’ to configure any changes. Then click on Finish which begins the installation process.

Select the Install option as shown below.

Follow the prompts to complete the installation. Afterward, the VM appears on the virt-manager dashboard as shown below. You can pause, shut down, or delete the VM from there.

And the process is complete.

Conclusion

Implementing KVM on a supported Linux Distribution like RHEL|AlmaLinux|CentOS expands KVM’s abilities including sharing common libraries, swap resources, and optimizing system performance. This guide has shown you how to install KVM Virtualization on CentOS 9|AlmaLinux 9|RHEL 9. With KVM you can run multiple virtual machines with different operating systems isolated from the host OS.

Other guides on RHEL 9 / CentOS 9:

LEAVE A REPLY

Please enter your comment!
Please enter your name here