User:Egberts/Drafts/QEMU Configuration Usage section

From Gentoo Wiki
Jump to:navigation Jump to:search


This assumes the most popular 10-year old Intel-based CPU motherboard and accessories

Environment variables

  • G_MESSAGES_DEBUG - Error reporting
  • LISTEN_FDNAMES - Pass the socket name descriptor(s) of the QEMU Disk Block Device to the systemd-socketd socket activation daemon.
  • LISTEN_FDS - Pass the file descriptor(s) (fds) of the QEMU Disk Block Device to the systemd-socketd socket activation daemon.
  • LISTEN_PID - Pass the process ID (PID) of the QEMU Disk Block Device to the systemd-socketd socket activation daemon.
  • QEMU_STRACE - Enable <cmd>strace</cmd> (only on BSD)
  • WAYLAND_DISPLAY - Pass the DISPLAY value to virtio-based video driver for Wayland-based Windows system (For rutabaga video only).
  • XDG_RUNTIME_DIR - pass audio settings to the PulseAudio daemon/driver.


  • /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 libvirtd daemon.


This section covers the auto-starting of QEMU using different SysV init.


Using virt-manager:

See virt-manager for full usage.


QEMU can be used in two ways, with GUI (QEMU/QEMU front-ends) and through the command line (virsh).


user $virt-manager --help
usage: virt-manager [options]

  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -c URI, --connect URI
                        Connect to hypervisor at URI
  --debug               Print debug output to stdout (implies --no-fork)
  --no-fork             Don't fork into background on startup
                        Show 'New VM' wizard
  --show-domain-editor NAME|ID|UUID
                        Show domain details window
  --show-domain-performance NAME|ID|UUID
                        Show domain performance window
  --show-domain-console NAME|ID|UUID
                        Show domain graphical console window
  --show-domain-delete NAME|ID|UUID
                        Show domain delete window
  --show-host-summary   Show connection details window

Also accepts standard GTK arguments like --g-fatal-warnings

Command line

QEMU binaries are used to run the virtualized guest. QEMU supports System Emulation and User Mode Emulation. These are the commands that emulate the x86_64 architecture.

user $qemu-system-x86_64 [options] [disk_image]
user $qemu-x86_64 [options] program [arguments...]

The compilation of user targets need to be enabled by adding the targets to QEMU_USER_TARGETS. This can be done in /etc/portage/make.conf

Creation of a disk image

This is how you would create a raw disk image with with 40G size:

user $qemu-img create -f raw my-systems-disk-image.img 40G

This is how you would create a raw image with copy-on-write disabled (nocow). "nocow" is a file attribute. (check with lsattr)

user $qemu-img create -f raw my-systems-disk-image.img -o nocow=on 40G

This would create a qcow2 image (useful if your filesystem doesn't support sparse files):

user $qemu-img create -f qcow2 my-systems-disk-image.qcow2 40G

Preparation of a bootable disk image from scratch

If you don't use a cdrom installation medium you can prepare a disk image and copy a system onto it. By default qemu uses a "bios-firmware" to boot the system. The disk can be prepared with a msdos disklabel 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 that place boot-code in the gap. A raw disk image can be prepared by attaching it as a loop device:

user $losetup -fP /path/to/my-systems-disk-image.img
  • -f find the first unused loop device
  • -P scans for the partitions

List the loop devices with this command:

user $losetup -l

Then the loop device can be formatted like a normal disk.
Print the partition table:

user $parted /dev/loop000 print

You can create a msdos disklabel with:

user $parted /dev/loop000-number-of-the-device-whose-data-will-be-lost mklabel msdos

Create an ext4 partition:

user $parted /dev/loop000 mkpart primary ext4 1Mib 40GiB

Set the boot flag:

user $parted /dev/loop000 set 1 boot on

Create a filesystem:

user $mkfs.ext4 /dev/loop000

Mount it somewhere

user $mount /dev/loop000 /mnt/my-new-fs

Create a boot/grub folder for grub.

user $mkdir -p /mnt/my-new-fs/boot/grub

Install grub on the loop device and advice grub to install its files in boot/grub

user $grub-install --boot-directory=/mnt/my-new-fs/boot/grub /dev/loop000

Unmount the filesystem and detach the loop device

user $umount /mnt/my-new-fs
user $losetup -d /dev/loop000

If the loop device is busy it will not return an error. You can verify it with

user $losetup -l

This is enough to boot into a grub2 boot prompt. This is can be used as the basis for a bootable system.

CPU selection

QEMU has "accelerators" like kvm(Kernel Virtual Machine) or tcg (Tiny Code Generator) or Xen (wikip[1]). The accelerator can usually only "accelerate" the features that are available on the host cpu. So the selection of the cpu affects the performance. Get a list of cpus:

user $qemu-system-x86_64 -cpu help

Show the available accelerators:

user $qemu-system-x86_64 -accel help

Starting QEMU

This is how you start a virtual machine with the same feature set as the host cpu, a raw disk image and 2G of ram. By default a vnc server is started that runs with no password protection and listens on the loop interface. You can advise QEMU to listen on a local UNIX socket with the following command. Set the file permissions appropriately to protect the VNC server from unauthorized access. You can add a cdrom image as a installation and boot medium with "-cdrom filename.img"

user $qemu-system-x86_64 -vnc unix:/home/user/.qemu-vnc-socket -cpu host -drive file=/var/virt/rootfs-build-tc,format=raw -m 2G

CAUTION: If you start the server with -vnc :0 it listens on port 5900 (first display) on all interfaces with no password protection.

Command line

QEMU binaries are used to run the virtualized guest.

user $qemu-system-x86_64 [options] [disk_image]


In order to run a KVM accelerated virtual machine without logging as root, add normal users to the kvm group. Replace <username> in the example command below with the appropriate user(s):

root #gpasswd -a <username> kvm





root #emerge --ask --depclean --verbose app-emulation/qemu