How to setup a virtual machine using QEMU

By ssmao | Linux Guru (en) | 23 Sep 2020


Configuring a virtual machine using QEMU may seem complicated, mainly due to the command line being scary, but the program proves itself to be not only easy to use but also very powerful. Here is therefore a quick start guide.

Creating the image file

The basic command looks like the following, with the -f option to tell the program the format to use. The main formats are raw and qcow2: the former behaves like a physical disk, taking up all the memory, but is faster. The second has the advantage of not occupying the entire block right away.

$ qemu-img create -f qcow2 image.img 4G

At this point you can install the operating system, showing QEMU the image file as a CD. You will probably need to increase the RAM to be assigned to the virtual machine, since the preset is 128 MB. The -hda option is used to show the system our disk file as a hard disk (hd[a-d] are available).

qemu-system-x86_64 -m 1G -hda immagine.img -cdrom live.iso

Since at the first boot the newly created disk does not contain anything good QEMU starts the virtual machine from cd-rom, while after the installation of an OS the hard disk takes the highest priority. This allows you to install an operating system on the newly created disk without worrying about the boot order.

Alternatively you can also specify the -boot once=d option to boot the first time from cd-rom and then restore the default order after a reboot.

Derived images

To test applications it can be useful to start from a stable system and create a disposable one, a bit like with non-persistent live Linux distro. By taking advantage of the copy-on-write mode you can get this benefit at a minimum cost of memory usage. The memory occupied by the new image is not the same as the original, but only the differences are saved on disk.

qemu-img create -f qcow2 -o backing_file=stable_image.img test01.img 
qemu-system-x86_64 -m 256 -hda test01.img

N.B. The stable image must be stable in the literal sense of the word, and cannot be started directly, indeed it is advisable to make it read-only, because it would corrupt all the derived images;

Improving performance

KVM

If your processor supports Intel VT-x or AMD-V, one of the two is enabled in BIOS and your kernel supports it then you can enable KVM, via the -enable-kvm option.

qemu-system-x86_64 -m 1G -hda image.img -enable-kvm

Compatibility and troubleshooting

Let's check if the processor supports it:

LC_ALL=C lscpu | grep Virtualization

If the processor is supported, remember to check that Intel VT-x/AMD-V is enabled in the BIOS to avoid unnecessary headaches.

Let's check if the kernel has the required modules available (if the output is not empty they are available):

find /lib/modules/$(uname -r) -type d | grep kvm

Let's check if the kernel modules have been loaded (if kvm and kvm_intel/kvm_amd are shown you are done):

lsmod | grep kvm

Otherwise we load the modules (if present):

sudo modprobe kvm_[intel/amd]
sudo modprobe kvm

How do you rate this article?

3


ssmao
ssmao

I love free software and all the nerdy stuff. Proudly from Italy.


Linux Guru (en)
Linux Guru (en)

Here you will find guides and nerdy stuff on Linux and the FOSS world. English version.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.