QEMU - Documentation

From IT-Arts.net


Return to Wiki Index


QEMU Basics

Architecture Emulation

QEMU can emulate different hardware architectures. For example, if you want to run a PowerPC-based system on an x86 host, you can use QEMU to emulate PowerPC hardware.

qemu-system-ppc -cdrom install.iso -m 1024

This command starts a virtual machine emulating a PowerPC architecture with 1024 MB of RAM and an installation ISO image mounted as a CD-ROM.

Virtualization vs. Emulation

There are two main modes of operation in QEMU:

1. **Emulation**: The full hardware emulation where QEMU emulates the CPU and other hardware devices. This is slower but more flexible. 2. **Virtualization**: When QEMU is combined with KVM (Kernel-based Virtual Machine), it can run virtual machines with near-native performance by taking advantage of hardware virtualization features.

To enable KVM-based virtualization, you can use the following command:

qemu-system-x86_64 -enable-kvm -m 2048 -cpu host -hda /path/to/disk_image

This enables KVM support, specifies 2 GB of memory, and runs a VM with a virtual disk image.

QEMU Command-Line Interface

Common Command-Line Options

QEMU uses a command-line interface with various options for controlling the behavior of virtual machines. Below are some of the commonly used command-line options:

System Emulator

qemu-system-<arch> [options]

Where `<arch>` can be replaced with the desired architecture (e.g., `x86_64`, `arm`, `mips`, etc.).

Some useful options include:

  • `-m` – Set memory size (e.g., `-m 2048` sets 2 GB of RAM).
  • `-hda` – Specify the hard disk image (e.g., `-hda /path/to/image`).
  • `-cdrom` – Mount a CD-ROM image (e.g., `-cdrom /path/to/iso`).
  • `-net` – Set network configuration (e.g., `-net nic -net user` for user-mode networking).
  • `-enable-kvm` – Enable hardware virtualization support (for KVM users).

Example: Running a QEMU system emulation with a 64-bit x86 system and 2 GB of RAM:

qemu-system-x86_64 -m 2048 -hda /path/to/disk.img -enable-kvm

User-mode Networking

QEMU provides user-mode networking for virtual machines. This type of networking is simple and does not require administrative privileges.

To enable user-mode networking, use the following command:

qemu-system-x86_64 -m 1024 -hda /path/to/disk.img -net nic -net user

This creates a virtual network interface inside the virtual machine, which can access the host's network.

Bridge Networking

Bridge networking allows virtual machines to connect directly to the host's network, making them behave like regular machines on the network.

qemu-system-x86_64 -m 1024 -hda /path/to/disk.img -net nic -net bridge,br=br0

This command creates a bridged network connection for the virtual machine, where `br0` is the name of the bridge on the host system.

Virtual Machine Disk Images

Creating Virtual Disks

QEMU supports various disk image formats, such as QCOW2, RAW, VMDK, and VDI. The most common format is QCOW2 because it supports features like snapshots and compression.

To create a new disk image, use the `qemu-img` tool:

qemu-img create -f qcow2 /path/to/disk.img 10G

This creates a 10 GB QCOW2 disk image. The `-f` option specifies the format (QCOW2 in this case).

Converting Disk Images

QEMU also allows converting between different disk image formats. To convert a QCOW2 disk to a raw format, use the following command:

qemu-img convert -f qcow2 -O raw /path/to/disk.qcow2 /path/to/disk.raw

The `-O` option specifies the output format (raw, in this case).

Using Disk Images with QEMU

When starting a virtual machine, specify the disk image with the `-hda` option:

qemu-system-x86_64 -m 2048 -hda /path/to/disk.img

QEMU will then boot the guest operating system from the specified disk image.

Snapshots in QEMU

Snapshots allow you to save the state of a virtual machine at a specific point in time and revert to that state later.

Creating a Snapshot

To create a snapshot of a running virtual machine, use the following QEMU command:

qemu-img snapshot -c snapshot_name /path/to/disk.img

This creates a snapshot of the virtual machine's current state, which can be useful for testing or recovery.

Reverting to a Snapshot

To revert a virtual machine to a specific snapshot, use:

qemu-img snapshot -a snapshot_name /path/to/disk.img

This restores the disk image to the state it was in when the snapshot was created.

Debugging and Tracing with QEMU

QEMU offers powerful debugging and tracing capabilities to help you troubleshoot and optimize your virtual machines.

Enabling QEMU Debugging

You can enable QEMU's built-in debugging facilities by passing the `-d` flag followed by the desired debug options.

Example for enabling CPU execution tracing:

qemu-system-x86_64 -m 2048 -hda /path/to/disk.img -d cpu

This command enables detailed logging of the CPU execution, which can be helpful for performance tuning or debugging.

QEMU Tracing with `-trace`

For more advanced tracing, QEMU provides the `-trace` option, which can capture trace events during virtual machine execution.

Example of enabling tracing for memory accesses:

qemu-system-x86_64 -m 2048 -hda /path/to/disk.img -trace memory

This records all memory access events, which can be analyzed for performance profiling or debugging.

QEMU Monitor Interface

The QEMU monitor provides an interactive interface for controlling virtual machines while they are running. It allows you to issue commands like pausing the VM, changing device configurations, and more.

Accessing the Monitor

To access the monitor, use the `-monitor` option when starting QEMU:

qemu-system-x86_64 -m 2048 -hda /path/to/disk.img -monitor telnet::4444,server,nowait

This starts a QEMU monitor server that listens on port 4444, which can be accessed via a Telnet client.

Common Monitor Commands

Some common commands available in the QEMU monitor include:

  • `info` – Show information about the current state of the VM, such as devices, CPUs, and memory.
  • `quit` – Exit the virtual machine.
  • `stop` – Pause the virtual machine.
  • `cont` – Resume a paused virtual machine.

For further reading and resources, consult the following links: