QEMU (Quick EMUlator) is a generic, open source hardware emulator and virtualization suite. Often it is used in conjunction with acceleration in the form of a Type-I hypervisor such as KVM (Kernel-based Virtual Machine) or Xen. If no accelerator is used, QEMU will run entirely in user-space using its built in binary translator TCG (Tiny Code Generator). Using QEMU without an accelerator is relatively inefficient and slow.
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).
BIOS and UEFI firmware
In order to utilize KVM either Vt-x or AMD-V must be supported by the processor. Vt-x or AMD-V are Intel and AMD's respective technologies for permitting multiple operating systems to concurrently execute operations on the processors.
To inspect hardware for virtualization support issue the following command:
grep --color -E "vmx|svm" /proc/cpuinfo
For a period manufacturers were shipping with virtualization turned off by default in the system BIOS. Note that changing this feature in the BIOS may actually require full removal of power from the system to take effect. If restarting the system does not work try shutting down, unplugging the system and pressing the power button in an unplugged state to discharge any residual energy from the power supply unit (PSU). Reapply power to the system to verify success.
If KVM support is available there should be a "kvm" device listed at /dev/kvm. This will take effect after the system has booted to a KVM enabled kernel.
Activate the following kernel options:
[*] Virtualization ---> <*> Kernel-based Virtual Machine (KVM) support
[*] Virtualization ---> <M> KVM for Intel processors support
[*] Virtualization ---> <M> KVM for AMD processors support
If both "KVM for Intel processors support" and "KVM for AMD processors support" are set as built into the kernel (
*) an error message will appear from kprint from early boot. Since the system has only one type processor (Intel or AMD) enabling one or both options as modules (
M) will make the error message disappear.
If the options are not available, check that General Setup -> Timers Subsystem -> High Resolution Timer Support is enabled
vhost-net USE flag (recommend):
[*] Virtualization ---> <*> Host kernel accelerator for virtio net
Device Drivers ---> [*] VHOST drivers ---> <*> Host kernel accelerator for virtio net
Device Drivers ---> [*] Network device support ---> [*] Network core driver support <*> Universal TUN/TAP device driver support
Needed for 802.1d Ethernet bridging:
[*] Networking support ---> Networking options ---> <*> The IPv6 protocol <*> 802.1d Ethernet Bridging
Some packages are aware of the
qemu USE flag.
Review the possible USE flags for QEMU:
USE flags for app-emulation/qemu QEMU + Kernel-based Virtual Machine userland tools
||Adds support for braille displays using brltty|
||Enables support for Linux's Async IO|
||Enable alsa output for sound emulation|
||Use the bzlib compression library|
||Use Linux capabilities library to control privilege|
||Enable disassembly support with dev-libs/capstone|
||Support ISOs / -cdrom directives via HTTP or HTTPS.|
||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|
||Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally|
||Enables firmware device tree support|
||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)|
||Enables FUSE block device export|
||Enables GlusterFS cluster fileystem via sys-cluster/glusterfs|
||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.|
||Add support for x11-libs/gtk+ (The GIMP Toolkit)|
||Enable Infiniband RDMA transport support|
||Enable efficient I/O via sys-libs/liburing.|
||Enable direct iSCSI support via net-libs/libiscsi instead of indirectly via the Linux block layer that sys-block/open-iscsi does.|
||Add support for the JACK Audio Connection Kit|
||Enable jemalloc allocator support|
||Enable jpeg image support for the VNC console server|
||Enable support for lzo compression|
||Enable multipath persistent reservation passthrough via sys-fs/multipath-tools.|
||Enable the ncurses-based console|
||Enable NFS support|
||Add Native Language Support (using gettextGNU locale utilities)|
||Enable NUMA support|
||Add support for OpenGL (3D graphics)|
||Add support for OSS (Open Sound System)|
||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.|
||Enable qemu plugin API via shared library loading.|
||Enable png image support for the VNC console server|
||Enable pulseaudio output for sound emulation|
||Add optional support/bindings for the Python language|
||Enable rados block device backend support, see https://docs.ceph.com/en/mimic/rbd/qemu-rbd/|
||Add support for the Simple Authentication and Security Layer|
||Enable the SDL-based console|
||SDL Image support for icons|
||Enable seccomp (secure computing mode) to perform system call filtering at runtime to increase security of programs|
||!!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur|
||Enable TCP/IP in hypervisor via net-libs/libslirp|
||Enable smartcard support|
||Enable support for Snappy compression (as implemented in app-arch/snappy)|
||Enable Spice protocol support via app-emulation/spice|
||Enable SSH based block device support via net-libs/libssh2|
||Build the User and Software MMU (system) targets as well as tools as static binaries|
||Build the User targets as static binaries|
||Enable SystemTAP/DTrace tracing|
||Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)|
||Enable virtual/udev integration (device discovery, power and storage device support, etc)|
||Enable USB passthrough via dev-libs/libusb|
||Use sys-apps/usbredir to redirect USB devices to another machine over TCP|
||Enable VDE-based networking|
||Enable accelerated networking using vhost-net, see https://www.linux-kvm.org/page/VhostNet|
||Enable shared file system access using the FUSE protocol carried over virtio.|
||Enable experimental Virgil 3d (virtual software GPU)|
||Enable VirtFS via virtio-9p-pci / fsdev. See https://wiki.qemu.org/Documentation/9psetup|
||Enable VNC (remote desktop viewer) support|
||Enable terminal support (x11-libs/vte) in the GTK+ interface|
||Add support for getting and setting POSIX extended attributes, through sys-apps/attr. Requisite for the virtfs backend.|
||Enables support for Xen backends|
||Support xfsctl() notification and syncing for XFS backed virtual disks.|
||Enable support for ZSTD compression|
More than one USE flag (
spice) can be enabled for graphical output. If graphics are desired it is generally recommended to enable more than one graphical USE flag.
If virt-manager is going to be used, be sure to enable the
spiceUSE flags on the qemu package for correct operation.
Additional ebuild configuration frobs are provided as the USE_EXPAND variables QEMU_USER_TARGETS and QEMU_SOFTMMU_TARGETS. See app-emulation/qemu for a list of all the available targets (there are a heck of a lot of them; most of them are very obscure and may be ignored; leaving these variables at their default values will disable almost everything which is probably just 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 shockingly ambitious) purpose of these targets is to "magically" allow importing user-space linux ELF binaries from a different architecture into the native system (that is, they are like multilib, without the awkward need for a software stack or CPU capable of running it).
In order to enable QEMU_USER_TARGETS and QEMU_SOFTMMU_TARGETS we can edit the variables globally in /etc/portage/make.conf, i.e.:
QEMU_SOFTMMU_TARGETS="arm x86_64 sparc" QEMU_USER_TARGETS="x86_64"
Or, we can edit the /etc/portage/package.use file(s). Two equivalent syntaxes are available: traditional "use-flag" syntax, i.e.:
app-emulation/qemu qemu_softmmu_targets_arm qemu_softmmu_targets_x86_64 qemu_softmmu_targets_sparc app-emulation/qemu qemu_user_targets_x86_64
and, a newer sexy USE_EXPAND-specific syntax:
app-emulation/qemu QEMU_SOFTMMU_TARGETS: arm x86_64 sparc QEMU_USER_TARGETS: x86_64
After reviewing and adding any desired USE flags, emerge app-emulation/qemu:
emerge --ask app-emulation/qemu
For Networking configuration, see the networking options documentation.
For IPv6 networking see the IPv6 subarticle.
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):
gpasswd -a <username> kvm
To make life easier, there are multiple user-friendly front ends to QEMU:
|AQEMU||app-emulation/aqemu||https://sourceforge.net/projects/aqemu/||Graphical interface for QEMU and KVM emulators, using Qt5.|
|GNOME Boxes||gnome-extra/gnome-boxes||https://wiki.gnome.org/Apps/Boxes||GNOME App to manage virtual and remote machines.|
|libvirt||app-emulation/libvirt||https://www.libvirt.org/||C toolkit to manipulate virtual machines.|
|QtEmu||https://gitlab.com/qtemu/gui||Qt-based front-end for QEMU.|
|qt-virt-manager||https://f1ash.github.io/qt-virt-manager/||A graphical user interface for libvirt written in Qt5.|
|virt-manager||app-emulation/virt-manager||https://virt-manager.org||A graphical tool for administering virtual machines.|
When using virt-manager, be sure to enable the
spiceUSE flags on the qemu package for correct operation.
After emerging, to run virt-manager as a normal user, ensure each user has been added to the libvirt group:
usermod -a -G libvirt <user>
Uncomment the following lines from the libvirtd configuration file:
auth_unix_ro = "none" auth_unix_rw = "none" unix_sock_group = "libvirt" unix_sock_ro_perms = "0777" unix_sock_rw_perms = "0770"
Be sure to have the user log out then log in again for the new group settings to be applied.
The libvirt group might not have been created, in which case another group, such as wheel can be used.
Issue the following command to restart the libvirtd service under OpenRC:
Issue the following command to restart the libvirtd service under systemd:
systemctl restart libvirtd
virt-admin should now be launchable as a regular user.
If permission denied issues are experienced when loading ISO images user directories (somewhere beneath /home/) then the /var/lib/libvirt/images/ directory can be used to store the images.
libvirt recommended kernel config
In addition to the kernel requirements listed above, the following kernel config is recommended by the libvirt installer; check the logs to see if any additional ones are requested by the build.
* CONFIG_BRIDGE_EBT_MARK_T Depends on: NET = y, NETFILTER = y, BRIDGE_NF_EBTABLES=y Location: -> Networking support (NET=y) -> Networking options -> Network packet filtering framework (Netfilter) (NETFILTER [=y]) -> Ethernet Bridge tables (ebtables) support (BRIDGE_NF_EBTABLES [=y]) * CONFIG_BRIDGE_NF_EBTABLES Depends on: Net = y, bridge = y, netfilter = y, netfilter_xtables = y location: networkign support -> networking options -> network packet filtering framework (Netfilter) = y * CONFIG_NETFILTER_ADVANCED depends on the above (Net = y, netfilter = y) * CONFIG_NETFILTER_XT_CONNMARK * CONFIG_NETFILTER_XT_TARGET_CHECKSUM * CONFIG_IP6_NF_NAT * CONFIG_BRIDGE_EBT_T_NAT * CONFIG_NET_ACT_POLICE * CONFIG_NET_CLS_FW: * CONFIG_NET_CLS_U32 * CONFIG_NET_SCH_HTB * CONFIG_NET_SCH_INGRESS: * CONFIG_NET_SCH_SFQ
Network interfaces for virt-manager can be checked by running virsh net-list
Issues with libvirt networking
The following sub-articles provide instructions on QEMU usage:
- Usage options - Contains common options used with QEMU.
- Linux guest - Describes the configuration steps needed to setup Linux to run on QEMU.
- Windows guest - Describes the configuration steps needed to setup Windows to run on QEMU.
- OS2WarpV3 guest - Describes the configuration steps needed to setup OS2WarpVs=3 to run on QEMU.
"kvm: already loaded the other module"
Sometimes during the early boot splash the error message "kvm: already loaded the other module" can be seen. This message indicates both the Intel and the AMD kernel virtual machine settings have been enabled in the kernel. To fix this, enable as a module or disable either the Intel or AMD KVM option specific to the system's processor in the kernel configuration. For example, if the system has an Intel processor enable the Intel KVM, then make sure the AMD KVM is set as a module (M) or is disabled (N). The relevant options to enable or disable can be found in the kernel's .config file via the CONFIG_KVM_INTEL and CONFIG_KVM_AMD variables or in the configuration section above.
Creating TUN/TAP device - No such file or directory
Sometimes this error can occur if TUN/TAP support cannot be found in the kernel. To solve this, try loading the driver:
If that works, add this to a file in /etc/modules-load.d/ to load on startup:
- Comparison of virtual machines — compares the features of several platform virtual machines.
- Fast Virtio VM
- GPU passthrough with libvirt qemu kvm — directly present an internal PCI GPU to a virtual machine
- QEMU/KVM_IPv6_Support — describes IPv6 support in QEMU/KVM.
- Virtualization — the concept and technique that permits running software in an environment separate from a computer operating system.