Setting up QEMU for networking under FreeBSD
Categories: FreeBSD, Sysadmin.
QEMU is a wonderful tool for kernel development. The default configuration emulates a sort of virtual network with a dhcp server and everything, and even the possibility to talk to the outside as from behind a firewall... all this without root privileges.
That's great, but not really suited for development: I usually use SSH, sometimes NFS, and if I install Xorg I generally configure it to listen for foreign requests on my local network. The default configuration does not allow me to do all this since I cannot reach the virtual machine from the outside of the virtual network.
Goal
What I want is having my virtual machines feeling like physical ones, in other words reachable from anywhere on my network. I don't want to need root privileges, that is not launching QEMU as root nor using su(1), sudo(8) or something like that.
Configuring everything
Set up software network interface
Software network interfaces are provided by the tap(4) driver. If tap is not built into your kernel, load it:
# kld_load if_tap.ko
The driver provides additional sysctrls, and we have to tweak two of them. The first one will enable tap devices as soon as they are created — avoiding to ifconfig tap0 up
witch has to be run as root. The second one allows regular users to access tap devices (think of the vfs.usermount sysctl):
# sysctl net.link.tap.up_on_open=1 # sysctl net.link.tap.user_open=1
Set up a bridge
The virtual network is ready to be setup by QEMU (at startup, the program will trigger the creation of the virtual network). Let's bridge it with the physical one!
Bridging is provided by if_bridge(4). Load it if it is not built into your kernel:
# kldload if_bridge.ko
We then have to configure it and enable it using sysctls:
# sysctl net.link.ether.bridge_cfg=rl0,tap0 # sysctl net.link.ether.bridge.enable=1
Of course, you will have to replace rl0 with the name of your physical network interface
Start QEMU
In order to use the virtual network we have set up, we only have to add -net nic -net tap
to QEMU arguments.
Sources
What is decribed here is a mix between the two following articles: