Wednesday, February 13, 2008

Setting up qemu with networking on a linux host

I wanted to install Qemu with networking enabled on my linux box (Which incidentally is on a Virtual Machine built on vmware). Through the setting up process, I had a tough time figuring out how to go about with the whole thing as I could not find any single document which precisely described what I wanted. But anyways, now I've figured it all out.. And so I've decided to share whatever I've garnered with you through this post.

Objective: To set up Microsoft Windows XP on a Qemu simulated PC with an OpenSUSE 10.3 linux box serving as host.

Host machine: OpenSUSE 10.3 linux on Intel i386 based PC

Packages used:
qemu-0.9.1-i386.tar.gz : http://fabrice.bellard.free.fr/qemu/
vde2-2.1.6.tar.gz : http://vde.sourceforge.net/

I wanted to compile qemu from source, but apparently qemu needs the gcc3.x compilers whereas OpenSUSE 10.3 I have on my system has the gcc4.x version.

Step1: extract qemu-0.9.1-i386.tar.gz int '/' as root.

Step2: extract vde2-2.1.6.tar.gz into a temporary location and build it
./configure
make
and then as root

make install
Step3: (As root) set up a VDE switch with one port connected to the host using a tap interface
host$ vde_switch -s /tmp/switch -tap tap0 -m 666 [-daemon]

The "-daemon" part is option; it will cause vde_switch to run in the background, whereas, if you do not give it, you get access to the switch console
Step4: (As root) set up an ip address for the newly created tap interface on the host
host$ ifconfig tap0 10.0.0.1
Step5: set up our virtual machine. For this,
5.1 Create a working directory for the machine and copy bios files into it.
host$ mkdir qemu-vm
host$ cd qemu-vm
host$ cp /usr/local/share/qemu/{bios,vgabios-cirrus}.bin ./
5.2 Create a disk for the machine.
host$ qemu-img -f qcow win-xp.qcow 4G
5.3 Now start the virtual machine as follows, with the windows-XP CD in the /dev/cdrom drive. Have the Product key ready at hand.
host$ vdeqemu -L . -m 128 -boot d -hda win-xp.qcow \
-cdrom /dev/cdrom -soundhw all -localtime -M pc \
-net nic,vlan=0 -net vde,vlan=0,sock=/tmp/switch
here, "-m 128" gives the virtual machine 128 mb RAM
"-boot d" makes cdrom as the default boot device.. Later you can set this to "-boot c" once you have an OS installed on the hard disk, to make hard disk as the default
"-net nic,vlan=0" adds one network interface card onto the virtual machine
"-net vde,vlan=0,sock=/tmp/switch" connects the above NIC to the switch we created.

5.4 When the system boots up, install windows on this guest machine. You will notice that it has one ethernet adapter for which the cable is already plugged (to our switch as we specified).

5.5 Configure IP address on this interface so that this interface is on the same Network (subnet) as the host's tap interface. In our case, we use "10.0.0.2" as the IP. Also set the guest's default gateway as 10.0.0.1 and provide your service provider's DNS ip to the guest.
Step6: Now you can try pinging 10.0.0.1 from the guest and 10.0.0.2 from the host. You will see that the pings go through successfully. However, the guest still cannot access the internet. For this we need to set up NAT (Network Address Translation) on the host ( or some form of bridging b/w the tap0 interface and the host's eth0 interface)

Step7: (As root) Set up the NAT.
host$ echo "1" > /proc/sys/net/ipv4/ip_forward
host$ iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
host$ iptables -A FORWARD -i tap0 -o eth0 -j ACCEPT
host$ iptables -A FORWARD -i eth0 -o tap0 \
-m state --state RELATED,ESTABLISHED -j ACCEPT
Now we are up and running!!



References:
[1] http://wiki.virtualsquare.org/index.php/VDE_Basic_Networking

.

1 comment:

VarunTewari said...

Nice Job Buddy.
It would have been better, if you could provide the HowTo's of the stuff.

Configuring the VmWare for Net Connectivity has always been bothering me, and thereby barring me from using it.
Thnx a lot to you effort for the same.