]> xenbits.xensource.com Git - libvirt.git/commitdiff
driver: introduce a driver method for probing default URIs
authorDaniel P. Berrangé <berrange@redhat.com>
Tue, 27 Mar 2018 16:24:44 +0000 (17:24 +0100)
committerDaniel P. Berrangé <berrange@redhat.com>
Thu, 12 Apr 2018 15:52:02 +0000 (16:52 +0100)
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@redhat.com>
docs/hvsupport.pl
src/bhyve/bhyve_driver.c
src/check-aclrules.pl
src/driver-hypervisor.h
src/libvirt.c
src/libxl/libxl_driver.c
src/lxc/lxc_driver.c
src/openvz/openvz_driver.c
src/qemu/qemu_driver.c
src/uml/uml_driver.c
src/vbox/vbox_common.c

index fc6eb1f152331b80c6dbf52c40cf49a91ca499c7..a2b980c502a31c5fc9cd7cef21a30518732833f0 100755 (executable)
@@ -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";
                 }
index 38e6442db72c8df6a0bcf2d0028122bf67912a58..084f782f9e535fef02e83e92b50ae6fbda3cf829 100644 (file)
@@ -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;
@@ -1673,6 +1680,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 */
index 5b6c711dc84f1d610089aaaeddb30bee8c94988a..8b146d8dba82692ce362417a25c545d98ad48343 100755 (executable)
@@ -59,6 +59,7 @@ my %whitelist = (
     "storageClose" => 1,
     "interfaceOpen" => 1,
     "interfaceClose" => 1,
+    "connectURIProbe" => 1,
     );
 
 # Temp hack - remove it once xen driver is fixed
index ce0e2b2525527be54f9a58545e387a000f836338..e71a72a441325bf3bf8b209a81ef66a44689da87 100644 (file)
@@ -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;
index 51acbbf83ee95045d4271a69748437a68944b281..d87efca6254627e734aa19defff4237ee8bb0dc5 100644 (file)
@@ -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) {
index c559bf65143aae6f0d6763b5634ef2814257e370..d574fa446e1e3ced04b4834e6b4127735ba35743 100644 (file)
@@ -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 */
index f61e0b33ef60ca7d1797adf37bc14790cd1b82c0..0bfa417ef2157c18c0c99fb06fcb9fa77480bac5 100644 (file)
@@ -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"))
@@ -5537,6 +5543,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 */
index ec9541840a4eb6cc42d3a7181d4ea8692918c6ee..4b458bf6fc2fba7493a19d90d188f7054e395624 100644 (file)
@@ -1331,6 +1331,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,
@@ -1341,14 +1355,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 ||
@@ -2450,6 +2457,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 */
index fd088174690c96de2afee2317e4a6dfd4dc72073..214584992224032a38f1fd4c72a21352fd58f145 100644 (file)
@@ -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 ||
@@ -21336,6 +21347,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 */
index ab7fa7f27381a448875bdabf1ba415f35df8704e..63350908ddde73b46b75adc1b53751bdd4d93a96 100644 (file)
@@ -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 */
index b5f8456f8dd66b2b133800582cdf1f74e5be0778..b86a0b28dc7c9e856f920f06c2d85639f8996ec7 100644 (file)
@@ -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,
 
 static virHypervisorDriver vboxCommonDriver = {
     .name = "VBOX",
+    .connectURIProbe = vboxConnectURIProbe,
     .connectOpen = vboxConnectOpen, /* 0.6.3 */
     .connectClose = vboxConnectClose, /* 0.6.3 */
     .connectGetVersion = vboxConnectGetVersion, /* 0.6.3 */