[libvirt] [PATCH 2/7] libvirt: pass a directory path into drivers for embedded usage

Daniel P. Berrangé berrange at redhat.com
Mon Dec 2 15:03:26 UTC 2019


The intent here is to allow the virt drivers to be run directly embedded
in an arbitrary process without interfering with libvirtd. To achieve
this they need to store all their configuration & state in a separate
directory tree from the main system or session libvirtd instances.

This can be useful for doing testing of the virt drivers in "make check"
without interfering with the user's own libvirtd instances.

It can also be used for applications using KVM/QEMU as a piece of
infrastructure to build an service, rather than for general purpose
OS hosting. A long standing example is libguestfs, which would prefer
if its temporary VMs did show up in the main libvirtd VM list, because
this confuses apps such as OpenStack Nova. A more recent example would
be Kata which is using KVM as a technology to build containers.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/driver-state.h                      |  1 +
 src/interface/interface_backend_netcf.c |  7 +++++++
 src/interface/interface_backend_udev.c  |  7 +++++++
 src/libvirt.c                           | 21 +++++++++++++++++++++
 src/libvirt_internal.h                  |  4 +++-
 src/libxl/libxl_driver.c                |  7 +++++++
 src/lxc/lxc_driver.c                    |  8 ++++++++
 src/network/bridge_driver.c             |  7 +++++++
 src/node_device/node_device_hal.c       |  7 +++++++
 src/node_device/node_device_udev.c      |  7 +++++++
 src/nwfilter/nwfilter_driver.c          |  7 +++++++
 src/qemu/qemu_driver.c                  |  7 +++++++
 src/remote/remote_daemon.c              |  1 +
 src/remote/remote_driver.c              |  1 +
 src/secret/secret_driver.c              |  7 +++++++
 src/storage/storage_driver.c            |  7 +++++++
 src/vz/vz_driver.c                      |  7 +++++++
 17 files changed, 112 insertions(+), 1 deletion(-)

