[libvirt] Supporting dynamic bridge names

Soren Hansen soren at ubuntu.com
Fri Apr 17 09:38:23 UTC 2009


On Fri, Apr 17, 2009 at 10:01:38AM +0100, Daniel P. Berrange wrote:
> The problem with this approach is that the bridge name potentially
> ends up being different every time the virtual network is started. The
> bridge name needs to be stable, 

Why? I can't remember ever having to refer to it, and if I did, I can
get that information at runtime by parsing the output from "virsh
net-dumpxml <name of the network>". What am I not thinking of? :)

> if (!def->bridge && !(def->bridge = virNetworkAllocateBridge(conn,
> nets)))
> 
> To check for %d, as well as NULL, and pass in the template name as your
> patch more or less does

Right, with my patch applied virNetworkAllocateBridge takes the
!def->bridge case into account.

I guess that if this is how you want it, the patch should be good as it
is. Here it is in its entirety:

Index: libvirt-0.6.1/src/network_conf.c
===================================================================
--- libvirt-0.6.1.orig/src/network_conf.c	2009-04-16 20:49:28.851655724 +0200
+++ libvirt-0.6.1/src/network_conf.c	2009-04-17 09:42:33.075084537 +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)))
@@ -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))) {
@@ -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;
     }
 
Index: libvirt-0.6.1/src/network_conf.h
===================================================================
--- libvirt-0.6.1.orig/src/network_conf.h	2009-04-16 20:49:28.899655820 +0200
+++ libvirt-0.6.1/src/network_conf.h	2009-04-17 09:42:33.075084537 +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,

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




More information about the libvir-list mailing list