Skip to content

Module 9 of 16 · 📖 6 min read · ⏱ 60 min total

FUTO 09 KVM Virtualisierung (EN)

Table of contents (6 sections)
  1. Concepts and Background
  2. Architecture Diagram
  3. Practical Steps
  4. Common Pitfalls
  5. Further Resources
  6. Knowledge Check

FUTO 09 KVM Virtualization

In this module, you will learn the fundamentals of virtualization with KVM/QEMU under Linux. You will learn how to efficiently create and manage virtual machines, and gain insights into the most important tools such as libvirt, virsh, and virt-manager. The focus is on practical application for use in small and medium-sized enterprises.

You will understand how KVM works as a hypervisor, how to optimize virtual hardware with virtio drivers, and how to use cloud-init for the automatic initialization of VMs. Upon completion of this module, you will be able to set up and manage your own virtual infrastructures.

Concepts and Background

KVM/QEMU
Kernel-based Virtual Machine (KVM) is a hypervisor that is directly integrated into the Linux kernel. QEMU (Quick Emulator) provides the hardware emulation. Together, they enable fully virtualized machines with near-native performance.
libvirt
The libvirt API is a collection of tools for managing virtualization platforms. It provides a unified interface for various hypervisors and is the foundation for many management tools such as virsh and virt-manager.
virtio
Virtio is a standard for paravirtualized devices in virtual machines. These special drivers enable significantly higher performance for network and disk accesses by reducing the overhead of emulation.
cloud-init
cloud-init is a standard for initializing cloud instances. In VMs, it ensures automatic configuration on first boot, such as setting hostnames, creating users, or mounting SSH keys.

Architecture Diagram

flowchart TB
    Host[Host-System
Linux with KVM] -->|Hardware Abstraction| Hypervisor[KVM Hypervisor] Hypervisor --> VM1[VM 1
Debian] Hypervisor --> VM2[VM 2
Ubuntu] Hypervisor --> VM3[VM 3
CentOS] VM1 -->|virtio| Netz[Network Interface] VM2 -->|virtio| Netz VM3 -->|virtio| Netz VM1 -->|virtio| Speicher[Virtual Hard Disk] VM2 -->|virtio| Speicher VM3 -->|virtio| Speicher

Practical Steps

  1. Install the necessary packages with
    sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
    . This installs the KVM hypervisor, the libvirt management tools, and the graphical interface.
  2. Add your user to the libvirt group with
    sudo usermod -aG libvirt $USER
    . This allows you to manage VMs without root privileges.
  3. Start and enable the libvirt daemon with
    sudo systemctl enable --now libvirtd
    . The daemon is responsible for managing the virtual machines.
  4. Create a virtual hard disk with
    qemu-img create -f qcow2 /var/lib/libvirt/images/debian.qcow2 20G
    . The qcow2 format enables snapshots and efficient storage usage.
  5. Install a VM using virt-install via the console or use virt-manager for graphical installation. For a Debian VM in the "default" network, use:
    virt-install --name debian --memory 2048 --vcpus 2 --disk path=/var/lib/libvirt/images/debian.qcow2,size=20 --os-variant debian10 --network network=default --graphics spice
    .
  6. Configure the VM with cloud-init for automatic initialization. Create a user-data file and attach it during installation:
    virt-install --cloudinit user-data=cloud-init.cfg ...
    .
  7. Manage VMs with virsh, the command-line interface to libvirt. Start a VM with
    virsh start debian
    and get information with
    virsh dominfo debian
    .
  8. Create snapshots of VMs with
    virsh snapshot-create-as debian pre-upgrade
    . Snapshots allow for quick restoration of a VM state.

Common Pitfalls

Further Resources

Knowledge Check

Four questions for self-assessment. Click on each question to see the correct answer and explanation.

What is the main difference between KVM and QEMU in virtualization?
  • A) KEMU is an abbreviation for Kernel-based Emulation Utility, while QEMU stands for Quick Emulator.
  • B) KVM is a hypervisor that is directly integrated into the Linux kernel, while QEMU provides hardware emulation.
  • C) KVM is only suitable for Windows hosts, while QEMU runs exclusively on Linux.
  • D) KVM is used for managing virtual machines, while QEMU is used exclusively for installing operating systems.

Correct Answer: B. KVM is a kernel-based hypervisor, while QEMU provides hardware emulation. Option A is incorrect as there is no such abbreviation as KEMU. Option C is incorrect as KVM was primarily developed for Linux hosts. Option D is incorrect as KVM does not directly serve for management but provides the virtualization layer.

What advantage does the use of virtio drivers in virtual machines offer?
  • A) They enable the use of 3D graphics acceleration in VMs.
  • B) They reduce the overhead for network and disk accesses through paravirtualized devices.
  • C) They allow direct connection of physical USB devices to the VM without going through the host.
  • D) They enable live migration of VMs without interrupting network traffic.

Correct Answer: B. They reduce the overhead for network and disk accesses through paravirtualized devices. Option A is incorrect as 3D acceleration is typically handled by different technologies (like SPICE or VGA passthrough). Option C is incorrect as USB device passthrough is handled by different mechanisms. Option D is incorrect as live migration is a feature of the hypervisor and not specifically dependent on virtio drivers.

What is the purpose of cloud-init in a virtual machine?
  • A) It manages the lifecycle of cloud instances, including creation, scaling, and termination.
  • B) It provides a web-based interface for managing VMs through a browser.
  • C) It handles the automatic initialization and configuration of a VM during its first boot.
  • D) It enables the connection of VMs to cloud storage services like Amazon S3.

Correct Answer: C. It handles the automatic initialization and configuration of a VM during its first boot. Option A describes a cloud orchestration tool, not cloud-init specifically. Option B describes a tool like virt-manager or a web interface, not cloud-init. Option D is incorrect as cloud-init does not directly handle cloud storage connections.

Why should you use the qcow2 format instead of raw for VM disks in production?
  • A) qcow2 provides better compression and reduces disk space usage.
  • B) qcow2 supports encryption for VM disks, which raw does not.
  • C) qcow2 enables features like snapshots and efficient storage management.
  • D) qcow2 allows for live migration of VMs without downtime.

Correct Answer: C. qcow2 enables features like snapshots and efficient storage management. While qcow2 does offer some compression, that's not its primary advantage in production. While qcow2 does support encryption, that's not its main benefit over raw. Live migration is possible with both formats and is not specific to qcow2.