]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
driver: Introduce virDriverShouldAutostart()
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 4 Oct 2019 14:57:04 +0000 (16:57 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 8 Oct 2019 14:42:19 +0000 (16:42 +0200)
Some of objects we manage can be autostarted on libvirtd startup
(e.g. domains, network, storage pools). The idea was that when
the host is started up these objects are started too without need
of user intervention. However, with the latest daemon split and
switch to socket activated, short lived daemons (we put --timeout
120 onto each daemon's command line) this doesn't do what we want
it to. The problem is not new though, we already had the session
daemon come and go and we circumvented this problem by
documenting it (see v4.10.0-92-g61b4e8aaf1). But now that we meet
the same problem at all fronts it's time to deal with it.

The solution implemented in this commit is to have a file (one
per each driver) that:

  1) if doesn't exist, is created and autostart is allowed for
     given driver,

  2) if it does exist, then autostart is suppressed for given
     driver.

All the files live in a location that doesn't survive host
reboots (/var/run/ for instance) and thus the file is
automatically not there on fresh host boot.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/driver.c
src/driver.h
src/libvirt_private.syms

index ed2d943ddf3aeb327713989d9fbd236a6a8d99dd..305c4c624b82802e18e664be3513fdc7b1f33c06 100644 (file)
@@ -29,6 +29,7 @@
 #include "virfile.h"
 #include "virlog.h"
 #include "virmodule.h"
+#include "virstring.h"
 #include "virthread.h"
 #include "configmake.h"
 
@@ -69,6 +70,44 @@ virDriverLoadModule(const char *name,
 
 /* XXX unload modules, but we can't until we can unregister libvirt drivers */
 
+/**
+ * virDriverShouldAutostart:
+ * @dir: driver's run state directory (usually /var/run/libvirt/$driver)
+ * @autostart: whether driver should initiate autostart
+ *
+ * Automatic starting of libvirt's objects (e.g. domains, networks, storage
+ * pools, etc.) doesn't play nice with using '--timeout' on daemon's command
+ * line because the objects are attempted to autostart on every start of
+ * corresponding driver/daemon. To resolve this problem, a file is created in
+ * driver's private directory (which doesn't survive host's reboot) and thus
+ * autostart is attempted only once.
+ */
+int
+virDriverShouldAutostart(const char *dir,
+                         bool *autostart)
+{
+    VIR_AUTOFREE(char *) path = NULL;
+
+    *autostart = false;
+
+    if (virAsprintf(&path, "%s/autostarted", dir) < 0)
+        return -1;
+
+    if (virFileExists(path)) {
+        VIR_DEBUG("Autostart file %s exists, skipping autostart", path);
+        return 0;
+    }
+
+    VIR_DEBUG("Autostart file %s does not exist, do autostart", path);
+    *autostart = true;
+
+    if (virFileTouch(path, 0600) < 0)
+        return -1;
+
+    return 0;
+}
+
+
 virThreadLocal connectInterface;
 virThreadLocal connectNetwork;
 virThreadLocal connectNWFilter;
index 68c0004d862f8c94f4c3d48bc0fd5f842372a831..faf4788a4f5f24a440e54fb8789d1054cfd89342 100644 (file)
@@ -114,6 +114,9 @@ int virDriverLoadModule(const char *name,
                         const char *regfunc,
                         bool required);
 
+int virDriverShouldAutostart(const char *name,
+                             bool *autostart);
+
 virConnectPtr virGetConnectInterface(void);
 virConnectPtr virGetConnectNetwork(void);
 virConnectPtr virGetConnectNWFilter(void);
index eeab820ecab00ce8a396b9222a558f0c8d6297c2..c818bc807ad81cce601928c3b382431b1eb9e58d 100644 (file)
@@ -1346,6 +1346,7 @@ virStreamClass;
 
 # driver.h
 virConnectValidateURIPath;
+virDriverShouldAutostart;
 virGetConnectInterface;
 virGetConnectNetwork;
 virGetConnectNodeDev;