-
Notifications
You must be signed in to change notification settings - Fork 23
VM network using tap
Xiaoguang Wang edited this page Aug 10, 2020
·
3 revisions
When we try to set up the popcorn VMs on a laptop (w/ wireless NIC), the bridge does not work well on my laptop. Instead, we can create a tap net for VM communication (as well as accessing the internet).
Use the following script to create tap0
and tap1
interfaces and set up the iptable rules (replace wlp59s0
in the iptables rule with your laptop's NIC name):
sudo ip tuntap add mode tap tap0
sudo ip addr add 10.2.0.1/24 dev tap0
sudo ip link set tap0 up
sudo ip tuntap add mode tap tap1
sudo ip addr add 10.2.1.1/24 dev tap1
sudo ip link set tap1 up
sudo iptables -t nat -A POSTROUTING -o wlp59s0 -j MASQUERADE
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
Use the following scripts to launch the x86 and arm QEMU VMs (the following QEMU/VM boot scripts are slightly different from the QEMU script here and here, as we use tap net):
sudo qemu-system-x86_64 \
-enable-kvm -cpu host -smp 2 -m 4096 -no-reboot -nographic \
-drive id=root,media=disk,file=x86.img \
-net nic,macaddr=00:da:bc:de:00:13 -net tap,ifname=tap0 \
-kernel linux-x86/arch/x86/boot/bzImage \
-append "root=/dev/sda1 console=ttyS0"
sudo qemu-system-aarch64 \
-machine virt -cpu cortex-a57 -m 4096 -nographic \
-drive id=root,if=none,media=disk,file=arm.img \
-device virtio-blk-device,drive=root \
-netdev type=tap,id=net0,ifname=tap1 \
-device virtio-net-device,netdev=net0,mac=00:da:bc:de:02:11 \
-kernel linux-arm/arch/arm64/boot/Image \
-append "root=/dev/vda console=ttyAMA0"
Set the network IP and gateway in the QEMU VMs to the same IP addresses as tap0
and tap1
.
popcorn@x86 [12:44:00 PM] [~]
-> % cat /etc/network/interfaces
source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet static
address 10.2.0.100
netmask 255.255.255.0
gateway 10.2.0.1
dns-nameservers 8.8.8.8
popcorn@arm:~$ cat /etc/network/interfaces.d/eth0
allow-hotplug eth0
iface eth0 inet static
address 10.2.1.100
netmask 255.255.255.0
gateway 10.2.1.1
dns-nameservers 8.8.8.8
Now, you can access to the counterpart VM and the internet:
popcorn@x86 [12:47:34 PM] [~]
-> % ping -c 1 10.2.1.100
PING 10.2.1.100 (10.2.1.100) 56(84) bytes of data.
64 bytes from 10.2.1.100: icmp_seq=1 ttl=63 time=1.19 ms
popcorn@x86 [12:47:38 PM] [~]
-> % ping -c 1 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=52 time=33.1 ms