Qemu networking

From openSUSE

Networking with Qemu

There are three possibilities for networking in Qemu:

  • User mode network stack
  • TAP network interfaces
  • VLANs

User mode network stack

The first is trivial and the default. It doesn't require any special kernel settings nor root privileges, but there are basically two limitations:

  • only TCP works
  • network traffic can only go from the emulated host to the network, not in the other direction

If you only want to install Windows because your tax software doesn't run on Linux, that's the way to go. But if you want to do more sophisticated things like adding the emulated host to a NIS domain, this will fail.

TAP network interfaces

This one is equivalent to the "Bridged mode" in Vmware if you ever used this, and basically in this mode your host appears in the network as new machine and you can do anything you want to do in that machine. However, you need to do changes in your Linux network setup. It's known to not work with WLAN or Network manager etc., so you've been warned. ;-)

After you installed the packages uml-utilities and bridge-utils, change the configuration of your NIC in /etc/sysconfig/network/ifcfg-eth-your:mac:address:

IPADDR='0.0.0.0'
BOOTPROTO='static'

So DHCP is disabled on the primary network interface, because DHCP is now run on the bridge. Create a new file called /etc/sysconfig/network/ifcfg-br0 or copy /etc/sysconfig/network/ifcfg.template and adjust to have following contents:

STARTMODE=auto
IFPLUGD_PRIORITY=
BOOTPROTO=dhcp
BRIDGE='yes'
BRIDGE_PORTS='eth0'
BRIDGE_AGEINGTIME=
BRIDGE_FORWARDDELAY=
BRIDGE_HELLOTIME=
BRIDGE_MAXAGE=
BRIDGE_PATHCOSTS=
BRIDGE_PORTPRIORITIES=
BRIDGE_PRIORITY=
BRIDGE_STP=

For an explanation of these variables see the comments in /etc/sysconfig/network/ifcfg.template. Because DHCP takes more time as usual on the br0 device, you should adjust the timout setting in /etc/sysconfig/network/dhcp:

DHCLIENT_WAIT_AT_BOOT="30"

Make sure you have IP forwarding enabled:

# echo "1" > /proc/sys/net/ipv4/ip_forward

Now try out your new settings by just calling

rcnetwork restart

Now create a file /usr/local/bin/qemu-tap (don't forget to make this executable) with following content:

#!/bin/sh
(lsmod | grep '\<tun\>' &>/dev/null) || sudo /sbin/modprobe tun
iface=$(sudo tunctl -b -u $UID)
qemu "$@" -net tap,ifname=$iface -net nic
sudo tunctl -d $iface &> /dev/null

After that, do the same with /etc/qemu-ifup:

#!/bin/sh
sudo /sbin/ifconfig $1 0.0.0.0 promisc up
sudo /sbin/brctl addif br0 $1 || exit 0

Now, just run

qemu-tap your parameters

to use qemu with bridged networking. Change to the Qemu monitor with Ctrl-Alt-2 and show your network settings with

(qemu) info network

If it shows something like

tap: ifname=tap10 setup_script=/etc/qemu-ifup
ne2000 pci macaddr=52:54:00:12:34:56

everything should work well. :)

References