Setting up QEMU in Alpine Linux including amd64, aarch64 and usb device Passtrough

Home | Posts

Those instructions are written with alpine in mind but they should apply to most distros.

Installing the basics

First of all you need to install the Qemu packages. Qemu is split into multiple packages so you can chose whatever packages you want, for example the qemu-audio-pipewire package provides support for audio using pipewire. I recommend using something like this:
# apk add qemu qemu-img qemu-system-x86_64 qemu-ui-gtk libvirt qemu-hw-display-qxl qemu-hw-display-virtio-gpu-gl qemu-hw-display-virtio-vga qemu-hw-display-virtio-vga-gl qemu-audio-pipewire libvirt-daemon-openrc libvirt-daemon qemu-hw-usb-host qemu-system-aarch64 qemu-doc
qemu-img is used to create qemu disk images, so it's necessary, qemu-system-x86_64 is the virtualisation layer for x86_64 (amd64), there also is qemu-system-aarch64 which is the same but for aarch64 (arm64), there are many more that you can install. Qemu-ui-gtk is the gui window, there are other variants, for example qemu-ui-sdl but I find the gtk one to be the best, it is necessary uf you plan to use a graphical environment in your virtual computer. Qemu-hw-display-qxl and qemu-hw-display-virtio-gpu-gl are graphics drivers, you need the for running a graphical environment, qxl is older usually slower but is more compatible, virtio is newer and usually faster (you can even do VirGL) but is lesscompatible (doesn't work on Windows).

Just a couple of things and you can start virtualising. First you need to add tun as a kernel module, yo do this with the following commands:
# echo tun >> /etc/modules # modprobe tun Tun is a virtual network driver. After that you need to add your user to qemu and kvm group.
# addgroup kvm # adduser qemu
and last but not least, start the libvirtd service.
# rc-update add libvirtd # rc-update add libvirt-guests # rc-service libvirtd start # rc-service libvirt-guests start

Running your first VM

When you have everything installed you can finally get into running vms. What I recommend to do is to have separate folder for every vm you are going to have. First you need to download the ISO, for this example I'll me using Q4OS because I wanted to try the Trinity desktop. When you have your ISO reedy, you need to create your disk image.
qemu-img create -f qcow2 "q4os.qcow2" 15G
In this example I am creating a 15G qcow2 image named q4os.qcow2, I could also create a raw disk image using. qemu-img create -f raw "q4os.raw" 15G which I wouldn't recommend since it uses the whole 15GB as compared to qcow2 which only uses the space it needs.

now you need to start the machine. I recommend creating a script called something like run.sh which will start the vm for you. Here's my run.sh:
#!/bin/sh
qemu-system-x86_64 -m 8G\
-enable-kvm\
-cpu host -smp 6\
-vga virtio\
-usbdevice tablet\
-hda q4os.qcow2\
-cdrom q4os-5.8-x64-tde.r1.iso\
-boot d

The -m 8G flag allocates 8GB of ram to the virtual machine, -enable-kvm enables kernel virtualisation which is much faster, trough not every computer supports it. -cpu host -smp 6 makes the cpu in the machine same as my host but only with 6 logical cores. -vga virtio tells it to use the virtio graphics driver, I could also specify qxl,vmware,std or cirrus, I recommend using VirtIO for Linux and QXL for everything else. -usbdevice tablet makes you emulate a graphics tablet instead of a mouse, it fixes some mouse issues. -hda attaches a drive, -cdrom attaches a cdrom (or an iso) and -boot d makes you boot from the cd you could also do a for floppy disk and b for hard drive.
Now launch the script and install the distro as normal. Once it asks you to reboot, power off the vm and remove those lines:
-cdrom q4os-5.8-x64-tde.r1.iso\
-boot d

co boot from the disk.

Tweaks and tips

faster OpenGL

You can make open gl run faster not only by using the VirtIO driver but you can also pass the open gl calls to your host using VirGL. Do this by adding this line:
-display gtk,gl=on\

Passing trough a USB device

First, figure out which USB device you want to give to the guest os (your vm). Do this by running command lsusb the output should look like something like this:
Bus 005 Device 001: ID 1d6b:0002 Linux 6.12.25-0-lts xhci-hcd xHCI Host Controller
Bus 001 Device 001: ID 1d6b:0002 Linux 6.12.25-0-lts xhci-hcd xHCI Host Controller
Bus 003 Device 005: ID 25a7:fa7b Compx 2.4G Dual Mode Mouse

Now lets say that I want to pass trough the Compx 2.4G Dual Mode Mouse, I do this by adding the following line:
-device qemu-xhci -device usb-host,vendorid=0x25a7,productid=0xfa7b\
Do note that you need to run the VM as root and it will disconnect the device from your host.

aarch64 (ARM64) emulation

First of all you are not virtualising aarch64 but emulating it, the diference is that you can virtualise only if the architecture of your host matches the one of your guest. There are many diferences between x86_64 and aarch64, one of them being that for aarch64 the system it self matters, there is a diference between a Raspberry pi 5 and your phone in how the architecture works, unlike x64 processors from Intel and AMD which are much more similar, this is why we are specifying a cpu and a system we want to emulate. Below is my script for launching Alpine Linux in QEMU:
qemu-system-aarch64\
-M virt\
-m 8G\
-cpu cortex-a55 -smp cores=4\
-nographic \
-device ramfb \
-nic user,model=virtio \
-rtc base=utc,clock=host \
-bios /usr/share/qemu/edk2-aarch64-code.fd \
-drive format=qcow2,file="alpine.qcow2"

As you can see. You also need to specify bios file, you can ether download it online from here (you need edk2-aarch64-code.fd.bz2) or it should be included with your QEMU install. -M virt means you want to emulate a generic aarch64 machine meant for virtualisation , you could change it to for example raspi4b. -cpu is the cpu you want to emulate, -device ramfb just needs to be there, I don't know why. If you want to attach a CD rom do it as you did for x64. Another interesting thing here is -nographic which makes you only use the tty, if you want fill graphical environment use -vga virtio or qxl, if you chose to do so I also suggest you to use -usbdevice tablet.

Other sources

This article is collection of various wikis, forums and qemu docs. If you want to learn more about qemu, I recommend looking at the arch wiki. I also want to thank the alpine wiki because the install section is largely taken from there and wisp since the aarch64 script is heavily inspired from it.