From 3e9b56113958dafa4cdb4ee12c807b4069a9b2c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Tue, 25 May 2021 10:45:06 +0100 Subject: [PATCH] remote: add support for probing drivers with modular daemons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the traditional libvirtd, the virConnectOpen call will probe active drivers server side to find which one to use when the URI is NULL/empty. With the modular daemons though, the remote client does not know which daemon to connect in the first place, so we can't rely on virConnectOpen probing. Currently the virtproxyd daemon has code to probe for a possible driver by looking at which sockets are listening or which binaries are installed. The remote client can thus connect to virtproxyd which in turn can connect to a real hypervisor driver. The virtproxyd probing code though isn't something that needs to live in virtproxyd. By moving it into the remote client we can get probing client side in all scenarios and avoid the extra trip via virtproxyd in the common case. Tested-by: Jim Fehlig Reviewed-by: Michal Privoznik Signed-off-by: Daniel P. Berrangé --- meson.build | 6 +++-- src/remote/remote_sockets.c | 53 +++++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/meson.build b/meson.build index 02900ff738..9caae5521b 100644 --- a/meson.build +++ b/meson.build @@ -1421,8 +1421,10 @@ if not get_option('driver_remote').disabled() endif endif -remote_default_mode = get_option('remote_default_mode').to_upper() -conf.set('REMOTE_DRIVER_MODE_DEFAULT', 'REMOTE_DRIVER_MODE_@0@'.format(remote_default_mode)) +remote_default_mode = get_option('remote_default_mode') +if remote_default_mode == 'direct' + conf.set('REMOTE_DRIVER_AUTOSTART_DIRECT', '1') +endif if not get_option('driver_libvirtd').disabled() use_libvirtd = true diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c index 85295f1bc2..2979576680 100644 --- a/src/remote/remote_sockets.c +++ b/src/remote/remote_sockets.c @@ -299,6 +299,9 @@ remoteGetUNIXSocket(remoteDriverTransport transport, g_autofree char *daemon_name = NULL; g_autofree char *direct_sock_name = NULL; g_autofree char *legacy_sock_name = NULL; +#ifdef REMOTE_DRIVER_AUTOSTART_DIRECT + g_autofree char *guessdriver = NULL; +#endif #ifndef WIN32 const char *env_name = remoteGetDaemonPathEnv(); #else @@ -310,12 +313,35 @@ remoteGetUNIXSocket(remoteDriverTransport transport, remoteDriverModeTypeToString(mode), driver, flags); +#ifdef REMOTE_DRIVER_AUTOSTART_DIRECT + if (!driver && mode != REMOTE_DRIVER_MODE_LEGACY) { + VIR_DEBUG("Client side modular daemon probe"); + /* + * If we don't have a driver (because URI is empty) + * in the direct case, we don't know which daemon + * to connect to. This logic attempts to be a rough + * equivalent of auto-probing from virConnectOpen + * in the libvirtd days. + */ + if (geteuid() != 0) { + if (remoteProbeSessionDriverFromSocket(false, &guessdriver) < 0) + return NULL; + + if (guessdriver == NULL && + remoteProbeSessionDriverFromBinary(&guessdriver) < 0) + return NULL; + } else { + if (remoteProbeSystemDriverFromSocket(flags & REMOTE_DRIVER_OPEN_RO, + &guessdriver) < 0) + return NULL; + } + driver = guessdriver; + } +#endif + if (driver) { direct_daemon = g_strdup_printf("virt%sd", driver); direct_sock_name = remoteGetUNIXSocketHelper(transport, direct_daemon, flags); - } else { - direct_daemon = g_strdup("virtproxyd"); - direct_sock_name = remoteGetUNIXSocketHelper(transport, "libvirt", flags); } legacy_daemon = g_strdup("libvirtd"); @@ -323,18 +349,29 @@ remoteGetUNIXSocket(remoteDriverTransport transport, if (mode == REMOTE_DRIVER_MODE_AUTO) { if (transport == REMOTE_DRIVER_TRANSPORT_UNIX) { + /* + * When locally accessing libvirtd, we pick legacy or + * modular daemons depending on which sockets we see + * existing. + */ 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 { - /* - * This constant comes from the configure script and - * maps to either the direct or legacy mode constant - */ - mode = REMOTE_DRIVER_MODE_DEFAULT; +#ifdef REMOTE_DRIVER_AUTOSTART_DIRECT + mode = REMOTE_DRIVER_MODE_DIRECT; +#else + mode = REMOTE_DRIVER_MODE_LEGACY; +#endif } } else { + /* + * When remotely accessing libvirtd, we always default to a legacy socket + * path, as there's no way for us to probe what's configured. This does + * not matter, since 'virt-ssh-helper' will be used if it is available + * and thus probe from context of the remote host + */ mode = REMOTE_DRIVER_MODE_LEGACY; } }