提交 e457d5ef 编写于 作者: D Daniel P. Berrange

Allow overriding default URI in config file

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
上级 6227a220
...@@ -52,6 +52,19 @@ uri_aliases = [ ...@@ -52,6 +52,19 @@ uri_aliases = [
set, no alias lookup will be attempted. set, no alias lookup will be attempted.
</p> </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> <h2>
<a name="URI_virsh">Specifying URIs to virsh, virt-manager and virt-install</a> <a name="URI_virsh">Specifying URIs to virsh, virt-manager and virt-install</a>
</h2> </h2>
...@@ -64,7 +77,8 @@ virsh <b>-c test:///default</b> list ...@@ -64,7 +77,8 @@ virsh <b>-c test:///default</b> list
<p> <p>
If virsh finds the environment variable If virsh finds the environment variable
<code>VIRSH_DEFAULT_CONNECT_URI</code> set, it will try this URI by <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>
<p> <p>
When using the interactive virsh shell, you can also use the When using the interactive virsh shell, you can also use the
......
...@@ -961,7 +961,7 @@ error: ...@@ -961,7 +961,7 @@ error:
} }
static char * static char *
virConnectConfigFile(void) virConnectGetConfigFilePath(void)
{ {
char *path; char *path;
if (geteuid() == 0) { if (geteuid() == 0) {
...@@ -989,6 +989,33 @@ error: ...@@ -989,6 +989,33 @@ error:
return NULL; 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_-" #define URI_ALIAS_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
static int static int
...@@ -1050,35 +1077,45 @@ virConnectOpenFindURIAliasMatch(virConfValuePtr value, const char *alias, char * ...@@ -1050,35 +1077,45 @@ virConnectOpenFindURIAliasMatch(virConfValuePtr value, const char *alias, char *
} }
static int static int
virConnectOpenResolveURIAlias(const char *alias, char **uri) virConnectOpenResolveURIAlias(virConfPtr conf,
const char *alias, char **uri)
{ {
char *config = NULL;
int ret = -1; int ret = -1;
virConfPtr conf = NULL;
virConfValuePtr value = NULL; virConfValuePtr value = NULL;
*uri = 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"))) if ((value = virConfGetValue(conf, "uri_aliases")))
ret = virConnectOpenFindURIAliasMatch(value, alias, uri); ret = virConnectOpenFindURIAliasMatch(value, alias, uri);
else else
ret = 0; 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: cleanup:
virConfFree(conf);
VIR_FREE(config);
return ret; return ret;
} }
...@@ -1089,6 +1126,7 @@ do_open (const char *name, ...@@ -1089,6 +1126,7 @@ do_open (const char *name,
{ {
int i, res; int i, res;
virConnectPtr ret; virConnectPtr ret;
virConfPtr conf = NULL;
virResetLastError(); virResetLastError();
...@@ -1096,20 +1134,20 @@ do_open (const char *name, ...@@ -1096,20 +1134,20 @@ do_open (const char *name,
if (ret == NULL) if (ret == NULL)
return 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 * If no URI is passed, then check for an environment string if not
* available probe the compiled in drivers to find a default hypervisor * available probe the compiled in drivers to find a default hypervisor
* if detectable. * if detectable.
*/ */
if (!name || name[0] == '\0') { if (!name &&
char *defname = getenv("LIBVIRT_DEFAULT_URI"); virConnectGetDefaultURI(conf, &name) < 0)
if (defname && *defname) { goto failed;
VIR_DEBUG("Using LIBVIRT_DEFAULT_URI %s", defname);
name = defname;
} else {
name = NULL;
}
}
if (name) { if (name) {
char *alias = NULL; char *alias = NULL;
...@@ -1124,7 +1162,7 @@ do_open (const char *name, ...@@ -1124,7 +1162,7 @@ do_open (const char *name,
name = "xen:///"; name = "xen:///";
if (!(flags & VIR_CONNECT_NO_ALIASES) && if (!(flags & VIR_CONNECT_NO_ALIASES) &&
virConnectOpenResolveURIAlias(name, &alias) < 0) virConnectOpenResolveURIAlias(conf, name, &alias) < 0)
goto failed; goto failed;
ret->uri = virURIParse (alias ? alias : name); ret->uri = virURIParse (alias ? alias : name);
...@@ -1308,9 +1346,12 @@ do_open (const char *name, ...@@ -1308,9 +1346,12 @@ do_open (const char *name,
} }
} }
virConfFree(conf);
return ret; return ret;
failed: failed:
virConfFree(conf);
virUnrefConnect(ret); virUnrefConnect(ret);
return NULL; return NULL;
...@@ -1325,11 +1366,11 @@ failed: ...@@ -1325,11 +1366,11 @@ failed:
* *
* Returns a pointer to the hypervisor connection or NULL in case of error * 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 * If @name is NULL, if the LIBVIRT_DEFAULT_URI environment variable is set,
* default driver to activate. This involves trying each hypervisor * then it will be used. Otherwise if the client configuration file
* in turn until one successfully opens. If the LIBVIRT_DEFAULT_URI * has the "uri_default" parameter set, then it will be used. Finally
* environment variable is set, then it will be used in preference * probing will be done to determine a suitable default driver to activate.
* to probing for a driver. * This involves trying each hypervisor in turn until one successfully opens.
* *
* If connecting to an unprivileged hypervisor driver which requires * If connecting to an unprivileged hypervisor driver which requires
* the libvirtd daemon to be active, it will automatically be launched * the libvirtd daemon to be active, it will automatically be launched
......
...@@ -10,3 +10,9 @@ ...@@ -10,3 +10,9 @@
# "hail=qemu+ssh://root@hail.cloud.example.com/system", # "hail=qemu+ssh://root@hail.cloud.example.com/system",
# "sleet=qemu+ssh://root@sleet.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"
...@@ -2632,7 +2632,16 @@ The file to log virsh debug messages. ...@@ -2632,7 +2632,16 @@ The file to log virsh debug messages.
=item VIRSH_DEFAULT_CONNECT_URI =item VIRSH_DEFAULT_CONNECT_URI
The hypervisor to connect to by default. Set this to a URI, in the same 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<LIBVIRT_DEFAULT_URI> 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<connect> option. This overrides
the default URI set in any client config file and prevents libvirt
from probing for drivers.
=item VISUAL =item VISUAL
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册