]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Write network config to disk after generating UUID
authorAndrea Bolognani <abologna@redhat.com>
Thu, 19 Nov 2020 15:08:13 +0000 (16:08 +0100)
committerAndrea Bolognani <abologna@redhat.com>
Fri, 20 Nov 2020 10:43:56 +0000 (11:43 +0100)
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@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
src/conf/virnetworkobj.c

index 1b29d7cc64af5bd9a97d111cd43e08afdd3353e3..1e167c78741f534f9d7bd7812b352b37f6879684 100644 (file)
@@ -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;