KVM is a free and open source virtualization tool for Linux like operating systems. When we install KVM on a Ubuntu Server then it becomes Type-2 KVM hypervisor. Minimum requirement for KVM is that server should have CPU virtualization extensions like Intel-VT and AMD-V. KVM is also known as Kernel based virtual machine because during KVM installation a kernel module (kvm-intel.ko / kvm-amd.ko) is loaded into the kernel.

In this article, we will demonstrate how to install and configure KVM hypervisor on Ubuntu 20.04 LTS server. We are assuming you already have freshly installed Ubuntu 20.04 LTS Server. Let’s deep dive into the KVM installation steps:

Step 1) Confirm Whether Server Support Hardware Virtualization

Login to your Ubuntu 20.04 LTS server and run following grep command,

[email protected]:~$ grep -E -c "vmx|svm" /proc/cpuinfo
2
[email protected]:~$

Above output confirms that server supports hardware virtualization. If the output is not greater than zero then reboot your server, go to its bios settings and enable VT technology.

If you wish to verify whether your server is capable of running KVM virtual machines then install “kvm-ok” utility using below apt command,

[email protected]:~$ sudo apt install -y cpu-checker

Once the “cpu-checker” package is installed, run “kvm-ok” command and verify its output

[email protected]:~$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
[email protected]:~$

Above output confirms that KVM based virtual machines can be installed on our server.

Step 2) Install KVM, virt-manager and bridge-utils

Run the beneath apt command to install KVM, bridge-utils, virt-manager and other kvm dependencies.

[email protected]:~$ sudo apt install -y qemu qemu-kvm libvirt-daemon bridge-utils virt-manager virtinst

Once all the packages have been installed successfully, verify whether KVM module is loaded into loaded or not using lsmod command,

[email protected]:~$ lsmod | grep -i kvm
kvm_intel             286720  0
kvm                   663552  1 kvm_intel
[email protected]:~$

Step 3) Start and verify libvirtd service

When we install KVM then libvirtd service get started automatically, run below command to verify its status,

[email protected]:~$ sudo systemctl status libvirtd.service

Output above command would look like below,

Libvirtd-service-status-ubuntu-20-04-lts-server

In case libvirtd service is not started by any reason then run following command to start and enable it

[email protected]:~$ sudo systemctl enable libvirtd --now

Step 4) Setup Network Bridge for KVM Virtual Machines

In this step, we will configure a network bridge for KVM VMs so that VMs can be accessed from outside. Though a virtual bridge “virbr0” is created automatically when we install KVM, but this is only used for testing purpose.

To create a network bridge, we need edit the file “/etc/netplan/00-installer-config.yaml” Following is the content of my server’s file before making the changes.

[email protected]:~$ cat /etc/netplan/00-installer-config.yaml

net-installer-config-file-before-bridge

Add network bridge entries, remove IP from enp0s3 interface and assign the same IP to the network bridge(br0). After making the changes, content of the file would look like below:

[email protected]:~$ sudo vi /etc/netplan/00-installer-config.yaml

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s3:
      dhcp4: no
      dhcp6: no
  version: 2
  bridges:
    br0:
      interfaces: [enp0s3]
      addresses: [172.20.10.9/28]
      gateway4: 172.20.10.1
      nameservers:
        addresses: [4.2.2.2, 8.8.8.8]

Network-Bridge-netplanner-config-ubuntu20-04-server

Run “netplan apply” command to activate the bride br0 and to make above changes into the effect.

[email protected]:~$ sudo netplan apply
[email protected]:~$

Run below commands to verify the status of br0 bridge

[email protected]:~$ sudo networkctl status br0

networkctl-status-br0-ubuntu

Status of bridge br0 can also be verified from following ip command,

[email protected]:~$ ip a s

network-bridge-ip-command-ubuntu

Great, above command’s output confirm that network bridge br0 is activated and configured successfully. Now let’s move to vm creation step.

Step 5) Create virtual machines with virt-manager & virt-install

virt-manager is X11-server based GUI (graphical user interface), so it requires xserver environment, run below commands to setup minimal xserver environment

[email protected]:~$ sudo apt update
[email protected]:~$ sudo apt-get install xorg -y

Once xorg packages are installed, reboot your server.

[email protected]:~$ sudo reboot

Once the system is available after reboot, open the terminal and type virt-manager command to start gui,

Start-Virt-Manager-Ubuntu-20-04

Now start creating a virtual a machine, click on Monitor Icon

Choose the first option and then click on forward,

Local-ISO-Media-KVM-Virt-Manager

Browse installation media like ISO file locally, in my case it’s Debian 10 ISO file which is placed in sysadm user’s home directory.

OS-ISO-File-KVM-Virt-Manager

Click on Forward,

In the next window, specify the RAM and vCPUs for your VM and then click on forward

RAM-CPUs-KVM-VM-Ubuntu20-04-Server

In the next window, specify the disk size for the VM,

Disk-for-KVM-VM-Ubuntu-20-04-Server

Click on Forward to proceed further,

Choose the network that you want to connect to the VM, in my case I am attaching the same network bridge (br0) that we have created in the above step and set the VM name as per the requirement.

Network-Selection-KVM-VM-Ubuntu-20-04-Server

Click on Finish to start OS installation.

Debian10-Installation-KVM-VM-Ubuntu-20-04-Server

Follow the screen instructions and complete Debian 10 OS installation.

Creating Virtual Machines using virt-install (command line tool)

virt-install is command line utility to install kvm VMs, example is shown below

[email protected]:~$ sudo virt-install -n CentOS-Server --description "VM for Database" --os-type=Linux --os-variant=rhel7 --ram=1096 --vcpus=1 --disk path=/var/lib/libvirt/images/centos-server.img,bus=virtio,size=10 --network bridge:br0 --graphics none --location /home/sysadm/CentOS-7-x86_64-DVD-1511.iso --extra-args console=ttyS0

Note: Change the parameters in above command that suits to your environment

We will get the following cli screen, follow the instructions and complete CentOS 7 installation,

virt-install-kvm-ubuntu20-04-lts-server

Refer “virt-install” man page to get more information about its different options and parameters.

That’s all from this article. I hope these steps help you to setup KVM hypervisor on your Ubuntu 20.04 LTS server, please don’t hesitate to share your feedback and comments.

from Linuxtechi https://www.linuxtechi.com/install-kvm-on-ubuntu-20-04-lts-server/