KVM, adding bridges

From Ubuntuwiki.net

Jump to: navigation, search

Adding bridged network adapters to VMs in the host config

If you have already created VMs before, you can make them use bridged networking if you change the XML definition (in /etc/libvirt/qemu/) for the network interface, adjusting the mac address as desired from:

   <interface type='network'>
     <mac address='00:11:22:33:44:55'/>
     <source network='default'/>
   </interface>

to:

   <interface type='bridge'>
     <mac address='00:11:22:33:44:55'/>
     <source bridge='br0'/>
   </interface>

Note: Make sure the first octet in your MAC address is EVEN (eg. 00:) as MAC addresses with ODD first-bytes (eg. 01:) are reserved for multicast communication and can cause confusing problems for you. For instance, the guest will be able to receive ARP packets and reply to them, but the reply will confuse other machines. This is not a KVM issue, but just the way Ethernet works.

If you need to add NEW network interfaces to an existing VM, simply add a new <interface></interface> section as above - make sure the mac addresses are not identical (I typically just add one to the end of the MAC for the existing bridge; ie in the above example I would make the MAC for my second bridge 00:11:22:33:44:56.)

You do not need to restart libvirtd to reload the changes; the easiest way is to log into virsh (a command line tool to manage VMs), stop the VM, reread its configuration file, and restart the VM:

you@host:/etc/libvirt/qemu$ ls
networks  vm0.xml
you@host:/etc/libvirt/qemu$ virsh --connect qemu:///system
Connecting to uri: qemu:///system
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # list
 Id Name                 State
----------------------------------
 10 vm0                  running

virsh # shutdown vm0
Domain mirror is being shutdown

virsh # define vm0.xml
Domain vm0 defined from vm0.xml

virsh # start vm0
Domain vm0 started

The VM "vm0" is now using bridged networking.

Generating a KVM MAC

If you are managing your guests via command line, the following script might be helpful to generate a randomized MAC:

MACADDR="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"; echo $MACADDR
Personal tools