[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v2] Allow overriding default URI in config file



From: "Daniel P. Berrange" <berrange redhat com>

Currently if the URI passed to virConnectOpen* is NULL, then we

 - Look for LIBVIRT_DEFAULT_URI env var
 - Probe for drivers

This changes it so that

 - Look for LIBVIRT_DEFAULT_URI env var
 - Look for 'uri_default' in $HOME/.libvirt/libvirt.conf
 - Probe for drivers
---
 docs/uri.html.in |   16 ++++++++-
 src/libvirt.c    |  107 +++++++++++++++++++++++++++++++++++++-----------------
 src/libvirt.conf |    6 +++
 tools/virsh.pod  |    4 ++-
 4 files changed, 98 insertions(+), 35 deletions(-)

diff --git a/docs/uri.html.in b/docs/uri.html.in
index 79f8786..2f76e8f 100644
--- a/docs/uri.html.in
+++ b/docs/uri.html.in
@@ -52,6 +52,19 @@ uri_aliases = [
   set, no alias lookup will be attempted.
 </p>
 
+    <h2><a name="URI_default">Default URI choice</a></h2>
+
+    <p>
+If the URI passed to <code>virConnectOpen*</code> is NULL, then libvirt will use the following
+logic to determine what URI to use.
+</p>
+
+    <ol>
+      <li>The environment variable <code>LIBVIRT_DEFAULT_URI</code></li>
+      <li>The client configuration file <code>uri_default</code> parameter</li>
+      <li>Probe each hypervisor in turn until one that works is found</li>
+    </ol>
+
     <h2>
       <a name="URI_virsh">Specifying URIs to virsh, virt-manager and virt-install</a>
     </h2>
@@ -64,7 +77,8 @@ virsh <b>-c test:///default</b> list
     <p>
 If virsh finds the environment variable
 <code>VIRSH_DEFAULT_CONNECT_URI</code> set, it will try this URI by
-default.
+default. Use of this environment variable is, however, deprecated
+now that libvirt supports <code>LIBVIRT_DEFAULT_URI</code> itself.
 </p>
     <p>
 When using the interactive virsh shell, you can also use the
diff --git a/src/libvirt.c b/src/libvirt.c
index e916aa0..1c0cdf7 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -961,7 +961,7 @@ error:
 }
 
 static char *
