Creating a virtual machine with QEMU
February 04, 2022 — (Last edited on February 05, 2022)
Summary
- Disclaimer
- Installing QEMU
- Creating virtual machine storage
- Running virtual machine
- Useful keybindings
- UEFI support
- 3D acceleration support
- Special thanks
Disclaimer
This guide makes assumes that:
- You’re running Arch
- You use
doas
instead ofsudo
These are far from being requirements, and are just a matter of my personal preference based on my system, you can use whatever you’re comfortable with
As a consequence, all the package names and file paths mentioned along this guide might differ, depending on your distro
Installing QEMU
doas pacman -S qemu
Creating virtual machine storage
The virtual machine storage is a file that holds all the data from a given virtual machine, run the command below to create it:
qemu-img create \
-f qcow2 \
Storage.img \
16G
- The
-f
flag represents the format that will be used for creating the storage file,qcow2
(QEMU Copy On Write 2) is recommended - You can use any name of the storage file, the extension is not a hard requirement but it’s recommended for explicitly specifying the file type
- The last parameter is the size of the storage used by the virtual machine, any
size can be passed, the suffix letter represents the measurement unit for the
storage size (for example,
M
for megabytes andG
for gigabytes)
Running virtual machine
qemu-system-x86_64 \
-enable-kvm \
-cdrom OS_ISO.iso \
-boot menu=on \
-drive file=Image.img \
-m 2G
- As the name says,
-enable-kvm
enables KVM (Kernel-based Virtual Machine), for better performance -cdrom
allows us to specify the.iso
file of the OS that will run on the virtual machine-boot
enables a boot menu-drive
allow us to specify the previously created storage file to act as the hard drive-m
sets the maximum amount of RAM that will be allocated for the virtual machine usage
Useful keybindings
Ctrl + Alt + G
- exit QEMU keyboard captureCtrl + Alt + F
- enables fullscreen mode
UEFI support
First, install the edk2-ovmf
package:
doas pacman -S edk2-ovmf
OVMF enables UEFI support for virtual machines, it contains sample UEFI firmware for QEMU and KVM
Then, run qemu
with the same parameters mentioned on the
Running virtual machine step, but this time with
the -bios
flag, example below:
qemu-system-x86_64 \
-bios /usr/share/edk2-ovmf/OVMF.fd \
-enable-kvm \
-cdrom OS_ISO.iso \
-boot menu=on \
-drive file=Image.img \
-m 2G
The -bios
flag allow us to specify a custom firmware for the virtual machine,
in this case, we are passing the path to edk2-ovmf
UEFI firmware file
3D acceleration support
In case you’re running a wayland compositor like Sway, river or Mutter, having 3D acceleration enabled is a must in order to have a working graphical environment
To enable it, simply add the -device virtio-vga-gl
flag when running the
virtual machine
Special thanks
- To DenshiVideo for his insightful video on YouTube