QEMU

From Gentoo Wiki
(Redirected from KVM)
Jump to:navigation Jump to:search
This page contains changes which are not marked for translation.


QEMU (Quick EMUlator) is a generic, open-source hardware emulator and virtualization suite.

Introduction

QEMU is a Type-2 hypervisor, which runs within user namespace on a host platform and performs virtual hardware emulation. Inside a virtual machine, QEMU can emulate multiple operating systems; it can also emulate embedded systems.

QEMU supports more than 32 CPU architectures. It emulates nearly all the opcodes of these CPUs, and can execute multiple virtual CPUs in parallel.

QEMU can be paired with KVM to run VMs at near-native speed. This is accomplished by using hardware extensions such as Intel VT-x or AMD-V. It can then emulate user-level processes, which allow applications, compiled for one architecture, to run on a different one.

When used in conjunction with an accelerator plugin, QEMU becomes a Type-1 hypervisor, which runs in kernel namespace. This allows a user namespace program access to the hardware virtualization features of various processors. Such an accelerator can be KVM (Kernel-based Virtual Machine) or Xen.

If no accelerator is used, QEMU will run entirely in user namespace, using its built-in binary translator, TCG (Tiny Code Generator). Using QEMU without an accelerator is relatively inefficient and slow. The table at the end of this section lists available accelerators.

Note
This article typically uses KVM as the accelerator of choice, due to its GPL licensing and availability. Without KVM, nearly all commands described here, will still work (unless KVM-specific).

QEMU has different operating modes. System mode emulates a full system, including processors and peripherals, which allows running different operating systems and configure hardware configurations. User mode emulates a specific CPU architecture, which allows running Linux binaries that have been compiled for a different instruction set architecture.

QEMU virtual machines (VMs) can interface with many types of physical host hardware, including CD-ROM drives, USB devices, audio interfaces, hard disks and network cards.

By default, QEMU defaults to using the qcow2 virtual disk image format. This format only uses as much host disk space as the guest OS grows to use. Using the snapshot method, the guest OS can revert back to its desired state in time.

QEMU can save and restore the state of VMs of all its running programs.

QEMU does not depend on graphical output methods on the host system. Instead, it makes use of an integrated VNC server to access the screen of the guest OS.

A number of plugins are available for QEMU, including several accelerator plug-ins:

Accelerator Virtualization type Description Gentoo package name
tcg full/software emulation QEMU's own Tiny Code Generator. This is the default. More frequently denoted as qemu and not qemu/tcg so often. app-emulation/qemu
hvf paravirtualization Apple's Hypervisor framework based on Intel VT.
whpx[1] hybrid Microsoft's Windows Hypervisor Platform based on Intel VT or AMD-V.
kvm paravirtualization Linux Type-1 Hypervisor. This is the common choice for hosts using amd64, arm64, or mips[2] . Supports Microsoft Windows. app-emulation/qemu
haxm[3] paravirtualization Intel VT, by Intel Corporation.

The following pages provide detailed instructions related to QEMU configuration and options:

  • Virtiofs — a shared file system that lets virtual machines access a directory tree on the host

Installation

BIOS and UEFI firmware

In order to utilize KVM, either Intel's Vt-x (vmx) or AMD's AMD-V (svm) must be supported by the processor. These technologies permit multiple operating systems to concurrently execute operations on processors.

To inspect hardware for virtualization support, run:

user $grep --color --extended-regexp "vmx|svm" "/proc/cpuinfo"
Important
For a period, manufacturers were shipping with virtualization turned off by default in the system's firmware. Toggling this feature in the firmware may require full removal of power from the system to take effect.

If KVM support is available, there should be a kvm device at /dev/kvm. This will take effect after the system has booted to a KVM-enabled kernel.

Kernel

Described below are the basic requirements for KVM kernel configuration for the host OS. A more complete and up-to-date list can be found at the KVM Tuning Kernel page.

Note
Different guest (virtualized) OS may require additional kernel options. These are covered in the corresponding Usage section.
KERNEL
General setup --->
 Timers subsystem --->
 [*] High Resolution Timer Support Search for <code>CONFIG_HIGH_RES_TIMERS</code> to find this item.
Note
This includes support for ARM64 processors.

Physical CPU processor support - Host

