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 |
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 -hd
n 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.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.