Author: Siro Mugabi

Category: QEMU

Summary:

An introduction to QEMU/KVM infrastructure.

Tags: qemu linux introduction ubuntu

Overview

The Quick EMUlator (QEMU)1 is a fast, cross-platform Open Source machine emulator. It emulates several major hardware architectures (e.g. x86, ARM, PowerPC and Sparc) and can execute on several processor platforms (e.g. x86, ARM, and PowerPC). In addition, it also can run on a number of operating systems (e.g. GNU/Linux or Microsoft Windows).

The emulated machine is referred to as the target while the platform that QEMU executes on is called the host. The OS running on the target is referred to as the guest. The QEMU instance (including the guest environment) constitutes the Virtual Machine.

Virtual platforms are instrumental in embedded development as they introduce numerous possibilities e.g. working with unacquired/non-existing hardware, better behavioural visibility during platform debugging, etc. In short, virtual platforms such as QEMU facilitate rapid prototyping and development.

QEMU provides two modes of emulation:

  • Full system emulation

    A complete and unmodified OS (i.e. guest) is run in a virtual machine.

  • Linux user mode emulation

    A Linux process compiled for one target CPU can be executed in a host based on another CPU.

Central to both modes of operation is the processor emulator. In full system emulation mode, QEMU additionally provides a complete virtual machine environment consisting of peripheral devices such as:

  • Descriptions of the emulated machine architectures

  • Emulated devices (network card, graphics device, hard drives etc)

  • Generic devices used to connect the emulated devices to the related host devices.

  • User interfaces.

  • Debugger

The exact set of available emulated devices will depend on the emulated machine architecture in question.

QEMU and KVM

When used as a machine emulator, QEMU can run OSes and programs made for one machine (e.g. an ARM board) on a different machine (e.g. your own PC). By using dynamic translation, it achieves very good performance.

The combination of QEMU and the Linux Kernel Virtual Machine (KVM) hypervisor offers a virtualization solution. When used as a virtualizer, QEMU achieves near native performance by executing the guest code directly on the host CPU, i.e. as opposed to dynamic translation in software. QEMU/KVM virtualization requires a target that is of the same type as the host architecture. QEMU also supports virtualization when executing under the Xen hypervisor.

The Linux KVM hypervisor is a kernel module that allows a userland program to exploit processor hardware virtualization features. Supported architectures include Intel and AMD processors (x86 and x86_64), 32-bit and 64-bit ARM, PPC 440, PPC 970, and MIPS.

On x86, to check for the availability of processor hardware virtualization extensions:

$ cat /proc/cpuinfo | egrep '(svm|vmx)'

where:

  • AMD: Secure Virtual Machine (SVM)
  • Intel: Virtual Machine eXtensions (VMX)

And for the KVM module (for instance):

$ lsmod | grep kvm
kvm_intel             137721  0 
kvm                   415549  1 kvm_intel

Initially a fork project, qemu-kvm was eventually merged with upstream QEMU. Follow this link for more details.

On the QEMU commandline, use the -enable-kvm option to ensure that KVM support is enabled. QEMU will automatically fall back to software emulation if it is unable to use the virtualization extensions. To check whether KVM accelaration is enabled for a live QEMU process, run the following on the QEMU monitor console (for instance):

(qemu) info kvm
kvm support: enabled.

Follow this link for an introduction to using the QEMU monitor console.

Obtaining QEMU

Build and Install From Source

Download

  • Git

    QEMU is developed using git. The main tree is located here. The latest development happens on the master branch. The stable tree is located in a stable-0.XX branch where 0.XX is the minor release version.

    $ git clone git://git.qemu.org/qemu.git
    

    Visit this page for more details.

  • Tarball

    Alternatively, a direct HTTP download of QEMU tarball archives can be done e.g:

    $ wget -c http://wiki.qemu-project.org/download/qemu-1.6.0.tar.bz2
    

Configuration

The following instructions assume that a download of the qemu-1.6.0.tar.bz2 archive has already been done to the current working directory:

$ tar xjf qemu-1.6.0.tar.bz2
$ mkdir {qemu-tmp,qemu-tools-1.6.0}
$ ls
qemu-1.6.0  qemu-1.6.0.tar.bz2  qemu-tmp  qemu-tools-1.6.0
$ WORKDIR=$PWD
$ SRC=${WORKDIR}/qemu-1.6.0
$ TMP=${WORKDIR}/qemu-tmp 
$ INSTALL=${WORKDIR}/qemu-tools-1.6.0

QEMU build is by way of GNU configure. Among other things, configure allows "out-of-tree" builds. This build style is optional, but recommended, and is the approach followed here. See Building packages with configure for more details on using Autoconf-generated configure scripts.

For the most general build configuration:

$ cd $TMP
${SRC}/configure --prefix=${INSTALL}/usr/local

However, note that this configuration will result in considerable build time due to the inclusion of support for all target architectures. In addition, certain features for specific targets (e.g. KVM support) may not be built as they may not be enabled by default.

To specify a particular set of architectures for build, the --target-list= option is available. For instance, the following configuration:

$ ${SRC}/configure --target-list=i386-softmmu,x86_64-softmmu,arm-softmmu,arm-linux-user --enable-kvm --enable-fdt --prefix=${INSTALL}/usr/local

translates to:

  • Build QEMU with support for only x86 (32-bit and 64-bit) and ARM (user mode and full system emulation)

  • Include KVM support in the build (i.e. --enable-kvm)

  • Include libfdt support in the build (i.e. --enable-fdt) to allow usage of the -dtb option in QEMU's commandline.

Build and Install

Once configure has completed successfully, run the following commands to complete the QEMU build and installation process:

$ make
$ make install

The fresh installation should resemble:

Static Vs. Dynamic linking

By default, QEMU builds result in dynamically linked ELF binaries. However, certain applications (e.g. foreign chrooting)2 require the presence of the statically linked versions of the QEMU user-mode interpreter. So, for instance, to build the static version of qemu-arm:

$ cd $WORKDIR
$ rm -r qemu-tmp
$ mkdir {qemu-tmp,qemu-tools-1.6.0-static}
$ INSTALLSTATIC=${WORKDIR}/qemu-tools-1.6.0-static 
$ cd $TMP
$ ${SRC}/configure --target-list=arm-linux-user --static --prefix=${INSTALLSTATIC}/usr/local
$ make 
$ make install

This should result in an installation similar to:

Distro Install

Ubuntu Install

Installing the Ubuntu distro QEMU package is relatively straightforward. With Ubuntu 10.04 and 12.04:

$ sudo apt-get install qemu-kvm-extras qemu-user-static

where:

  • qemu-kvm-extras installs full system emulation support for supported architectures including x86 (qemu-kvm), ARM, CRIS, PPC, MicroBlaze and SPARC. It also installs the dynamically linked versions of the corresponding QEMU usermode utilities by the qemu-user package in addition to other tools such as qemu-img and qemu-nbd.

  • The qemu-user-static package is required for the installation of the statically linked versions of the QEMU usermode interpreter and for binfmt misc support.

With Ubuntu 13.04, it is possible to specify QEMU support for (a) specific architecture(s). For x86:

$ sudo apt-get install qemu-system-x86
...
The following NEW packages will be installed:
cpu-checker ipxe-qemu kvm-ipxe libaio1 libboost-thread1.49.0 librados2
librbd1 libsdl1.2debian libseccomp1 libspice-server1 libxen-4.2
libxenstore3.0 msr-tools qemu-keymaps qemu-system-common qemu-system-x86
qemu-utils seabios sharutils vgabios
...

This will result in the following installation:

$ ls /usr/bin/ | egrep ’(kvm|qemu)’
kvm
kvm-img
kvm-nbd
kvm-spice
qemu
qemu-img
qemu-io
qemu-nbd
qemu-system-i386
qemu-system-x86_64
qemu-system-x86_64-spice

Note that this Ubuntu QEMU package release is post qemu-kvm+upstream merge:

$ qemu-system-x86_64 -version
QEMU emulator version 1.4.0 (Debian 1.4.0+dfsg-1expubuntu4), Copyright (c) 2003-2008 Fabrice Bellard

$ cat /usr/bin/kvm
#! /bin/sh
echo "W: kvm binary is deprecated, please use qemu-system-x86_64 instead" >&2
exec qemu-system-x86_64 -machine accel=kvm:tcg "$@"

Support for other architectures can be installed in a similar fashion:

$ sudo apt-get install qemu-system-arm
...
The following NEW packages will be installed:
libfdt1 qemu-system-arm
...
$ sudo apt-get install qemu-system-ppc
...
The following NEW packages will be installed:
libfdt1 qemu-system-ppc
...

To perform an installation of both full system and usermode emulation for all supported QEMU targets in one go:

$ sudo apt-get install qemu-system qemu-user qemu-user-static

Mini-HOWTO: Distro install on QEMU disk image

The Linux distro used here is an Ubuntu 13.04 AMD64 ISO image.

  • Create a QEMU disk image

    There are a number of QEMU disk image formats. Commonly used formats include qcow2 and raw. To create a disk image in these formats:

    $ qemu-img create -f qcow2 prestine-13.04-desktop-amd64-img.qcow2 10G
    $ qemu-img create -f raw prestine-13.04-desktop-amd64-img.raw 10G
    

    See Manipulting QEMU Disk Images with qemu-img for general information on QEMU disk image formats.

  • Install the distro in the disk image

    • If the ISO image resides on harddisk e.g. current working directory:

      $ ls
      ubuntu-13.04-desktop-amd64.iso
      
      $ qemu-system-x86_64 -enable-kvm -smp 2 -m 1024 -localtime -hda prestine-13.04-desktop-amd64-img.qcow2 -cdrom ubuntu-13.04-desktop-amd64.iso -boot d
      
    • If the ISO image is on CDROM:

      $ qemu-system-x86_64 -enable-kvm -smp2 -m 1024 -localtime -hda prestine-13.04-desktop-amd64-img.qcow2 -boot d
      

    See qemu(1) for an explanation of the commandline options used.

  • Test the installation

    Once the installation is complete, reboot into the fresh distro installation with the following (or similar) commandline:

    $ qemu-system-x86_64 -enable-kvm -smp 2 -m 1024 -drive file=prestine-13.04-desktop-amd64-img.qcow2,cache=writeback
    

Also See

Resources

  • wiki.qemu.org

Footnotes

1. I read this somewhere -- can't seem to find the reference. Some argue that QEMU is not an acronym. [go back]

2. See QEMU Usermode for examples of performing foreign chroot. [go back]