[libvirt] [PATCH 2/2] network: check for bridge name conflict with existing devices

Laine Stump laine at laine.org
Thu Apr 23 19:03:20 UTC 2015


Since some people use the same naming convention as libvirt for bridge
devices they create outside the context of libvirt, it is much nicer
if we check for those devices when looking for a bridge device name to
auto-assign to a new network.
---
 src/network/bridge_driver.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index abacae3..3b879cd 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -2037,11 +2037,13 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
 
     /* Create and configure the bridge device */
     if (!network->def->bridge) {
-        /* this can only happen if the config files were edited
-         * directly. Otherwise networkValidate() (called after parsing
-         * the XML from networkCreateXML() and networkDefine())
-         * guarantees we will have a valid bridge name before this
-         * point.
+        /* bridge name can only be empty if the config files were
+         * edited directly. Otherwise networkValidate() (called after
+         * parsing the XML from networkCreateXML() and
+         * networkDefine()) guarantees we will have a valid bridge
+         * name before this point. Since hand editing of the config
+         * files is explicitly prohibited we can, with clear
+         * conscience, log an error and fail at this point.
          */
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("network '%s' has no bridge name defined"),
@@ -2762,8 +2764,9 @@ static int networkIsPersistent(virNetworkPtr net)
 
 /*
  * networkFindUnusedBridgeName() - try to find a bridge name that is
- * unused by the currently configured libvirt networks, and set this
- * network's name to that new name.
+ * unused by the currently configured libvirt networks, as well as by
+ * the host system itself (possibly created by someone/something other
+ * than libvirt). Set this network's name to that new name.
  */
 static int
 networkFindUnusedBridgeName(virNetworkObjListPtr nets,
@@ -2777,7 +2780,13 @@ networkFindUnusedBridgeName(virNetworkObjListPtr nets,
     do {
         if (virAsprintf(&newname, templ, id) < 0)
             goto cleanup;
-        if (!virNetworkBridgeInUse(nets, newname, def->name)) {
+        /* check if this name is used in another libvirt network or
+         * there is an existing device with that name. ignore errors
+         * from virNetDevExists(), just in case it isn't implemented
+         * on this platform (probably impossible).
+         */
+        if (!(virNetworkBridgeInUse(nets, newname, def->name) ||
+              virNetDevExists(newname) == 1)) {
             VIR_FREE(def->bridge); /*could contain template */
             def->bridge = newname;
             ret = 0;
-- 
2.1.0




More information about the libvir-list mailing list