Virtualization with KVM

All for One

Wake-Up Call

Now that you have an image, you can launch a virtual machine directly:

qemu -hda windows.img -cdrom /dev/cdrom U
   -boot once=d -m 512M

Some distributions, including OpenSUSE call this program qemu-kvm, but the parameters stay the same (see the "Double Trouble" box):

qemu-kvm -hda windows.img U
   -cdrom /dev/cdrom -boot once=d -m 512M

Double Trouble

Qemu is actually a genuine emulator that emulates a complete PC including a processor. To accelerate the execution of x86 programs, Qemu's inventor, Fabrice Bellard, also developed the commercial kqemu kernel module. Shortly after the introduction of Intel-VT and AMD-V, the KVM project was launched in parallel. KVM delivered a modified version of Qemu along with its driver, which made the kqemu module obsolete. The Qemu developers pulled the plug, first placing kqemu under the GPL and finally dropping it entirely in favor of KVM.

As of version 0.10.0, Qemu officially supports KVM, and as of version 0.12.0, kqemu is history. However, the KVM developers still build their own version based on the official Qemu, but they do at least keep to the same versioning scheme as Qemu. To find out which version you have on your disk, type qemu -version. The KVM variant will report back as qemu-kvm.

Qemu not only creates virtual machines with x86 processors, but it can also emulate PowerPC, ARM and MIPS systems – although they do not benefit from acceleration on x86 hardware. You can see the full list in the Qemu documentation [4]. Distributors tend to release these additional emulators in a separate package; on Ubuntu, the package name is qemu-kvm-extras. openSUSE 11.3 even bundles the complete, official Qemu system into its qemu package, which leads to curious situation where you can launch Qemu by typing either qemu-kvm or qemu. The first of these programs comes from the KVM project and automatically accesses KVM. With the second, you need to explicitly enable acceleration using the additional -enable-kvm command-line parameter.@KE:

Qemu mounts the image that you created as the first hard disk (-hda) then mounts the physical CD-ROM drive (which uses a device called /dev/cdrom in the sample). Qemu immediately boots from the CD-ROM (-boot once=d; to boot from the first hard disk, you would need once=c). Then Qemu opens a new window in which all the screen output from the virtual machine is displayed. If you do not want to use the physical CD drive, you can integrate an ISO image instead :

qemu -hda windows.img U
   -cdrom /home/tim/debian.iso U
   -boot once=d -m 512M

This process provides a fairly simple way to try out a new distribution without needing to waste a CD.

By default, Qemu gives virtual machines just 128MB of RAM – state of art operating systems will just laugh at this. To assign more RAM, you need the -m parameter; the example assigns 512MB. You will need at least 1GB Windows 7. You should avoid assigning all of your physical memory resources to the virtual machines. Doing so would leave the host system and Qemu without enough air to breathe. The amount of RAM needed depends on the distribution. Ubuntu and openSUSE will need at least 512MB.

Figure 4: Qemu was unable to access the KVM services here. You can normally ignore the message about the missing romfile as it relates to booting off the network.
Figure 5: As the lsmod command shows, both KVM kernel modules (kvm and kvm_intel) have loaded correctly.

Modulo

Qemu output shows important status messages to the terminal window (Figure 4). In Figure 5, the output is complaining about a lack of support from KVM. In a case like this, you should first check whether the driver is loaded correctly. The driver comprises the kvm.ko kernel module, which provides some basic functionality to Qemu, and a processor specific module, kvm-intel.ko or kvm-amd.ko. Issuing the following command should provide your current combination:

lsmod | egrep '(kvm)'

Otherwise, the following commands will let you load the module manually:

sudo modprobe kvm
sudo modprobe kvm-intel

or for an AMD model:

sudo modprobe kvm
sudo modprobe kvm-amd

If this approach doesn't work, the messages in your kernel log will provide more information about the problem. The KVM FAQ [5] is also useful if you need help, and questions are answered on the KVM mailing list or the forum [6].

Grabbers

You can install the operating system in the normal way on the virtual machine. Clicking the window with the mouse captures the mouse cursor and lets you use it on the guest system. Pressing Ctrl+Alt gives the mouse back. Some Linux distributions detect Qemu and do without mouse cursor integration. You can then use Qemu like any normal application on the host system.

Besides a Cirrus CLGD 5446 PCI graphics adapter, Qemu emulates older standard hardware for which nearly any operating system will have matching drivers. However, you can't run 3D applications, especially not games.

If the desired operating system refuses to launch, you can try switching off KVM by executing qemu with the -no-kvm parameter. You can also try replacing the virtual graphics adapter with a simple model by adding a -std-vga switch. For Windows XP, you also need to disable the power saver function by adding -no-acpi.

Figure 6: The Qemu Monitor lets you control your virtual machines. The "info block" command outputs details of the drives.

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Virsh Libvert Tool

    With the command-line tool virsh, a part of the libvirt library, you can query virtual machines to discover their state of health, launch or shut down virtual machines, and perform other tasks – all of which can be conveniently scripted.

  • Secure Your KVM Virtual Machines
    A common misconception posits that software cannot cause mischief if you lock the system away in a virtual machine, because even if an intruder compromises the web server on the virtual machine, it will only damage the guest. If you believe this, you are in for a heap of hurt.
  • Controlling virtual machines with VNC and Spice
    Administrators on Linux virtual machines tend to use VNC to transfer the graphical system to Virtual Machine Manager or a VNC client. One alternative is Spice: If the guest system is running the QXL driver, you can look forward to fast graphics and audio pass through.
  • Hardware-assisted Virtualization

    The Intel VT and AMD-V extensions bring x86 virtualization to the 21st Century. Learn why hardware-assisted virtualization is important and what to watch for the next time you buy a computer. We'll also show you how to configure virtualization on a typical Linux system.

  • Live snapshots with Virtual Machine Manager
    In the scope of developing Fedora 20, the live snapshot function, which has long been supported by libvirt, was integrated with the graphical front end. If you prefer to avoid command-line acrobatics à la Virsh, you can now freeze your virtual KVM and Xen machines in VMM at the press of a button.
comments powered by Disqus