man in black shirt wearing black headphones
Thu Oct 19

What is Kernel-based Virtual Machine (KVM)?

Kernel-based Virtual Machine (KVM) is a powerful and popular virtualization technology for Linux. It allows you to create and manage multiple [virtual machines (VMs)](https://www.binaryte.com/blog/everything-you-need-to-know- about-virtual-machine) on a single physical server, each with its own operating system and resources. In this tutorial, you will learn how to install and set up KVM on Ubuntu 20.04, how to create a VM using different tools, and how to manage your VMs using common commands.

What is KVM and why use it

KVM stands for Kernel-based Virtual Machine. It is a module that enables the Linux kernel to act as a hypervisor, which is a software layer that controls the hardware and creates virtual machines. KVM supports both Intel and AMD processors with virtualization extensions, such as VT-x and AMD-V.

KVM has many advantages over other virtualization solutions, such as:

  • It is open source and free to use
  • It is integrated with the Linux kernel, which means it has high performance and stability
  • It supports a wide range of guest operating systems, such as Windows, Linux, BSD, Solaris, and more
  • It supports various features, such as live migration, snapshots, hotplug, nested virtualization, and more
  • It has a rich ecosystem of tools and interfaces, such as libvirt, virt-manager, virsh, qemu, and more

Installing KVM on Ubuntu 20.04

Before you can use KVM, you need to install it on your Ubuntu 20.04 server. You also need to make sure that your server has a CPU that supports virtualization extensions and that they are enabled in the BIOS.

To install KVM on Ubuntu 20.04, follow these steps:

  1. Update your system packages:

    sudo apt update sudo apt upgrade

  2. Install the required packages:

    sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager

  3. Add your user to the libvirt and kvm groups:

    sudo usermod -aG libvirt $USER sudo usermod -aG kvm $USER

Log out and log back in for the changes to take effect.

Verify that KVM is installed and working:

sudo virsh list --all

You should see an output like this:

Id 	Name 	State
-----------------

This means that KVM is running and ready to create VMs.

Creating a VM using KVM

There are different ways to create a VM using KVM. You can use command-line tools such as virt-install or virsh, or graphical tools such as virt-manager. In this tutorial, we will show you how to use both methods.

Using virt-install

virt-install is a command-line tool that allows you to create a VM from scratch or from an existing disk image. You can specify various options for your VM, such as name, memory, CPU, disk size, network interface, graphics type, etc.

To create a VM using virt-install, follow these steps:

Download an ISO image of the guest operating system you want to install on your VM. For example, you can download Ubuntu 20.04 from here: https://releases.ubuntu.com/20.04/

Create a directory for your VM disk image:

mkdir ~/kvm-vm
  1. Run the virt-install command with the appropriate options:

    sudo virt-install
    —name ubuntu-vm
    —ram 2048
    —vcpus 2
    —disk path=~/kvm-vm/ubuntu-vm.qcow2,size=10
    —os-type linux
    —os-variant ubuntu20.04
    —network bridge=br0
    —graphics spice
    —cdrom ~/Downloads/ubuntu-20.04-desktop-amd64.iso

This command will create a VM named ubuntu-vm with 2 GB of RAM, 2 CPUs, 10 GB of disk space in qcow2 format (a compressed image format). The command will also use the spice protocol for graphics, which allows you to connect to the VM using a remote viewer such as virt-viewer. Finally, the command will use the downloaded ISO image as the installation media for the VM.

After running the command, you will see a graphical window that shows the installation process of the guest OS. Follow the instructions on the screen to complete the installation.

Once the installation is finished, you can start and connect to your VM using the following command:

sudo virsh start ubuntu-vm
virt-viewer ubuntu-vm

You can also use other options with virt-install, such as —import to create a VM from an existing disk image, —location to specify a URL for the installation media, —extra-args to pass additional arguments to the installer, etc. For more information, see the virt-install man page.

Using virsh

virsh is a command-line tool that allows you to manage your VMs using libvirt commands. You can use virsh to create, start, stop, reboot, delete, migrate, and perform other operations on your VMs.

To create a VM using virsh, follow these steps:

  1. Create an XML file that defines the configuration of your VM. You can use a text editor or a tool such as virt-xml to generate the XML file. For example, you can create a file named ubuntu-vm.xml with the following content:

    ubuntu-vm 2048 2 hvm destroy restart restart /usr/bin/qemu-system-x86_64

This XML file defines a VM with the same configuration as the one created by virt-install in the previous section.

  1. Run the virsh define command to create the VM from the XML file:

    sudo virsh define ubuntu-vm.xml

  2. Run the virsh start command to start the VM:

    sudo virsh start ubuntu-vm

  3. Run the virt-viewer command to connect to the VM:

    virt-viewer ubuntu-vm

You can also use other commands with virsh, such as list, shutdown, reboot, destroy, undefine, edit, dumpxml, migrate, etc. For more information, see the virsh man page.

Using virt-manager

virt-manager is a graphical tool that allows you to create and manage your VMs using a user-friendly interface. You can use virt-manager to perform various tasks on your VMs, such as creating, editing, cloning, deleting, starting, stopping, rebooting, migrating, and more.

To create a VM using virt-manager, follow these steps:

  1. Launch virt-manager from your desktop menu or by running this command:

    virt-manager

Click on File > New Virtual Machine or click on the plus icon.

Choose how you want to install the guest OS on your VM. You can choose from Local install media, Network install, Import existing disk image, or PXE boot. For this tutorial, we will choose Local install media and click on Forward.

Browse to the ISO image of the guest OS you want to install on your VM and click on Forward.

Choose the amount of memory and CPUs you want to allocate to your VM and click on Forward.

Choose the amount of disk space you want to allocate to your VM and click on Forward.

Enter a name for your VM and click on Finish.

The installation process of the guest OS will start in a new window. Follow the instructions on the screen to complete the installation.

Once the installation is finished, you can start and connect to your VM by clicking on it in the virt-manager window.

You can also use other options with virt-manager, such as changing the hardware configuration, taking snapshots, cloning, migrating, etc. For more information, see the virt-manager documentation.

Managing a VM using KVM

Once you have created a VM using KVM, you can manage it using various tools and commands. In this section, we will show you some common tasks that you can perform on your VMs using KVM.

Starting a VM

To start a VM using KVM, you can use one of these methods:

  • Using virt-manager: Select the VM in the virt-manager window and click on the play icon or right-click on it and choose Run.

  • Using virsh: Run this command:

    sudo virsh start vm-name

Replace vm-name with the name of your VM.

  • Using virt-install: Run this command:

    sudo virt-install —name vm-name —import

Replace vm-name with the name of your VM and add any other options you want.

Stopping a VM

To stop a VM using KVM, you can use one of these methods:

  • Using virt-manager: Select the VM in the virt-manager window and click on the stop icon or right-click on it and choose Shut Down.

  • Using virsh: Run this command:

    sudo virsh shutdown vm-name

Replace vm-name with the name of your VM.

  • Using virt-install: Run this command:

    sudo virt-install —name vm-name —destroy

Replace vm-name with the name of your VM and add any other options you want.

Rebooting a VM

To reboot a VM using KVM, you can use one of these methods:

  • Using virt-manager: Select the VM in the virt-manager window and click on the reboot icon or right-click on it and choose Reboot.

  • Using virsh: Run this command:

    sudo virsh reboot vm-name

Replace vm-name with the name of your VM.

  • Using virt-install: Run this command:

    sudo virt-install —name vm-name —restart

Replace vm-name with the name of your VM and add any other options you want.

Deleting a VM

To delete a VM using KVM, you can use one of these methods:

  • Using virt-manager: Select the VM in the virt-manager window and click on the delete icon or right-click on it and choose Delete.

  • Using virsh: Run these commands:

    sudo virsh destroy vm-name sudo virsh undefine vm-name

Replace vm-name with the name of your VM.

  • Using virt-install: Run this command:

    sudo virt-install —name vm-name —remove-all-storage —delete

Replace vm-name with the name of your VM and add any other options you want.

Conclusion

In this tutorial, you learned how to use KVM to create and manage virtual machines on Ubuntu 20.04. You learned how to install KVM, how to create a VM using different tools, and how to perform common tasks on your VMs using KVM. You also learned some basic concepts and features of KVM and its ecosystem.

KVM is a powerful and versatile virtualization technology that can help you run multiple operating systems and applications on a single server. It is also open source and free to use, which makes it an attractive option for many users and developers.