Ubuntu Core: Preparing a virtual machine with graphics support

Key Value
Summary A guide to setting up an Ubuntu Core virtual machine with support for hardware graphics using qemu-virgil.
Categories iot
Difficulty 3
Author alan.griffiths@canonical.com

Ubuntu Core: Preparing a virtual machine with graphics support

Overview

Duration: 0:30

When developing for Ubuntu Core it is often convenient to work in a virtual machine [VM]. Unfortunately, the archive build of qemu on Ubuntu doesn’t support graphics passthrough. This is a quick guide to setting up an Ubuntu Core VM using qemu-virgil.

Setup qemu with graphics support

Duration: 1:00

As the default build of qemu on Ubuntu doesn’t support graphics, we will use a handy snap containing a build that enables this:

$ sudo snap install qemu-virgil
$ sudo snap connect qemu-virgil:kvm

Check the snap is working:

$ qemu-virgil --version

Download and prepare the Ubuntu Core image

Duration: 2:00

We also download an Ubuntu Core image. On an x86 computer download the ubuntu-core-20-amd64.img.xz image from here:

https://cdimage.ubuntu.com/ubuntu-core/20/stable/current/

Now, uncompress the image and move it to a convenient location:

$ unxz ~/Downloads/ubuntu-core-20-amd64.img.xz
$ mv ~/Downloads/ubuntu-core-20-amd64.img ~/snap/qemu-virgil/common/

Initialize a VM with the Ubuntu Core image

Duration: 4:00

$ qemu-virgil -enable-kvm -m 512 -device virtio-vga,virgl=on\
 -display sdl,gl=on -netdev user,id=ethernet.0,hostfwd=tcp::10022-:22\
 -device rtl8139,netdev=ethernet.0\
 -drive file=/snap/qemu-virgil/current/usr/share/qemu/edk2-x86_64-code.fd,if=pflash,format=raw,unit=0,readonly=on\
 ~/snap/qemu-virgil/common/ubuntu-core-20-amd64.img

This will create a QEMU window on your desktop and you need to follow through the prompts to initialise the VM with your launchpad account. (If you don’t have a launchpad account, you will need it to work with Ubuntu Core: get one and set up a public SSH key.)

Connect to the VP using ssh from a terminal window (ignore the address shown in the QEMU window and use the port set in the above command):

$ ssh -p 10022 <launchpad account>@localhost

Congratulations!

You should now have terminal access to your VM and a window containing a virtual display. You can install and run a variety of graphical snaps. You can stop the VM and restart it at any time using the same commands as in the last step.

Further reading

2 Likes

Unfortunately, I have been unable to get this to work.

qemu-virgil -enable-kvm -m 512 -device virtio-vga,virgl=on\
 -display sdl,gl=on -netdev user,id=ethernet.0,hostfwd=tcp::10022-:22\
 -device rtl8139,netdev=ethernet.0\
 -drive file=/snap/qemu-virgil/current/usr/share/qemu/edk2-x86_64-code.fd,if=pflash,format=raw,unit=0,readonly=on\
 ~/snap/qemu-virgil/common/ubuntu-core-20-amd64.img
WARNING: Image format was not specified for '/home/me/snap/qemu-virgil/common/ubuntu-core-20-amd64.img' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
Could not access KVM kernel module: No such file or directory
qemu-system-x86_64: failed to initialize KVM: No such file or directory

All files specified in that command seem to be there

Did you connect the kvm snap interface as described above? And does your host machine have /dev/kvm ?

1 Like

Based on the above this is what I get:

sudo snap install qemu-virgil
qemu-virgil 4.2.0 from Oliver Grawert (ogra) installed

sudo snap connect qemu-virgil:kvm

qemu-virgil --version
QEMU emulator version 4.2.0 (v4.2.0-dirty)
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers

So I guess kvm needs to be installed and configured. I was looking to create a VM under ESXi, but if that means creating it under kvm them exporting, I guess that’s what I’l have to do.

Well, are you running Ubuntu?

There is the kvm-ok script that should be shipped by default to check if kvm is supported by your hardware… you do/should not have to install anything, the kvm kernel module is a default of every Ubuntu kernel and should just auto-load when needed.

Could it be that virtualization is disabled in your BIOS/UEFI settings?

1 Like

Fresh copies of the Ubuntu server and desktop 22.04 running as a VM. ESXi supports nested virtualisation, so it should be no issue.

Urgh, you are right, the “Expose hardware assisted virtualization to the guest OS” was disabled within the VM. So that gets around the initial issue. Now I am seeing a permission denied issue.

sudo qemu-virgil -enable-kvm -m 512 -device virtio-vga,virgl=on -display sdl,gl=on -netdev user,id=ethernet.0,hostfwd=tcp::10022-:22 -device rtl8139,netdev=ethernet.0 -drive file=/snap/qemu-virgil/current/usr/share/qemu/edk2-x86_64-code.fd,if=pflash,format=raw,unit=0,readonly=on ~/snap/qemu-virgil/common/ubuntu-core-20-amd64.img
mkdir: cannot create directory '/run/user/0': Permission denied
qemu-system-x86_64: /home/me/snap/qemu-virgil/common/ubuntu-core-20-amd64.img: Could not open '/home/me/snap/qemu-virgil/common/ubuntu-core-20-amd64.img': Permission denied

Without sudo, a slightly differnt error, but ultimately still permission denied:

WARNING: Image format was not specified for '/home/me/snap/qemu-virgil/common/ubuntu-core-20-amd64.img' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
Could not access KVM kernel module: Permission denied
qemu-system-x86_64: failed to initialize KVM: Permission denied

do you have a /dev/kvm and does lsmod | grep kvm return anything ?

Yes, /dev/kvm exists, and the output of lsmod | grep kvm is:

lsmod | grep kvm
kvm_intel             368640  0
kvm                  1028096  1 kvm_intel

Is the device in /dev owned by the kvm group and is your user in that group?

Hey @ogra, getting there…

You are correct in that my user was not part of the kvm group (which does own /dev/kvm with read/write permissions):

ls -l /dev/kvm
crw-rw----+ 1 root kvm 10, 232 Jan 19 02:30 /dev/kvm
groups me
me : me adm cdrom sudo dip plugdev kvm lpadmin lxd sambashare

However, adding the user to the group doesn’t resolve the issue - I still see the same permission errors when running qemu-virgil with in the user or sudo context.

you should not use sudo (that might mess up some things, i have never tested it in that context), try removing the snap with the --purge option set and then re-install it (dont forget to re-connect the kvm interface) … i.e.:

$ sudo snap remove --purge qemu-virgil
...
$ sudo snap install qemu-virgil
$ sudo snap connect qemu-virgil:kvm
...

also make sure the image has proper ownership and permissions for your user … if this does not work i fear i’m running out of ideas …

Tada :tada: Superstar. I have a console spinning up right now. Lets see what else I can break :slight_smile:

1 Like

Hi,

Tutorial starts with sudo snap install qemu-virgil
Do we have to create a droplet (in DO, for example) first and then follow the tutorial?

Thanks.