This is an old revision of the document!
Running QEMU guests on OpenWrt
Introduction
It's possible to use OpenWrt as a QEMU host and run guests on it. If you want to run OpenWrt as a QEMU guest, see OpenWrt in QEMU.
OpenWrt provides QEMU packages for ARM and x86 platforms. This article focuses on the x86 target, the networking is done via qemu-bridge-helper
Installing QEMU
You need the following packages on your device: kmod-tun qemu-bridge-helper qemu-x86_64-softmmu. If your hardware supports it, also install kmod-kvm-amd or kmod-kvm-intel for better performance.
Example for an Intel system:
opkg install kmod-tun qemu-bridge-helper qemu-x86_64-softmmu kmod-kvm-intel
After the first installation, reboot your device.
Running a guest
For the guest operation system, use a distribution that comes with virtio drivers by default (Debian or Fedora for example). There are several guides available on how to install a Linux distribution inside a QEMU image: Debian
The following QEMU command uses a physical disk, but you can use a qcow2 image as well. Below are explanations of the networking options.
qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 2G \ -drive file=/dev/sda,cache=none,if=virtio,format=raw \ -device virtio-net-pci,mac=E2:F2:6A:01:9D:C9,netdev=br0 \ -netdev bridge,br=br-lan,id=br0
Line 3 creates a virtio net device with a fixed MAC address and is told to use the network backend with id=br0, which gets created in line 4. The network backend is of type bridge and br=br-lan tells QEMU to connect to the bridge network br-lan of the OpenWrt host. For that qemu-bridge-helper will automatically create the needed TUN/TAP interfaces.
If your guest uses DHCP it should now receive an address by the server running in br-lan.
You can choose any other bridge interface available on your OpenWrt host as well. See Network basics on how to configure networking interfaces.
Init script
To automatically start the VM at boot and shut it down cleanly using QMP, create an init script like the one below.
Be careful with copying the heredoc part. This code block is left unindented on purpose to avoid problems with whitespaces. If you want, you can indent the lines with tabs and use <<- instead of <<, but since tabs are printed as whitespaces in dokuwiki code blocks this example is used without indentation. You can read more about here documents in the advanced bash-scripting guide
- /etc/init.d/qemu
#!/bin/sh /etc/rc.common START=99 STOP=1 start() { qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 2G \ -drive file=/dev/sda,cache=none,if=virtio,format=raw \ -device virtio-net-pci,mac=E2:F2:6A:01:9D:C9,netdev=br0 \ -netdev bridge,br=br-lan,id=br0 \ -qmp tcp:127.0.0.1:4444,server,nowait \ -daemonize &> /var/log/qemu.log } stop() { nc localhost 4444 <<QMP { "execute": "qmp_capabilities" } { "execute": "system_powerdown" } QMP sleep 10s }
This script currently uses a fixed time of 10 seconds to wait for the VM shutdown. Provide a better way to wait for the qemu process to finish
Test the the script by running /etc/init.d/qemu start and look for errors in /var/log/qemu.log.
If the script works as desired, enable it for every boot: /etc/init.d/qemu enable