Skip to main content

Accelerate QEMU virtual machines in Termux with pKVM

·
Categories Smartphones Termux Tutorials
Tags Termux QEMU-KVM
Table of Contents

I discussed the problem of running virtual machines on Android phones with Termux in Termux + QEMU Linux VM. The conclusion: it is slow as hell.

Without KVM, everything depends on software emulation. The result is that you can only run 20-year-old Windows XP systems; modern operating systems are simply too heavy.

Phones equipped with Google Tensor processors introduced pKVM after Android 15, which is the technology supported underneath the AVF framework.

At the moment, except for Android 16’s experimental Debian Linux Terminal, ordinary apps cannot access pKVM. You need root privileges to enable pKVM.

1. Install packages
#

With KVM acceleration, you should run a virtual machine of the same architecture. According to Termux developers, using the qemu-system-aarch64-headless package can give you a pKVM-accelerated virtual machine.

Install the following packages. On ARM machines, it is best to enable UEFI.

pkg install qemu-system-aarch64-headless qemu-utils qemu-common ovmf sudo

There is no Virt Manager in the graphical interface, and the Libvirt service probably will not run, so we have to use AVNC to access the virtual machine display.

2. Create a Linux virtual machine in Termux
#

  1. Download the Ubuntu ARM ISO and put it in Termux’s home directory

  2. Confirm that KVM exists

sudo ls /dev/kvm
  1. Create a virtual disk
qemu-img create -f qcow2 ubuntu.qcow2 32G
  1. Create files to store UEFI
truncate -s 64m varstore.img

truncate -s 64m efi.img

dd if=$PREFIX/share/qemu/edk2-aarch64-code.fd of=efi.img conv=notrunc
  1. Add a boot script
touch startubuntu.sh

chmod +x  startubuntu.sh

vim startubuntu.sh
  1. The contents are as follows
qemu-system-aarch64 -M virt \
-enable-kvm  \
-drive if=pflash,format=raw,file="efi.img",readonly \
-drive if=pflash,format=raw,file="varstore.img" \
-smp cpus=2 \
-m 1024 \
-cpu host \
-nographic \
-netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net,netdev=n1 \
-drive file=ubuntu.qcow2 \
-boot d \
-vnc :0 \
-cdrom ubuntu-24.04.3-live-server-arm64.iso
  1. Add sudo when running it.
sudo ./startubuntu.sh

If everything goes smoothly, you can open localhost:59000 in AVNC and see the virtual machine display.

The strange thing is that it can boot without enabling KVM, but once KVM is enabled it hits the qemu-system-aarch64: Failed to put registers after init: Invalid argument error.

If you switch to chroot and run KVM inside it, maybe the success rate will go up?

References
#

Related


Thank you for reading. Public comments are not available on this website. I write to explore ideas honestly, not to chase social engagement or traffic. I would be glad to hear your thoughts after reading the article with care. If you found any errors, technical issues, or would like to share feedback, feel free to contact me via the email listed on the About page.