Virtualization with KVM

Introducing KVM, its internals and how to configure and install it.
How KVM Compares to Existing Hypervisors

KVM is a fairly recent project compared with its competitors. In an interview with Avi Kivity, the main developer, he compared KVM with alternative solutions:

In many ways, VMware is a ground-breaking technology. VMware manages to fully virtualize the notoriously complex x86 architecture using software techniques only, and to achieve very good performance and stability. As a result, VMware is a very large and complex piece of software. KVM, on the other hand, relies on the new hardware virtualization technologies that have appeared recently. As such, it is very small (about 10,000 lines) and relatively simple. Another big difference is that VMware is proprietary, while KVM is open source.

Xen is a fairly large project, providing both paravirtualization and full virtualization. It is designed as a standalone kernel, which only requires Linux to perform I/O. This makes it rather large, as it has its own scheduler, memory manager, timer handling and machine initialization.

KVM, in contrast, uses the standard Linux scheduler, memory management and other services. This allows the KVM developers to concentrate on virtualization, building on the core kernel instead of replacing it.

QEMU is a user-space emulator. It is a fairly amazing project, emulating a variety of guest processors on several host processors, with fairly decent performance. However, the user-space architecture does not allow it to approach native speeds without a kernel accelerator. KVM recognizes the utility of QEMU by using it for I/O hardware emulation. Although KVM is not tied to any particular user space, the QEMU code was too good not to use—so we used it.

KVM, however, is not perfect due to its newness; it has some limitations including the following:

  • At the time of this writing, KVM supports only Intel and AMD virtualization, whereas Xen supports IBM PowerPC and Itanium as well.

  • SMP support for hosts is lacking in the current release.

  • Performance tuning.

However, the project is continuing at a rapid pace, and according to Avi Kivity, KVM already is further ahead than Xen in some areas and surely will catch up in other areas in the future.

Installing KVM

KVM has been added to many distribution-specific repositories, including OpenSUSE/SUSE, Fedora 7 (which comes with KVM built-in), Debian and Ubuntu (Feisty).

For other distributions, you need to download a kernel of version 2.6.20 and above. When compiling a custom kernel, select Device Drivers→Virtualization when configuring the kernel, and enable support for hardware-based virtualization. You also can get the KVM module along with the required user-space utilities from sourceforge.net/project/showfiles.php?group_id=180599.

I have installed the OpenSUSE packages; hence, filenames used in the examples in this article may be different from those in your release.

Creating the Guest OS

Using the compiled kernel with virtualization support enabled, the next step is to create a disk image for the guest operating system. You do so with qemu-img, as shown below. Note that the size of the image is 6GB, but using QEMU's copy-on-write format (qcow), the file will grow as needed, instead of occupying the full 6GB:

# qemu-img create -f qcow image.img 6G

Instantiation of a new guest operating system is provided by a utility called qemu-kvm. This utility works with the kvm module, using /dev/kvm to load a guest, associate it with the virtual disk (a regular QEMU qcow file in the host operating system), and then boot it. In some distributions this utility may be called kvm.

With your virtual disk created, load the guest operating system into it. The following example assumes that the guest operating system is on a CD-ROM. In addition to populating the virtual disk with the CD-ROM ISO image, you must boot the image when it's done:

# qemu-kvm -m 384 -cdrom guestos.iso -hda image.img -boot d

The I/O in the current release of KVM is handled by QEMU, so let's look at some important QEMU switches:

  • -m: memory in terms of megabytes.

  • -cdrom: the file, ideally an ISO image, acts as a CD-ROM drive to the VM. If no cdrom switch is specified, the ide1 master acts as the CD-ROM.

  • -hda: points to a QEMU copy-on-write image file. For more hard disks we could specify:

    #qemu-kvm -m 384 -hda vmdisk1.img -hdb vmdisk2.img -hdc vmdisk3.img
    

  • -boot: allows us to customize the boot options; the -d switch boots from the CD-ROM.

The default command starts the guest OS in a subwindow, but you can start in full-screen mode, by passing the following switch:

-full-screen

Additionally, KVM allows low-level control over the hardware of the virtualized environment. You can redirect serial, parallel and USB ports to specific devices by specifying the appropriate switches. Sound in the VM is supported as well, and you can pass your sound card to the VM via the -soundhw switch to enable sound.

The following are some keyboard shortcuts:

  • Ctrl-Alt-F: toggle full screen.

  • Ctrl-Alt-N: switch to virtual console N.

  • Ctrl-Alt: toggle mouse and keyboard.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thanks for the heads up!

Anonymous's picture

Thanks for the original link....I like that article better!

Suspicious circumstances

noticer's picture

I may be mistaken, but the content of this article appears to be copied (at times word for word) from an earlier article written on April 18th 2007, found here - http://www.ibm.com/developerworks/linux/library/l-linux-kvm/ . If so the circumstances are unfortunate and apalling.

An example is the section in this article (written on 1st Feb 2008)

"Two of the most common approaches to software-emulated virtualization are full virtualization and paravirtualization. In full virtualization, a layer, commonly ..... so each cooperates with the hypervisor to virtualize the underlying hardware. "

is almost the IDENTICAL to

"Two of the most common approaches to virtualization are full virtualization and para-virtualization. With full virtualization, a layer exists between the virtualized operating systems ...... so each cooperates with the hypervisor to virtualize the underlying hardware." from the article by Tim Jones linked above.

SHAME!

Oops

Mitch Frazier's picture

I would have to agree with you, it appears that much of this was copied from the article that you mention. We do attempt, when circumstances warrant it, to check the web to make sure content is original, but mostly we just have to rely on the honesty of our authors.

So yes, shame on us and shame on the author.

Mitch Frazier is an Associate Editor for Linux Journal.

HOW to get KVM going on Suse 10.3

awilcox's picture

Hello,
I installed the two KVM packages using Yast. That was the easy part.

Now what do I do?

"kvm-intel" isn't recognized. Is there some script you didn't mention? Am I supposed to use modprobe?

Webcast
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6

Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.

Learn more about catching the bad guy in this free white paper.

Learn More

Sponsored by DLT Solutions