QEMU/KVM Virtual Machines

Level: Intermediate Module: Virtualization & Containers 8 min read Lesson 55 of 66

Overview

  • What you’ll learn: How to create and manage virtual machines using QEMU with KVM hardware acceleration, including disk image formats, CPU and memory allocation, and virtio drivers on Ubuntu.
  • Prerequisites: Virtualization Concepts (Lesson 54), hardware virtualization enabled in BIOS/UEFI
  • Estimated reading time: 20 minutes

Introduction

QEMU (Quick Emulator) is a versatile open-source machine emulator and virtualizer. When combined with KVM (Kernel-based Virtual Machine), it provides near-native performance for virtual machines on Linux hosts. QEMU handles device emulation while KVM provides hardware-accelerated CPU and memory virtualization through the Linux kernel.

This combination is the foundation of most Linux virtualization stacks, including those used by major cloud providers. Understanding QEMU/KVM gives you direct control over virtual machine creation and configuration, and provides the knowledge needed to work with higher-level management tools like libvirt.

In this lesson, you will install QEMU and KVM on Ubuntu, learn to create and manage disk images in various formats, launch virtual machines with custom hardware configurations, and optimize performance using virtio paravirtualized drivers.

Installing QEMU and KVM on Ubuntu

Before creating virtual machines, you need to install the QEMU and KVM packages and verify that your system supports hardware virtualization. The kvm-ok utility from the cpu-checker package provides a quick verification.

# Install QEMU, KVM, and supporting tools
$ sudo apt update
$ sudo apt install -y qemu-system-x86 qemu-utils cpu-checker bridge-utils

# Verify KVM support
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

# Check KVM kernel module
$ lsmod | grep kvm
kvm_intel            368640  0
kvm                 1028096  1 kvm_intel

# Add your user to the kvm group for non-root access
$ sudo usermod -aG kvm $USER
$ newgrp kvm

The qemu-system-x86 package provides the x86_64 system emulator, qemu-utils includes disk image management tools, and cpu-checker provides the kvm-ok command. Adding your user to the kvm group allows access to /dev/kvm without root privileges.

Disk Image Formats and Management

Virtual machines require virtual disk images to store their operating systems and data. QEMU supports several disk image formats, each with different characteristics suited to various use cases.

qcow2 Format

The QEMU Copy-On-Write version 2 (qcow2) format is the most commonly used format for QEMU/KVM. It supports thin provisioning (the file grows as data is written), snapshots, compression, and encryption.

Raw Format

Raw images are simple byte-for-byte representations of the virtual disk. They offer the best I/O performance but consume the full allocated size immediately and do not support advanced features like snapshots.

# Create a 20GB qcow2 disk image (thin provisioned)
$ qemu-img create -f qcow2 ubuntu-vm.qcow2 20G
Formatting 'ubuntu-vm.qcow2', fmt=qcow2 cluster_size=65536 ...

# Create a raw disk image
$ qemu-img create -f raw ubuntu-vm.raw 20G

# View image information
$ qemu-img info ubuntu-vm.qcow2
image: ubuntu-vm.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 196 KiB
cluster_size: 65536

# Convert between formats
$ qemu-img convert -f qcow2 -O raw ubuntu-vm.qcow2 ubuntu-vm.raw
$ qemu-img convert -f raw -O qcow2 ubuntu-vm.raw ubuntu-vm.qcow2

# Resize a qcow2 image (add 10GB)
$ qemu-img resize ubuntu-vm.qcow2 +10G

# Create a snapshot-based overlay image
$ qemu-img create -f qcow2 -b ubuntu-vm.qcow2 -F qcow2 overlay.qcow2

Notice how the qcow2 image occupies only 196 KiB on disk despite having a virtual size of 20 GiB. This thin provisioning is one of qcow2’s key advantages. The overlay image technique creates a new image that stores only the differences from the base image, which is useful for testing and rapid VM cloning.

Launching Virtual Machines

