Setting Up KVM and Virtualization on Ubuntu
What is Virtualization?
In computing, virtualization is the method of creating a software-based instance of a specific tool that is typically physical. The kinds of tools one can virtualize are:
- Operating system (OS), the most common example of virtualization.
- Storage device.
- Computer network.
Virtualization enables the creation of a virtual machine (VM) that is different from a physical machine but where its physical resources are assigned. It is a great way of reducing the amount of needed physical servers or resources and enables the separation of certain applications on several different machines. This separation helps to troubleshoot specific issues that arise.
Besides popular 3rd party virtualization applications like Oracle’s VirtualBox or VMware, the Linux kernel’s built-in module called kernel-based virtual machine (KVM) enables the Linux kernel to function as a hypervisor. KVM merged into the Linux kernel in 2007 and has seen rapid expansion in popularity. It was adopted on various operating systems and supported on most CPU platforms (x86, PowerPC, ARM, etc.).
When it comes to virtualization structure and methodologies, every VM (also known as a guest) gets created and run by software or hardware-based hypervisor. The hypervisor virtualization runs on physical hardware, also known as a host.
Types of Hypervisors
There are two distinct hypervisors: Type 1 and Type 2.
Type 1 hypervisors are directly installed on physical hardware, without an operating system or other software in between. This type is called a bare-metal hypervisor.
Type 2 hypervisors run on operating systems, which separates the hypervisor from the physical server. The software or OS enables running VMs on the host system.
The distinction between the two types is often blurred, as KVM can be categorized as either Type 1 or Type 2. By nature, the KVM module turns the Linux kernel into a Type 1 bare-metal hypervisor. However, it is viewed as Type 2 since the host OS is fully functional, and all running VMs are seen by it as standard Linux processes.
In today’s Ubuntu server virtualization tutorial, KVM is installed on top of Ubuntu 20.04 LTS, classifying it as a Type 2 hypervisor.
Like many other Linux distributions, Ubuntu virtualization supports various technologies and apps. However, the majority of commands run in the terminal require root access or sudo privileges.
Verify Whether Your Hardware Supports Virtualization
Before KVM installation and setup, review whether or not your CPU supports hardware virtualization. To do so, run the following command. If the output shows any number besides 0, your CPU in its current state can run virtualization.
root@ubuntu:~# egrep -c '(vmx|svm)' /proc/cpuinfo 2
If your output is 0:
- Reboot your system.
- Access the BIOS settings.
- Select CPU.
- Intel Processors: Intel Virtualization Technology (Intel VT).
- AMD: AMD-V.
Virtualization is disabled by default and must be enabled to utilize various virtualization technologies like KVM. Depending on your CPU manufacturer, it may or may not provide an option to enable virtualization.
Once enabling virtualization in the BIOS settings, recheck it using the below command. The lscpu command works for both processors and yields the same information in a different way.
root@ubuntu:~# lscpu | grep 'Virtualization\|Hypervisor' Virtualization: AMD-V Hypervisor vendor: KVM Virtualization type: full
CPU: Intel Processors
To verify virtualization is enabled on an Intel processor, use the following command. If the output shows vmx flags, it means your CPU supports hardware virtualization.
grep --color vmx /proc/cpuinfo
Use the below command to verify virtualization is enabled on AMD CPUs.
grep --color svm /proc/cpuinfo
Review if your computer supports KVM acceleration after confirming virtualization is enabled on your CPU.
In general, KVM acceleration improves the performance of the VM, enabling it to run applications at almost native speeds. This contributes to KVM being faster than any other industry hypervisor. Use the below command to check if your computer supports KVM acceleration.
root@ubuntu:~# kvm-ok INFO: /dev/kvm exists KVM acceleration can be used
Install the kvm-ok tool by using the apt command on your Ubuntu OS if not already installed.
root@ubuntu:~# apt install cpu-checker
Install KVM on Ubuntu Server
Update your OS repositories.
root@ubuntu:~# apt update
Next, install the following tools (packages) and their dependencies:
- qemu is a tool that runs OS emulation. qemu-kvm is the main package.
- libvirt-daemon is a daemon used for KVM virtualization.
- libvirt-clients is a package that contains virsh and various client tools.
- bridge-utils allows enabling users to access the VM.
- virt-manager is a graphical user interface (GUI)-based application for creating and managing VMs.
root@ubuntu:~# apt install -y qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager
Once installation of the essential packages completes, check the status of the libvirt daemon.
root@ubuntu:~# service libvirtd status ● libvirtd.service - Virtualization daemon Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-09-16 13:14:47 CEST; 1min 3s ago
If the daemon is not running, start it with the systemctl command.
root@ubuntu:~# systemctl enable --now libvirtd
Use the lsmod command to check if the KVM modules are loaded.
root@ubuntu:~# lsmod | grep -i kvm kvm_amd 114688 0 kvm 823296 1 kvm_amd
If multiple users exist on Ubuntu OS, add them to each of the below groups enabling them to create and start virtual machines.
Add the users to libvirt with the following command.
root@ubuntu:~# adduser 'Tom' libvirt Adding user `Tom' to group `libvirt' ... Adding user Tom to group libvirt Done.
Add the users to the KVM with the command below.
root@ubuntu:~# adduser 'Tom' kvm Adding user `Tom' to group `kvm' ... Adding user Tom to group kvm Done.
Create a Virtual Machine Using KVM
In this tutorial, CentOS functions as the guest OS on our Ubuntu virtualization host.
Create VMs with the command line interface (CLI) or KVM’s virt-manager GUI.
Command Line Interface
Create a VM via CLI by using the virt-install command.
root@ubuntu:/ # virt-install --name=centos \ --vcpus=1 \ --memory=1024 \ --os-variant=centos8 \ --disk path=/var/lib/libvirt/images/centosvm-os.qcow2,format=qcow2,bus=virtio,size=20 \ --cdrom=/home/tom/Downloads/CentOS-8.3.2011-x86_64-boot.iso \ --network bridge:eth1 \ --graphics none
After defining the above parameters and running the command, installation of the CentOS VM will begin.
Here is a short clarification of the options used above:
- --name: Define the name of your VM.
- --vcpus: Number of virtual CPUs you want to assign to your VM.
- --memory: The amount of RAM to be assigned.
- --os-variant: This option is not required, but defining it will help VM performance.
- --disk path: Defines the location for VM installation (it’s disk file), usually used with size option to assign amount in gigabytes.
- --cdrom: Path to ISO (optical disc image) file.
- --network bridge: Used to set network interface.
- --graphics: Used to set up graphical interface. In our case, none is needed.
If you are unsure of your OS family, install the libosinfo-bin package.
root@ubuntu:/# apt install libosinfo-bin
Use the following command to review the OS family.
root@ubuntu:/# osinfo-query os
Graphical User Interface (GUI)
To create a virtual machine, search for and start VM Manager from the Applications menu.
As VM Manager starts up, click on the computer icon in the top left.
Next, select the Local install media (ISO image or CDROM) installation option. Click Forward to continue.
Click Browse... and choose the ISO file, then define the operating system and click Forward to continue.
Allocate CPU and RAM resources for the VM and click Forward to continue.
Next, adjust the storage options and allocate the disk space for the VM. Click Forward.
Finally, designate a VM name, review the configuration, and define the network settings. Clicking the Finish button will start the installation of the VM OS from the ISO file.
This tutorial covered Ubuntu virtualization concepts, including what is virtualization, Type 1 and Type 2 hypervisors, and the use of KVM to set up an Ubuntu VM host using CLI and GUI.
Liquid Web’s dedicated hosting and managed private cloud hosting provide Ubuntu server options for your next project. Our Sales team is available 24/7 for any assistance you might need getting set up. Contact them today.
- 4 Methods for How to Install Yarn on Windows Server
- How to Install Bpytop Resource Monitoring Tool on AlmaLinux
- How to Fix “This Site Can’t Provide a Secure Connection” Error
- How to Install MongoDB on AlmaLinux
- How to Install PostgreSQL on AlmaLinux
- How to Use the WP Toolkit to Secure and Update WordPress
About the Author: Thomas Janson
Thomas Janson joined Liquid Web's Operations team in 2019. When he is not behind the keyboard, he enjoys reading books, financial statements, playing tennis, and spending time outdoors.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
What Is WebP and What Makes it Different from Other Image Formats?Read Article
Top 10 Password Security StandardsRead Article
Top 10 Password Security StandardsRead Article
How to Install MongoDB on AlmaLinuxRead Article
How to Use the WP Toolkit to Secure and Update WordPressRead Article