diff --git a/docs/uri.html.in b/docs/uri.html.in index 79f878678c..2f76e8f390 100644 --- a/docs/uri.html.in +++ b/docs/uri.html.in @@ -52,6 +52,19 @@ uri_aliases = [ set, no alias lookup will be attempted.

+

Default URI choice

+ +

+If the URI passed to virConnectOpen* is NULL, then libvirt will use the following +logic to determine what URI to use. +

+ +
    +
  1. The environment variable LIBVIRT_DEFAULT_URI
  2. +
  3. The client configuration file uri_default parameter
  4. +
  5. Probe each hypervisor in turn until one that works is found
  6. +
+

Specifying URIs to virsh, virt-manager and virt-install

@@ -64,7 +77,8 @@ virsh -c test:///default list

If virsh finds the environment variable VIRSH_DEFAULT_CONNECT_URI set, it will try this URI by -default. +default. Use of this environment variable is, however, deprecated +now that libvirt supports LIBVIRT_DEFAULT_URI itself.

When using the interactive virsh shell, you can also use the diff --git a/src/libvirt.c b/src/libvirt.c index e916aa04bd..1c0cdf7c63 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 c54903ce1c..016cd242e5 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 64b00ee03b..ecf6aaad05 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -2632,7 +2632,16 @@ 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 option. +format as accepted by the B option. This environment variable +is deprecated in favour of the global B variable +which serves the same purpose. + +=item LIBVIRT_DEFAULT_URI + +The hypervisor to connect to by default. Set this to a URI, in the +same format as accepted by the B option. This overrides +the default URI set in any client config file and prevents libvirt +from probing for drivers. =item VISUAL