[libvirt PATCH v2 1/3] conf: Write network config to disk after generating UUID

Andrea Bolognani abologna at redhat.com
Thu Nov 19 15:36:28 UTC 2020


While we generally expect libvirt objects to be defined using the
appropriate APIs, there are cases where it's reasonable for an
external entity, usually a package manager, to drop a valid
configuration file under /etc/libvirt and have libvirt take over
from there: notably, this is exactly how the default network is
handled.

For the most part, whether the configuration is saved back to disk
after being parsed by libvirt doesn't matter, because we'll end up
with the same values anyway, but an obvious exception to this is
data that gets randomly generated when not present, namely MAC
address and UUID.

Historically, both were handled by our build system, but commit
a47ae7c004e9 moved handling of the former inside libvirt proper;
this commit extends such behavior to the latter as well.

Proper error handling for the virNetworkSaveConfig() call, which
was missing until now, is introduced in the process.

Signed-off-by: Andrea Bolognani <abologna at redhat.com>
---
 src/conf/virnetworkobj.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c
index 1b29d7cc64..1e167c7874 100644
--- a/src/conf/virnetworkobj.c
+++ b/src/conf/virnetworkobj.c
@@ -1001,6 +1001,7 @@ virNetworkLoadConfig(virNetworkObjListPtr nets,
     char *configFile = NULL, *autostartLink = NULL;
     virNetworkDefPtr def = NULL;
     virNetworkObjPtr obj;
+    bool saveConfig = false;
     int autostart;
 
     if ((configFile = virNetworkConfigFile(configDir, name)) == NULL)
@@ -1029,7 +1030,10 @@ virNetworkLoadConfig(virNetworkObjListPtr nets,
     case VIR_NETWORK_FORWARD_OPEN:
         if (!def->mac_specified) {
             virNetworkSetBridgeMacAddr(def);
-            virNetworkSaveConfig(configDir, def, xmlopt);
+            /* We just generated a new MAC address, and we need to persist
+             * the configuration to disk to avoid the network getting a
+             * different one the next time the daemon is started */
+            saveConfig = true;
         }
         break;
 
@@ -1049,6 +1053,17 @@ virNetworkLoadConfig(virNetworkObjListPtr nets,
         goto error;
     }
 
+    /* The network didn't have a UUID so we generated a new one, and
+     * we need to persist the configuration to disk to avoid the network
+     * getting a different one the next time the daemon is started */
+    if (!def->uuid_specified)
+        saveConfig = true;
+
+    if (saveConfig &&
+        virNetworkSaveConfig(configDir, def, xmlopt) < 0) {
+        goto error;
+    }
+
     if (!(obj = virNetworkObjAssignDef(nets, def, 0)))
         goto error;
 
-- 
2.26.2




More information about the libvir-list mailing list