Historically URIs handled by the remote driver will always connect to
the libvirtd UNIX socket. There will now be one daemon per driver, and
each of these has its own UNIX sockets to connect to.
It will still be possible to run the traditional monolithic libvirtd
though, which will have the original UNIX socket path.
In addition there is a virproxyd daemon that doesn't run any drivers,
but provides proxying for clients accessing libvirt over IP sockets, or
tunnelling to the legacy libvirtd UNIX socket path.
Finally when running inside a daemon, the remote driver must not reject
connections unconditionally. For example, the QEMU driver needs to be
able to connect to the network driver. The remote driver must thus be
willing to handle connections even when inside the daemon, provided no
local driver is registered.
This refactoring causes the remote driver to prefer connecting to the
per-driver daemons. The URI parameter "mode" accepts the values
"auto",
"direct" and "legacy" to control which daemons are connected to.
The client side libvirt.conf config file also supports a "remote_mode"
setting which is used if the URI parameter is not set.
If neither the config file or URI parameter set a mode, then "auto"
is used, whereby the client looks to see which sockets actually exist
right now.
The remote driver will only ever spawn the per-driver daemons, or
the legacy libvirtd. It won't ever try to spawn virtproxyd, as
that is only there for IP based connectivity, or for access from
legacy remote clients.
If connecting to a remote host over any kind of ssh tunnel, for now we
must assume only the legacy socket exists. A future patch will introduce
a netcat replacement that is tailored for libvirt to make remote
tunnelling easier.
The configure arg '--with-remote-default-mode=legacy|direct' allows
packagers to set a default at build time. If not given, it will default
to direct mode.
In RPM builds this is overriden, because before we can default to the
new daemons, we must get SELinux policy written & the timeframe for that
is unclear at this stage.
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
libvirt.spec.in | 1 +
m4/virt-driver-remote.m4 | 15 +++
src/driver.h | 2 +
src/libvirt.c | 24 +++++
src/remote/remote_driver.c | 207 ++++++++++++++++++++++++++++++++-----
src/remote/remote_driver.h | 3 -
6 files changed, 226 insertions(+), 26 deletions(-)
diff --git a/libvirt.spec.in b/libvirt.spec.in
index 3e897459f1..be4bbdfa1f 100644
--- a/libvirt.spec.in
+++ b/libvirt.spec.in
@@ -1170,6 +1170,7 @@ rm -f po/stamp-po
--without-xenapi \
--without-vz \
--without-bhyve \
+ --with-remote-default-mode=legacy \
--with-interface \
--with-network \
--with-storage-fs \
diff --git a/m4/virt-driver-remote.m4 b/m4/virt-driver-remote.m4
index c7af5b3fc6..67ba580446 100644
--- a/m4/virt-driver-remote.m4
+++ b/m4/virt-driver-remote.m4
@@ -19,6 +19,7 @@ dnl
AC_DEFUN([LIBVIRT_DRIVER_ARG_REMOTE], [
LIBVIRT_ARG_WITH_FEATURE([REMOTE], [remote driver], [yes])
+ LIBVIRT_ARG_WITH([REMOTE_DEFAULT_MODE], [remote driver default mode], [direct])
])
AC_DEFUN([LIBVIRT_DRIVER_CHECK_REMOTE], [
@@ -26,6 +27,20 @@ AC_DEFUN([LIBVIRT_DRIVER_CHECK_REMOTE], [
AC_DEFINE_UNQUOTED([WITH_REMOTE], 1, [whether Remote driver is enabled])
fi
AM_CONDITIONAL([WITH_REMOTE], [test "$with_remote" = "yes"])
+
+ case "$with_remote_default_mode" in
+ legacy)
+ REMOTE_DRIVER_MODE_DEFAULT=REMOTE_DRIVER_MODE_LEGACY
+ ;;
+ direct)
+ REMOTE_DRIVER_MODE_DEFAULT=REMOTE_DRIVER_MODE_DIRECT
+ ;;
+ *)
+ AC_MSG_ERROR([Unknown remote mode '$with_remote_default_mode'])
+ ;;
+ esac
+
+ AC_DEFINE_UNQUOTED([REMOTE_DRIVER_MODE_DEFAULT],[$REMOTE_DRIVER_MODE_DEFAULT], [Default
remote driver mode])
])
AC_DEFUN([LIBVIRT_DRIVER_RESULT_REMOTE], [
diff --git a/src/driver.h b/src/driver.h
index 898fb96df4..f7d667a03c 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -108,6 +108,8 @@ int virSetSharedNWFilterDriver(virNWFilterDriverPtr driver)
ATTRIBUTE_RETURN_CHE
int virSetSharedSecretDriver(virSecretDriverPtr driver) ATTRIBUTE_RETURN_CHECK;
int virSetSharedStorageDriver(virStorageDriverPtr driver) ATTRIBUTE_RETURN_CHECK;
+bool virHasDriverForURIScheme(const char *scheme);
+
int virDriverLoadModule(const char *name,
const char *regfunc,
bool required);
diff --git a/src/libvirt.c b/src/libvirt.c
index 4d6def2e0a..22efd4da95 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -601,6 +601,30 @@ virRegisterConnectDriver(virConnectDriverPtr driver,
}
+/**
+ * virHasDriverForURIScheme:
+ * @scheme: the URI scheme
+ *
+ * Determine if there is a driver registered that explicitly
+ * handles URIs with the scheme @scheme.
+ *
+ * Returns: true if a driver is registered
+ */
+bool virHasDriverForURIScheme(const char *scheme)
+{
+ size_t i, j;
+ for (i = 0; i < virConnectDriverTabCount; i++) {
+ if (!virConnectDriverTab[i]->uriSchemes)
+ continue;
+ for (j = 0; virConnectDriverTab[i]->uriSchemes[j]; j++) {
+ if (STREQ(virConnectDriverTab[i]->uriSchemes[j], scheme))
+ return true;
+ }
+ }
+
+ return false;
+}
+
/**
* virRegisterStateDriver:
* @driver: pointer to a driver block
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 6dc282c139..667a92714f 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -72,6 +72,22 @@ VIR_ENUM_IMPL(remoteDriverTransport,
REMOTE_DRIVER_TRANSPORT_LAST,
"tls", "unix", "ssh", "libssh2",
"ext", "tcp", "libssh");
+typedef enum {
+ /* Prefer per-driver virt*d daemons, but fallback to legacy libvirtd */
+ REMOTE_DRIVER_MODE_AUTO,
+ /* Always use the legacy libvirtd */
+ REMOTE_DRIVER_MODE_LEGACY,
+ /* Always use the per-driver virt*d daemons */
+ REMOTE_DRIVER_MODE_DIRECT,
+
+ REMOTE_DRIVER_MODE_LAST
+} remoteDriverMode;
+
+VIR_ENUM_DECL(remoteDriverMode);
+VIR_ENUM_IMPL(remoteDriverMode,
+ REMOTE_DRIVER_MODE_LAST,
+ "auto", "legacy", "direct");
+
#if SIZEOF_LONG < 8
# define HYPER_TO_TYPE(_type, _to, _from) \
do { \
@@ -92,6 +108,7 @@ VIR_ENUM_IMPL(remoteDriverTransport,
static bool inside_daemon;
+
struct private_data {
virMutex lock;
@@ -740,11 +757,12 @@ remoteConnectSupportsFeatureUnlocked(virConnectPtr conn,
static char *
-remoteGetUNIXSocket(remoteDriverTransport transport,
- unsigned int flags)
+remoteGetUNIXSocketHelper(remoteDriverTransport transport,
+ const char *sock_prefix,
+ unsigned int flags)
{
char *sockname = NULL;
- VIR_AUTOFREE(char *userdir);
+ VIR_AUTOFREE(char *) userdir = NULL;
if (flags & VIR_DRV_OPEN_REMOTE_USER) {
if (transport != REMOTE_DRIVER_TRANSPORT_UNIX) {
@@ -758,21 +776,126 @@ remoteGetUNIXSocket(remoteDriverTransport transport,
if (!(userdir = virGetUserRuntimeDirectory()))
return NULL;
- if (virAsprintf(&sockname,
- "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0)
+ if (virAsprintf(&sockname, "%s/%s-sock",
+ userdir, sock_prefix) < 0)
return NULL;
} else {
- if (VIR_STRDUP(sockname,
- flags & VIR_DRV_OPEN_REMOTE_RO ?
- LIBVIRTD_PRIV_UNIX_SOCKET_RO :
- LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
+ if (virAsprintf(&sockname, "%s/run/libvirt/%s-%s",
+ LOCALSTATEDIR, sock_prefix,
+ flags & VIR_DRV_OPEN_REMOTE_RO ?
+ "sock-ro" : "sock") < 0)
return NULL;
}
- VIR_DEBUG("Chosen UNIX sockname %s", sockname);
+ VIR_DEBUG("Built UNIX sockname %s for transport %s prefix %s flags=0x%x",
+ sockname, remoteDriverTransportTypeToString(transport),
+ sock_prefix, flags);
return sockname;
}
+
+static char *
+remoteGetUNIXSocket(remoteDriverTransport transport,
+ remoteDriverMode mode,
+ const char *driver,
+ char **daemon,
+ unsigned int flags)
+{
+ char *sock_name = NULL;
+ VIR_AUTOFREE(char *) direct_daemon = NULL;
+ VIR_AUTOFREE(char *) legacy_daemon = NULL;
+ VIR_AUTOFREE(char *) direct_sock_name = NULL;
+ VIR_AUTOFREE(char *) legacy_sock_name = NULL;
+
+ if (driver &&
+ virAsprintf(&direct_daemon, "virt%sd", driver) < 0)
+ return NULL;
+
+ if (VIR_STRDUP(legacy_daemon, "libvirtd") < 0)
+ return NULL;
+
+ if (driver &&
+ !(direct_sock_name = remoteGetUNIXSocketHelper(transport, direct_daemon,
flags)))
+ return NULL;
+
+ if (!(legacy_sock_name = remoteGetUNIXSocketHelper(transport, "libvirt",
flags)))
+ return NULL;
+
+ if (mode == REMOTE_DRIVER_MODE_AUTO) {
+ if (transport == REMOTE_DRIVER_TRANSPORT_UNIX) {
+ if (direct_sock_name && virFileExists(direct_sock_name)) {
+ mode = REMOTE_DRIVER_MODE_DIRECT;
+ } else if (virFileExists(legacy_sock_name)) {
+ mode = REMOTE_DRIVER_MODE_LEGACY;
+ } else if (driver) {
+ /*
+ * This constant comes from the configure script and
+ * maps to either the direct or legacy mode constant
+ */
+ mode = REMOTE_DRIVER_MODE_DEFAULT;
+ } else {
+ mode = REMOTE_DRIVER_MODE_LEGACY;
+ }
+ } else {
+ mode = REMOTE_DRIVER_MODE_LEGACY;
+ }
+ }
+
+ if (mode == REMOTE_DRIVER_MODE_LEGACY) {
+ sock_name = legacy_sock_name;
+ legacy_sock_name = NULL;
+ *daemon = legacy_daemon;
+ legacy_daemon = NULL;
+ } else if (mode == REMOTE_DRIVER_MODE_DIRECT) {
+ if (transport != REMOTE_DRIVER_TRANSPORT_UNIX) {
+ virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
+ _("Cannot use direct socket mode for %s
transport"),
+ remoteDriverTransportTypeToString(transport));
+ return NULL;
+ }
+
+ if (!direct_sock_name) {
+ virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+ _("Cannot use direct socket mode if no URI is
set"));
+ return NULL;
+ }
+
+ sock_name = direct_sock_name;
+ direct_sock_name = NULL;
+ *daemon = direct_daemon;
+ direct_daemon = NULL;
+ } else {
+ virReportEnumRangeError(remoteDriverMode, mode);
+ return NULL;
+ }
+
+ VIR_DEBUG("Chosen UNIX sockname %s daemon %s "
+ "for mode %s transport %s flags=0x%x",
+ sock_name, NULLSTR(*daemon),
+ remoteDriverModeTypeToString(mode),
+ remoteDriverTransportTypeToString(transport),
+ flags);
+ return sock_name;
+}
+
+
+#ifndef WIN32
+static const char *
+remoteGetDaemonPathEnv(void)
+{
+ /* We prefer a VIRTD_PATH env var to use for all daemons,
+ * but if it is not set we will fallback to LIBVIRTD_PATH
+ * for previous behaviour
+ */
+ if (virGetEnvBlockSUID("VIRTD_PATH") != NULL) {
+ return "VIRTD_PATH";
+ } else {
+ return "LIBVIRTD_PATH";
+ }
+}
+#endif /* WIN32 */
+
+
/*
* URIs that this driver needs to handle:
*
@@ -819,11 +942,20 @@ doRemoteOpen(virConnectPtr conn,
VIR_AUTOFREE(char *) sshauth = NULL;
VIR_AUTOFREE(char *) knownHostsVerify = NULL;
VIR_AUTOFREE(char *) knownHosts = NULL;
+ VIR_AUTOFREE(char *) mode_str = NULL;
+ VIR_AUTOFREE(char *) daemon_name = NULL;
bool sanity = true;
bool verify = true;
#ifndef WIN32
bool tty = true;
#endif
+ int mode;
+
+ if (inside_daemon && !conn->uri->server) {
+ mode = REMOTE_DRIVER_MODE_DIRECT;
+ } else {
+ mode = REMOTE_DRIVER_MODE_AUTO;
+ }
/* We handle *ALL* URIs here. The caller has rejected any
* URIs we don't care about */
@@ -908,7 +1040,7 @@ doRemoteOpen(virConnectPtr conn,
EXTRACT_URI_ARG_STR("known_hosts", knownHosts);
EXTRACT_URI_ARG_STR("known_hosts_verify", knownHostsVerify);
EXTRACT_URI_ARG_STR("tls_priority", tls_priority);
-
+ EXTRACT_URI_ARG_STR("mode", mode_str);
EXTRACT_URI_ARG_BOOL("no_sanity", sanity);
EXTRACT_URI_ARG_BOOL("no_verify", verify);
#ifndef WIN32
@@ -955,6 +1087,21 @@ doRemoteOpen(virConnectPtr conn,
goto failed;
}
+ if (conf && !mode_str &&
+ virConfGetValueString(conf, "remote_mode", &mode_str) < 0)
+ goto failed;
+
+ if (mode_str &&
+ (mode = remoteDriverModeTypeFromString(mode_str)) < 0)
+ goto failed;
+
+ /* Sanity check that nothing requested !direct mode by mistake */
+ if (inside_daemon && !conn->uri->server && mode !=
REMOTE_DRIVER_MODE_DIRECT) {
+ virReportError(VIR_ERR_INVALID_ARG, "%s",
+ _("Connections from inside daemon must be direct"));
+ return VIR_DRV_OPEN_ERROR;
+ }
+
VIR_DEBUG("proceeding with name = %s", name);
/* For ext transport, command is required. */
@@ -971,7 +1118,8 @@ doRemoteOpen(virConnectPtr conn,
transport == REMOTE_DRIVER_TRANSPORT_LIBSSH ||
transport == REMOTE_DRIVER_TRANSPORT_LIBSSH2) &&
!sockname &&
- !(sockname = remoteGetUNIXSocket(transport, flags)))
+ !(sockname = remoteGetUNIXSocket(transport, mode, driver_str,
+ &daemon_name, flags)))
goto failed;
VIR_DEBUG("Chosen UNIX socket %s", NULLSTR(sockname));
@@ -1060,13 +1208,15 @@ doRemoteOpen(virConnectPtr conn,
#ifndef WIN32
case REMOTE_DRIVER_TRANSPORT_UNIX:
- if ((flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) &&
- !(daemonPath = virFileFindResourceFull("libvirtd",
- NULL, NULL,
- abs_top_builddir "/src",
- SBINDIR,
- "LIBVIRTD_PATH")))
- goto failed;
+ if (flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) {
+ const char *env_name = remoteGetDaemonPathEnv();
+ if (!(daemonPath = virFileFindResourceFull(daemon_name,
+ NULL, NULL,
+ abs_top_builddir
"/src",
+ SBINDIR,
+ env_name)))
+ goto failed;
+ }
if (!(priv->client = virNetClientNewUNIX(sockname,
flags &
VIR_DRV_OPEN_REMOTE_AUTOSTART,
@@ -1177,7 +1327,7 @@ doRemoteOpen(virConnectPtr conn,
{
remote_connect_open_args args = { &name, flags };
- VIR_DEBUG("Trying to open URI %s", name);
+ VIR_DEBUG("Trying to open URI '%s'", name);
if (call(conn, priv, 0, REMOTE_PROC_CONNECT_OPEN,
(xdrproc_t) xdr_remote_connect_open_args, (char *) &args,
(xdrproc_t) xdr_void, (char *) NULL) == -1)
@@ -1279,9 +1429,20 @@ remoteConnectOpen(virConnectPtr conn,
remoteSplitURIScheme(conn->uri, &driver, &transport) < 0)
goto cleanup;
- if (inside_daemon && (!conn->uri || !conn->uri->server)) {
- ret = VIR_DRV_OPEN_DECLINED;
- goto cleanup;
+ if (inside_daemon) {
+ if (!conn->uri) {
+ ret = VIR_DRV_OPEN_DECLINED;
+ goto cleanup;
+ }
+
+ /* If there's a driver registered we must defer to that.
+ * If there isn't a driver, we must connect in "direct"
+ * mode - see doRemoteOpen */
+ if (!conn->uri->server &&
+ virHasDriverForURIScheme(driver)) {
+ ret = VIR_DRV_OPEN_DECLINED;
+ goto cleanup;
+ }
}
if (!(priv = remoteAllocPrivateData()))
diff --git a/src/remote/remote_driver.h b/src/remote/remote_driver.h
index 132e478ef3..1fab5a6cc4 100644
--- a/src/remote/remote_driver.h
+++ b/src/remote/remote_driver.h
@@ -31,9 +31,6 @@ unsigned long remoteVersion(void);
#define LIBVIRTD_LISTEN_ADDR NULL
#define LIBVIRTD_TLS_PORT "16514"
#define LIBVIRTD_TCP_PORT "16509"
-#define LIBVIRTD_PRIV_UNIX_SOCKET LOCALSTATEDIR "/run/libvirt/libvirt-sock"
-#define LIBVIRTD_PRIV_UNIX_SOCKET_RO LOCALSTATEDIR
"/run/libvirt/libvirt-sock-ro"
-#define LIBVIRTD_USER_UNIX_SOCKET "libvirt-sock"
/* Defaults for PKI directory. */
#define LIBVIRT_PKI_DIR SYSCONFDIR "/pki"
--
2.21.0