[libvirt] Supporting dynamic bridge names

Soren Hansen soren at ubuntu.com
Fri Apr 17 08:46:47 UTC 2009


I'm fairly sure we've discussed this before, but I couldn't find it in
my archives..

A rather long time ago (0.4.0 timeframe, I think) we switched the
default network in Ubuntu to use a bridge whose name was defined as
"virbr%d". This was done to be able to actually supply a default,
enabled network without the risk of interfering with an existing bridge
called "virbr0" (ignoring the possible implications of unconditionally
using 192.168.122.0/24 (about which, I might add, I've never had /any/
complaints)).

Now, since 0.5.0 (or thereabouts, I think), libvirt doesn't support
this, which

a) makes it rather difficult to achieve our goal of not clashing with
existing bridges named "virbr0", and
b) causes some amount of grief for updates.

To overcome this, I've changed virNetworkAllocateBridge like so:

--- libvirt-0.6.1.orig/src/network_conf.c	2009-03-03 09:23:22.000000000 +0100
+++ libvirt-0.6.1/src/network_conf.c	2009-04-16 20:36:43.660996644 +0200
@@ -875,16 +875,20 @@
 }
 
 char *virNetworkAllocateBridge(virConnectPtr conn,
-                               const virNetworkObjListPtr nets)
+                               const virNetworkObjListPtr nets,
+                               const char *template)
 {
 
     int id = 0;
     char *newname;
 
+	if (!template)
+		template = "virbr%d";
+
     do {
         char try[50];
 
-        snprintf(try, sizeof(try), "virbr%d", id);
+        snprintf(try, sizeof(try), template, id);
 
         if (!virNetworkBridgeInUse(nets, try, NULL)) {
             if (!(newname = strdup(try))) {
--- libvirt-0.6.1.orig/src/network_conf.h	2009-03-03 09:23:22.000000000 +0100
+++ libvirt-0.6.1/src/network_conf.h	2009-04-16 15:36:46.975452201 +0200
@@ -174,7 +174,8 @@
                           const char *skipname);
 
 char *virNetworkAllocateBridge(virConnectPtr conn,
-                               const virNetworkObjListPtr nets);
+                               const virNetworkObjListPtr nets,
+                               const char *template);
 
 int virNetworkSetBridgeName(virConnectPtr conn,
                             const virNetworkObjListPtr nets,


And virNetworkSetBridgeName like so:

--- libvirt-0.6.1.orig/src/network_conf.c	2009-03-03 09:23:22.000000000 +0100
+++ libvirt-0.6.1/src/network_conf.c	2009-04-16 20:36:43.660996644 +0200
@@ -909,7 +913,7 @@
 
     int ret = -1;
 
-    if (def->bridge) {
+    if (def->bridge && !strstr(def->bridge, "%d")) {
         if (virNetworkBridgeInUse(nets, def->bridge, def->name)) {
             networkReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
                                _("bridge name '%s' already in use."),
@@ -918,7 +922,7 @@
         }
     } else {
         /* Allocate a bridge name */
-        if (!(def->bridge = virNetworkAllocateBridge(conn, nets)))
+        if (!(def->bridge = virNetworkAllocateBridge(conn, nets, def->bridge)))
             goto error;
     }
 
And finally virNetworkLoadConfig like so:
--- libvirt-0.6.1.orig/src/network_conf.c	2009-03-03 09:23:22.000000000 +0100
+++ libvirt-0.6.1/src/network_conf.c	2009-04-16 20:36:43.660996644 +0200
@@ -747,7 +747,7 @@
     /* Generate a bridge if none is found, but don't check for collisions
      * if a bridge is hardcoded, so the network is at least defined
      */
-    if (!def->bridge && !(def->bridge = virNetworkAllocateBridge(conn, nets)))
+    if (!(def->bridge = virNetworkAllocateBridge(conn, nets, def->bridge)))
         goto error;
 
     if (!(net = virNetworkAssignDef(conn, nets, def)))


This is far from perfect, though.

a) As used to be the case for the VNC port, "virsh net-dumpxml" will
give you /current/ bridge name, so e.g. "virsh net-edit" will cause you
to hardcode the bridge name if you're not careful to replace the current
name with one that says '%d' somewhere. This can be fixed by e.g. either
adding a "template" or "current" attribute on the bridge element in the
network XML (depending on whether you want the "name" attribute to be
set to the current value or the template value).

b) Even if you /do/ remember to put '%d' in place of the assigned number
during editing, the bridge number will be increased, because
virNetworkBridgeInUse thinks the bridge name is in use. This should be
easy to fix, though.

I'm perfectly willing to work on this, but I'd like to get you guys' gut
feeling on this first.

-- 
Soren Hansen                 | 
Lead Virtualisation Engineer | Ubuntu Server Team
Canonical Ltd.               | http://www.ubuntu.com/




More information about the libvir-list mailing list