Kernel-Based Virtual Machine (KVM) is an open-source Linux-based hypervisor that is inbuilt within the Linux Linux Operating systems. Is grouped as a type 1 hypervisor as it possesses type 1 characteristics such as direct access to hardware with VM’s. It has kvm.ko kernel module which creates a core virtualization infrastructure and supports both Intel and AMD processors.

KVM modifies Linux kernel to hypervisor making it possible to create and run as many virtual machines as possible. Virtual machines can be installed either via command line or GUI with help of virtual manager.

Rocky Linux 8 is the most recent Linux operating system which seems to be taking over in very future following late announcement of end of life for Cent-OS. Being a Linux system simply means that can be used to virtualize a data center. In this article we are going to learn how to install KVM on rocky Linux 8.

Features of KVM

1. Live migration. When moving VMs from one host to another in KVM you don’t need to switch off the VMS. VMS can be moved while still active and without any interruption in the network unless otherwise.

2. Security – Makes use of security enhanced Linux (SELinux) and secure virtualization (sVirt) to guarantee VMs security and isolation.

3. Storage independent – can utilize any type of storage used with Linux systems.

4. Scheduling and resource control – KVM is able to manage and control resources such as memory and storage assigned to different instances in a fair manner.

5. Hardware support – KVM supports all Linux supported kernel which means it can be as well supported in windows.

Install and Configure of KVM on Rocky Linux 8

Point to note:

Before you start the installation, ensure that your machine has hardware virtualization and if you are installing on a virtual machine ensure that nested virtualization is enabled.

Step 1: Check if hardware or nested virtualization is enabled

Check if hardware virtualization is enable using command below.

$ cat /proc/cpuinfo | egrep "vmx|svm"

If using VM running in virtual-box use command below to enable nested virtualization.

$ egrep --color -i "svm|vmx" /proc/cpuinfo

If the command below has no output go a head and enable with help of commands below. Make sure you shutdown of the V .

List all VMS installed in Virtualbox.

$ Vboxmanage list vms

output;

"rocky-linuxu-8" {8c977920-b8e7-4edb-8e0d-26a453facb40}

Enable nested virtualization to rocky Linux 8

$ vboxmanage modifyvm "rocky-linux-8" --nested-hw-virt on

Verify;

$ egrep --color -i "svm|vmx" /proc/cpuinfo

output:

flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq monitor vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx rdrand hypervisor lahf_lm pti tpr_shadow vnmi flexpriority vpid fsgsbase md_clear flush_l1d

At this point our environment is good to install KVM on Rocky Linux 8. Power on the VM to continue to installation stage.

Step 2: Install KVM Virtualization software on Rock Linux-8

Run command bellow to install KVM.

$ sudo dnf install @virt virt-top libguestfs-tools virt-instal

Screen output;

Last metadata expiration check: 0:14:20 ago on Tue May 25 02:12:44 2021.
Dependencies resolved.
======================================================================================================================================================
 Package                                  Architecture         Version                                                  Repository               Size
======================================================================================================================================================
Installing:
 libguestfs-tools                         noarch               1:1.40.2-25.module+el8.3.0+254+8534553e                  appstream                27 k
 virt-install                             noarch               2.2.1-3.el8                                              appstream                98 k
 virt-top                                 x86_64               1.0.8-32.el8                                             appstream               728 k
Installing group/module packages:
 libguestfs                               x86_64               1:1.40.2-25.module+el8.3.0+254+8534553e                  appstream               2.7 M
 libvirt-client                           x86_64               6.0.0-28.1.module+el8.3.0+254+8534553e                   appstream               360 k
