Virtualization on the command line with KVM

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Quick Start

To start a virtual machine quickly with a Live system, use the command qemu -cdrom img.iso , where img.iso is the name of the ISO image that the Live CD/DVD uses to start the guest system. If you've already burned a CD or DVD, simply replace the filename for the device with the appropriate drive, such as:

$ qemu -cdrom /dev/cdrom

The desktop for the started system will now appear in a separate window. Most Linux distributions recognize that you're running a virtual machine that can be run as a normal application, but this can be a bit iffy in some cases. Ubuntu 13.04, for example, has the particular problem of swallowing the mouse pointer or not building the screen correctly, in which case you can add the -vga std option when running QEMU:

$ qemu -cdrom ubuntu.iso -vga std

This command applies another, simpler virtual graphics card to the simulated computer system.

In some cases, you need to click again in the virtual machine window. QEMU then "catches" the mouse pointer. You exit the window by pressing Ctrl+Alt. To toggle back and forth from full-screen mode, press Ctrl+Alt+F, which, unfortunately, doesn't always work and can result in a black virtual machine screen. If you have problems with the graphics, start the Linux guest system in Safe mode.

By default, QEMU provides a virtual graphics card of the Cirrus CLGD 5446 PCI variety. Although most operating systems can handle this GPU, 3D applications won't work. Setting up the network card normally also requires some attention. By default, QEMU provides a network interface card (NIC) with the Realtek chipset, for which all reasonably current operating systems provide a corresponding driver.

The built-in virtual router automatically provides an IP address from its DHCP server. The virtual system not only uses it to access the network or Internet, it accesses the host system with address 10.0.2.2 (Figure 2). If an SSH server is running on the guest, you can use Secure Copy (scp ) to transfer files.

Figure 2: Thanks to the built-in router, which includes a firewall, Ubuntu can access the Internet without any additional settings.

You should absolutely resist the temptation of closing the virtual machine window with the close icon on the title bar. For the guest system, it's as if you pulled the plug on the PC. Instead, power down the guest system as usual from the physical PC, which automatically closes all the windows.

Virtual Disks

If you want not only to test the Live CDs but also install operating systems, you need to have enough hard disk space. With QEMU and KVM, you can choose to use either the physical machine's hard drive for the virtual machine, or simulate one with a corresponding large enough file. The qemu-img utility can create such a hard drive image with:

$ qemu-img create -f qcow2 my_img.img 30G

This command creates the image in QCOW2 format in the my_img.img file, where the virtual hard drive uses a maximum of 30GB. Note that qemu-img also lets you convert among various formats (see "QEMU Conversion Artist").

The size of the disk image in QCOW2 format corresponds exactly to the scope of the files stored therein, with the image size growing according to its content. Only when full does it grow to the complete 30GB. This can happen relatively quickly if you download a video in the virtual machine or install software packages willy-nilly. The physical hard drive should always have enough free space for the virtual hard disk file.

You submit a newly created hard disk image by adding a final parameter when starting QEMU:

$ qemu -cdrom ubuntu.iso -m 1024M my_img.img

The -m 1024M option provides the virtual machine with an additional 1GB of RAM, although QEMU and KVM tap this off the physical main memory. So, be sure that you give the hosting system enough breathing room. Without the -m parameter, the virtual machine gets only 128MB of main memory.

Windows XP (Figure 3) can work reasonably well with that, but Windows 7 and 8 (Figure 4) should get at least 2GB. Ubuntu will be happy with just 512MB. By the way, you can always substitute -m 1G for -m 1024M .

Figure 3: An older Windows XP runs just as well on QEMU/KVM…
Figure 4: …as a newer Windows 7.

The Windows Vista, 7, and 8 installations can take rather a long time. Even if the physical PC is quite fast, let it be for at least an hour. The physical process then returns to its usual self.

In place of a QCOW2 image, you can use the ready-made hard disk images from VirtualBox (ending in .vdi ), VirtualPC (ending in .vhd ), or VMware (ending in .vmdk ). If you want to use a real hard drive instead, replace my_img.img in the previous example with the corresponding device file:

$ qemu -cdrom ubuntu.iso -m 1024M /dev/sdb

Be aware, however, that if the hard drive already has an installed operating system, it might start in the virtual machine but will find completely different hardware (Table 1). Windows especially will not only not install the matching drivers but also will want to be reactivated once again.

Table 1

QEMU: Virtual Hardware

Standard Components
BIOS SeaBIOS
Graphics card Cirrus Logic GD5446
Network card Realtek RTL8139 PCI
Host PCI bridge i440FX
PCI to ISA bridge PIIX3
Keyboard/Mouse PS/2
IDE interface 2
Floppy drive 1
Serial/parallel interface 1/1
Options (Added Manually)
Sound card Various models, among them Soundblaster 16, AC97, HDA
USB support PCI UHCI USB controller, virtual USB hub

Buy this article as PDF

Express-Checkout as PDF

Pages: 5

Price $0.99
(incl. VAT)

Buy Ubuntu User

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

US / Canada

Get it on Google Play

UK / Australia

Related content