Hey,
for a project I want to host a QEMU guest on a x86 device running OpenWrt (maybe I'll write a more detailed post about what I'm doing later). Previously I often had problems setting up the network between guest and host, but I realized that it's really easy with OpenWrt and qemu-bridge-helper and I wanted to share a quick recipe.
Using that, you can connect the guest to every bridge network of your OpenWrt host (every Interface with option type 'bridge'
).
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.
If you install these packages via opkg, reboot afterwards.
Now you're ready to run QEMU guests. In my example the guest is a Debian Stretch installation on /dev/sda
, but you can use an image as well. Just use a distribution that comes with virtio drivers by default.
The complete command looks like this:
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 it to connect to the bridge network br-lan
of the OpenWrt host.
That's it, if your guest uses DHCP it should receive an address by the server running in br-lan
.
To automatically start the VM at boot and shutting it down cleanly before a reboot, I created this init script:
#!/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
}
It uses QMP to initiate the VM shutdown.
I didn't run a benchmark yet, but the guest is performing really well so far.