Installing dependencies:
 autogen-libopts                          x86_64               5.18.12-8.el8                                            appstream                74 k
 gnutls-dane                              x86_64               3.6.14-8.el8                                             appstream                50 k
 gnutls-utils                             x86_64               3.6.14-8.el8                                             appstream               346 k
 hexedit                                  x86_64               1.2.13-12.el8                                            appstream                45 k
 hivex                                    x86_64               1.3.18-20.module+el8.3.0+254+8534553e                    appstream               111 k
 libguestfs-tools-c                       x86_64               1:1.40.2-25.module+el8.3.0+254+8534553e                  appstream               5.6 M
 libvirt-bash-completion                  x86_64               6.0.0-28.1.module+el8.3.0+254+8534553e                   appstream                53 k
 perl-Class-Inspector                     noarch               1.32-2.el8                                               appstream                35 k
 perl-Sys-Guestfs                         x86_64               1:1.40.2-25.module+el8.3.0+254+8534553e                  appstream               308 k
 perl-Sys-Virt                            x86_64               6.0.0-1.module+el8.3.0+254+8534553e                      appstream               314 k
 perl-hivex                               x86_64               1.3.18-20.module+el8.3.0+254+8534553e                    appstream                59 k
 perl-libintl-perl                        x86_64               1.29-2.el8                                               appstream               816 k
 python3-argcomplete                      noarch               1.9.3-6.el8                                              appstream                59 k
 python3-libvirt                          x86_64               6.0.0-1.module+el8.3.0+254+8534553e                      appstream               304 k
 scrub                                    x86_64               2.5.2-14.el8.rocky                                       appstream                44 k
 supermin                                 x86_64               5.1.19-10.module+el8.3.0+254+8534553e                    appstream               708 k
 syslinux                                 x86_64               6.04-4.el8                                               baseos                  577 k
 syslinux-extlinux                        x86_64               6.04-4.el8                                               baseos                  140 k
 syslinux-extlinux-nonlinux               noarch               6.04-4.el8                                               baseos                  385 k
 syslinux-nonlinux                        noarch               6.04-4.el8                                               baseos                  551 k
 virt-manager-common                      noarch               2.2.1-3.el8                                              appstream               889 k
Installing weak dependencies:
 libguestfs-xfs                           x86_64               1:1.40.2-25.module+el8.3.0+254+8534553e                  appstream               8.2 k
 perl-File-ShareDir                       noarch               1.104-3.el8                                              appstream                28 k
Installing module profiles:
 virt/common                                                                                                                                         

Transaction Summary
======================================================================================================================================================
Install  28 Packages

Total download size: 15 M
Installed size: 55 M
Is this ok [y/N]: y

Press Y then click Enter to continue.

After installation is complete you will see a verification message below the screen as shown below.


Installed:
  autogen-libopts-5.18.12-8.el8.x86_64                                   gnutls-dane-3.6.14-8.el8.x86_64                                             
  gnutls-utils-3.6.14-8.el8.x86_64                                       hexedit-1.2.13-12.el8.x86_64                                                
  hivex-1.3.18-20.module+el8.3.0+254+8534553e.x86_64                     libguestfs-1:1.40.2-25.module+el8.3.0+254+8534553e.x86_64                   
  libguestfs-tools-1:1.40.2-25.module+el8.3.0+254+8534553e.noarch        libguestfs-tools-c-1:1.40.2-25.module+el8.3.0+254+8534553e.x86_64           
  libguestfs-xfs-1:1.40.2-25.module+el8.3.0+254+8534553e.x86_64          libvirt-bash-completion-6.0.0-28.1.module+el8.3.0+254+8534553e.x86_64       
  libvirt-client-6.0.0-28.1.module+el8.3.0+254+8534553e.x86_64           perl-Class-Inspector-1.32-2.el8.noarch                                      
  perl-File-ShareDir-1.104-3.el8.noarch                                  perl-Sys-Guestfs-1:1.40.2-25.module+el8.3.0+254+8534553e.x86_64             
  perl-Sys-Virt-6.0.0-1.module+el8.3.0+254+8534553e.x86_64               perl-hivex-1.3.18-20.module+el8.3.0+254+8534553e.x86_64                     
  perl-libintl-perl-1.29-2.el8.x86_64                                    python3-argcomplete-1.9.3-6.el8.noarch                                      
  python3-libvirt-6.0.0-1.module+el8.3.0+254+8534553e.x86_64             scrub-2.5.2-14.el8.rocky.x86_64                                             
  supermin-5.1.19-10.module+el8.3.0+254+8534553e.x86_64                  syslinux-6.04-4.el8.x86_64                                                  
  syslinux-extlinux-6.04-4.el8.x86_64                                    syslinux-extlinux-nonlinux-6.04-4.el8.noarch                                
  syslinux-nonlinux-6.04-4.el8.noarch                                    virt-install-2.2.1-3.el8.noarch                                             
  virt-manager-common-2.2.1-3.el8.noarch                                 virt-top-1.0.8-32.el8.x86_64                                                

