Install and Use KVM Virtualization on Rocky Linux 9

Posted on 117 views

Virtualization is an old technology that still finds high use in this era of cloud computing. It can be defined as dividing the computer resources logically. By doing so, it helps create software-based or virtual versions of computer resources such as networks, storage, servers, and applications. Moreso, organizations are able to partition a single server into different Virtual Machines that interact independently

To facilitate virtualization, a hypervisor is used either on top of the operating system or directly installed on the hardware. This brings the two major Hypervisor types:

  • Bare Metal/Type 1 Hypervisors: These hypervisors are installed directly on top of the physical machine. They are considered secure hypervisors when compared to Type2 Hypervisors. They include Microsoft Hyper-V, open-source Kernel-based VMs (KVMs), VMware ESXi
  • Hosted/Type 2 Hypervisors: These are installed on top of an existing operating system. They have more latency as compared to Type 1 due to their hosted nature. These include VMware Workstation and Oracle VirtualBox

Kernel-based Virtual Machine abbreviated as KVM is an open-source hypervisor that allows your Linux system to host and run multiple isolated virtual environments. It works by converting the Linux system into a Type1/bare-metal hypervisor. It avails the host resources including Memory, CPUs, and virtual devices to the virtual machine instances.

KVM offers a lot of features that include high performance, scalability, scheduling, and resource control, lower latency, live migration, memory management e.t.c Amongst the amazing feature is the ability to automate deployments on KVM using Ansible and other automation tools. This makes it easy to run several deployments at once.

In this guide, we will take a walk through how to install and use KVM Virtualization on Rocky Linux 9.

Getting Started

You need to confirm if the required virtualization extensions are available. These are Intel VT for Intel or AMD-V for AMD processors. If not enabled, you may be required to make the configuration in BIOS.

cat /proc/cpuinfo | egrep --color "vmx|svm"

Alternatively, you can use the command:

$ lscpu | grep Virtualization
Virtualization:                  VT-x
Virtualization type:             full

1. Install KVM tools on Rocky Linux 9

The KVM packages reside in the default Rocky Linux 9 repositories and can be installed using the command:

sudo dnf install qemu-kvm libvirt virt-manager virt-install 

Install other managements tools:

sudo dnf install epel-release -y
sudo dnf -y install bridge-utils virt-top libguestfs-tools bridge-utils virt-viewer 

Once complete, check if the kernel modules are loaded:

$ lsmod | grep kvm
kvm_intel             364544  0
kvm                  1056768  1 kvm_intel
irqbypass              16384  1 kvm

Start and enable the service:

sudo systemctl start libvirtd
sudo systemctl enable libvirtd

Verify if the service is running:

$ systemctl status libvirtd
 libvirtd.service - Virtualization daemon
     Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: disabled)
     Active: active (running) since Fri 2022-07-22 14:28:15 CEST; 8s ago
TriggeredBy: ● libvirtd.socket
             ● libvirtd-ro.socket
             ● libvirtd-admin.socket
             ○ libvirtd-tcp.socket
             ○ libvirtd-tls.socket
       Docs: man:libvirtd(8)
             https://libvirt.org
   Main PID: 35007 (libvirtd)
      Tasks: 21 (limit: 32768)
     Memory: 14.6M
        CPU: 293ms
     CGroup: /system.slice/libvirtd.service
             ├─35007 /usr/sbin/libvirtd --timeout 120
             ├─35119 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_>
             └─35120 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_

To execute commands, you need to add your system user to the KVM group:

sudo usermod -aG libvirt $USER
newgrp libvirt

2. Create a Network Bridge for KVM instances

By default, a network bridge with the name virbr0 is created to provide NAT. The VMs using this bridge do not have external connectivity.

The available bridge networks can be checked using the command:

$ brctl show
bridge name	bridge id		STP enabled	interfaces
virbr0		8000.5254002f2d76	yes		

For external connections, you need to create a network bridge. In this guide, I will show you how to create a network bridge using NMCLI.

Begin by identifying the available connections:

$ sudo nmcli connection show 
NAME     UUID                                  TYPE      DEVICE  
enp6s18  f546038c-74f7-359d-849b-c7be2f0bd125  ethernet  enp6s18 
virbr0   221e4b7d-ebef-4acb-b3e5-b729338f0d1d  bridge    virbr0  

After identifying the device, export the related variables for the bridge:

BR_NAME="br0"
BR_INT="enp6s18"
SUBNET_IP="192.168.205.10/24"
GW="192.168.205.1"
DNS1="8.8.8.8"
DNS2="8.8.4.4"

Here;

  • BR_NAME: The name of the bridge to be created.
  • BR_INT: the physical network device to be used as bridge slave.
  • SUBNET_IP: IP address and subnet assigned to the bridge created.
  • GW: The IP address of the default gateway
  • DNS1 and DNS2: IP addresses of the DNS servers to be used.