If KVM support is not available, insert CONFIG_KVM=y into the /usr/src/linux/.config and rebuild/reinstall the kernel (and its initramfs image). Come back here after the host is rebooted.

KERNEL
[*] Virtualization Search for <code>CONFIG_CONFIG_VIRTUALIZATION</code> to find this item. --->
 <*> Kernel-based Virtual Machine (KVM) support Search for <code>CONFIG_CONFIG_KVM</code> to find this item.
Note
This includes support for ARM64 processors.

Processor support

KERNEL
[*] Virtualization Search for <code>CONFIG_CONFIG_VIRTUALIZATION</code> to find this item. --->
 <M> KVM for Intel processors support Search for <code>CONFIG_CONFIG_KVM_INTEL</code> to find this item.
KERNEL
[*] Virtualization Search for <code>CONFIG_CONFIG_VIRTUALIZATION</code> to find this item. --->
 <M> KVM for AMD processors support Search for <code>CONFIG_CONFIG_KVM_AMD</code> to find this item.
Warning
If both KVM support for Intel processors and KVM support for AMD processors are set to be built into the kernel (*), an error message will be returned by kprint at early boot. Since the system can only have one processor type: Intel or AMD. Enabling one or both options as modules (M) will solve this issue.

Handling kernel config at CLI

To set the various kernel configuration settings from the command lines, the linux/scripts/kconfig/merge_config.sh shall be used here:

Mandatory kernel configuration options to set:

FILE /usr/src/kernel-kconfig-qemu-host.config
CONFIG_VIRTUALIZATION=y
CONFIG_KVM=y
CONFIG_KVM_INTEL=y
CONFIG_KVM_AMD=y
root #cd "/usr/src/linux"
root #"./scripts/kconfig/merge_config.sh" ".config" "/usr/src/kernel-kconfig-qemu-host.config"

utility

Useful kernel configuration options to use:

