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?

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState