Wednesday, January 26, 2011

Qemu networking with TUN/TAP and forwarding.

Task definition.
Have: Linux desktop with (routed) internet connection.
Want: run (Qemu) KVM with network access from vm to desktop and internet and network access from desktop to vm.

Pre-word.
There is a lot of step-by-step recipes reg. this in the web. The problem is that I hate to use recipes I don't understand. One step aside - and everything break and you don't know what to do. Therefore I'm going to configure network manually. This note is mostly for me - to remember required steps.

Solution.

On the host
1. Create tap device.
host# tunctl -u -g kvm
There is almost no documentation reg. tunctl. This spell had been composed "by example". After this command you must see new network interface tap0 by issuing "ifconfig -a".

2. allow NAT in iptables.
host# iptables -A POSTROUTING -o eth0 -j MASQUERADE
where is network card with WAN connection.

3. allow packet forwarding. Add to /etc/sysctl.conf:
net.ipv4.ip_forward=1

4. activate tap interface before setup routing:
host# ifconfig tap0 0.0.0.0 promisc up

5. to allow connection from host to vm, we need define routing *on the host*:
host# route add -host 192.168.1.2 dev tap0
where 192.168.1.2 is IP address of vm.

On the vm

6. my host IP and my vm IP are in different subnets, therefore I need to define routing to host first:
vm# route add -host dev eth0
I'm able to PING host by IP from vm after this step.

7. and then default gateway:
vm# route add default gw
and I'm able to PING other machines by IP after this step.

8. Now define nameserver in /etc/resolv.conf. The same as on host.

No comments: