Oct 24
kvm/qemu bridging with dummy network card
I wanted to setup some VMs using kvm (i had the same problem using xen). The solution a chose to connect my VMs to the internet was to do it through bridging.
My only problem is that my eth0 (main and only ethernet card is connected straight to the Internet. So bridging loses its meaning because I could not choose new IP addresses for my virtual boxes.
The solution: creating a dummy network interface!
With this scenario, i’ll have all my dumUs bridged to the dummy interface (dummy0) and the bridge (br0) NATed through eth0. I hope I’m clear enough. : )
Here is how my /etc/network/interfaces file looks like:
auto lo
iface lo inet loopback# The bridge network interface(s)
auto br0
iface br0 inet static
bridge_ports dummy0
bridge_maxwait 0
address 10.1.1.1
netmask 255.255.255.0
auto eth0
iface eth0 inet dhcp
Now restart your networking (sudo /etc/init.d/networking restart)
The outout of your ifconfig should look like this:
br0 Link encap:Ethernet HWaddr D2:EA:4F:D1:B9:76
inet addr:10.1.1.1 Bcast:10.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::d0ea:4fff:fed1:b976/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:272660 errors:0 dropped:0 overruns:0 frame:0
TX packets:495913 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:14516784 (13.8 MB) TX bytes:736380564 (702.2 MB)
dummy0 Link encap:Ethernet HWaddr D2:EA:4F:D1:B9:76
inet6 addr: fe80::d0ea:4fff:fed1:b976/64 Scope:Link
UP BROADCAST RUNNING NOARP MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:217 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:31032 (30.3 KB)
eth0 Link encap:Ethernet HWaddr 00:30:1B:BC:17:38
inet addr:XXX.XXX.XXX.XXX Bcast:81.67.67.255 Mask:255.255.255.0
inet6 addr: fe80::230:1bff:febc:1738/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:651487 errors:0 dropped:0 overruns:0 frame:0
TX packets:391485 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:866501509 (826.3 MB) TX bytes:32927949 (31.4 MB)
Interrupt:16
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:161 errors:0 dropped:0 overruns:0 frame:0
TX packets:161 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:33971 (33.1 KB) TX bytes:33971 (33.1 KB)
The ip of eth0 (XXX.XXX.XXX.XXX) is supposed to be your PUBLIC IP.
Now let’s create the script that will be used for the bridging when lauching your virtual machines: qemu-ifup
#!/bin/sh BRIDGE=br0 ifconfig $1 0.0.0.0 up brctl addif $BRIDGE $1
To NAT br0 behind eth0, this is magic!
sudo /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo sysctl -w net.ipv4.ip_forward=1
It is now time to setup our virtual machines using kvm and qemu.
We first create the harddisk image using qemu:
qemu-img create your_OS.img -f qcow 6G
Launch the installation of the OS using your CDROM:
kvm -no-acpi -m 512 -net nic -net tap,ifname=tap0,script=/path/to//qemu-ifup -cdrom /dev/cdrom -boot d your_OS.img
Launch the installation of the OS using an iso file:
kvm -no-acpi -m 512 -net nic -net tap,ifname=tap0,script=/path/to//qemu-ifup -cdrom /path/to/install_image.iso -boot d your_OS.img
Install your OS. If your are asked to define an IP, define an unused IP address neighboring the one defined for br0 (e.g. 10.1.1.2).
To avoid entering this everytime, create a shell script:
#!/bin/sh
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sysctl -w net.ipv4.ip_forward=1
kvm /path/to/your_OS.img
-net nic
-net tap,ifname=tap0,script=/path/to/qemu-ifup
-m 512
-smp 1
-no-acpi
-localtime
et voilĂ !
Hope this helps,
cheers,
Ghantoos
Sources:
http://imil.net/wp/?p=152
http://compsoc.dur.ac.uk/~djw/qemu.html
https://help.ubuntu.com/community/KVM