The qemu-system-x86_64 command launches a virtual machine. You configure the VM’s hardware through command-line parameters, specifying CPU type, memory, disk devices, network interfaces, and more.

# Basic VM launch with KVM acceleration
$ qemu-system-x86_64 
    -enable-kvm 
    -m 2048 
    -smp 2 
    -cpu host 
    -hda ubuntu-vm.qcow2 
    -cdrom ubuntu-22.04-live-server-amd64.iso 
    -boot d 
    -vnc :1

# Headless VM with virtio devices and port forwarding
$ qemu-system-x86_64 
    -enable-kvm 
    -m 4096 
    -smp cores=2,threads=2 
    -cpu host 
    -drive file=ubuntu-vm.qcow2,format=qcow2,if=virtio 
    -netdev user,id=net0,hostfwd=tcp::2222-:22 
    -device virtio-net-pci,netdev=net0 
    -nographic 
    -serial mon:stdio

# VM with multiple drives and a bridged network
$ qemu-system-x86_64 
    -enable-kvm 
    -m 8192 
    -smp 4 
    -cpu host 
    -drive file=system.qcow2,format=qcow2,if=virtio 
    -drive file=data.qcow2,format=qcow2,if=virtio 
    -netdev bridge,id=br0,br=virbr0 
    -device virtio-net-pci,netdev=br0,mac=52:54:00:12:34:56 
    -vnc :0

Key parameters explained: -enable-kvm activates KVM hardware acceleration. -m sets RAM in megabytes. -smp configures virtual CPU topology. -cpu host passes through the host CPU model for maximum compatibility and performance. The -drive option with if=virtio uses the paravirtualized virtio block driver. The hostfwd option in user-mode networking forwards host port 2222 to guest port 22 for SSH access.

Virtio Drivers and Performance Optimization

Virtio is a standardized framework for paravirtualized I/O devices in virtual machines. Using virtio drivers significantly improves disk and network performance compared to emulated legacy devices because they avoid the overhead of emulating real hardware.

# Inside the guest VM, verify virtio devices
$ lspci | grep -i virtio
00:04.0 SCSI storage controller: Red Hat, Inc. Virtio block device
00:05.0 Ethernet controller: Red Hat, Inc. Virtio network device

# Check virtio kernel modules in the guest
$ lsmod | grep virtio
virtio_blk             20480  2
virtio_net             57344  0
virtio_pci             28672  0
virtio_ring            40960  3 virtio_blk,virtio_net,virtio_pci

# Enable virtio-balloon for dynamic memory management
$ qemu-system-x86_64 
    -enable-kvm 
    -m 4096 
    -device virtio-balloon-pci,id=balloon0 
    -drive file=vm.qcow2,format=qcow2,if=virtio 
    ...

# Monitor VM from QEMU monitor (press Ctrl+A, C in -nographic mode)
(qemu) info status
VM status: running
(qemu) info block
virtio0: ubuntu-vm.qcow2 (qcow2)
(qemu) info network
virtio-net-pci: net0

The virtio-balloon device allows the host to reclaim unused memory from the guest dynamically. The QEMU monitor provides a control interface for inspecting and managing the running VM, including taking snapshots, hot-plugging devices, and monitoring resource usage.

Key Takeaways

  • QEMU provides device emulation while KVM provides hardware-accelerated CPU and memory virtualization through the Linux kernel, together delivering near-native VM performance.
  • The qcow2 disk format supports thin provisioning, snapshots, and overlay images, making it the preferred format for QEMU/KVM virtual machines.
  • Virtual machine hardware is configured through qemu-system-x86_64 command-line parameters including CPU topology (-smp), memory (-m), disk drives (-drive), and network devices (-netdev/-device).
  • Virtio paravirtualized drivers for block and network devices provide significantly better performance than emulated legacy hardware by reducing virtualization overhead.
  • User-mode networking with port forwarding provides simple connectivity for development, while bridged networking integrates VMs into the physical network for production use.

What’s Next

In Lesson 56, you will learn about Libvirt and virsh Management, which provides a higher-level abstraction over QEMU/KVM with XML-based domain definitions, storage pool management, and virtual network configuration.

繁體中文

概述

  • 學習目標:了解如何使用 QEMU 搭配 KVM 硬體加速來建立和管理虛擬機器,包括磁碟映像格式、CPU 和記憶體分配,以及 Ubuntu 上的 virtio 驅動程式。
  • 先決條件:虛擬化概念(第 54 課),BIOS/UEFI 中已啟用硬體虛擬化
  • 預計閱讀時間:20 分鐘

簡介

QEMU(Quick Emulator)是一個多功能的開源機器模擬器和虛擬化工具。當與 KVM(基於核心的虛擬機器)結合時,它可以在 Linux 主機上為虛擬機器提供接近原生的效能。QEMU 負責裝置模擬,而 KVM 透過 Linux 核心提供硬體加速的 CPU 和記憶體虛擬化。

這種組合是大多數 Linux 虛擬化堆疊的基礎,包括主要雲端供應商使用的堆疊。了解 QEMU/KVM 可以直接控制虛擬機器的建立和配置,並為使用 libvirt 等更高階管理工具提供所需知識。

在本課程中,您將在 Ubuntu 上安裝 QEMU 和 KVM、學習建立和管理各種格式的磁碟映像、使用自訂硬體配置啟動虛擬機器,以及使用 virtio 半虛擬化驅動程式最佳化效能。

在 Ubuntu 上安裝 QEMU 和 KVM

在建立虛擬機器之前,您需要安裝 QEMU 和 KVM 套件並驗證您的系統是否支援硬體虛擬化。

# 安裝 QEMU、KVM 和支援工具
$ sudo apt update
$ sudo apt install -y qemu-system-x86 qemu-utils cpu-checker bridge-utils

# 驗證 KVM 支援
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

# 將使用者加入 kvm 群組
$ sudo usermod -aG kvm $USER

磁碟映像格式和管理

虛擬機器需要虛擬磁碟映像來儲存其作業系統和資料。QEMU 支援多種磁碟映像格式,每種格式具有不同的特性。

qcow2 格式

qcow2 是 QEMU/KVM 最常用的格式。它支援精簡佈建(檔案隨資料寫入而增長)、快照、壓縮和加密。

Raw 格式

Raw 映像是虛擬磁碟的簡單位元組表示。提供最佳 I/O 效能,但會立即佔用完整的分配大小。

# 建立 20GB qcow2 磁碟映像
$ qemu-img create -f qcow2 ubuntu-vm.qcow2 20G

# 查看映像資訊
$ qemu-img info ubuntu-vm.qcow2
image: ubuntu-vm.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 196 KiB

# 格式之間轉換
$ qemu-img convert -f qcow2 -O raw ubuntu-vm.qcow2 ubuntu-vm.raw

# 建立基於快照的覆蓋映像
$ qemu-img create -f qcow2 -b ubuntu-vm.qcow2 -F qcow2 overlay.qcow2

啟動虛擬機器

qemu-system-x86_64 命令啟動虛擬機器。您可以透過命令列參數配置 VM 的硬體。

# 使用 KVM 加速的基本 VM 啟動
$ qemu-system-x86_64 
    -enable-kvm 
    -m 2048 
    -smp 2 
    -cpu host 
    -hda ubuntu-vm.qcow2 
    -cdrom ubuntu-22.04-live-server-amd64.iso 
    -boot d 
    -vnc :1

# 帶有 virtio 裝置和連接埠轉發的無頭 VM
$ qemu-system-x86_64 
    -enable-kvm 
    -m 4096 
    -smp cores=2,threads=2 
    -cpu host 
    -drive file=ubuntu-vm.qcow2,format=qcow2,if=virtio 
    -netdev user,id=net0,hostfwd=tcp::2222-:22 
    -device virtio-net-pci,netdev=net0 
    -nographic

關鍵參數:-enable-kvm 啟動 KVM 硬體加速。-m 設定 RAM(MB)。-smp 配置虛擬 CPU 拓撲。-cpu host 傳遞主機 CPU 模型。if=virtio 使用半虛擬化的 virtio 區塊驅動程式。

Virtio 驅動程式和效能最佳化

Virtio 是虛擬機器中半虛擬化 I/O 裝置的標準化框架。使用 virtio 驅動程式可以顯著改善磁碟和網路效能。

# 在客戶 VM 內驗證 virtio 裝置
$ lspci | grep -i virtio
00:04.0 SCSI storage controller: Red Hat, Inc. Virtio block device
00:05.0 Ethernet controller: Red Hat, Inc. Virtio network device

# 檢查客戶中的 virtio 核心模組
$ lsmod | grep virtio
virtio_blk             20480  2
virtio_net             57344  0

重點摘要

  • QEMU 提供裝置模擬,KVM 透過 Linux 核心提供硬體加速的 CPU 和記憶體虛擬化,共同實現接近原生的 VM 效能。
  • qcow2 磁碟格式支援精簡佈建、快照和覆蓋映像,是 QEMU/KVM 虛擬機器的首選格式。
  • 虛擬機器硬體透過 qemu-system-x86_64 命令列參數配置,包括 CPU 拓撲、記憶體、磁碟驅動和網路裝置。
  • Virtio 半虛擬化驅動程式透過減少虛擬化開銷,為區塊和網路裝置提供比模擬傳統硬體顯著更好的效能。
  • 使用者模式網路的連接埠轉發為開發提供簡單連線,橋接網路則將 VM 整合到實體網路中用於生產環境。

下一步

在第 56 課中,您將學習 Libvirt 和 virsh 管理,它提供了比 QEMU/KVM 更高層級的抽象,包括基於 XML 的網域定義、儲存池管理和虛擬網路配置。

日本語

概要

  • 学習内容:KVM ハードウェアアクセラレーションを使用した QEMU での仮想マシンの作成と管理、ディスクイメージフォーマット、CPU とメモリの割り当て、Ubuntu での virtio ドライバー。
  • 前提条件:仮想化の概念(レッスン54)、BIOS/UEFI でハードウェア仮想化が有効
  • 推定読了時間:20分

はじめに

QEMU(Quick Emulator)は、多機能なオープンソースのマシンエミュレータおよびバーチャライザです。KVM(カーネルベースの仮想マシン)と組み合わせることで、Linux ホスト上の仮想マシンにほぼネイティブのパフォーマンスを提供します。QEMU はデバイスエミュレーションを処理し、KVM は Linux カーネルを通じてハードウェアアクセラレーションされた CPU とメモリの仮想化を提供します。

この組み合わせは、主要なクラウドプロバイダーが使用するものを含む、ほとんどの Linux 仮想化スタックの基盤です。QEMU/KVM を理解することで、仮想マシンの作成と構成を直接制御でき、libvirt などのより高レベルの管理ツールを使用するための知識が得られます。

このレッスンでは、Ubuntu に QEMU と KVM をインストールし、さまざまなフォーマットのディスクイメージを作成・管理し、カスタムハードウェア構成で仮想マシンを起動し、virtio 準仮想化ドライバーでパフォーマンスを最適化します。

Ubuntu への QEMU と KVM のインストール

仮想マシンを作成する前に、QEMU と KVM パッケージをインストールし、システムがハードウェア仮想化をサポートしていることを確認する必要があります。

# QEMU、KVM、サポートツールをインストール
$ sudo apt update
$ sudo apt install -y qemu-system-x86 qemu-utils cpu-checker bridge-utils

# KVM サポートを確認
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used

# ユーザーを kvm グループに追加
$ sudo usermod -aG kvm $USER

ディスクイメージフォーマットと管理

qcow2 フォーマット

qcow2 は QEMU/KVM で最も一般的に使用されるフォーマットです。シンプロビジョニング、スナップショット、圧縮、暗号化をサポートします。

Raw フォーマット

Raw イメージは仮想ディスクの単純なバイト表現です。最高の I/O パフォーマンスを提供しますが、割り当てサイズ全体を即座に消費します。

# 20GB の qcow2 ディスクイメージを作成
$ qemu-img create -f qcow2 ubuntu-vm.qcow2 20G

# イメージ情報を表示
$ qemu-img info ubuntu-vm.qcow2
image: ubuntu-vm.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 196 KiB

# フォーマット間の変換
$ qemu-img convert -f qcow2 -O raw ubuntu-vm.qcow2 ubuntu-vm.raw

# スナップショットベースのオーバーレイイメージを作成
$ qemu-img create -f qcow2 -b ubuntu-vm.qcow2 -F qcow2 overlay.qcow2

仮想マシンの起動

qemu-system-x86_64 コマンドで仮想マシンを起動します。コマンドラインパラメータで VM のハードウェアを構成します。

# KVM アクセラレーションによる基本的な VM 起動
$ qemu-system-x86_64 
    -enable-kvm 
    -m 2048 
    -smp 2 
    -cpu host 
    -hda ubuntu-vm.qcow2 
    -cdrom ubuntu-22.04-live-server-amd64.iso 
    -boot d 
    -vnc :1

# virtio デバイスとポートフォワーディングによるヘッドレス VM
$ qemu-system-x86_64 
    -enable-kvm 
    -m 4096 
    -drive file=ubuntu-vm.qcow2,format=qcow2,if=virtio 
    -netdev user,id=net0,hostfwd=tcp::2222-:22 
    -device virtio-net-pci,netdev=net0 
    -nographic

主要パラメータ:-enable-kvm は KVM ハードウェアアクセラレーションを有効にします。-m は RAM(MB)を設定。-smp は仮想 CPU トポロジーを構成。-cpu host はホスト CPU モデルをパススルー。if=virtio は準仮想化 virtio ブロックドライバーを使用します。

Virtio ドライバーとパフォーマンス最適化

Virtio は仮想マシンにおける準仮想化 I/O デバイスの標準化フレームワークです。virtio ドライバーを使用すると、実際のハードウェアのエミュレーションオーバーヘッドを回避できるため、ディスクとネットワークのパフォーマンスが大幅に向上します。

# ゲスト VM 内で virtio デバイスを確認
$ lspci | grep -i virtio
00:04.0 SCSI storage controller: Red Hat, Inc. Virtio block device
00:05.0 Ethernet controller: Red Hat, Inc. Virtio network device

# ゲストの virtio カーネルモジュールを確認
$ lsmod | grep virtio
virtio_blk             20480  2
virtio_net             57344  0

重要ポイント

  • QEMU はデバイスエミュレーションを提供し、KVM は Linux カーネルを通じてハードウェアアクセラレーションされた CPU とメモリの仮想化を提供し、ほぼネイティブの VM パフォーマンスを実現します。
  • qcow2 ディスクフォーマットはシンプロビジョニング、スナップショット、オーバーレイイメージをサポートし、QEMU/KVM 仮想マシンの推奨フォーマットです。
  • 仮想マシンのハードウェアは qemu-system-x86_64 のコマンドラインパラメータで構成されます(CPU トポロジー、メモリ、ディスクドライブ、ネットワークデバイスなど)。
  • Virtio 準仮想化ドライバーは仮想化オーバーヘッドを削減し、エミュレートされたレガシーハードウェアよりも大幅に優れたパフォーマンスを提供します。
  • ユーザーモードネットワーキングのポートフォワーディングは開発用の簡単な接続を提供し、ブリッジネットワーキングは本番用に VM を物理ネットワークに統合します。

次のステップ

レッスン56では、Libvirt と virsh 管理について学びます。QEMU/KVM のより高レベルの抽象化を提供し、XML ベースのドメイン定義、ストレージプール管理、仮想ネットワーク構成について学びます。

You Missed