Proceed and define the bridge network:

sudo nmcli connection add type bridge autoconnect yes con-name $BR_NAME ifname $BR_NAME

Now add the IP, gateway, and DNS to the bridge:

sudo nmcli connection modify $BR_NAME ipv4.addresses $SUBNET_IP ipv4.method manual
sudo nmcli connection modify $BR_NAME ipv4.gateway $GW
sudo nmcli connection modify $BR_NAME ipv4.dns $DNS1 +ipv4.dns $DNS2

Add the identified network device as a slave to the bridge:

sudo nmcli connection delete $BR_INT
sudo nmcli connection add type bridge-slave autoconnect yes con-name $BR_INT ifname $BR_INT master $BR_NAME

Verify the creation:

sudo nmcli connection show 

Sample Output:

KVM-Virtualization-on-Rocky-Linux-9-2

Start the network bridge:

sudo nmcli connection up br0

Verify if the network is running:

sudo nmcli connection show br0

You can also use the command:

$ ip ad
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp6s18:  mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000
    link/ether 72:b2:b9:a5:70:7c brd ff:ff:ff:ff:ff:ff
3: br0:  mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 72:b2:b9:a5:70:7c brd ff:ff:ff:ff:ff:ff
    inet 192.168.205.10/24 brd 192.168.205.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever

For the bridge to be used by KVM, edit the below file:

sudo vim /etc/qemu-kvm/bridge.conf

Add the line:

allow all

Then restart KVM:

sudo systemctl restart libvirtd

3. Create Virtual Machines on KVM

There are two ways how to create VMs on KVM. These are:

  • Using the CLI
  • Using Virtual Machine Manager

To proceed, you need an ISO file required for the OS installation.

Option 1 – Using virt-install CLI tool

Spinning a VM from the CLI is simple especially if you are familiar with the KVM concepts, all you need to do is run a command with the following variables captured. But first, set the right ownership of the libvirt directory:

sudo chown -R $USER:libvirt /var/lib/libvirt/

Then proceed as shown:

virt-install \
--name Rocky9 \
--ram 2048 \
--vcpus 1 \
--disk path=/var/lib/libvirt/images/rocky-9.img,size=20 \
--os-variant centos-stream9 \
--os-type linux \
--network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--location /home/rocky9/Downloads/Rocky-9.0-x86_64-minimal.iso 

Remember :

  • –disk path=/var/lib/libvirt/images/rocky-9.img,size=20 is the path to create the disk and disk size in GBs
  • –vcpus 1 is the number of CPUs to be used
  • –ram 2048 is the allocated memory.
  • –network bridge=br0 flag specifies the network bridge to use.
  • –graphics vnc,listen=0.0.0.0 specifies the VNC listen address. You can also set a password by editing the line to be –graphics vnc,listen=0.0.0.0,password=password
  • –location /home/rocky9/Downloads/Rocky-9.0-x86_64-minimal.iso is the path of your ISO file.

Once the command is executed, VNC will be launched as shown.

KVM-Virtualization-on-Rocky-Linux-9

You can view available VMs using the command:

$ virsh list --all
 Id   Name              State
---------------------------------
 1    centos-stream-9   running
 2    Rocky9            running

Option 2 – Using the Virtual Machine Manager GUI tool

The GUI tool provides an easy-to-understand method. Here, all the activities performed above are done in a GUI tool launched as shown:

KVM-Virtualization-on-Rocky-Linux-9-1

To create a VM, click on the highlighted icon.

KVM-Virtualization-on-Rocky-Linux-9-3

Load the ISO file

KVM-Virtualization-on-Rocky-Linux-9-4

Browse and load the ISO file:

KVM-Virtualization-on-Rocky-Linux-9-7

Configure the CPU and Memory for the VM.

KVM-Virtualization-on-Rocky-Linux-9-5

Create and set the hard disk size.

KVM-Virtualization-on-Rocky-Linux-9-6

Allow customization before installation by checking the box as shown below. You can also configure the VM network and select the network bridge.

KVM-Virtualization-on-Rocky-Linux-9-8

On the overview windows, you can configure the system chipset and Firmware(whether BIOS or UEFI).

KVM-Virtualization-on-Rocky-Linux-9-9

Add/configure a VNC server. You can as well set a password and port.

KVM-Virtualization-on-Rocky-Linux-9-10

You can also add the required hardware such as TPM required for some Operating systems such as Windows 11

KVM-Virtualization-on-Rocky-Linux-9-11

Once desired configurations have been made, click finish and apply the changes. On the overview page, click Begin Installation and proceed:

KVM-Virtualization-on-Rocky-Linux-9-12

More demonstration of how to enable create a VM on KVM and enable TPM 2.0, UEFI, and many other VM configurations has been captured in the guide on how to:

That is it!

We have triumphantly walked through how to install and use KVM Virtualization on Rocky Linux 9. I hope this was important to you.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.