-virConnectConfigFile(void)
+virConnectGetConfigFilePath(void)
 {
     char *path;
     if (geteuid() == 0) {
@@ -989,6 +989,33 @@ error:
     return NULL;
 }
 
+static int
+virConnectGetConfigFile(virConfPtr *conf)
+{
+    char *filename = NULL;
+    int ret = -1;
+
+    *conf = NULL;
+
+    if (!(filename = virConnectGetConfigFilePath()))
+        goto cleanup;
+
+    if (!virFileExists(filename)) {
+        ret = 0;
+        goto cleanup;
+    }
+
+    VIR_DEBUG("Loading config file '%s'", filename);
+    if (!(*conf = virConfReadFile(filename, 0)))
+        goto cleanup;
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(filename);
+    return ret;
+}
+
 #define URI_ALIAS_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
 
 static int
@@ -1050,35 +1077,45 @@ virConnectOpenFindURIAliasMatch(virConfValuePtr value, const char *alias, char *
 }
 
 static int
-virConnectOpenResolveURIAlias(const char *alias, char **uri)
+virConnectOpenResolveURIAlias(virConfPtr conf,
+                              const char *alias, char **uri)
 {
-    char *config = NULL;
     int ret = -1;
-    virConfPtr conf = NULL;
     virConfValuePtr value = NULL;
 
     *uri = NULL;
 
-    if (!(config = virConnectConfigFile()))
-        goto cleanup;
-
-    if (!virFileExists(config)) {
-        ret = 0;
-        goto cleanup;
-    }
-
-    VIR_DEBUG("Loading config file '%s'", config);
-    if (!(conf = virConfReadFile(config, 0)))
-        goto cleanup;
-
     if ((value = virConfGetValue(conf, "uri_aliases")))
         ret = virConnectOpenFindURIAliasMatch(value, alias, uri);
     else
         ret = 0;
 
+    return ret;
+}
+
+
+static int
+virConnectGetDefaultURI(virConfPtr conf,
+                        const char **name)
+{
+    int ret = -1;
+    virConfValuePtr value = NULL;
+    char *defname = getenv("LIBVIRT_DEFAULT_URI");
+    if (defname && *defname) {
+        VIR_DEBUG("Using LIBVIRT_DEFAULT_URI '%s'", defname);
+        *name = defname;
+    } else if ((value = virConfGetValue(conf, "uri_default"))) {
+        if (value->type != VIR_CONF_STRING) {
+            virLibConnError(VIR_ERR_INTERNAL_ERROR, "%s",
+                            _("Expected a string for 'uri_default' config parameter"));
+            goto cleanup;
+        }
+        VIR_DEBUG("Using config file uri '%s'", value->str);
+        *name = value->str;
+    }
+
+    ret = 0;
 cleanup:
-    virConfFree(conf);
-    VIR_FREE(config);
     return ret;
 }
 
@@ -1089,6 +1126,7 @@ do_open (const char *name,
 {
     int i, res;
     virConnectPtr ret;
+    virConfPtr conf = NULL;
 
     virResetLastError();
 
@@ -1096,20 +1134,20 @@ do_open (const char *name,
     if (ret == NULL)
         return NULL;
 
+    if (virConnectGetConfigFile(&conf) < 0)
+        goto failed;
+
+    if (name && name[0] == '\0')
+        name = NULL;
+
     /*
      *  If no URI is passed, then check for an environment string if not
      *  available probe the compiled in drivers to find a default hypervisor
      *  if detectable.
      */
-    if (!name || name[0] == '\0') {
-        char *defname = getenv("LIBVIRT_DEFAULT_URI");
-        if (defname && *defname) {
-            VIR_DEBUG("Using LIBVIRT_DEFAULT_URI %s", defname);
-            name = defname;
-        } else {
-            name = NULL;
-        }
-    }
+    if (!name &&
+        virConnectGetDefaultURI(conf, &name) < 0)
+        goto failed;
 
     if (name) {
         char *alias = NULL;
@@ -1124,7 +1162,7 @@ do_open (const char *name,
             name = "xen:///";
 
         if (!(flags & VIR_CONNECT_NO_ALIASES) &&
-            virConnectOpenResolveURIAlias(name, &alias) < 0)
+            virConnectOpenResolveURIAlias(conf, name, &alias) < 0)
             goto failed;
 
         ret->uri = virURIParse (alias ? alias : name);
@@ -1308,9 +1346,12 @@ do_open (const char *name,
         }
     }
 
+    virConfFree(conf);
+
     return ret;
 
 failed:
+    virConfFree(conf);
     virUnrefConnect(ret);
 
     return NULL;
@@ -1325,11 +1366,11 @@ failed:
  *
  * Returns a pointer to the hypervisor connection or NULL in case of error
  *
- * If @name is NULL then probing will be done to determine a suitable
- * default driver to activate. This involves trying each hypervisor
- * in turn until one successfully opens. If the LIBVIRT_DEFAULT_URI
- * environment variable is set, then it will be used in preference
- * to probing for a driver.
+ * If @name is NULL, if the LIBVIRT_DEFAULT_URI environment variable is set,
+ * then it will be used. Otherwise if the client configuration file
+ * has the "uri_default" parameter set, then it will be used. Finally
+ * probing will be done to determine a suitable default driver to activate.
+ * This involves trying each hypervisor in turn until one successfully opens.
  *
  * If connecting to an unprivileged hypervisor driver which requires
  * the libvirtd daemon to be active, it will automatically be launched
diff --git a/src/libvirt.conf b/src/libvirt.conf
index c54903c..016cd24 100644
--- a/src/libvirt.conf
+++ b/src/libvirt.conf
@@ -10,3 +10,9 @@
 #  "hail=qemu+ssh://root hail cloud example com/system",
 #  "sleet=qemu+ssh://root sleet cloud example com/system",
 #]
+
+#
+# This can be used to prevent probing of the hypervisor
+# driver when no URI is supplied by the application.
+
+#uri_default = "qemu:///system"
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 64b00ee..4cc91cc 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2632,7 +2632,9 @@ The file to log virsh debug messages.
 =item VIRSH_DEFAULT_CONNECT_URI
 
 The hypervisor to connect to by default. Set this to a URI, in the same
-format as accepted by the B<connect> option.
+format as accepted by the B<connect> option. This environment variable
+is deprecated in favour of the global B<LIVIRT_DEFAULT_URI> variable
+which serves the same purpose.
 
 =item VISUAL
 
-- 
1.7.7.6


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]