User:Egberts/Drafts/QEMU

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

QEMU is a [Type-2 hypervisor] that runs within user space and performs virtual hardware emulation.

QEMU when used in conjunction with an accelerator becomes a Type-1 hypervisor that runs in kernel space, that allows a user space program access to the hardware virtualization features of various processors. Such accelerator can be 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.

Concepts
There are two types of hypervisors:


 * Type 1 hypervisors are installed directly on hardware and are also called bare-metal hypervisors. (VMware ESXi)
 * Type 2 hypervisors are installed on the underlying operating system. (VMware Workstation, QEMU, QEMU/KVM)

Type 1 hypervisors provide a higher level of performance as there is no overhead from the interaction of the hypervisor with the guest OS.

Type-2 hypervisor subcategories are broken down into:


 * Software-assisted full virtualization
 * use of machine code translator (qemu/tcg, (VMware Worksation <7)


 * (Pure) hardware-assisted (native) full virtualization - Hardware-assisted virtualization ensures that virtual machines have high performance because “part” of the physical CPU is mapped directly to the virtual CPU (vCPU), and there is no overhead to translate instructions from a vCPU to CPU.
 * use of CPU VT to directly execute binary code. Page Table translation occurs when VM directly process the CPU instructions.  (qemu/kvm, VMware Workstations 7+)  A "pure" hardware-assisted virtualization approach, using entirely unmodified guest operating systems, involves many VM traps, and thus high CPU overheads, limiting scalability and the efficiency of server consolidation. Hardware-assisted virtualization reduces the maintenance overhead of paravirtualization as it reduces (ideally, eliminates) the changes needed in the guest operating system. It is also considerably easier to obtain better performance.


 * Hybrid hardware-assisted full virtualization - This performance hit can be mitigated by the use of paravirtualized drivers; the combination has been called "hybrid virtualization"



Virtualization concepts
QEMU supports the following accelerator add-ons as well as its default software-emulation mode.

Virtualization Recap
QEMU is a Type-2 virtualization. (PCI bus pass-through is not supported here so all device drivers must be virtualized mostly using VirtIO).

QEMU with KVM (qemu/kvm) is a Type-1 virtualization. To achieve software-assisted full virtualization of QEMU/KVM, all devices must use VirtIO guest drivers. but not quite the full, full virtualization but more so a paravirtualization where virtual drivers inside the guest VM ensures cooperation between VMs.

Host setup
This section details the QEMU setup of the Linux kernel for a host that is using Gentoo OS on either a x86_64 or an amd64 CPU hardware platform.

BIOS and UEFI firmware
In order to utilize KVM either Vt-x (vmx) or AMD-V (svm) 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:

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. 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 [//www.linux-kvm.org/page/Tuning_Kernel KVM Tuning Kernel] page.

Physical CPU Processor Support - Host
For the QEMU host, choose one of the two CPU architectures found on its hardware platform:

or

Virtual CPU Processor Support - Guest(s)
For a list of supporting CPUs that the guest platform(s) on this host should support, consult the [QEMU use flags] and insert desire target(s) into the USE= string in the portage configuration  file.

Handling Kernel Config at CLI
To set the various kernel configuration settings from the command lines, the shall be used here:

Mandatory kernel configuration options to set:

Useful kernel configuration options to use:

Accelerated networking, required for  USE flag (recommend):

Needed for 802.1d Ethernet bridging:

Intel VT-g (integrated graphics adapter virtualization)
Mediated device passthrough for Intel GPUs (Broadwell and newer) [//forums.gentoo.org/viewtopic-p-8157704.html].

USE flags
Some packages are aware of the USE flag.

Review the possible USE flags for QEMU:

USE_EXPAND
Additional ebuild configuration frobs are provided as the USE_EXPAND variables QEMU_USER_TARGETS and QEMU_SOFTMMU_TARGETS. See 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  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). 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, i.e.:

Or, the file(s) can be modified. Two equivalent syntaxes are available: traditional USE flag syntax, i.e.:

Another alternative is to use the newer sexy USE_EXPAND -specific syntax:

Install QEMU host
After reviewing and adding any desired USE flags, emerge :

Kernel Configuration
As a demonstration of completeness of configuration settings for the Linux kernel, the kernel configuration file will be initialized to tinyconfig which is the smallest buildable Linux kernel (but not necessarily the bootable or functional one).

Now blow away the .config with the smallest default setting possible, execute: