You are not logged in.

#1 2016-01-22 09:07:40

e_tank
Member
Registered: 2006-12-21
Posts: 80

[SOLVED] qemu vm tap networking with internet via host wifi

after hours tinkering i finally figured out how to setup my qemu win 7 vm with tap device networking.  the host machine is connected to the internet via wifi (wlan0 in my case) and it shares this connection with the vm.  thought i'd post the exact commands i used to get this up and running in order to save others the time and trouble.

i found the qemu arch wiki page really wasn't entirely clear on how to do this, especially for ppl like me who don't have a firm grasp on networking concepts and who's eyes tend to gloss over when reading up on the subject.

i'm still not entirely sure why it works, for instance why must i create a bridge (which iirc is like a virtual switch) and a tap device (which iirc is like a virtual ethernet port)?  why not just a tap device?

i originally attempted to do this with just a tap device much in the same way you'd hook 2 machines up via cross over ethernet cable, but that didn't seem to really work right.  although i was able to gain internet access on the guest, windows bitched about the connection being problematic and svchost.exe got stuck at 100% cpu usage (though that latter could've just been a coincidence, windows being windows and all..), so i decided to give up on that approach.

anyway, below is the list of commands i've been using (execute with root privileges).  dnsmasq is required, but that's only b/c i'm lazy and don't feel like setting the ip link info manually on the guest.  also, make sure to change wlan0 to whatever interface device name you use to connect to the internet with.

ip link add name br0 type bridge
ip addr add 172.20.0.1/16 dev br0
ip link set br0 up
dnsmasq --interface=br0 --bind-interfaces --dhcp-range=172.20.0.2,172.20.255.254

modprobe tun

[[ ! -d /etc/qemu ]] && mkdir /etc/qemu
echo allow br0 > /etc/qemu/bridge.conf

sysctl net.ipv4.ip_forward=1
sysctl net.ipv6.conf.default.forwarding=1
sysctl net.ipv6.conf.all.forwarding=1

iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i tap0 -o wlan0 -j ACCEPT

# now run (as your user):
# $ qemu-system-x86_64 ... -net nic,model=virtio -net bridge,br=br0

the above implicitly makes use of qemu-bridge-helper to automatically bring up a tap device that's assumed will be named tap0 (maybe i could change the firewall rule and replace tap0 with br0 to avoid making this assumption?  idk and i don't feel like experimenting anymore..).  however if you want to manually create the tap device yourself i can confirm that the following will accomplish the same thing as the above:

ip link add name br0 type bridge
ip addr add 172.20.0.1/16 dev br0
ip link set br0 up
dnsmasq --interface=br0 --bind-interfaces --dhcp-range=172.20.0.2,172.20.255.254

modprobe tun
ip tuntap add dev tap0 mode tap user "YOUR_USER_NAME_HERE"
ip link set tap0 up promisc on
ip link set tap0 master br0

sysctl net.ipv4.ip_forward=1
sysctl net.ipv6.conf.default.forwarding=1
sysctl net.ipv6.conf.all.forwarding=1

iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i tap0 -o wlan0 -j ACCEPT

# now run (as your user):
# $ qemu-system-x86_64 ... -net nic,model=virtio -net tap,ifname=tap0,script=no,downscript=no

edit: since this is a solution and not a problem post i added [SOLVED] to the posts subject text

Last edited by e_tank (2016-01-22 09:53:08)

Offline

#2 2017-10-12 15:02:56

DanielOliveira
Member
From: Minas Gerais, Brazil
Registered: 2017-10-12
Posts: 1

Re: [SOLVED] qemu vm tap networking with internet via host wifi

Thank you very much. This work perfectly for me.

Offline

#3 2017-10-12 21:13:25

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,846
Website

Re: [SOLVED] qemu vm tap networking with internet via host wifi


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

Board footer

Powered by FluxBB