Setting Up KVM and QEMU: A Practical Installation Guide

Virtualization has become an essential aspect of modern computing infrastructure, enabling efficient utilization of hardware resources and providing a sandboxed environment for testing and development. Kernel-based Virtual Machine (KVM) and Quick EMUlator (QEMU) are two powerful open-source tools that enable hardware-assisted virtualization on Linux systems. This comprehensive guide will walk you through the process of setting up KVM and QEMU on your Linux machine, allowing you to create and manage virtual machines with ease.

Prerequisites

Before diving into the installation process, ensure that your system meets the necessary requirements. Your CPU should support hardware virtualization extensions, such as Intel VT-x or AMD-V. Additionally, make sure that virtualization support is enabled in your BIOS settings. You’ll need a Linux distribution with a recent kernel, preferably running a version above 2.6.20.

Installing KVM and QEMU

Updating Package Repositories

Begin by updating your package repositories to ensure you have access to the latest software packages. Open a terminal and execute the following command:

sudo apt update

Installing KVM and QEMU Packages

Install the KVM and QEMU packages along with their dependencies using the package manager:

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

Verifying Installation

After the installation is complete, verify that KVM has been set up correctly by checking if the kvm module is loaded:

lsmod | grep kvm

Creating a Virtual Machine

Now that KVM and QEMU are installed, you can create your first virtual machine.

Downloading OS ISO

Download the ISO image of the operating system you want to install in your virtual machine. For example, to download Ubuntu Server 20.04:

wget https://releases.ubuntu.com/20.04/ubuntu-20.04.3-live-server-amd64.iso

Creating a Virtual Disk

Create a virtual disk where the OS will be installed:

qemu-img create -f qcow2 ubuntu-20.04.qcow2 20G

Installing the OS

Start the virtual machine installation process:

virt-install --name my-vm --ram 2048 --disk path=ubuntu-20.04.qcow2,size=20 --vcpu 2 --cdrom ubuntu-20.04.3-live-server-amd64.iso --network bridge=virbr0 --graphics vnc,listen=0.0.0.0

Managing Virtual Machines

Starting a VM

To start a virtual machine, use the following command:

virsh start my-vm

Stopping a VM

To stop a virtual machine, run:

virsh shutdown my-vm

Connecting via VNC

You can connect to your VM’s console using a VNC viewer and the IP address of your host machine.

Conclusion

Setting up KVM and QEMU provides a robust virtualization environment on your Linux system. This guide has covered the installation process and basic VM management. From here, you can explore advanced features like snapshotting, cloning, and networking configurations to enhance your virtualization experience. Virtualization offers flexibility and efficiency, whether you’re a developer, sysadmin, or just someone curious to explore different operating systems in a controlled environment.

Related Articles