I recently started playing with Qemu. One of the first things that I wanted to setup was a communication link between the host(physical machine) and guest(the vm). Qemu provides a number of techniques to achieve this. In this post I will explain about setting up network with tun/tap device.
It is easy to set it up when you know what it is. I spent couple of hours while I was fiddling with it. Read a lot of documents but dint find one which explained this completely and hence this post. I received a lot of help on the Qemu irc channel(#qemu on OFTC).
I assume that Qemu is already installed on your machine and you also have installed OS on the virtual disc. If not you can download the latest source code and install Qemu from it. Also, you can download a sample virtual disk. Once all these are ready, follow the steps below.
First, start the qemu emulator on the host machine.
$ qemu-system-x86_64 -hda cirros-0.3.0-x86_64-disk.img -netdev tap,id=net0,script=no,downscript=no -device e1000,netdev=net0
The above command will start the Qemu with KVM. The most interesting parts of the command is the -netdev option. Here, we instruct the Qemu to use tap interface to provide networking capability. Once we have started the Qemu, lets configure the host to send/receive packets. On the host, check the name of the tap device that Qemu is using to communicate the packets. It picks up the first unused device.
Once we know the name of the tap device, we should bring the interface up and also assign an IP address to it. Here, we are assuming the interface name as tap0.
Now assign IP address to the tap interface
$ ip addr add 10.1.70.1/24 broadcast + dev tap0
Once we do this, we are all set to send/receive the packets from the host. Now, we must configure the guest to send/receive the packets over the tap device. Once the guest machine is up, assign an IP address to the guest machine from the same subnet.
$ ip addr add 10.1.70.2/24 broadcast + dev eth0
Since we had already instructed Qemu to use the tap device, it will send the packets through it. Now, check for connectivity by running
The above IP address is the IP that we assigned to the tap device. The interesting thing to note here is that when we try to access the IP address of your host machine, ping command will return with network unreachable error message. The reason being that the guest machine doesn't know the route to follow to reach the host when this IP address is given. Let us add the necessary rule which would be used to send/received the packets.
$ route add default gw 10.1.70.1 dev eth0
Now, we can use the IP address of tap or host to access the host machine.
Links: