Virtualization on the command line with KVM

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Conclusion

QEMU allows for countless other settings, many of which relate to the network. For example, you can set up port forwarding, via TUN/TAP devices that integrate virtual machines in the network or access Windows guests over an SBM server on directories of the host system. Multiple devices can connect to a private network, and you can remote manage individual ones per VNC and freeze a snapshot of its entire state.

The options necessary for the network setup are not exactly intuitive at times. If you want to experiment with it, take a look at the QEMU documentation [5], which is more or less a reference and therefore dry as a bone. Beginners might want to take a look at an older, albeit free, QEMU book by Robert Warnke and Thomas Ritzau [6]. More quick guides and how-tos are in the KVM wiki [7]. Unfortunately, many of the instructions, tips, and tricks on the Internet are outdated or even obsolete.

Once you internalize the many options, you can operate the KVM command line on a virtual machine faster than the required three mouse clicks in VirtualBox, etc.

Qemu-img Conversion Artist

The qemu-img tool creates not only a hard drive image but also can convert among various formats. The following command example creates an image in QCOW2 format from the VirtualBox ubuntu.vdi image:

$ qemu-img convert -O qcow2 ubuntu.vdi ubuntuqcow2.img

The qemu-img --help command shows which formats qemu-img can digest. Among them are the popular file formats such as VirtualBox (vdi ), Parallels, VMware (vmdk ), VirtualPC (vhd and vpc ), and Bochs or Images for Mac OS X (dmg ). The raw format is an uncompressed image of a hard drive that's created, for example, with dd . The qed format stands for QEMU Enhanced Disk Format, the designated successor to QCOW2 [8].

Depending on the size of the image and the performance of the computer, the conversion process can take some time. If the converted image is in QCOW2 format you can also encrypt it, as follows:

$ qemu-img convert -O qcow2 -o encryption=on ubuntu.vdi ubuntuqcow.img

Qemu-img now requests a password. The encryption itself is with AES and a 128-bit key. You only need to provide QEMU with the (encrypted) image as usual. The virtual machine then starts up, albeit with a black screen. From there, press Ctrl+Alt+2 to switch to the monitor, continue the processing with the c command, and type in the password for encrypting the hard disk.

As an alternative to encryption, QEMU can use the Zlib compression library for a QCOW2 image, as follows:

$ qemu-img convert -c -O qcow2 ubuntu.vdi ubuntuqcow.img

You pass even the compressed image as a normal image over to QEMU. Compression and encryption might save some hard disk space by creating smaller images that a stranger can't peek at, but both processes cost some computer power.

The QCOW2 format provides another handy feature: You can freeze the state of an image file. QEMU stores all subsequent changes in a second, smaller image, which can be useful if you're experimenting with new software a lot. The base system in the first image remains intact, while the second image captures all the changes. To get a fresh system, you only need to remove the second image. To use this function you first create a hard disk image as usual, as follows:

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

You start this in QEMU and install the desired operating system on it. Afterwards, stop QEMU and create a second, so-called overlay, image that captures your changes:

$ qemu-img create -f qcow2 -o backing_file=base.img overlay.img

Now when you start QEMU, you're using only overlay.img . The base.img must continue to be in the same directory, with the data contained within acting as a starting point. When you later want to discard the changes, simply delete overlay.img and start using base.img again. You can layer as many overlay images as you want on base.img If you want to transfer the state of overlay.img onto base.img , use the following simple command:

$ qemu-img commit overlay.img

Alternatively, use the commit command in the QEMU monitor. It may also be worthwhile converting a QCOW2 image into a new QCOW2 image: QEMU detects unused sectors and doesn't translate them into the target image – the resulting image file will become smaller than the original one. You can extend the method even further and embed the image files via -snapshot , as follows:

$ qemu -m 1024M -boot -snapshot base.img

QEMU now reads from the hard disk only. It saves all changes to a temporary file in the /tmp directory, which QEMU removes as soon as the virtual machine ends operation. The guest system, as with a Live CD, thus returns to its original state.

Should you doubt the integrity of the image file, the following command can help:

$ qemu-img check my_image.img

The command checks the specified image for errors. This works for QCOW2, QED and VDI formats only.

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