TechBuilder Recipe: Living in a Virtual (Linux) World


Blowing away a computer and installing a different operating system is all part of a day's work for the Test Center. In fact, some projects in the Test Center over the past year have included a lot of time spent simply installing and reinstalling various Linux distributions on a single machine.

There are good reasons for removing a Linux distro entirely and doing a clean install. For testing and evaluation purposes, however, it's helpful to have multiple systems available simultaneously for comparison. This is where virtual machines come in handy.

A virtual machine is an x86 PC environment that mimics a PC while running as a guest on the actual machine. Multiple virtual machines can concurrently operate on the same host machine.

VMware has dominated headlines with its VMware Server, but there are plenty of other vendors with their flavor of VM software, including Microsoft's HyperVisor.

Creating virtual machines on a Linux host system can be a frustrating experience. Installing VMware Server on a Linux box to run multiple Linux distributions was difficult enough that it was preferable to install VMware on a Windows machine and run multiple Linux virtual machines instead. For this Techbuilder recipe, virtual machines were created in Linux using open-source QEMU, a free application from Fabrice Bellard.

A command-line application, QEMU is a fast processor emulator and virtualizer. Currently, the package supports ARM, PowerPC, SPARC and x86 emulation. While we missed the easy point-and-click interface that comes with Windows packages, creating virtual machines was a less aggravating process. Using QEMU, virtual machines for Fedora, CentOS, Gentoo, Mandriva and PuppyLinux were created on top of the Ubuntu host.

While many modern Linux distributions have QEMU preinstalled, Ubuntu didn't. It was simple enough to download and install using Ubuntu's package manager; the latest source is also always available from fabrice.bellard.free.fr.

Emulation and virtualization are, at heart, two different things. An emulator pretends to be a different machine or to have a different hardware configuration from the host. In fact, an emulator can mimic an entirely different processor architecture. For example, an emulator can run as a SPARC or a PowerPC machine on an Intel x86 PC host. Fabrice's Web site lists all the processors that QEMU can emulate, as well as prebuilt images of various operating systems available for download. However, performance is not all that great.

Virtualization, on the other hand, actually executes code on the system processor instead of mimicking the processor. As a result, the virtualizer has better performance than the emulator. QEMU offers software virtualization, not hardware virtualization.

On this Ubuntu host, QEMU was located in /usr/bin.

Tip 1: Creating The Virtual Machine

The Test Center first had to define a virtual hard disk on the system to hold the Fedora ISO that was downloaded. This was doned using the qemu-img command, which creates a raw format disk image by default with 256 MB of memory.

$ qemu-img create 256M
Formatting '', fmt=raw, size=262144 kB

At this point, the virtual machine is an empty file on the host machine and has no operating system running on it.

Tip 2: Installing An OS On The Virtual Machine

The next step is to use an ISO image to boot up the virtual machine.

qemu -cdrom
-hda
-m 256 -boot d

The "cdrom parameter lists where the ISO image file is located on the host system. Despite the name of the parameter, the ISO does not need to be on a physical CD or in the CD-ROM. The -hda parameter defines the path to the virtual hard disk that was created using the qemu-img command. The "m switch allocated memory to the virtual machine, in which case, it was only 256 MB of RAM (Fedora 7 does fine with this). The "boot parameter assigns a drive letter to the ISO image.

The virtual machine will see two drives; the "hda flag refers to the primary master and the "cdrom flag refers to the secondary master. From the host system's point of view, these drives are still two files while the virtual machine treats them as real drives.

If the virtual machine had been created correctly in the previous step and the ISO file is valid, a window will open. After going through the bootup sequence, the contents of the ISO-"whether it's an installer or a Live Desktop"-will appear. For all intents and purposes, the software running in the window thinks this is a real machine. So the installation can be launched by clicking on the installer on the desktop and proceeding as normal. If the install needs to reboot, that's fine, as rebooting the virtual machine won't shut down QEMU. Once the installation is complete, it's safe to shut down the virtual machine and QEMU.

Clicking anywhere inside the QEMU session window captures the mouse for the virtual machine. To release the mouse to work on the host machine, press Ctl-Alt. The Ctl-Alt-F command toggles the QEMU session between normal and full-screen mode.

Tip 3: Starting Up The Virtual Machine

The previous step ran the ISO image on the virtual machine, not the installation that just wrote the operation system to the virtual machine file. The following command opens up the virtual machine in QEMU with the installed operating system.

qemu "hda
-m 256

There are other options available when starting the virtual machine, such as adding the ""soundhw sb16" option to simulate a SoundBlaster 16 card; the ""std-vga" option to simulate a standard VGA card; or ""localtime" option in case the host OS is not using GMT for its clock. Networking is enabled by default, and QEMU obtains an IP address from the DHCP server automatically.

The virtual drive can be edited to increase the amount of memory used:

qemu -hda
-m 512

None of the QEMU commands are too complicated or difficult to type, but there are many qemu parameters for the network, USB, file-sharing and I/O devices. A quick glance through the QEMU page lists them all and suggests many possible ways for using them. Typing qemu at the prompt without any parameters will also list some possible parameters.

Tip 4: GUI Options

There are several graphical tools that simplify QEMU's advanced features. QEMU Launcher and QEMUlator are just two of the many options. QEMU Launcher is available from https://gna.org/projects/qemulaunch
. The QEMUlator is available from http://qemulator.createweb.de/
.

First, QEMU Launcher organizes its various features under three tabs: Configurations, Launcher Settings and About. Under the Configuration tab, virtual machines are created using existing QEMU images. All that was needed was the name of the configuration being created, the location of the virtual drive (created by qemu-img), and memory to be allocated. After creation, the new virtual machine can be launched directly from the interface.

Even running qemu-img is unnecessary, as a brand-new virtual machine image can be created via the New button next to the hard-disk 0 label. The pop-up dialog will accept all the required information to create the virtual drive, as well as assigning the CD-ROM to boot up from the ISO image. Even though qemu-img command is straightforward, the Launcher makes this step even simpler.

Through the Launcher, the virtual machines can be configured to work with the network card, an alternate video device, set the clock or turn on system sounds. There are also options providing access to a Samba share.

QEMUlator is even more powerful than QEMU Launcher. It can create brand-new disk images, install distributions and monitor running virtual machines. QEMUlator has an Install Wizard that steps the through the process of installing another distribution. The wizard is launched by clicking on System from the menu bar and selecting "Install system."

Under QEMUlator, running the virtual machine with the actual installed distribution is as simple as clicking the run arrow at the top of the screen. All existing virtual machines are listed under the My Machines tab and can be modified with the Show Settings button. QEMUlator also lists all running virtual machines in one place. The Show option for each virtual machine displays the exact qemu command with all the parameters that are being run. This is handy for scripting purposes, when the interface is not useful.

Tip 5: Boosting For Better Performance

The virtual machine can be modified to run with more memory on the fly by increasing allocated memory when starting QEMU. For a greater performance boost, QEMU Accelerator, available on http://fabrice.bellard.free.fr/qemu/kqemu-doc.html can be installed. This is a driver that speeds up the emulator.

Even though the machines are virtual, they use their share of real system resources, such as processor power and memory. The more pumped up the host machine's hardware is, the easier it will be to run multiple virtual machines.