Networking with qemu
I’ve to setup a virtual machine for networking testing purpose. Because i spent a lot of time on it, i wrote here a little tutorial on how to setup a qemu virtual machine and use networking via a tun/tap device. So, let’s start.
First of all, you have to
# aptitude install qemu
Or compile it from source. If you don’t have a tool like apt, download latest version of qemu from
http://fabrice.bellard.free.fr/qemu/
and then, as usual
$ ./configure
$ make
$ make install
Ok, now you’ve correctly installed qemu. Time to create an image of a disk. You can do this simply by
$ qemu-img create imagename.img 1G
If you need more than 1G of disk-space, just change it
Download a GNU/Linus iso, i reccomend
$ wget http://cdimage.debian.org/debian-cd/3.1_r2/i386/iso-cd/debian-31r2-i386-netinst.iso
and then install it with
$ qemu -cdrom isoname.iso -boot d imagename.img
So, after you can boot your brand new virtual machine with
$ qemu imagename.img
And that’s all. By now, you don’t have a public IP, but you’re tunneled with the host network-interface. That is (was, maybe) a big problem if you have to do some networking testing like me. So, here’s the solution.
First of all,
# aptitude install bridge-utils
In /etc/network/interfaces add the following lines
# The bridge network interface(s)
auto br0
iface br0 inet static
address 192.168.1.123 # Change with an IP from your subnet
network 192.168.1.0 # Change that with yout subnet
netmask 255.255.255.0 # Same as above
broadcast 192.168.1.255 # Same as above
gateway 192.168.1.1 # Your gateway
bridge_ports eth0 # This is your host interface
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off
And add also a file called /etc/qemu-ifup like this:
#!/bin/sh
echo “Executing /etc/qemu-ifup”
echo “Bringing up $1 for bridged mode…”
sudo /sbin/ifconfig $1 0.0.0.0 promisc up
echo “Adding $1 to br0…”
sudo /usr/sbin/brctl addif br0 $1
sleep 2
Ok, last thing to do is to create a file (i placed mine in /usr/local/bin) like this:
#!/bin/sh
ARGS=”-hda /path/to/imagename.img -boot c -net nic -net tap,vlan=0,script=/etc/qemu-ifup -m 256 -localtime”
sudo modprobe kqemu # Comment out if you don’t have kqemu kernel module
exec qemu $ARGS
And that’s all! Now you have on your host machine a new network interface called tap0 and one called br0, and on the virtual machine just eth0.



