Virtualization in Linux: Using KVM and QEMU

Virtualization is a powerful technology that allows multiple operating systems to run concurrently on a single physical machine. In the Linux ecosystem, Kernel-based Virtual Machine (KVM) combined with QEMU, a hosted virtual machine monitor, provides a robust framework for creating and managing virtual environments. This post delves into setting up virtualization on a Linux machine using KVM and QEMU.

Prerequisites

Before diving into the setup, ensure your system supports hardware virtualization. For Intel processors, VT-x must be enabled, and for AMD processors, AMD-V should be on. You can verify this using the following commands:

# Verify CPU support for virtualization
$ egrep -c '(vmx|svm)' /proc/cpuinfo

A result greater than 0 indicates support. Next, confirm that KVM modules are available:

# List KVM modules
$ lsmod | grep kvm

If correctly installed, you will see “kvm_intel” or “kvm_amd” listed.

Installing KVM and QEMU

Install the necessary packages for KVM, QEMU, and other tools like libvirt and virt-manager (a graphical tool for managing virtual machines):

# Update package lists and install necessary virtualization packages
$ sudo apt update
$ sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager

Enable and start the libvirt service:

# Enable and start the libvirt service
$ sudo systemctl enable libvirtd
$ sudo systemctl start libvirtd

Creating a Virtual Machine

  1. Verify Group Membership: Ensure your user is part of the libvirt and kvm groups.

     # Add user to groups for virtualization rights
     $ sudo usermod -aG libvirt,kvm $USER
     $ newgrp libvirt
    
  2. Create a Virtual Disk:

    Use qemu-img to create a disk image for the virtual machine. This example creates a 20GB disk:

     # Create a new virtual disk
     $ qemu-img create -f qcow2 ~/vm_disk.qcow2 20G
    
  3. Define a Virtual Machine:

    Use virt-install to define and start the installation of the OS on the VM. This command starts an Ubuntu server VM with a CDROM boot:

     # Install a new virtual machine
     $ sudo virt-install \
     --name UbuntuServer \
     --ram 2048 \
     --disk path=~/vm_disk.qcow2,size=20 \
     --vcpus 2 \
     --os-variant ubuntu20.04 \
     --cdrom /path/to/ubuntu-20.04.iso \
     --network network=default \
     --graphics none
    

Accessing the Virtual Machine

For headless servers, access the newly created VM using virsh:

# Connect to the virtual machine
$ virsh list --all

# Start your virtual machine
$ virsh start UbuntuServer

# Access your virtual machine console
$ virsh console UbuntuServer

Managing Virtual Machines

The virt-manager GUI provides a user-friendly interface to manage VMs. Launch it using:

# Open Virt-Manager
$ virt-manager

From here, you can start, stop, and configure your virtual machines with ease.

Conclusion

KVM and QEMU, complemented by tools like libvirt and virt-manager, offer a comprehensive solution for virtualization on Linux. This guide has walked you through setting up a basic virtual environment and managing virtual machines. Explore further with different configurations and optimizations to tailor the virtual environment to your specific needs. Happy virtualizing!