Complete!

Confirm that necessary kernel modules are loaded.

$ sudo lsmod | grep kvm

kvm_intel             303104  0
kvm                   798720  1 kvm_intel
irqbypass              16384  1 kvm

Step 3: Configuring KVM Virtualization on Rocky Linux 8

Next we have to create a directory where we will upload our ISO files for installation of VMs. At this part we are also going to configure storage domains where VM will be installed.

Create directory.

$ sudo mkdir -p /kvm/images/iso

Create virtual storage.

$ sudo qemu-img create -f qcow2 /var/lib/libvirt/images/linuxmint.qcow2 20G
Formatting '/var/lib/libvirt/images/linuxmint', fmt=qcow2 size=21474836480 cluster_size=65536 lazy_refcounts=off refcount_bits=16

Allow non-root user to run KVM commands. Run commands below as a non root user.

Add libvirtd group to Rocky Linux 8

$ sudo groupadd --system libvirt

Add user to the new group.

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

Step 4: Install Virtual Machine on KVM in Rocky Linux 8

Run command below to install VM on KVM using CLI.

$ sudo virt-install 
--virt-type kvm 
--name Linuxmint 
--ram 2024 
--disk /var/lib/libvirt/images/Linuxmint 
--network network=default 
--graphics vnc,listen=0.0.0.0 
--noautoconsole 
--os-type=linux 
--os-variant=generic –cdrom=/kvm/iso/linuxmint-20.1-cinnamon-64bit.iso

User virsh command to confirm that VM has started.

$ sudo virsh list --all

#output
ID       Name            State
--------------------------------
1        Linuxmint       Running

You view and manage VM on GUI with help of Virt-Manger

Step 5: Install Virt-Manager on Rocky Linux 8 (Optional)

$ sudo dnf install virt-manager
Dependencies resolved.
======================================================================================================================================================
 Package                               Architecture                    Version                               Repository                          Size
======================================================================================================================================================
Installing:
 virt-manager                          noarch                          2.2.1-3.el8                           appstream                          368 k

Transaction Summary
======================================================================================================================================================
Install  1 Package

Total download size: 368 k
Installed size: 2.3 M
Is this ok [y/N]: y

Press y then click Enter to continue.

When installation is complete, launch virt-manager on rocky-linux-8

Screen below will appear showing one VM is running.

Create a virtual machine using virt-manager.

Choose installation method.

Select ISO file to use.

Set memory and CPU.

Create a virtual disk.

Provide name for the new VM.

Click finish to start installation.

Screen below shows running VMs.

Select the VM and click on open to finalize installaation.

Success, linux mint VM is running in trial mode.

Below are virsh commands that can be used to manage VMs in CLI.

#Start VM
$ sudo virsh start VM_name

#Stop VM
$ sudo virsh stop VM_name

#Save current state of running VM
$ sudo virsh save VM_name VM_name_save

#Restore saved VM
$ sudo restore VM_name_save

#Reboot
$ sudo virsh reboot VM_name

#Pause/Suspend VM
$ sudo virsh suspend VM_name

#Resume Suspended VM
$ sudo resume VM_name

#Shutdown
$ sudo virsh shutdown VM_name

#Expunge
$ sudo virsh destroy VM_name

Check Virsh Cheat Sheet for comprehensive command line management tips.

Conclusion

So far so good. We have managed to install KVM on Rocky Linux 8 and manged to make some configurations such as creating virtual disks and adding libvirt groups. We also went a head and launched a VM on command line. For better management, we have also installed virt-manager which provides Graphical interface to enhance deploying and managing of VMs on KVM. Hope the guide was helpful. Comment and share with friends.