KVM Network Bridge to assign Static IP

I installed Ubuntu server 14.04. and assigned static IP to it... then installed KVM and Virtual machine manager... then created a test virtual machine and it worked fine also with local IP address over NAT.

I have two LAN Card and only one is connected to internet.

What i want is configuration to bridge em2 to guest machines. So, that static ip address work also for guest machines. Right now if i assing static ip to guest machine there is no connectivity.

My ifconfig details is:

ifconfig -a

em1       Link encap:Ethernet  HWaddr 0c:c4:7a:4d:96:aa  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1  
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:1000  
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)  
          Memory:f7100000-f717ffff  

em2       Link encap:Ethernet  HWaddr 0c:c4:7a:4d:96:ab  
          inet addr:68.168.105.130  Bcast:68.168.105.255  Mask:255.255.255.0  
          inet6 addr: fe80::ec4:7aff:fe4d:96ab/64 Scope:Link  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:3717 errors:0 dropped:10 overruns:0 frame:0  
          TX packets:373 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:1000  
          RX bytes:269655 (269.6 KB)  TX bytes:239608 (239.6 KB)  
          Interrupt:20 Memory:f7200000-f7220000  

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:65536  Metric:1  
          RX packets:123 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:123 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0  
          RX bytes:8881 (8.8 KB)  TX bytes:8881 (8.8 KB)  

virbr0    Link encap:Ethernet  HWaddr 52:54:00:23:1c:65  
          inet addr:192.168.100.1  Bcast:192.168.100.255  Mask:255.255.255.0  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1  
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0  
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)  

virbr0-nic Link encap:Ethernet  HWaddr 52:54:00:23:1c:65  
          BROADCAST MULTICAST  MTU:1500  Metric:1  
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:500  
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B) 

and /etc/network/interfaces details is:

auto lo  
iface lo inet loopback  


auto em2  
iface em2 inet static  
        address 68.168.105.130  
       netmask 255.255.255.0  
       network 68.168.105.0  
       broadcast 68.168.105.255  
       gateway 68.168.105.1  
       # dns-* options are implemented by the resolvconf package, if installed  
        dns-nameservers 8.8.8.8  
        dns-search dedicated.codero.net 

Solution 1:

Step 1: Create the bridge

You probably have bridge-utils installed already, but in case you don't:

sudo apt-get install bridge-utils

In the host and as root, edit /etc/network/interfaces:

auto lo
iface lo inet loopback

auto br0
iface br0 inet static
address 68.168.105.130
netmask 255.255.255.0
network 68.168.105.0
broadcast 68.168.105.255
gateway 68.168.105.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 8.8.8.8
dns-search dedicated.codero.net 
bridge_ports em2
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off

Now you can either re-boot or:

sudo ifup br0

Reference

Step 2: Modify your VM definition to use the bridge

Note: There is probably a way to do this step using virt-manager, however I do not use it and do not know how.

Use virsh edit and change your interface definition lines to use bridging instead of what is currently being used. Here is an example from one of my VMs. Before:

<interface type='network'>
  <mac address='52:54:00:0d:ed:95'/>
  <source network='default'/>
  <model type='virtio'/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>

after:

<interface type='bridge'>
  <mac address='52:54:00:0d:ed:95'/>
  <source bridge='br0'/>
  <model type='virtio'/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>

Step 3: Specify the static IP address in the guest VM

Now, while running the guest VM, which might have network issues for the moment, edit /etc/network/interfaces and add the static definition for the interface name. For example, and guessing a little for your setup:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 68.168.105.101
netmask 255.255.255.0
network 68.168.105.0
broadcast 68.168.105.255
gateway 68.168.105.1
dns-nameservers 8.8.8.8
dns-search dedicated.codero.net 

Notes:

The default editor used by virsh edit is as defined by the $EDITOR environment variable, or vi if it does not exist. add export EDITOR="/bin/nano" to your ~/.bashrc file to set, for example, nano as your default editor.

You MUST use virsh edit do not edit the XML file directly, as virsh edit will do a bunch of checks upon save and exit. From any directory, and for example, do:

virsh edit desk_tt