QEMU 2 as a versatile virtualization platform

QEMU Monitor – Controlling the Virtual Machine

To control your virtual machines at run time, use QEMU Monitor. The software is controlled by keyboard shortcuts and offers a versatile feature scope. You can use it to reject or replace removable media, freeze the state of a virtual machine, reactivate the machine as needed, and backup and restore states.

QEMU Monitor also lets you inspect the state of a virtual machine and migrate a virtual machine to another host. The tool even lets you modify the hardware and trigger emulated hardware errors.

After launching QEMU, press Ctrl+Alt+2 to change to QEMU Monitor. (See Table 1 for a list of useful keyboard shortcuts.) The info kvm command tells you whether KVM hardware virtualization is enabled. The typical output looks like this:

(qemu) info kvm
kvm support: enabled

Table 1

Important Keyboard Shortcuts for QEMU Monitor

Keyboard Shortcut

Action

Ctrl+Alt

Releases the mouse and keyboard.

Ctrl+Alt+1

Changes to the guest operating system's display.

Ctrl+Alt+2

Changes to console 2, QEMU Monitor.

Ctrl+Alt+3

Changes to console 3, serial output.

Ctrl+Alt+4

Changes to console 4, parallel output.

Ctrl+Alt+H

Outputs the help with the option -nographic.

Ctrl+Alt+F

Toggles between full-screen and window mode.

Ctrl+Alt+ +

Enlarges the screen output.

Ctrl+Alt+ -

Reduces the size of the screen output.

Ctrl+Alt+U

Enables the original window size.

Using the history parameter, you can output the command history:

(qemu) info history
0: 'help'
1: 'info'
2: 'info version'
3: 'info kvm'
4: 'info history'

To terminate an instance, without shutting down the guest system before doing so, simply use the quit command. This is equivalent to pressing the off button on the computer and therefore can cause loss of data:

(qemu) quit

If you want to reset a virtual machine, use the system_reset command as follows to do so:

(qemu) system_reset

The QEMU environment can also gracefully shut down the installed guest system. For this to happen, the guest needs to understand and interpret ACPI commands. The simulator uses the system_powerdown command to send an appropriate ACPI signal to the guest. In the case of a Linux guest system, you need to install the acpid package:

(qemu) system_powerdown

To pause an instance, use the stop command. You can look at the status of a (paused) instance by using the info status command, as shown in the following example:

(qemu) stop
(qemu) info status
VM status: paused

To run the instance again, use the cont option. Again, you can verify the status with info status:

(qemu) cont
(qemu) info status
VM status: running

QEMU has a very practical protection mechanism that fields various keyboard combinations instead of passing them through to the guest system. This is true, for example, of the keyboard combination Ctrl+Alt+Del. QEMU Monitor offers you an option for passing through these commands, though. For example, if you want to pass through the combination mentioned above, the following input will do the trick:

(qemu) sendkey ctrl-alt-delete

If you want to create screenshots of the guest, the screendump command will help. This command creates a PPM file:

(qemu) screendump screenshot.ppm

To eject a medium from the CD-ROM drive, use:

(qemu) eject cdrom

If you need more options, you can type help to view the complete list of available commands for QEMU Monitor.

Accessing Storage Media

For a QEMU 2 instance to be able to access storage media, a medium first must be registered in the virtualization environment. To access media that already exist and query their status, you can use the info block command in QEMU Monitor. This lists the names and states of the storage media. If the input contains notes to the effect of ide-hd and ide-cd or scsi-hd and scsi-cd, this means that you are emulating IDE and SCSI hard disks and DVD/CD drives.

To access a virtual hard disk, you can use the options -hda <file>, -hdb <file>, -hdc <file>, -hdd <file>, and -drive, where <file> typically refers to an image. For example:

qemu-system-x86_64 -hda Hard_disc1.img -hdb Hard_disc2.img

If you are only using one virtual hard disk, you can dispense with the -hdn option:

qemu-system-x86_64 Hard_disc1.img

To access a (virtual) CD/DVD drive, use the options -cdrom <file> and -drive, where <file> refers to an image file or a physical device:

qemu-system-x86_64 -hda disc.img -cdrom cd.iso

If the host system has a CD drive, you can pass it through to the guest. If the host is a Linux system, the drive will be, for example, /dev/cdrom or /dev/dvd. To boot from the CD drive in this scenario, use the following command:

qemu-system-x86_64 -cdrom /dev/dvd

If you are using a Windows system as the host, you need the drive letter to integrate the physical drive. For example, the following command boots a CD/DVD from drive C:

qemu-system-x86_64 -L . -cdrom c:

You can also protect the storage media you use against changes. To do so, use the -snapshot option, which ensures the changes are not written to the storage medium itself but to temporary files. However, note that any changes you make are lost when you terminate the virtual machine:

qemu-system-x86_64 disc.img -snapshot

The info block commands tells QEMU Monitor to show you the temporary files:

(qemu) info block
ide0-hd0: removable=0 io-status=ok file=/tmp/a2.2B4u3P backing_file=disc.img ro=0 drv=qcow2 encrypted=0

To store changes on the storage medium, use Ctrl+Alt+S. You can also use the commit command to store the changes in QEMU Monitor. The additional all parameter stores the changes on all connected drives:

(qemu) commit all

Under normal circumstances, you will typically want to write content to one drive in a targeted way. To save the changes on your first hard disk, run:

(qemu) commit hda

Because running the commit command is typically time consuming – the guest system is frozen to do this – you can use the no-shutdown option instead of the -snapshot option.

QEMU and KVM are capable not only of creating new virtual storage media but also of converting storage media for other virtualization programs, such as VMware. The qemu-img tool is used for this purpose; it supports all relevant image formats such as RAW (the default format), VMDK (VMware), VDI (VirtualBox), DMG (Mac image file), HDD (Parallels), and many more.

Storing Your Own Images

You can use QEMU for more than just executing virtual systems; it can also create an image – and in all of the popular formats. For example, you can quickly and easily create an image file and then provide it to third parties.

To do this, QEMU again uses the integrated command-line tool qemu-img. The create parameter lets you create your own image files. You need to specify the file name of the image file and the virtual size, as in the following example:

qemu-img create own_image.img 1G
Formating 'own_Image.img', fmt=raw, size=1048576 kB

After creating an initial image, you can use the info parameter to access the data for the image file:

qemu-img info own_image.img
image: disc.img
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 0

If you just want to exchange the image between QEMU installations, your best bet is to use the qed and qcow2 formats. In particular, qed is optimized for fast access.

Besides letting you create images, qemu-img can also convert, compress, encrypt, and resize these images. To convert an image file to a different format, you would use convert; the -O parameter defines the target format. The conversion mechanism typically identifies the original format, but you can also state it explicitly with the -f parameter. If you want to convert a virtual hard disk in raw to the qcow2 format, use the following command:

qemu-img convert -f raw -O qcow2 source-image.img target-Image.img

The qemu-img command also lets you encrypt an image. However, this only works if you use the image formats qcow and qcow2, as in the following:

Host ~$ qemu-img convert -O qcow2 -o encryption original-image.img encrypted_image.img
Disk image 'encrypted_image.img' is encrypted.
password: ********

QEMU uses a 128-bit AES encryption key.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

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

US / Canada

Get it on Google Play

UK / Australia

Related content

  • KVM

    KVM brings the kernel into the virualization game. We’ll explain why the Linux world is so interested in this promising virtualization alternative.

  • QEMU and Qemu Manager

    Carry a virtual Linux machine with you wherever you go.

  • QEMU System Emulation

    Do you ever wish you could run Linux within Linux? Or how about DOS within Linux? QEMU is an open source application that lets you emulate a complete hardware environment within your Linux system.

  • Qemu Flaw Lets the Guest Escape

    Xen project announces a privilege escalation problem for Qemu host systems

  • KVM Front Ends

    If you want to care for a zoo full of exotic KVM guest systems on your desktop, you could use a little help from a graphical front end.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News