Sungju's Slow Life

Personal journal


Configuring a Bridge Interface for the KVM-based virtual machines

If you are going to run virtualized host as a web server or some services for the clients, the systems should be accessible from the outside. Unfortunately, the default network configuration in KVM is NAT (Network Address Translation). It is good for some VMs who will co-work with other VMs, but for the server, it can be a big obstacle.

The bridged network is a solution for that circumstances. Bridged network allows VMs to have their own IP addresses which are accessible from the outside.

I’ll show how you can create bridged network in here with one physical network device – eth0. In ethernet environment, you can use ‘brctl’ command to administrate bridged network.

[root@dhcp-160-193 ~]# brctl show
bridge name	bridge id		STP enabled	interfaces

Before you create bridge, you should make a backup of the files which will be used in this example.

[root@dhcp-165-234 ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
[root@dhcp-165-234 ~]# cd /etc/sysconfig/network-scripts/
[root@dhcp-165-234 network-scripts]# ls ifcfg-*
ifcfg-eth0  ifcfg-lo
[root@dhcp-165-234 network-scripts]# mkdir backup
[root@dhcp-165-234 network-scripts]# cp ifcfg-* backup

Dump current eth0 configuration using virsh command.

[root@dhcp-165-234 network-scripts]# virsh iface-dumpxml eth0

  
  
  
    
  

[root@dhcp-165-234 network-scripts]# virsh iface-dumpxml eth0 > bakup/eth0.xml

It will save your eth0’s configuration into backup/eth0.xml file. Next thing you need to is create XML file for bridge using the saved eth0.xml. Following is the br0.xml I created from eth0.xml.


  
  
    
  

  
    
      
    
  

Next thing to do is define the bridge interface to libvirt as shown below.

[root@dhcp-165-234 network-scripts]# virsh iface-define br0.xml
error: Failed to define interface from br0.xml
error: invalid argument in virGetInterface

It will show error messages but it’s OK at this point. Next step is to configure ifcfg-eth0 and create ifcfg-br0 interface. Following is my configuration.

[root@dhcp-160-181 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
HWADDR=00:21:9B:04:05:99
ONBOOT=yes
BRIDGE=br0
[root@dhcp-160-181 network-scripts]# cat ifcfg-br0
DEVICE=br0
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=dhcp
PEERDNS=yes

Next step is restart network service. You also need to stop NetworkManager service.

[root@dhcp-160-181 network-scripts]# service network restart
Shutting down interface eth0:  bridge br0 does not exist!
                                                           [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Disabling IPv4 packet forwarding:  net.ipv4.ip_forward = 0
                                                           [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
Bringing up interface br0:                                 [  OK  ]

Now, br0 is activated.

[root@dhcp-160-181 network-scripts]# virsh iface-list
Name                 State      MAC Address
--------------------------------------------
br0                  active     00:21:9b:04:05:99
lo                   active     00:00:00:00:00:00

[root@dhcp-160-181 network-scripts]# ifconfig 
br0       Link encap:Ethernet  HWaddr 00:21:9B:04:05:99  
          inet addr:100.20.50.181  Bcast:100.20.50.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1021814 errors:0 dropped:0 overruns:0 frame:0
          TX packets:583293 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:373480692 (356.1 MiB)  TX bytes:170390832 (162.4 MiB)

eth0      Link encap:Ethernet  HWaddr 00:21:9B:04:05:99  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1812236 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1342244 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:666795722 (635.9 MiB)  TX bytes:263334525 (251.1 MiB)
          Memory:fdfc0000-fdfe0000 
...

Now you are ready to use bridge interface in VMs. If you have a guest already, you only need to change the interface part as shown below. You don’t need to change mac address of course. 🙂

[root@dhcp-160-181 networks]# virsh dumpxml win2003 | grep -A 3 interface
    
      
      
      
    

If you are creating new virtual machines, your only job is to choose ‘eth0 (Bridge br0)’ in Network configuration step.

Unfortunately, at this time I write this, RHEL5 does not have some features in libvirt so not working well. I tested this on Fedora 12 and some other useful links when you use RHEL5.4 instead of Fedora 12 are http://www.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5.4/html/Virtualization_Guide/sect-Virtualization-Network_Configuration-Bridged_networking_with_libvirt.html and http://benincosa.org/blog/?p=93.



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: