351.3 QEMU (weight: 4)

Tech Tutorial: 351.3 QEMU (weight: 4) #

Exam Objective: #

The main focus of this tutorial is to understand and utilize QEMU, a powerful hardware virtualization and machine emulation software. By the end of this tutorial, you should be able to set up, configure, and manage virtual machines using QEMU.

Key Knowledge Areas: #

  • Understanding QEMU architecture and modes (system emulation, user-level emulation).
  • Installation of QEMU.
  • Basic QEMU commands and usage.
  • Networking in QEMU.
  • Managing virtual disks and devices.

Utilities: #

  • qemu-img
  • qemu-system-x86_64
  • qemu-nbd

Introduction #

QEMU (Quick EMUlator) is a versatile emulator and virtualizer that allows users to run programs and operating systems designed for different architectures on their machines. It can operate in two modes: as a system emulator or a user-level emulator, providing flexibility depending on the user’s requirements.

Step-by-Step Guide #

Installation #

QEMU can be installed from the package repositories of most Linux distributions. Here is how you can install it on Ubuntu:

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

Basic QEMU Commands and Usage #

1. Creating a Virtual Disk Image #

To create a new virtual disk image, qemu-img is used. Here’s how to create a 10 GB qcow2 image:

qemu-img create -f qcow2 ubuntu-disk.img 10G

2. Starting a Virtual Machine #

To start a virtual machine using qemu-system-x86_64, you need to specify the disk image and installation media:

qemu-system-x86_64 -hda ubuntu-disk.img -cdrom ubuntu-20.04-desktop-amd64.iso -m 2048 -boot d

This command starts a VM with the following specifications:

  • hda ubuntu-disk.img: Uses ubuntu-disk.img as the hard drive.
  • cdrom ubuntu-20.04-desktop-amd64.iso: Boots from the provided Ubuntu ISO.
  • m 2048: Allocates 2048 MB of memory.
  • boot d: Boots from the CD-ROM drive.

Networking in QEMU #

Networking can be setup using different modes in QEMU. A simple user-network setup is as follows:

qemu-system-x86_64 -hda ubuntu-disk.img -net user,hostfwd=tcp::2222-:22 -net nic

This command sets up:

  • A user-mode network stack.
  • Port forwarding from host’s port 2222 to the virtual machine’s port 22 (SSH).

Managing Virtual Disks and Devices #

1. Modifying Disk Image Size #

To resize a disk image:

qemu-img resize ubuntu-disk.img +5G

This command increases the size of ubuntu-disk.img by 5 GB.

2. Converting Disk Image Formats #

To convert an image file from raw to qcow2 format:

qemu-img convert -f raw -O qcow2 original.img converted.qcow2

Detailed Code Examples #

Using qemu-nbd to Access Disk Images #

To access and modify the contents of a QEMU disk image from the host system, you can use the network block device (NBD) feature:

sudo modprobe nbd max_part=8
sudo qemu-nbd -c /dev/nbd0 ubuntu-disk.img
sudo mount /dev/nbd0p1 /mnt
# Now you can access the filesystem to perform operations
sudo umount /mnt
sudo qemu-nbd -d /dev/nbd0

Conclusion #

This tutorial provided an overview and detailed instructions on how to use QEMU for system emulation, including the setup of virtual machines, disk management, and networking. With these skills, you can leverage QEMU to test software across different configurations and operating systems, or even build and test network applications in a controlled environment. Remember, QEMU’s capabilities are vast, and this tutorial just scratches the surface. Experimenting with its options and features will give you a deeper understanding and more advanced usage scenarios.