]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
libvirt: pass a directory path into drivers for embedded usage
authorDaniel P. Berrangé <berrange@redhat.com>
Fri, 17 May 2019 11:30:45 +0000 (12:30 +0100)
committerDaniel P. Berrangé <berrange@redhat.com>
Mon, 27 Jan 2020 11:02:16 +0000 (11:02 +0000)
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.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Cole Robinson <crobinso@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
17 files changed:
src/driver-state.h
src/interface/interface_backend_netcf.c
src/interface/interface_backend_udev.c
src/libvirt.c
src/libvirt_internal.h
src/libxl/libxl_driver.c
src/lxc/lxc_driver.c
src/network/bridge_driver.c
src/node_device/node_device_hal.c
src/node_device/node_device_udev.c
src/nwfilter/nwfilter_driver.c
src/qemu/qemu_driver.c
src/remote/remote_daemon.c
src/remote/remote_driver.c
src/secret/secret_driver.c
src/storage/storage_driver.c
src/vz/vz_driver.c

index 69e2678dfcba75b78de815f899842c2fa28d506f..1e2f6ed247787b9c853541cb51e6d1b32fcbe623 100644 (file)
@@ -32,6 +32,7 @@ typedef enum {
 
 typedef virDrvStateInitResult
 (*virDrvStateInitialize)(bool privileged,
+                         const char *root,
                          virStateInhibitCallback callback,
                          void *opaque);
 
index 65cb7eae629a88d5dd3920dd2068067a6af8cd37..06eb1ace0898613d25d9e0c7cbad070f8a4cf9ab 100644 (file)
@@ -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;
 
index 7cc098eb3375986fd81dbc412b2ab10ec78212d9..e87b884c17635d7134a9879af11048eba9548eda 100644 (file)
@@ -1145,11 +1145,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;
 
index 86bb6551ed486587fcf1c910beb8c978176f096e..f61a24db28fdc8e6427e529c548e0c966a4c9428 100644 (file)
@@ -605,16 +605,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)
 {
@@ -629,6 +649,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);
index 4a74dbc2af3b667ac2821b9463813b4ae79713bb..00ef7aaf2585a962f9972bd0004b9bd8967ddaae 100644 (file)
@@ -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);
index eb166244ba38512f04c742928a9c2524e521e6f3..e0a8ec5c24fbba853ae3b3010d6a5b2b88da069c 100644 (file)
@@ -648,6 +648,7 @@ libxlAddDom0(libxlDriverPrivatePtr driver)
 
 static int
 libxlStateInitialize(bool privileged,
+                     const char *root,
                      virStateInhibitCallback callback,
                      void *opaque)
 {
@@ -656,6 +657,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;
 
index 10df6e632bf74a2064315d2eb7c002d47bc0e459..e73583daed1f8ccc55718a8c6c1fcbb6de6bf7b6 100644 (file)
@@ -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,12 +1527,19 @@ lxcSecurityInit(virLXCDriverConfigPtr cfg)
 
 
 static int lxcStateInitialize(bool privileged,
+                              const char *root,
                               virStateInhibitCallback callback G_GNUC_UNUSED,
                               void *opaque G_GNUC_UNUSED)
 {
     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");
index c9c45df758f59089846a6ed6d6efb841948e2040..b66135f2d9f47b6a33929845998bad2bd5b9384b 100644 (file)
@@ -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;
 
index 4cef7c2c1242cd5b272346fdf644f9de267e4a1a..c3ca310bb7027470311bb171d01323e911bebd91 100644 (file)
@@ -580,6 +580,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)
 {
@@ -591,6 +592,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);
index 4b33dc25d831534de04c06bada7d60b1712c1063..396763fa2997bd3c2ddd37fc50ebdd009c4c198f 100644 (file)
@@ -1781,6 +1781,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)
 {
@@ -1788,6 +1789,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;
 
index cc3ce98cc5d46779c03d64d7e30c2b376d79eb23..1c407727dbb1ed4fbeae8b3e41466e3d0a123fab 100644 (file)
@@ -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;
index 0b23c747cefadb6eb70f5ca5ab585c6900f54f6b..de857f190326bbaf0cb05c5c2db0448ebc4f36ca 100644 (file)
@@ -631,6 +631,7 @@ qemuDomainFindMaxID(virDomainObjPtr vm,
  */
 static int
 qemuStateInitialize(bool privileged,
+                    const char *root,
                     virStateInhibitCallback callback,
                     void *opaque)
 {
@@ -644,6 +645,12 @@ qemuStateInitialize(bool privileged,
     const char *defsecmodel = NULL;
     g_autofree virSecurityManagerPtr *sec_managers = NULL;
 
+    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;
 
index 1c224f8050de09491e3fb9125c9c59fd3a33d23a..33697988b697886c738d4ebf478bb7ae93016b2d 100644 (file)
@@ -835,6 +835,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"));
index f6e725dcbf9a96a4626127d786803d7397182d08..7971cde76934c8f077ce85e5a1592ed4f9a19141 100644 (file)
@@ -232,6 +232,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)
 {
index c3c57a41c786849b0eca647810efcaf2c16cd030..a31005c7311191a31a9570758e548f1e0c91ce41 100644 (file)
@@ -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;
 
index 0bb116cf08c3dcd179fd281c390e9606fe8690a6..2dd093a9da5290259315dd44f6bd455668859f7f 100644 (file)
@@ -250,6 +250,7 @@ storageDriverAutostart(void)
  */
 static int
 storageStateInitialize(bool privileged,
+                       const char *root,
                        virStateInhibitCallback callback G_GNUC_UNUSED,
                        void *opaque G_GNUC_UNUSED)
 {
@@ -257,6 +258,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;
 
index bd427fb07ea927ce733f3f98dce3d1156677fd68..e8a7522732fbdee98cb160c93ac4971d5aad0efa 100644 (file)
@@ -4094,12 +4094,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) {