This script allow you to create virtual private networks for qemu, similarly to what is done in vmware. You can see the output of ifconfig below the script.
#!/bin/sh # # This script configures private LAN networks for qemu # You can then use those virtual private networks as if they # were real network interfaces attached to your machine. # This means you can use iptables, dhcpd and the rest # to manage those networks. This also means your private # networks will be secured from each other depending on # iptables rules. # You also need to set proper routing for those networks, just # like if they were real private LANs. Hint, use something like: # $IPTABLES -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j MASQUERADE # to let your qemu VMs with private network IP addresses access # the world. If they have public IP addresses, # just route like you would do on a real LAN
# You have to be familiar with iptables and routing to use this. # Otherwise, it will be much simpler for you to just bridge your # qemu virtual machines to your real physical network interface like # shown here: # https://en.wikibooks.org/wiki/QEMU/Networking
# There is some documentation floating around on the Internet # about setting tap devices for qemu to bridge with your real # network interface, similarly to vmnet8 in vmware. # This script does something different. It doesn't touch your real # network interface at all. It sets private virtual LANs, # similarly to vmnet1, vmnet2, etc. in vmware.
# USE start/stop as parameter to bring the vmnets up and # down. Note that you won't be able to bring down the # subnets properly if any qemu virtual machine running # is using a tap device. # qemu-if start # qemu-if stop
#List the virtual network indexes you want to configure here: # networks will be named vmnet1 vmnet3 vmnet4 # tap devices will be named tap1-0, tap1-1, tap1-2... # tap3-0 tap3-1... tap4-0, tap4-1... VMNET_INDEX=(1 3 4)
# List the matching IP, in the same order, of the gateways # on the bare metal host system to access the vmnets # You need to have as many gateways as vmnets ! GATEWAYS=(192.168.2.1 192.168.3.1 192.168.4.1)
# Number of tap devices you need for each vmnet. # 10 will create 10 tap devices on each vmnet # The tap devices will be named tap1-0, tap1-1, etc. # You need at least one tap device for every virtual # machine you run with qemu # Each virtual network interface in your guest # hosts matches one to one to a tap device on your # bare metal host. # You can then start qemu like this, note "tap1-0" # below, it means that virtual machine will be # using interface 0 on vmnet1: # qemu-system-x86_64 -m 256 -display vnc=127.0.0.1:11 \ # -cdrom slackware64-14.0-install-dvd.iso \ # -net nic,macaddr=52:54:00:12:FF:57 \ # -net tap,ifname=tap1-0,script=no,downscript=no & NB_TAPS=10
# User that will be able to access the tap devices. This is # the same user that will run the qemu virtual machine # Note that it doesn't need to be root although this # script must be ran as root. # This username must exist on your system! QEMU_USER=qemu
# YOU DO NOT NEED TO EDIT BELOW ;-) # Number of subnets/gatways, this is the size of the array NB_VMNETS=${#VMNET_INDEX[@]}
if [ "$1" = "start" ] then
# # create the bridges #
let i=0 let j=0 while [ $j -lt $NB_VMNETS ] do VMNET_NAME=vmnet${VMNET_INDEX[j]} /usr/sbin/brctl addbr $VMNET_NAME while [ $i -lt $NB_TAPS ] do TAP_NAME=tap${VMNET_INDEX[j]}-${i} /usr/sbin/openvpn --mktun --dev $TAP_NAME --user $QEMU_USER /sbin/ifconfig $TAP_NAME 0.0.0.0 promisc up /usr/sbin/brctl addif $VMNET_NAME $TAP_NAME let i=$i+1 done # only a single bridge so loops are not possible, turn off spanning tree # protocol /usr/sbin/brctl stp $VMNET_NAME off /sbin/ifconfig $VMNET_NAME ${GATEWAYS[j]} let i=0 let j=$j+1 done
elif [ "$1" = "stop" ] then
let i=0 let j=0 while [ $j -lt $NB_VMNETS ] do VMNET_NAME=vmnet${VMNET_INDEX[j]} /sbin/ifconfig ${VMNET_NAME} down /usr/sbin/brctl delbr ${VMNET_NAME} while [ $i -lt $NB_TAPS ] do ##TAP_NAME=tap${VMNET_INDEX[j]}-${i} TAP_NAME=tap${VMNET_INDEX[j]}-${i} /usr/sbin/openvpn --rmtun --dev $TAP_NAME let i=$i+1 done let i=0 let j=$j+1 done
else echo Please use start/stop as parameter... fi
# end of script
Here is the output of ifconfig once the script has been ran: eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.6 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::215:f2ff:fee9:f75a prefixlen 64 scopeid 0x20<link> ether 00:15:f2:e9:f7:5a txqueuelen 1000 (Ethernet) RX packets 35689628 bytes 34266310082 (31.9 GiB) RX errors 1 dropped 0 overruns 0 frame 1 TX packets 20835127 bytes 3404148417 (3.1 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device interrupt 23
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 16436 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 1737014 bytes 248666448 (237.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1737014 bytes 248666448 (237.1 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap1-0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1500 inet6 fe80::ac65:fbff:fec9:5776 prefixlen 64 scopeid 0x20<link> ether ae:65:fb:c9:57:76 txqueuelen 100 (Ethernet) RX packets 20304 bytes 1767490 (1.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 164024 bytes 233805110 (222.9 MiB) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap1-1: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::6848:54ff:fe24:6c40 prefixlen 64 scopeid 0x20<link> ether 6a:48:54:24:6c:40 txqueuelen 100 (Ethernet) RX packets 20367 bytes 1651454 (1.5 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 90941 bytes 137343904 (130.9 MiB) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap1-2: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::7c96:5fff:fedc:7d5d prefixlen 64 scopeid 0x20<link> ether 7e:96:5f:dc:7d:5d txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 2 overruns 0 carrier 0 collisions 0
tap1-3: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::58f8:bff:fe48:4141 prefixlen 64 scopeid 0x20<link> ether 5a:f8:0b:48:41:41 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap1-4: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::889e:2aff:fea6:342c prefixlen 64 scopeid 0x20<link> ether 8a:9e:2a:a6:34:2c txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap1-5: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::4419:20ff:fedf:195b prefixlen 64 scopeid 0x20<link> ether 46:19:20:df:19:5b txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap1-6: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::10fb:84ff:fe4a:9bc3 prefixlen 64 scopeid 0x20<link> ether 12:fb:84:4a:9b:c3 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap1-7: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::7c66:56ff:feab:405a prefixlen 64 scopeid 0x20<link> ether 7e:66:56:ab:40:5a txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 2 overruns 0 carrier 0 collisions 0
tap1-8: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::4a6:28ff:fe00:9e49 prefixlen 64 scopeid 0x20<link> ether 06:a6:28:00:9e:49 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 2 overruns 0 carrier 0 collisions 0
tap1-9: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::14eb:23ff:fee4:2d99 prefixlen 64 scopeid 0x20<link> ether 16:eb:23:e4:2d:99 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap3-0: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::b03e:e8ff:fede:5caa prefixlen 64 scopeid 0x20<link> ether b2:3e:e8:de:5c:aa txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap3-1: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::88d9:3bff:fee9:c63a prefixlen 64 scopeid 0x20<link> ether 8a:d9:3b:e9:c6:3a txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap3-2: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::c423:7eff:fe43:cd7 prefixlen 64 scopeid 0x20<link> ether c6:23:7e:43:0c:d7 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap3-3: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::5c01:d5ff:fee5:dbdf prefixlen 64 scopeid 0x20<link> ether 5e:01:d5:e5:db:df txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap3-4: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::1431:b7ff:fe4d:5edc prefixlen 64 scopeid 0x20<link> ether 16:31:b7:4d:5e:dc txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap3-5: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::50c9:cbff:fe55:a5e prefixlen 64 scopeid 0x20<link> ether 52:c9:cb:55:0a:5e txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap3-6: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::c58:51ff:fee6:c8a9 prefixlen 64 scopeid 0x20<link> ether 0e:58:51:e6:c8:a9 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap3-7: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::d817:bbff:fe67:d02d prefixlen 64 scopeid 0x20<link> ether da:17:bb:67:d0:2d txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap3-8: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::a838:e1ff:fe66:a7a5 prefixlen 64 scopeid 0x20<link> ether aa:38:e1:66:a7:a5 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap3-9: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::c851:3aff:fe6a:3281 prefixlen 64 scopeid 0x20<link> ether ca:51:3a:6a:32:81 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap4-0: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::9cd5:37ff:fedf:8187 prefixlen 64 scopeid 0x20<link> ether 9e:d5:37:df:81:87 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 2 overruns 0 carrier 0 collisions 0
tap4-1: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::9:efff:fed5:82e1 prefixlen 64 scopeid 0x20<link> ether 02:09:ef:d5:82:e1 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap4-2: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::9070:30ff:fe01:7575 prefixlen 64 scopeid 0x20<link> ether 92:70:30:01:75:75 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap4-3: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::1c9c:d1ff:feec:f716 prefixlen 64 scopeid 0x20<link> ether 1e:9c:d1:ec:f7:16 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap4-4: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::92:2eff:fee9:8d5b prefixlen 64 scopeid 0x20<link> ether 02:92:2e:e9:8d:5b txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap4-5: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::1062:eff:fed0:5a26 prefixlen 64 scopeid 0x20<link> ether 12:62:0e:d0:5a:26 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap4-6: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::841a:d5ff:fe6c:867 prefixlen 64 scopeid 0x20<link> ether 86:1a:d5:6c:08:67 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 1 overruns 0 carrier 0 collisions 0
tap4-7: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::d839:42ff:fe24:474c prefixlen 64 scopeid 0x20<link> ether da:39:42:24:47:4c txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap4-8: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 inet6 fe80::809e:97ff:fedf:1d74 prefixlen 64 scopeid 0x20<link> ether 82:9e:97:df:1d:74 txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tap4-9: flags=4355<UP,BROADCAST,PROMISC,MULTICAST> mtu 1500 ether 0a:28:fa:e8:29:4d txqueuelen 100 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vmnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.2.1 netmask 255.255.255.0 broadcast 192.168.2.255 inet6 fe80::4a6:28ff:fe00:9e49 prefixlen 64 scopeid 0x20<link> ether 06:a6:28:00:9e:49 txqueuelen 0 (Ethernet) RX packets 40634 bytes 2847486 (2.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 254466 bytes 371120036 (353.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vmnet3: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.3.1 netmask 255.255.255.0 broadcast 192.168.3.255 ether 0e:58:51:e6:c8:a9 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vmnet4: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.4.1 netmask 255.255.255.0 broadcast 192.168.4.255 ether 02:09:ef:d5:82:e1 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
|