Qemu+kvm is an excellent free virtualisation solution for linux host systems. It can run any gui operating system pretty much like virtualbox or vmware.
KVM provides virtualised access to cpu and memory whereas qemu emulates other hardware components like disk drives. Together they provide a complete solution to launch a virtual machine that a user can use much like any other operating system environment.
There are gui tools like virt-manager (based on libvirt) that can be used to easily create, setup and manage virtual machines for qemu+kvm. But in this post we shall be using command line tools to boot live iso of Ubuntu inside qemu.
The command line tools are a easy way to quickly launch, test and play around with live distros.
Download Ubuntu 23.04 Live Iso
At the time of writing this article 23.04 is the latest version of Ubuntu. We can download it from the official website or command-line using the wget command. Its about 4.6 GB.
$ wget https://releases.ubuntu.com/23.04/ubuntu-23.04-desktop-amd64.iso
Once the iso is downloaded, you might want to verify the md5 or sha checksums.
Check Qemu version: Before we boot live ubuntu iso with qemu, check the qemu version on the Host OS to make sure you don't have a very old version, which might not support some of the features discussed later on in the article.
$ qemu-system-x86_64 -version QEMU emulator version 7.2.0 (Debian 1:7.2+dfsg-5ubuntu2) Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers $
Boot with Qemu
Booting with qemu/kvm virtual machine is quite simple. Just run the following command and it should launch a gui window.
$ qemu-system-x86_64 --enable-kvm -cpu host -m 4096 -boot d -cdrom ubuntu-23.04-desktop-amd64.iso
The options used are follows:
- --enable-kvm : Enables kvm acceleration if the cpu supports
- -cpu host : Enables use of host hardware cpu. Needed for "--enable-kvm" to work
- -m 4096 : Amount of ram memory in MB to allocate to the virtual machine instance
- -boot d : Device to boot from. "d" means cdrom
- -cdrom ubuntu-23.04-desktop-amd64.iso : File path of the iso image to be used as a virtual cdrom
If everything goes fine, Ubuntu should boot in live mode and look something like this.
Mouse integration: Mouse can move from host to guest automatically.
Display resolution: Supports only a few standard display resolutions. Graphics emulation is not as good as virtualbox with guest additions. I was able to use 1440x900 resolution right away, which provided adequate screen size to work with.
Networking works: Internet works and websites can be opened with firefox browser. However ping command does not seem to work.
Increase number of cpus
The command shown above, by default will use a single virtual cpu core for the guest os. To improve performance we can provide the guest os with more cpu cores, using the smp
option.
$ qemu-system-x86_64 --enable-kvm -cpu host -smp 2 -m 4096 -boot d -cdrom ubuntu-23.04-desktop-amd64.iso
If you got lots of cores, like an octacore cpu, then you might let the virtual machine access 4 or more cores. "SMP" probably stands for "Symmetric multiprocessing" system.
3D Video Acceleration and Native resolution
By default qemu provides only a bunch of standard resolutions to the guest os. It is possible to get full native resolution similar to the host os, if we enable 3d video acceleration. Qemu supports 3d acceleration with the following 2 options:
- -vga virtio :
- -display gtk,gl=on :
$ qemu-system-x86_64 --enable-kvm -cpu host -smp 4 -m 4096 -vga virtio -display gtk,gl=on -boot d -cdrom ubuntu-23.04-desktop-amd64.iso
Sometimes you can skip the "-display" option and it would still work.
The modified command would be like this:
$ qemu-system-x86_64 --enable-kvm -cpu host -smp 4 -m 4096 -vga virtio -boot d -cdrom ubuntu-23.04-desktop-amd64.iso
Now ubuntu desktop should automatically switch to maximum available screen space and adjust its resolution dynamically. The desktop should also feel a lot smoother due to better video rendering with 3d acceleration.
To check that 3d acceleration is working run the following commands in the ubuntu guest:
$ sudo apt update $ sudo apt install mesa-utils $ glxinfo | grep -i rendering direct rendering: Yes $
The "direct rendering: Yes" confirms that 3d acceleration is being used.
With the virtio vga driver enabled we can check it in the guest os like this:
ubuntu@ubuntu:~$ lsmod | grep -i virt virtio_gpu 90112 1 virtio_dma_buf 16384 1 virtio_gpu drm_shmem_helper 24576 1 virtio_gpu drm_kms_helper 249856 3 virtio_gpu drm 692224 5 drm_kms_helper,drm_shmem_helper,virtio_gpu ubuntu@ubuntu:~$
The virtio_gpu module in the list confirms its use. More details about the driver can be fetched using the modinfo command or with lspci like this:
ubuntu@ubuntu:~$ lspci -vnn | grep -i vga -A 12 00:02.0 VGA compatible controller [0300]: Red Hat, Inc. Virtio GPU [1af4:1050] (rev 01) (prog-if 00 [VGA controller]) Subsystem: Red Hat, Inc. Virtio GPU [1af4:1100] Flags: bus master, fast devsel, latency 0, IRQ 10 Memory at fe000000 (32-bit, prefetchable) [size=8M] Memory at fe800000 (64-bit, prefetchable) [size=16K] Memory at febb0000 (32-bit, non-prefetchable) [size=4K] Expansion ROM at 000c0000 [disabled] [size=128K] Capabilities: <access denied> Kernel driver in use: virtio-pci 00:03.0 Ethernet controller [0200]: Intel Corporation 82540EM Gigabit Ethernet Controller [8086:100e] (rev 03) Subsystem: Red Hat, Inc. QEMU Virtual Machine [1af4:1100] Physical Slot: 3 ubuntu@ubuntu:~$
The kernel driver indicated is virtio-pci for this display adapter.
Use Virtio VGA Device: The same 3d acceleration can be enabled by attaching the "virtio-vga" device instead of changing the -vga parameters. The command is slightly different but the result is the same as above:
$ qemu-system-x86_64 --enable-kvm -cpu host -smp 4 -m 4096 -device virtio-vga -boot d -cdrom ubuntu-23.04-desktop-amd64.iso
Other issues
Numlock inverted: I noted that the keyboard numpad Numlock got inverted inside the Qemu emulator. When the host os (kubuntu) had num lock on, qemu had it off and vice-versa.
No shared clipboard: This was kind of expected. You cannot copy paste across qemu guest os and host os like virtualbox.
NAT Network
By default the nic is in nat mode and uses an ip address like 10.0.2.15. The host os will likely be on a lan with ip address 192.168.x.x and it cannot communicate with the guest os with such a network setup.
ubuntu@ubuntu:~$ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff altname enp0s3 inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute ens3 valid_lft 85746sec preferred_lft 85746sec inet6 fec0::6f3a:e4b6:df25:e6a0/64 scope site temporary dynamic valid_lft 86227sec preferred_lft 14227sec inet6 fec0::4e4f:e64e:a57c:d84a/64 scope site dynamic mngtmpaddr noprefixroute valid_lft 86227sec preferred_lft 14227sec inet6 fe80::85dd:df9c:b734:7be0/64 scope link noprefixroute valid_lft forever preferred_lft forever ubuntu@ubuntu:~$ ubuntu@ubuntu:~$ ip route default via 10.0.2.2 dev ens3 proto dhcp src 10.0.2.15 metric 100 10.0.2.0/24 dev ens3 proto kernel scope link src 10.0.2.15 metric 100 169.254.0.0/16 dev ens3 scope link metric 1000 ubuntu@ubuntu:~$
The solution is to setup the nic in bridge mode.
Qemu vs Virtualbox
The graphics inside qemu feel slow compared to virtualbox because the latter has better gpu emulation. This however does not mean the qemu is slow.
Qemu+kvm is technically faster that virtualbox, because kvm is right inside the linux kernal and close to the cpu hardware. Moreover qemu+kvm has a much smaller footprint making it more efficient. If you need to run ubuntu server in a virtual machine, then qemu would be a better option compared to virtualbox.
However if you want to play around with gui tools and test distros for fun and practice, then go with virtualbox.
Potential Errors
If you do not use the "-cpu host" option you might get this error.
qemu-system-x86_64: warning: host doesn't support requested feature: CPUID.80000001H:ECX.svm [bit 2]
The "-cpu host" option ensures that the virtual machine gets to work with the host os cpu.
Conclusion
So that was a short guide on how to boot live linux distros like ubuntu using qemu with a simple command. The next steps would be actually create a virtual machine disk and install ubuntu inside it. We shall discuss those in upcoming articles.
Qemu is a very featureful hard emulation and virtualisation (with kvm) tool. To learn more about it check out the tutorials linked at the end of the article.
For any other questions, let us know in the comments below.
Links and Resources:
qemu command man page:
https://manpages.debian.org/testing/qemu-system-x86/qemu-system-x86_64.1.en.html
Tutorial on Qemu on Archwiki:
https://wiki.archlinux.org/title/QEMU
Tutorial on Ubuntu Discourse:
https://discourse.ubuntu.com/t/virtualisation-with-qemu/11523