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

Daniel P. Berrange berrange at redhat.com
Wed Mar 14 12:37:18 UTC 2012


From: "Daniel P. Berrange" <berrange at 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 |   13 +++++++
 src/libvirt.c    |  107 +++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 87 insertions(+), 33 deletions(-)

diff --git a/docs/uri.html.in b/docs/uri.html.in
index 79f8786..48f6498 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>
diff --git a/src/libvirt.c b/src/libvirt.c
index e916aa0..c5c2e84 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,32 @@ 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 +1076,46 @@ 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;
+    }
+
+    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;
+        }
+
+        *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
-- 
1.7.7.6




More information about the libvir-list mailing list