FILE /usr/src/kernel-kconfig-qemu-host-optional.config
CONFIG_VHOST_NET=y
CONFIG_HIGH_RES_TIMERS=y
CONFIG_HPET=y
CONFIG_COMPACTION=y
CONFIG_MIGRATION=y
CONFIG_KSM=y
CONFIG_SYSFS=y
CONFIG_PROC_FS=y
CONFIG_TRANSPARENT_HUGEPAGE=y
CONFIG_CGROUPS=y
CONFIG_KVM_HYPERV=y
root #"./scripts/kconfig/merge_config.sh" ".config" "/usr/src/kernel-kconfig-qemu-host-optional.config"
Important
Recent Windows guests (at least Windows 10 22H2 and up) are required to set CONFIG_KVM_HYPERV (as per the optional configuration above). If this is not selected, VMs will fail to provision (or boot) with errors like: Failed to set MSR and Assertion `ret == cpu->kvm_msr_buf->nmsrs' failed.
Important
Kernel Samepage Merging (KSM) requires, in addition to the kernel's KSM module, a daemon that does de-duplication of RAM, sys-process/uksmd . As of 2026年08月19日, this package is not yet keyworded for stable.

Networking

Accelerated networking, required for vhost-net USE flag (recommended):

KERNEL
Device Drivers --->
 [*] VHOST drivers --->
 <*> Host kernel accelerator for virtio net Search for <code>CONFIG_CONFIG_VHOST_NET</code> to find this item.
KERNEL
Device Drivers --->
 [*] Network device support Search for <code>CONFIG_CONFIG_NETDEVICES</code> to find this item. --->
 [*] Network core driver support Search for <code>CONFIG_CONFIG_NET_CORE</code> to find this item.
 <*> Universal TUN/TAP device driver support Search for <code>CONFIG_CONFIG_TUN</code> to find this item.

802.1d Ethernet bridging

Needed for 802.1d Ethernet bridging:

KERNEL
[*] Networking support Search for <code>CONFIG_CONFIG_NET</code> to find this item. --->
 Networking options --->
 <*> The IPv6 protocol Search for <code>CONFIG_CONFIG_IPV6</code> to find this item.
 <*> 802.1d Ethernet Bridging Search for <code>CONFIG_CONFIG_BRIDGE</code> to find this item.

Intel VT-g (integrated graphics adapter virtualization)

 As of 2026年08月22日, the information in this section is probably outdated. You can help the Gentoo community by verifying and updating this section.

Mediated device passthrough for Intel GPUs (Broadwell to Comet Lake)[4]

KERNEL Intel VT-g (CONFIG_VFIO_MDEV, CONFIG_DRM_I915_GVT, CONFIG_DRM_I915_GVT_KVMGT)
Device Drivers --->
 <*> VFIO Non-Privileged userspace driver framework
 <*> Mediated device driver framework
 Graphics Support --->
 <*> Intel 8xx/9xx/G3x/G4x/HD Graphics
 [*] Enable Intel GVT-g graphics virtualization host support
 <*> Enable KVM host support Intel GVT-g graphics virtualization

USE flags

Some packages have a qemu USE flag, to enable QEMU support.

The USE flags for QEMU itself are:

USE flags for app-emulation/qemu QEMU + Kernel-based Virtual Machine userland tools

+aio Enables support for Linux's Async IO
+curl Support ISOs / -cdrom directives via HTTP or HTTPS.
+doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
+fdt Enables firmware device tree support
+filecaps Use Linux file capabilities to control privilege rather than set*id (this is orthogonal to USE=caps which uses capabilities at runtime e.g. libcap)
+gnutls Enable TLS support for the VNC console server. For 1.4 and newer this also enables WebSocket support. For 2.0 through 2.3 also enables disk quorum support.
+jpeg Enable jpeg image support for the VNC console server
+oss Add support for OSS (Open Sound System)
+pin-upstream-blobs Pin the versions of BIOS firmware to the version included in the upstream release. This is needed to sanely support migration/suspend/resume/snapshotting/etc... of instances. When the blobs are different, random corruption/bugs/crashes/etc... may be observed.
+png Enable png image support for the VNC console server
+seccomp Enable seccomp (secure computing mode) to perform system call filtering at runtime to increase security of programs
+slirp Enable TCP/IP in hypervisor via net-libs/libslirp
+vhost-net Enable accelerated networking using vhost-net, see https://www.linux-kvm.org/page/VhostNet
+vnc Enable VNC (remote desktop viewer) support
X Add support for X11
accessibility Adds support for braille displays using brltty
alsa Enable alsa output for sound emulation
bpf Enable eBPF support for RSS implementation.
bzip2 Enable bzip2 compression support
capstone Enable disassembly support with dev-libs/capstone
debug Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces
fuse Enables FUSE block device export
glusterfs Enables GlusterFS cluster fileystem via sys-cluster/glusterfs
gtk Add support for x11-libs/gtk+ (The GIMP Toolkit)
infiniband Enable Infiniband RDMA transport support
io-uring Enable the use of io_uring for efficient asynchronous IO and system requests
iscsi Enable direct iSCSI support via net-libs/libiscsi instead of indirectly via the Linux block layer that sys-block/open-iscsi does.
jack Add support for the JACK Audio Connection Kit
jemalloc Use dev-libs/jemalloc for memory management
keyutils Support Linux keyrings via sys-apps/keyutils
lzo Enable support for lzo compression
multipath Enable multipath persistent reservation passthrough via sys-fs/multipath-tools.
ncurses Enable the ncurses-based console
nfs Enable NFS support
nls Add Native Language Support (using gettext - GNU locale utilities)
numa Enable NUMA support
opengl Add support for OpenGL (3D graphics)
pam Add support for PAM (Pluggable Authentication Modules) - DANGEROUS to arbitrarily flip
passt Enable TCP/IP in hypervisor via net-misc/passt
pipewire Enable pipewire output for sound emulation
plugins Enable qemu plugin API via shared library loading.
pulseaudio Enable pulseaudio output for sound emulation
python Add optional support/bindings for the Python language
rbd Enable rados block device backend support, see https://docs.ceph.com/en/mimic/rbd/qemu-rbd/
sasl Add support for the Simple Authentication and Security Layer
sdl Enable the SDL-based console
sdl-image SDL Image support for icons
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
smartcard Enable smartcard support
snappy Enable support for Snappy compression (as implemented in app-arch/snappy)
spice Enable Spice protocol support via app-emulation/spice
ssh Enable SSH based block device support via net-libs/libssh2
static-user Build the User targets as static binaries
systemtap Enable SystemTap/DTrace tracing
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)
udev Enable virtual/udev integration (device discovery, power and storage device support, etc)
usb Enable USB passthrough via dev-libs/libusb
usbredir Use sys-apps/usbredir to redirect USB devices to another machine over TCP
valgrind Enable annotations for accuracy. May slow down runtime slightly. Safe to use even if not currently using dev-debug/valgrind
vde Enable VDE-based networking
verify-sig Verify upstream signatures on distfiles
virgl Enable experimental Virgil 3d (virtual software GPU)
virtfs Enable VirtFS via virtio-9p-pci / fsdev. See https://wiki.qemu.org/Documentation/9psetup
vte Enable terminal support (x11-libs/vte) in the GTK+ interface
wayland Enable dev-libs/wayland backend
xattr Add support for getting and setting POSIX extended attributes, through sys-apps/attr. Requisite for the virtfs backend.
xdp Enable support for XDP through net-libs/xdp-tools
xen Enables support for Xen backends
zstd Enable support for ZSTD compression
Data provided by the Gentoo Package Database · Last update: 2026年08月16日 13:31 More information about USE flags
Note
More than one of gtk , ncurses , sdl , and spice can be enabled for graphical output. If graphics are desired, it is generally recommended to enable more than one such flag.
Note
If virt-manager is going to be used, be sure to enable the usbredir and spice USE flags on app-emulation/qemu for correct operation.

USE_EXPAND

Additional ebuild configuration is provided by the USE_EXPAND variables QEMU_USER_TARGETS and QEMU_SOFTMMU_TARGETS. The USE flags for app-emulation/qemu (as shown by e.g. equery from app-portage/gentoolkit )include all the available targets. Most are very obscure and may be ignored; leaving these variables at their default values will disable almost everything, which is probably fine for most users.

For each target specified, a qemu executable will be built. A softmmu target is the standard QEMU use-case of emulating an entire system, like VirtualBox or VMware, but with optional support for emulating CPU hardware along with peripherals. user targets execute user-mode code only; the (somewhat ambitious) purpose of these targets is to "magically" allow importing user namespace Linux ELF binaries from a different architecture into the native system (like multilib, without the need for a software stack or a CPU capable of running it).

In order to enable QEMU_USER_TARGETS and QEMU_SOFTMMU_TARGETS, add the following to /etc/portage/package.use :

FILE /etc/portage/package.use/qemu
app-emulation/qemu QEMU_SOFTMMU_TARGETS: arm x86_64 sparc QEMU_USER_TARGETS: x86_64

Emerge

After reviewing and adding any desired USE flags, emerge app-emulation/qemu :

root #emerge --ask app-emulation/qemu

Additional software

To connect to the SPICE server of QEMU, a GUI client like net-misc/spice-gtk is required.

Configuration

The following sub-articles provide detailed instructions on QEMU configurations and options:

Environment variables

name description
G_MESSAGES_DEBUG Enables debug messages for components using GLib's logging system. If set to all, logs are output to stderr. Other options are QEMU, libvirt, gtk, and pulse. Use commas to separate options.
LISTEN_FDS Number of file descriptors passed. Used with QEMU activated by systemd.
LISTEN_PID PID of the receiving process (usually set to the current PID). Used with QEMU activated by systemd.
QEMU_AUDIO_DRV Specifies the audio backend driver to use. Options are alsa, pa, oss, and none.
XDG_RUNTIME_DIR Specifies where user-specific runtime files and sockets should be stored. On Gentoo, typically set to /run/user/$(id -u)/, where the output of id -u is the UID of the user. Used by a variety of software, including Wayland, PulseAudio, and virt-manager. Refer to XDG/Base Directories for further information.

Files

QEMU uses the following files and directories for configuration:

  • /etc/libvirt/qemu.conf - QEMU configuration file.
  • /etc/libvirt/qemu-lockd.conf - QEMU lock files
  • /etc/libvirt/qemu-sanlock.conf - QEMU SAN lock
  • /etc/libvirt/qemu/<domain-name>.xml - Domain XML setting for a virtual machine or container.
  • /etc/libvirt/qemu/autostart/<domain-name>.xml - Autostart this domain (virtual machine or container).
  • /etc/libvirt/qemu/networks/<network-name>.xml - Network XML setting file for a network connection
  • /etc/libvirt/qemu/networks/autostart/<network-name>.xml - Autostart this network connection.
  • /var/lib/libvirt/qemu/channel/target/<domain-name>/<socket-file> - UNIX socket file for Libvertd daemon API
  • /var/cache/libvirt/qemu/capabilities/<hash-value>.xml - Host OS capabilities in XML format
  • /var/lib/libvirt/qemu/checkpoint/
  • /var/lib/libvirt/qemu/<domain-9-XXXX>/ - holds UNIX sockets and AES keys for this domain.
  • /var/lib/libvirt/qemu/dump/
  • /var/lib/libvirt/qemu/nvram/
  • /var/lib/libvirt/qemu/ram/
  • /var/lib/libvirt/qemu/save/ - holding directory of hibernation images
  • /var/lib/libvirt/qemu/snapshot/ - holding directory of snapshots
  • /var/run/libvirt/qemu - various UNIX socket and PID files for the libvirtd daemon.

Usage

Todo:

  • This section needs a few basic examples, prior to the "Starting QEMU with a VNC server" subsection, of invoking a QEMU binary (including running it with a Live CD and no disk image).


QEMU can be used from the command line or via a GUI front-end. For information about available front-ends, refer to QEMU/Front-ends.

Permissions

In order to run a KVM-accelerated virtual machine without root privileges, one can add normal users to the kvm group:

root #gpasswd -a larry kvm

Disk image formats

QEMU supports the following disk image formats:

  • QEMU copy-on-write: .qcow2, .qed, .qcow, .cow
  • VirtualBox Virtual Disk Image: .vdi
  • CD/DVD (ISO-9660) images: .iso
  • Raw images, that guest OS can control: .img
  • VFAT-16
  • VMware Virtual Machine Disk: .vmdk
  • Virtual PC Virtual Hard Disk: .vhd
  • Parallels disk image (read-only): .hdd, .hds
  • Apple macOS Universal Disk Image Format (read-only): .dmg
  • Bochs (read-only)
  • Hyper-V Virtual Hard Disk: .vhdx
  • Linux cloop (read-only)
  • LUKS disk images

See qemu-img for more disk image information.

Creating a disk image

To create a 4 GiB raw disk image:

user $qemu-img create -f raw "/home/larry/qemu/my-systems-disk-image.img" 4G
Formatting 'my-systems-disk-image.img', fmt=raw size=4294967296
user $ls -lh
total 4
-rw-r--r-- 1 larry larry 4.0G Apr 12 11:23 my-systems-disk-image.img

To create a raw disk image with copy-on-write (COW) disabled:

user $qemu-img create -f raw "/home/larry/qemu/my-systems-disk-image.img" -o nocow=on 4G
Formatting 'my-systems-disk-image.img', fmt=raw size=4294967296 nocow=on
user $ls -lh
total 4
-rw-r--r-- 1 larry larry 4.0G Apr 12 11:23 my-systems-disk-image.img

The nocow option is also a file attribute, which can be determined via the command lsattr(1) [5] .

The following will create a qcow2 disk image (useful if the host filesystem does not support sparse files):

user $qemu-img create -f qcow2 "/home/larry/qemu/my-systems-disk-image.qcow2" 4G
Formatting 'my-systems-disk-image.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=4294967296 lazy_refcounts=off refcount_bits=16
user $ls -l
total 196K
-rw-r--r-- 1 larry larry 193K Apr 12 11:30 my-systems-disk-image.qcow2

Preparing a bootable disk image from scratch

A system can be copied onto a disk image without using a CD-ROM installation medium.

By default, QEMU uses BIOS firmware to boot the system.

The disk image can be prepared with an msdos disk label and a gap between the end of the 512 byte MBR (Master Boot Record) and the start of the first partition. The gap is needed for boot loaders like GRUB, which place boot code within this gap.

The following example uses the raw disk image created above.

A raw disk image can be prepared by attaching it as a loop device:

root #losetup --find --partscan --show "/home/larry/qemu/my-systems-disk-image.img"
/dev/loop0
  • The --find option finds the first unused loop device.
  • The --partscan option forces the Linux kernel to scan the partition table on the newly created loop device, where a default sector size of 512 bytes is assumed.
  • The --show option displays the name of the assigned loop device, when the --find option is used.

Attached loop devices can be listed with the following command:

root #losetup --list
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop0 0 0 0 0 /home/larry/qemu/my-systems-disk-image.img 0 512

The loop device can then be formatted like a normal disk.

To print the partition table, use parted(8) :

root #parted "/dev/loop0" "unit mib print"
Error: /dev/loop0: unrecognised disk label
Model: Loopback device (loopback)
Disk /dev/loop0: 4096MiB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
...

Next, create a new partition table with an msdos disk label:

Warning
Make absolutely sure to select the correct loop device, since this might overwrite an existing partition table, resulting in data loss if the overwritten partition table cannot be recovered.
root #parted "/dev/loop0" "mklabel msdos"
Information: You may need to update /etc/fstab.

The returned information can be ignored, since an entry in the configuration file /etc/fstab is not needed.

parted now indicates that the partition table is msdos:

root #parted "/dev/loop0" "unit mib print"
Model: Loopback device (loopback)
Disk /dev/loop0: 4096MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
...

Next, create an ext4 partition with an offset of 2 MiB:

root #parted "/dev/loop0" "mkpart primary ext4 2MiB -1"

The value of -1 represents the last sector of the partition.

To check that the first partition has been successfully created:

root #parted "/dev/loop0" "unit mib print"
Model: Loopback device (loopback)
Disk /dev/loop0: 4096MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
 
Number Start End Size Type File system Flags
 1 2.00MiB 4095MiB 4093MiB primary

This will also attach a new loop device at /dev/loop0p1:

root #ls -l "/dev/loop0"*
brw-rw---- 1 root disk 7, 0 Apr 12 12:33 /dev/loop0
brw-rw---- 1 root disk 259, 0 Apr 12 12:33 /dev/loop0p1

Set the boot flag:

root #parted "/dev/loop0" set 1 boot on

All partition flags can be found in the "Flags" column:

root #parted "/dev/loop0" "unit mib print"
Model: Loopback device (loopback)
Disk /dev/loop0: 4096MiB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
 
Number Start End Size Type File system Flags
 1 2.00MiB 4095MiB 4093MiB primary boot

Create the ext4 filesystem declared via parted earlier:

root #mkfs.ext4 "/dev/loop0p1"
mke2fs 1.47.2 (1-Jan-2025)
Discarding device blocks: done
Creating filesystem with 1047808 4k blocks and 262144 inodes
Filesystem UUID: 0e344af7-6f7b-4d27-8238-89d46a5920d6
Superblock backups stored on blocks:
 32768, 98304, 163840, 229376, 294912, 819200, 884736
 
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

Mount it at /mnt:

root #mount /dev/loop0p1 /mnt
root #df --human-readable --print-type "/mnt/"
Filesystem Type Size Used Avail Use% Mounted on
/dev/loop0p1 ext4 3.9G 24K 3.7G 1% /mnt

Create the /mnt/boot/grub directory, which will be used by GRUB later:

root #mkdir --parents --verbose "/mnt/boot/grub"
mkdir: created directory '/mnt/boot'
mkdir: created directory '/mnt/boot/grub'

Install GRUB on the loop device, instructing it to install its files to /mnt/boot/grub/:

root #grub-install --target="i386-pc" --boot-directory="/mnt/boot/" "/dev/loop0"
root #tree -F "/mnt/boot/grub/"
/mnt/boot/grub/
├── fonts/
│   └── unicode.pf2
├── grubenv
├── i386-pc/
│   ├── acpi.mod
│   ├── adler32.mod
│   ├── affs.mod
│   ├── afs.mod
│   ├── afsplitter.mod
│   ├── ahci.mod
│   ├── all_video.mod
│   ├── aout.mod
│   ├── archelp.mod
│   ├── ata.mod
[...]

Unmount the filesystem and detach the loop device:

root #umount "/mnt/"
root #losetup --detach "/dev/loop0"

If the loop device is still busy - for example, processes are still accessing /mnt/ - no error will be returned. This can be verified and solved with the following commands:

root #losetup --list
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop0 0 0 0 0 /home/larry/qemu/my-systems-disk-image.img 0 512
root #lsof | grep "/mnt"
sleep 31813 root cwd DIR 259,0 4096 131074 /mnt/boot/grub
root #kill -SIGTERM 31813

This is sufficient to boot into a GRUB boot prompt.

This setup can be used as the basis for a bootable system.

CPU selection

QEMU supports around 34 different CPU architectures. To list those available:

user $ls "/usr/bin/qemu-system-"*
/usr/bin/qemu-system-aarch64 /usr/bin/qemu-system-mips /usr/bin/qemu-system-rx
/usr/bin/qemu-system-alpha /usr/bin/qemu-system-mips64 /usr/bin/qemu-system-s390x
/usr/bin/qemu-system-arm /usr/bin/qemu-system-mips64el /usr/bin/qemu-system-sh4
/usr/bin/qemu-system-avr /usr/bin/qemu-system-mipsel /usr/bin/qemu-system-sh4eb
/usr/bin/qemu-system-cris /usr/bin/qemu-system-nios2 /usr/bin/qemu-system-sparc
/usr/bin/qemu-system-hppa /usr/bin/qemu-system-or1k /usr/bin/qemu-system-sparc64
/usr/bin/qemu-system-i386 /usr/bin/qemu-system-ppc /usr/bin/qemu-system-tricore
/usr/bin/qemu-system-loongarch64 /usr/bin/qemu-system-ppc64 /usr/bin/qemu-system-x86_64
/usr/bin/qemu-system-m68k /usr/bin/qemu-system-ppc64le /usr/bin/qemu-system-x86_64-microvm
/usr/bin/qemu-system-microblaze /usr/bin/qemu-system-riscv32 /usr/bin/qemu-system-xtensa
/usr/bin/qemu-system-microblazeel /usr/bin/qemu-system-riscv64 /usr/bin/qemu-system-xtensaeb

To get a list of CPUs for a specific architecture, use the -cpu help option with the binary for that architecture, e.g.:

user $qemu-system-x86_64 -cpu help
Available CPUs:
 486 (alias configured by machine type)
 486-v1 
 Broadwell (alias configured by machine type)
 Broadwell-IBRS (alias of Broadwell-v3)
 Broadwell-noTSX (alias of Broadwell-v2)
...

As noted in the introduction, QEMU CPUs can have additional support for accelerators. An accelerator can usually only accelerate the features available on the host CPU, so the selection of CPU affects performance.

To list available accelerators, pass the -accel help option to the relevant binary, e.g.:

user $qemu-system-x86_64 -accel help
Accelerators supported in QEMU binary:
tcg
mshv
kvm

Starting QEMU with a VNC server

Warning
By default, a QEMU VNC server starts without password protection and listens on the loop interface. On X, if the VM is started with -vnc :0, it will listen on port 5900 on all interfaces without password protection, as the value :0 represents the first display of the host machine, not a port number.

To start QEMU with a VNC server listening on a local UNIX socket:

user $qemu-system-x86_64 -vnc "unix:/run/user/$(id -u)/qemu-vnc.sock" -enable-kvm -cpu host -drive "file=/home/larry/qemu/my-systems-disk-image.img,format=raw" -m 2G
Important
The file permissions of /run/user/$(id -u)/qemu-vnc.sock must be set appropriately to protect the VNC server from unauthorized access.

A CD-ROM can be added by using the -cdrom option, e.g. -cdrom <image>, where <image> should be replaced with the name of an ISO image.

Connecting to a QEMU VNC server

Any VNC viewer can be used to connect to the VNC server, e.g. vncviewer, provided by net-misc/tigervnc :

user $vncviewer "/run/user/$(id -u)/qemu-vnc.sock"
TigerVNC viewer v1.15.0
Built on: 2025年05月13日 12:30
Copyright (C) 1999-2025 TigerVNC team and many others (see README.rst)
See https://www.tigervnc.org for information on TigerVNC.
 
Tue May 13 14:44:36 2025
 DecodeManager: Detected 4 CPU core(s)
 DecodeManager: Creating 4 decoder thread(s)
 CConn: Connected to socket /run/user/1000/qemu-vnc.sock
 CConnection: Server supports RFB protocol version 3.8
 CConnection: Using RFB protocol version 3.8
 CConnection: Choosing security type None(1)
 CConn: Using pixel format depth 24 (32bpp) little-endian rgb888
 CConn: SetDesktopSize failed: 3

This will open a separate window with the display output of the QEMU VM:

Qemu minimal vm with grub2.png

Troubleshooting

Refer to QEMU/troubleshooting.

Removal

Unmerge

root #emerge --ask --depclean --verbose app-emulation/qemu
Note
There may be image files left behind after the removal of the QEMU package.

See also

QEMU

libvirt and virt-manager

General

External resources

References