diff --git a/src/driver-state.h b/src/driver-state.h
index 69e2678dfc..1e2f6ed247 100644
--- a/src/driver-state.h
+++ b/src/driver-state.h
@@ -32,6 +32,7 @@ typedef enum {
 
 typedef virDrvStateInitResult
 (*virDrvStateInitialize)(bool privileged,
+                         const char *root,
                          virStateInhibitCallback callback,
                          void *opaque);
 
diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interface_backend_netcf.c
index 4f46717cf3..951a7e6c23 100644
--- a/src/interface/interface_backend_netcf.c
+++ b/src/interface/interface_backend_netcf.c
@@ -89,9 +89,16 @@ virNetcfDriverStateDispose(void *obj)
 
 static int
 netcfStateInitialize(bool privileged,
+                     const char *root,
                      virStateInhibitCallback callback G_GNUC_UNUSED,
                      void *opaque G_GNUC_UNUSED)
 {
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (virNetcfDriverStateInitialize() < 0)
         return VIR_DRV_STATE_INIT_ERROR;
 
diff --git a/src/interface/interface_backend_udev.c b/src/interface/interface_backend_udev.c
index b7b06ed67a..a4f5deb26d 100644
--- a/src/interface/interface_backend_udev.c
+++ b/src/interface/interface_backend_udev.c
@@ -1150,11 +1150,18 @@ udevStateCleanup(void);
 
 static int
 udevStateInitialize(bool privileged,
+                    const char *root,
                     virStateInhibitCallback callback G_GNUC_UNUSED,
                     void *opaque G_GNUC_UNUSED)
 {
     int ret = VIR_DRV_STATE_INIT_ERROR;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (VIR_ALLOC(driver) < 0)
         goto cleanup;
 
diff --git a/src/libvirt.c b/src/libvirt.c
index 9d783761e6..bd2952d036 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -637,16 +637,36 @@ virRegisterStateDriver(virStateDriverPtr driver)
  * virStateInitialize:
  * @privileged: set to true if running with root privilege, false otherwise
  * @mandatory: set to true if all drivers must report success, not skipped
+ * @root: directory to use for embedded mode
  * @callback: callback to invoke to inhibit shutdown of the daemon
  * @opaque: data to pass to @callback
  *
  * Initialize all virtualization drivers.
  *
+ * Passing a non-NULL @root instructs the driver to run in embedded mode.
+ * Instead of using the compile time $prefix as the basis for directory
+ * paths, @root should be used instead. In addition any '/libvirt'
+ * component of the paths should be stripped.
+ *
+ * eg consider a build with prefix=/usr/local. A driver might use the
+ * locations
+ *
+ *    /usr/local/etc/libvirt/$DRIVER/
+ *    /usr/local/var/lib/libvirt/$DRIVER/
+ *    /usr/local/run/libvirt/$DRIVER/
+ *
+ * When run with @root, the locations should instead be
+ *
+ *    @root/etc/$DRIVER/
+ *    @root/var/lib/$DRIVER/
+ *    @root/run/$DRIVER/
+ *
  * Returns 0 if all succeed, -1 upon any failure.
  */
 int
 virStateInitialize(bool privileged,
                    bool mandatory,
+                   const char *root,
                    virStateInhibitCallback callback,
                    void *opaque)
 {
@@ -661,6 +681,7 @@ virStateInitialize(bool privileged,
             VIR_DEBUG("Running global init for %s state driver",
                       virStateDriverTab[i]->name);
             ret = virStateDriverTab[i]->stateInitialize(privileged,
+                                                        root,
                                                         callback,
                                                         opaque);
             VIR_DEBUG("State init result %d (mandatory=%d)", ret, mandatory);
diff --git a/src/libvirt_internal.h b/src/libvirt_internal.h
index 4a74dbc2af..00ef7aaf25 100644
--- a/src/libvirt_internal.h
+++ b/src/libvirt_internal.h
@@ -31,8 +31,10 @@ typedef void (*virStateInhibitCallback)(bool inhibit,
 
 int virStateInitialize(bool privileged,
                        bool mandatory,
+                       const char *root,
                        virStateInhibitCallback inhibit,
-                       void *opaque);
+                       void *opaque)
+    ATTRIBUTE_NONNULL(2);
 int virStateCleanup(void);
 int virStateReload(void);
 int virStateStop(void);
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 44a74e8779..23ab29c4e8 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -647,6 +647,7 @@ libxlAddDom0(libxlDriverPrivatePtr driver)
 
 static int
 libxlStateInitialize(bool privileged,
+                     const char *root,
                      virStateInhibitCallback callback G_GNUC_UNUSED,
                      void *opaque G_GNUC_UNUSED)
 {
@@ -655,6 +656,12 @@ libxlStateInitialize(bool privileged,
     char ebuf[1024];
     bool autostart = true;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (!libxlDriverShouldLoad(privileged))
         return VIR_DRV_STATE_INIT_SKIPPED;
 
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 826bf074e3..7c2c4798e5 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -85,6 +85,7 @@ VIR_LOG_INIT("lxc.lxc_driver");
 
 
 static int lxcStateInitialize(bool privileged,
+                              const char *root,
                               virStateInhibitCallback callback,
                               void *opaque);
 static int lxcStateCleanup(void);
@@ -1526,6 +1527,7 @@ lxcSecurityInit(virLXCDriverConfigPtr cfg)
 
 
 static int lxcStateInitialize(bool privileged,
+                              const char *root,
                               virStateInhibitCallback callback G_GNUC_UNUSED,
                               void *opaque G_GNUC_UNUSED)
 {
@@ -1533,6 +1535,12 @@ static int lxcStateInitialize(bool privileged,
     virLXCDriverConfigPtr cfg = NULL;
     bool autostart = true;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     /* Check that the user is root, silently disable if not */
     if (!privileged) {
         VIR_INFO("Not running privileged, disabling driver");
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index e360645969..90f894bd78 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -702,6 +702,7 @@ firewalld_dbus_filter_bridge(DBusConnection *connection G_GNUC_UNUSED,
  */
 static int
 networkStateInitialize(bool privileged,
+                       const char *root,
                        virStateInhibitCallback callback G_GNUC_UNUSED,
                        void *opaque G_GNUC_UNUSED)
 {
@@ -713,6 +714,12 @@ networkStateInitialize(bool privileged,
     DBusConnection *sysbus = NULL;
 #endif
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (VIR_ALLOC(network_driver) < 0)
         goto error;
 
diff --git a/src/node_device/node_device_hal.c b/src/node_device/node_device_hal.c
index b40f93df46..1cc484a4ce 100644
--- a/src/node_device/node_device_hal.c
+++ b/src/node_device/node_device_hal.c
@@ -588,6 +588,7 @@ device_prop_modified(LibHalContext *ctx G_GNUC_UNUSED,
 
 static int
 nodeStateInitialize(bool privileged G_GNUC_UNUSED,
+                    const char *root,
                     virStateInhibitCallback callback G_GNUC_UNUSED,
                     void *opaque G_GNUC_UNUSED)
 {
@@ -599,6 +600,12 @@ nodeStateInitialize(bool privileged G_GNUC_UNUSED,
     DBusConnection *sysbus;
     DBusError err;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     /* Ensure caps_tbl is sorted by capability name */
     qsort(caps_tbl, G_N_ELEMENTS(caps_tbl), sizeof(caps_tbl[0]),
           cmpstringp);
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index fabd2ec454..97d9982ac5 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -1782,6 +1782,7 @@ udevPCITranslateInit(bool privileged G_GNUC_UNUSED)
 
 static int
 nodeStateInitialize(bool privileged,
+                    const char *root,
                     virStateInhibitCallback callback G_GNUC_UNUSED,
                     void *opaque G_GNUC_UNUSED)
 {
@@ -1789,6 +1790,12 @@ nodeStateInitialize(bool privileged,
     struct udev *udev = NULL;
     virThread enumThread;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (VIR_ALLOC(driver) < 0)
         return VIR_DRV_STATE_INIT_ERROR;
 
diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c
index cc3ce98cc5..1c407727db 100644
--- a/src/nwfilter/nwfilter_driver.c
+++ b/src/nwfilter/nwfilter_driver.c
@@ -177,11 +177,18 @@ virNWFilterTriggerRebuildImpl(void *opaque)
  */
 static int
 nwfilterStateInitialize(bool privileged,
+                        const char *root,
                         virStateInhibitCallback callback G_GNUC_UNUSED,
                         void *opaque G_GNUC_UNUSED)
 {
     DBusConnection *sysbus = NULL;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (virDBusHasSystemBus() &&
         !(sysbus = virDBusGetSystemBus()))
         return VIR_DRV_STATE_INIT_ERROR;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index b5300241a8..31fe921ee3 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -646,6 +646,7 @@ qemuDomainFindMaxID(virDomainObjPtr vm,
  */
 static int
 qemuStateInitialize(bool privileged,
+                    const char *root,
                     virStateInhibitCallback callback,
                     void *opaque)
 {
@@ -657,6 +658,12 @@ qemuStateInitialize(bool privileged,
     bool autostart = true;
     size_t i;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (VIR_ALLOC(qemu_driver) < 0)
         return VIR_DRV_STATE_INIT_ERROR;
 
diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c
index b400b1dd10..f0a15279e0 100644
--- a/src/remote/remote_daemon.c
+++ b/src/remote/remote_daemon.c
@@ -834,6 +834,7 @@ static void daemonRunStateInit(void *opaque)
      * seriously delay OS bootup process */
     if (virStateInitialize(virNetDaemonIsPrivileged(dmn),
                            mandatory,
+                           NULL,
                            daemonInhibitCallback,
                            dmn) < 0) {
         VIR_ERROR(_("Driver state initialization failed"));
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index a1384fc655..d8eb6e7833 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -236,6 +236,7 @@ static int remoteSplitURIScheme(virURIPtr uri,
 
 static int
 remoteStateInitialize(bool privileged G_GNUC_UNUSED,
+                      const char *root G_GNUC_UNUSED,
                       virStateInhibitCallback callback G_GNUC_UNUSED,
                       void *opaque G_GNUC_UNUSED)
 {
diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c
index 93b4256450..d248121327 100644
--- a/src/secret/secret_driver.c
+++ b/src/secret/secret_driver.c
@@ -452,9 +452,16 @@ secretStateCleanup(void)
 
 static int
 secretStateInitialize(bool privileged,
+                      const char *root,
                       virStateInhibitCallback callback G_GNUC_UNUSED,
                       void *opaque G_GNUC_UNUSED)
 {
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (VIR_ALLOC(driver) < 0)
         return VIR_DRV_STATE_INIT_ERROR;
 
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 580a5e6f15..6b3a36821b 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -251,6 +251,7 @@ storageDriverAutostart(void)
  */
 static int
 storageStateInitialize(bool privileged,
+                       const char *root,
                        virStateInhibitCallback callback G_GNUC_UNUSED,
                        void *opaque G_GNUC_UNUSED)
 {
@@ -258,6 +259,12 @@ storageStateInitialize(bool privileged,
     g_autofree char *rundir = NULL;
     bool autostart = true;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     if (VIR_ALLOC(driver) < 0)
         return VIR_DRV_STATE_INIT_ERROR;
 
diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c
index 6b925653d5..67ea4f8786 100644
--- a/src/vz/vz_driver.c
+++ b/src/vz/vz_driver.c
@@ -4089,12 +4089,19 @@ vzStateCleanup(void)
 
 static int
 vzStateInitialize(bool privileged,
+                  const char *root,
                   virStateInhibitCallback callback G_GNUC_UNUSED,
                   void *opaque G_GNUC_UNUSED)
 {
     if (!privileged)
         return VIR_DRV_STATE_INIT_SKIPPED;
 
+    if (root != NULL) {
+        virReportError(VIR_ERR_INVALID_ARG, "%s",
+                       _("Driver does not support embedded mode"));
+        return -1;
+    }
+
     vz_driver_privileged = privileged;
 
     if (virFileMakePathWithMode(VZ_STATEDIR, S_IRWXU) < 0) {
-- 
2.23.0




More information about the libvir-list mailing list