[libvirt] [PATCH 3/8] driver: introduce a driver method for probing default URIs

Daniel P. Berrangé berrange at redhat.com
Mon Apr 9 15:45:46 UTC 2018


Currently the virDrvConnectOpen method is supposed to handle both
opening an explicit URI and auto-probing a driver if no URI is
given. Introduce a dedicated virDrvConnectURIProbe method to enable the
probing functionality to be split from the driver opening functionality.

It is still possible for NULL to be passed to the virDrvConnectOpen
method after this change, because the remote driver needs special
handling to enable probing of the URI against a remote libvirtd daemon.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 docs/hvsupport.pl          |  6 +++---
 src/bhyve/bhyve_driver.c   | 18 +++++++++++++-----
 src/driver-hypervisor.h    |  4 ++++
 src/libvirt.c              | 13 +++++++++++++
 src/libxl/libxl_driver.c   | 17 ++++++++++++-----
 src/lxc/lxc_driver.c       | 17 ++++++++++++-----
 src/openvz/openvz_driver.c | 24 ++++++++++++++++--------
 src/qemu/qemu_driver.c     | 30 +++++++++++++++++++++---------
 src/uml/uml_driver.c       | 20 +++++++++++++-------
 src/vbox/vbox_common.c     | 13 ++++++++++---
 10 files changed, 117 insertions(+), 45 deletions(-)

diff --git a/docs/hvsupport.pl b/docs/hvsupport.pl
index fc6eb1f152..a2b980c502 100755
--- a/docs/hvsupport.pl
+++ b/docs/hvsupport.pl
@@ -184,7 +184,7 @@ foreach my $drivertable (@drivertable) {
                 my $api;
                 if (exists $apis{"vir$name"}) {
                     $api = "vir$name";
-                } elsif ($name =~ /\w+(Open|Close)/) {
+                } elsif ($name =~ /\w+(Open|Close|URIProbe)/) {
                     next;
                 } else {
                     die "driver $name does not have a public API";
@@ -241,12 +241,12 @@ foreach my $src (@srcs) {
 
                 next if $api eq "no" || $api eq "name";
 
-                die "Method $meth in $src is missing version" unless defined $vers;
+                die "Method $meth in $src is missing version" unless defined $vers || $api eq "connectURIProbe";
 
                 die "Driver method for $api is NULL in $src" if $meth eq "NULL";
 
                 if (!exists($groups{$ingrp}->{apis}->{$api})) {
-                    next if $api =~ /\w(Open|Close)/;
+                    next if $api =~ /\w(Open|Close|URIProbe)/;
 
                     die "Found unexpected method $api in $ingrp\n";
                 }
diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c
index 849d3abcd3..a0bc400480 100644
--- a/src/bhyve/bhyve_driver.c
+++ b/src/bhyve/bhyve_driver.c
@@ -180,6 +180,17 @@ bhyveDomObjFromDomain(virDomainPtr domain)
     return vm;
 }
 
+
+static int
+bhyveConnectURIProbe(char **uri)
+{
+    if (bhyve_driver == NULL)
+        return 0;
+
+    return VIR_STRDUP(*uri, "bhyve:///system");
+}
+
+
 static virDrvOpenStatus
 bhyveConnectOpen(virConnectPtr conn,
                  virConnectAuthPtr auth ATTRIBUTE_UNUSED,
@@ -189,11 +200,7 @@ bhyveConnectOpen(virConnectPtr conn,
      virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
      if (conn->uri == NULL) {
-         if (bhyve_driver == NULL)
-             return VIR_DRV_OPEN_DECLINED;
-
-         if (!(conn->uri = virURIParse("bhyve:///system")))
-             return VIR_DRV_OPEN_ERROR;
+         return VIR_DRV_OPEN_DECLINED;
      } else {
          if (!conn->uri->scheme || STRNEQ(conn->uri->scheme, "bhyve"))
              return VIR_DRV_OPEN_DECLINED;
@@ -1689,6 +1696,7 @@ bhyveConnectGetDomainCapabilities(virConnectPtr conn,
 
 static virHypervisorDriver bhyveHypervisorDriver = {
     .name = "bhyve",
+    .connectURIProbe = bhyveConnectURIProbe,
     .connectOpen = bhyveConnectOpen, /* 1.2.2 */
     .connectClose = bhyveConnectClose, /* 1.2.2 */
     .connectGetVersion = bhyveConnectGetVersion, /* 1.2.2 */
diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h
index ce0e2b2525..e71a72a441 100644
--- a/src/driver-hypervisor.h
+++ b/src/driver-hypervisor.h
@@ -25,6 +25,9 @@
 #  error "Don't include this file directly, only use driver.h"
 # endif
 
+typedef int
+(*virDrvConnectURIProbe)(char **uri);
+
 typedef virDrvOpenStatus
 (*virDrvConnectOpen)(virConnectPtr conn,
                      virConnectAuthPtr auth,
@@ -1300,6 +1303,7 @@ typedef virHypervisorDriver *virHypervisorDriverPtr;
  */
 struct _virHypervisorDriver {
     const char *name; /* the name of the driver */
+    virDrvConnectURIProbe connectURIProbe;
     virDrvConnectOpen connectOpen;
     virDrvConnectClose connectClose;
     virDrvConnectSupportsFeature connectSupportsFeature;
diff --git a/src/libvirt.c b/src/libvirt.c
index 51acbbf83e..d87efca625 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -975,6 +975,19 @@ virConnectOpenInternal(const char *name,
     } else {
         if (virConnectGetDefaultURI(conf, &uristr) < 0)
             goto failed;
+
+        if (uristr == NULL) {
+            VIR_DEBUG("Trying to probe for default URI");
+            for (i = 0; i < virConnectDriverTabCount && uristr == NULL; i++) {
+                if (virConnectDriverTab[i]->hypervisorDriver->connectURIProbe) {
+                    if (virConnectDriverTab[i]->hypervisorDriver->connectURIProbe(&uristr) < 0)
+                        goto failed;
+                    VIR_DEBUG("%s driver URI probe returned '%s'",
+                              virConnectDriverTab[i]->hypervisorDriver->name,
+                              uristr ? uristr : "");
+                }
+            }
+        }
     }
 
     if (uristr) {
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index c559bf6514..d574fa446e 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -827,6 +827,16 @@ libxlStateReload(void)
 }
 
 
+static int
+libxlConnectURIProbe(char **uri)
+{
+    if (libxl_driver == NULL)
+        return 0;
+
+    return VIR_STRDUP(*uri, "xen:///system");
+}
+
+
 static virDrvOpenStatus
 libxlConnectOpen(virConnectPtr conn,
                  virConnectAuthPtr auth ATTRIBUTE_UNUSED,
@@ -836,11 +846,7 @@ libxlConnectOpen(virConnectPtr conn,
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
     if (conn->uri == NULL) {
-        if (libxl_driver == NULL)
-            return VIR_DRV_OPEN_DECLINED;
-
-        if (!(conn->uri = virURIParse("xen:///system")))
-            return VIR_DRV_OPEN_ERROR;
+        return VIR_DRV_OPEN_DECLINED;
     } else {
         /* Only xen scheme */
         if (conn->uri->scheme == NULL || STRNEQ(conn->uri->scheme, "xen"))
@@ -6466,6 +6472,7 @@ libxlConnectBaselineCPU(virConnectPtr conn,
 
 static virHypervisorDriver libxlHypervisorDriver = {
     .name = LIBXL_DRIVER_NAME,
+    .connectURIProbe = libxlConnectURIProbe,
     .connectOpen = libxlConnectOpen, /* 0.9.0 */
     .connectClose = libxlConnectClose, /* 0.9.0 */
     .connectGetType = libxlConnectGetType, /* 0.9.0 */
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index cb481bf20a..7fbeabc994 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -152,6 +152,16 @@ lxcDomObjFromDomain(virDomainPtr domain)
 
 /* Functions */
 
+static int
+lxcConnectURIProbe(char **uri)
+{
+    if (lxc_driver == NULL)
+        return 0;
+
+    return VIR_STRDUP(*uri, "lxc:///system");
+}
+
+
 static virDrvOpenStatus lxcConnectOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                        virConfPtr conf ATTRIBUTE_UNUSED,
@@ -161,11 +171,7 @@ static virDrvOpenStatus lxcConnectOpen(virConnectPtr conn,
 
     /* Verify uri was specified */
     if (conn->uri == NULL) {
-        if (lxc_driver == NULL)
-            return VIR_DRV_OPEN_DECLINED;
-
-        if (!(conn->uri = virURIParse("lxc:///system")))
-            return VIR_DRV_OPEN_ERROR;
+        return VIR_DRV_OPEN_DECLINED;
     } else {
         if (conn->uri->scheme == NULL ||
             STRNEQ(conn->uri->scheme, "lxc"))
@@ -5533,6 +5539,7 @@ lxcDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
 /* Function Tables */
 static virHypervisorDriver lxcHypervisorDriver = {
     .name = LXC_DRIVER_NAME,
+    .connectURIProbe = lxcConnectURIProbe,
     .connectOpen = lxcConnectOpen, /* 0.4.2 */
     .connectClose = lxcConnectClose, /* 0.4.2 */
     .connectSupportsFeature = lxcConnectSupportsFeature, /* 1.2.2 */
diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c
index f1132490e8..f30db8eec3 100644
--- a/src/openvz/openvz_driver.c
+++ b/src/openvz/openvz_driver.c
@@ -1420,6 +1420,20 @@ openvzDomainSetVcpus(virDomainPtr dom, unsigned int nvcpus)
     return openvzDomainSetVcpusFlags(dom, nvcpus, VIR_DOMAIN_AFFECT_LIVE);
 }
 
+
+static int
+openvzConnectURIProbe(char **uri)
+{
+    if (!virFileExists("/proc/vz"))
+        return 0;
+
+    if (access("/proc/vz", W_OK) < 0)
+        return 0;
+
+    return VIR_STRDUP(*uri, "openvz:///system");
+}
+
+
 static virDrvOpenStatus openvzConnectOpen(virConnectPtr conn,
                                           virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                           virConfPtr conf ATTRIBUTE_UNUSED,
@@ -1430,14 +1444,7 @@ static virDrvOpenStatus openvzConnectOpen(virConnectPtr conn,
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
     if (conn->uri == NULL) {
-        if (!virFileExists("/proc/vz"))
-            return VIR_DRV_OPEN_DECLINED;
-
-        if (access("/proc/vz", W_OK) < 0)
-            return VIR_DRV_OPEN_DECLINED;
-
-        if (!(conn->uri = virURIParse("openvz:///system")))
-            return VIR_DRV_OPEN_ERROR;
+        return VIR_DRV_OPEN_DECLINED;
     } else {
         /* If scheme isn't 'openvz', then its for another driver */
         if (conn->uri->scheme == NULL ||
@@ -2585,6 +2592,7 @@ openvzDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
 
 static virHypervisorDriver openvzHypervisorDriver = {
     .name = "OPENVZ",
+    .connectURIProbe = openvzConnectURIProbe,
     .connectOpen = openvzConnectOpen, /* 0.3.1 */
     .connectClose = openvzConnectClose, /* 0.3.1 */
     .connectGetType = openvzConnectGetType, /* 0.3.1 */
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 5c31dfdd58..99ec51f304 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1115,6 +1115,25 @@ qemuStateCleanup(void)
 }
 
 
+static int
+qemuConnectURIProbe(char **uri)
+{
+    virQEMUDriverConfigPtr cfg = NULL;
+    int ret = -1;
+
+    if (qemu_driver == NULL)
+        return 0;
+
+    cfg = virQEMUDriverGetConfig(qemu_driver);
+    if (VIR_STRDUP(*uri, cfg->uri) < 0)
+        goto cleanup;
+
+    ret = 0;
+ cleanup:
+    virObjectUnref(cfg);
+    return ret;
+}
+
 static virDrvOpenStatus qemuConnectOpen(virConnectPtr conn,
                                         virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                         virConfPtr conf ATTRIBUTE_UNUSED,
@@ -1125,15 +1144,7 @@ static virDrvOpenStatus qemuConnectOpen(virConnectPtr conn,
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
     if (conn->uri == NULL) {
-        if (qemu_driver == NULL) {
-            ret = VIR_DRV_OPEN_DECLINED;
-            goto cleanup;
-        }
-
-        cfg = virQEMUDriverGetConfig(qemu_driver);
-
-        if (!(conn->uri = virURIParse(cfg->uri)))
-            goto cleanup;
+        return VIR_DRV_OPEN_DECLINED;
     } else {
         /* If URI isn't 'qemu' its definitely not for us */
         if (conn->uri->scheme == NULL ||
@@ -21340,6 +21351,7 @@ qemuDomainSetLifecycleAction(virDomainPtr dom,
 
 static virHypervisorDriver qemuHypervisorDriver = {
     .name = QEMU_DRIVER_NAME,
+    .connectURIProbe = qemuConnectURIProbe,
     .connectOpen = qemuConnectOpen, /* 0.2.0 */
     .connectClose = qemuConnectClose, /* 0.2.0 */
     .connectSupportsFeature = qemuConnectSupportsFeature, /* 0.5.0 */
diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c
index ab7fa7f273..63350908dd 100644
--- a/src/uml/uml_driver.c
+++ b/src/uml/uml_driver.c
@@ -1185,6 +1185,17 @@ static void umlShutdownVMDaemon(struct uml_driver *driver,
 }
 
 
+static int umlConnectURIProbe(char **uri)
+{
+    if (uml_driver == NULL)
+        return 0;
+
+    return VIR_STRDUP(*uri, uml_driver->privileged ?
+                      "uml:///system" :
+                      "uml:///session");
+}
+
+
 static virDrvOpenStatus umlConnectOpen(virConnectPtr conn,
                                        virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                        virConfPtr conf ATTRIBUTE_UNUSED,
@@ -1193,13 +1204,7 @@ static virDrvOpenStatus umlConnectOpen(virConnectPtr conn,
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
     if (conn->uri == NULL) {
-        if (uml_driver == NULL)
-            return VIR_DRV_OPEN_DECLINED;
-
-        if (!(conn->uri = virURIParse(uml_driver->privileged ?
-                                      "uml:///system" :
-                                      "uml:///session")))
-            return VIR_DRV_OPEN_ERROR;
+        return VIR_DRV_OPEN_DECLINED;
     } else {
         if (conn->uri->scheme == NULL ||
             STRNEQ(conn->uri->scheme, "uml"))
@@ -2947,6 +2952,7 @@ umlDomainHasManagedSaveImage(virDomainPtr dom, unsigned int flags)
 
 static virHypervisorDriver umlHypervisorDriver = {
     .name = "UML",
+    .connectURIProbe = umlConnectURIProbe,
     .connectOpen = umlConnectOpen, /* 0.5.0 */
     .connectClose = umlConnectClose, /* 0.5.0 */
     .connectGetType = umlConnectGetType, /* 0.5.0 */
diff --git a/src/vbox/vbox_common.c b/src/vbox/vbox_common.c
index 3bcca43d32..ef71e650c6 100644
--- a/src/vbox/vbox_common.c
+++ b/src/vbox/vbox_common.c
@@ -499,6 +499,13 @@ vboxAttachStorageControllers(virDomainDefPtr def,
 }
 
 
+static int
+vboxConnectURIProbe(char **uri)
+{
+    return VIR_STRDUP(*uri, geteuid() ? "vbox:///session" : "vbox:///system");
+}
+
+
 static virDrvOpenStatus
 vboxConnectOpen(virConnectPtr conn,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
@@ -510,9 +517,8 @@ vboxConnectOpen(virConnectPtr conn,
 
     virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
-    if (conn->uri == NULL &&
-        !(conn->uri = virURIParse(uid ? "vbox:///session" : "vbox:///system")))
-        return VIR_DRV_OPEN_ERROR;
+    if (conn->uri == NULL)
+        return VIR_DRV_OPEN_DECLINED;
 
     if (conn->uri->scheme == NULL ||
         STRNEQ(conn->uri->scheme, "vbox"))
@@ -7980,6 +7986,7 @@ vboxDomainSendKey(virDomainPtr dom,
 
 virHypervisorDriver vboxCommonDriver = {
     .name = "VBOX",
+    .connectURIProbe = vboxConnectURIProbe,
     .connectOpen = vboxConnectOpen, /* 0.6.3 */
     .connectClose = vboxConnectClose, /* 0.6.3 */
     .connectGetVersion = vboxConnectGetVersion, /* 0.6.3 */
-- 
2.14.3




More information about the libvir-list mailing list