This is an old revision of the document!


Running QEMU guests on OpenWrt

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

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.

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.

To automatically start the VM at boot and shut it down cleanly using QMP, create an init script like the following:

/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
}

FIXME 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

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2018/06/03 15:45
  • by sebastian