[libvirt PATCH v4 10/31] qemu: add functions to start and stop nbdkit

Jonathon Jongsma jjongsma at redhat.com
Fri Jan 20 22:03:04 UTC 2023


Add some helper functions to build a virCommand object and run the
nbdkit process for a given virStorageSource.

Signed-off-by: Jonathon Jongsma <jjongsma at redhat.com>
---
 src/qemu/qemu_nbdkit.c | 255 +++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_nbdkit.h |  10 ++
 2 files changed, 265 insertions(+)

diff --git a/src/qemu/qemu_nbdkit.c b/src/qemu/qemu_nbdkit.c
index 840710862e..34462f784a 100644
--- a/src/qemu/qemu_nbdkit.c
+++ b/src/qemu/qemu_nbdkit.c
@@ -24,6 +24,7 @@
 #include "virerror.h"
 #include "virlog.h"
 #include "virpidfile.h"
+#include "virtime.h"
 #include "virutil.h"
 #include "qemu_block.h"
 #include "qemu_conf.h"
@@ -667,6 +668,167 @@ qemuNbdkitInitStorageSource(qemuNbdkitCaps *caps,
 }
 
 
+static int
+qemuNbdkitProcessBuildCommandCurl(qemuNbdkitProcess *proc,
+                                  virCommand *cmd)
+{
+    g_autoptr(virURI) uri = qemuBlockStorageSourceGetURI(proc->source);
+    g_autofree char *uristring = virURIFormat(uri);
+
+    /* nbdkit plugin name */
+    virCommandAddArg(cmd, "curl");
+    if (proc->source->protocol == VIR_STORAGE_NET_PROTOCOL_HTTP) {
+        /* allow http to be upgraded to https via e.g. redirect */
+        g_autofree char* protocols = g_strdup_printf("%s,%s",
+                                                     virStorageNetProtocolTypeToString(proc->source->protocol),
+                                                     virStorageNetProtocolTypeToString(VIR_STORAGE_NET_PROTOCOL_HTTPS));
+        virCommandAddArgPair(cmd, "protocols", protocols);
+    } else {
+        virCommandAddArgPair(cmd, "protocols",
+                             virStorageNetProtocolTypeToString(proc->source->protocol));
+    }
+    virCommandAddArgPair(cmd, "url", uristring);
+
+    if (proc->source->auth) {
+        g_autoptr(virConnect) conn = virGetConnectSecret();
+        g_autofree uint8_t *secret = NULL;
+        size_t secretlen = 0;
+        g_autofree char *password = NULL;
+        int secrettype;
+        virStorageAuthDef *authdef = proc->source->auth;
+
+        virCommandAddArgPair(cmd, "user",
+                             proc->source->auth->username);
+
+        if ((secrettype = virSecretUsageTypeFromString(proc->source->auth->secrettype)) < 0) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("invalid secret type %s"),
+                           proc->source->auth->secrettype);
+            return -1;
+        }
+
+        if (virSecretGetSecretString(conn,
+                                     &authdef->seclookupdef,
+                                     secrettype,
+                                     &secret,
+                                     &secretlen) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("failed to get auth secret for storage"));
+            return -1;
+        }
+
+        /* ensure that the secret is a NULL-terminated string */
+        password = g_strndup((char*)secret, secretlen);
+
+        /* for now, just report an error rather than passing the password in
+         * cleartext on the commandline */
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Password not yet supported for nbdkit sources"));
+        return -1;
+    }
+
+    if (proc->source->ncookies > 0) {
+        /* for now, just report an error rather than passing cookies in
+         * cleartext on the commandline */
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Cookies not yet supported for nbdkit sources"));
+        return -1;
+    }
+
+    if (proc->source->sslverify == VIR_TRISTATE_BOOL_NO) {
+        virCommandAddArgPair(cmd, "sslverify", "false");
+    }
+
+    if (proc->source->timeout > 0) {
+        g_autofree char *timeout = g_strdup_printf("%llu", proc->source->timeout);
+        virCommandAddArgPair(cmd, "timeout", timeout);
+    }
+
+    return 0;
+}
+
+
+static int
+qemuNbdkitProcessBuildCommandSSH(qemuNbdkitProcess *proc,
+                                 virCommand *cmd)
+{
+    const char *user = NULL;
+    virStorageNetHostDef *host = &proc->source->hosts[0];
+    g_autofree char *portstr = g_strdup_printf("%u", host->port);
+
+    /* nbdkit plugin name */
+    virCommandAddArg(cmd, "ssh");
+
+    virCommandAddArgPair(cmd, "host", host->name);
+    virCommandAddArgPair(cmd, "port", portstr);
+    virCommandAddArgPair(cmd, "path", proc->source->path);
+
+    if (proc->source->auth)
+        user = proc->source->auth->username;
+    else if (proc->source->ssh_user)
+        user = proc->source->ssh_user;
+
+    if (user)
+        virCommandAddArgPair(cmd, "user", user);
+
+    if (proc->source->ssh_host_key_check_disabled)
+        virCommandAddArgPair(cmd, "verify-remote-host", "false");
+
+    return 0;
+}
+
+
+static virCommand *
+qemuNbdkitProcessBuildCommand(qemuNbdkitProcess *proc)
+{
+    g_autoptr(virCommand) cmd = virCommandNewArgList(proc->caps->path,
+                                                     "--unix",
+                                                     proc->socketfile,
+                                                     "--foreground",
+                                                     NULL);
+
+    if (proc->source->readonly)
+        virCommandAddArg(cmd, "--readonly");
+
+    if (qemuNbdkitCapsGet(proc->caps, QEMU_NBDKIT_CAPS_FILTER_READAHEAD) &&
+        proc->source->readahead > 0)
+        virCommandAddArgPair(cmd, "--filter", "readahead");
+
+    switch (proc->source->protocol) {
+        case VIR_STORAGE_NET_PROTOCOL_HTTP:
+        case VIR_STORAGE_NET_PROTOCOL_HTTPS:
+        case VIR_STORAGE_NET_PROTOCOL_FTP:
+        case VIR_STORAGE_NET_PROTOCOL_FTPS:
+        case VIR_STORAGE_NET_PROTOCOL_TFTP:
+            if (qemuNbdkitProcessBuildCommandCurl(proc, cmd) < 0)
+                return NULL;
+            break;
+        case VIR_STORAGE_NET_PROTOCOL_SSH:
+            if (qemuNbdkitProcessBuildCommandSSH(proc, cmd) < 0)
+                return NULL;
+            break;
+
+        case VIR_STORAGE_NET_PROTOCOL_NONE:
+        case VIR_STORAGE_NET_PROTOCOL_NBD:
+        case VIR_STORAGE_NET_PROTOCOL_RBD:
+        case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG:
+        case VIR_STORAGE_NET_PROTOCOL_GLUSTER:
+        case VIR_STORAGE_NET_PROTOCOL_ISCSI:
+        case VIR_STORAGE_NET_PROTOCOL_VXHS:
+        case VIR_STORAGE_NET_PROTOCOL_NFS:
+        case VIR_STORAGE_NET_PROTOCOL_LAST:
+            virReportError(VIR_ERR_NO_SUPPORT,
+                           _("protocol '%s' is not supported by nbdkit"),
+                           virStorageNetProtocolTypeToString(proc->source->protocol));
+            return NULL;
+    }
+
+    virCommandDaemonize(cmd);
+
+    return g_steal_pointer(&cmd);
+}
+
+
 void
 qemuNbdkitProcessFree(qemuNbdkitProcess *proc)
 {
@@ -675,3 +837,96 @@ qemuNbdkitProcessFree(qemuNbdkitProcess *proc)
     g_clear_object(&proc->caps);
     g_free(proc);
 }
+
+
+int
+qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
+                       virDomainObj *vm,
+                       virQEMUDriver *driver)
+{
+    g_autoptr(virCommand) cmd = NULL;
+    int rc;
+    int exitstatus = 0;
+    int cmdret = 0;
+    VIR_AUTOCLOSE errfd = -1;
+    virTimeBackOffVar timebackoff;
+    bool socketCreated = false;
+
+    if (!(cmd = qemuNbdkitProcessBuildCommand(proc)))
+        return -1;
+
+    VIR_DEBUG("starting nbdkit process for %s", proc->source->nodestorage);
+    virCommandSetErrorFD(cmd, &errfd);
+    virCommandSetPidFile(cmd, proc->pidfile);
+
+    if (qemuExtDeviceLogCommand(driver, vm, cmd, "nbdkit") < 0)
+        goto error;
+
+    if (qemuSecurityCommandRun(driver, vm, cmd, proc->user, proc->group, &exitstatus, &cmdret) < 0)
+        goto error;
+
+    if (cmdret < 0 || exitstatus != 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Could not start 'nbdkit'. exitstatus: %d"), exitstatus);
+        goto error;
+    }
+
+    if ((rc = virPidFileReadPath(proc->pidfile, &proc->pid)) < 0) {
+        virReportSystemError(-rc,
+                             _("Failed to read pidfile %s"),
+                             proc->pidfile);
+        goto error;
+    }
+
+    if (virTimeBackOffStart(&timebackoff, 1, 1000) < 0)
+        goto error;
+
+    while (virTimeBackOffWait(&timebackoff)) {
+        if ((socketCreated = virFileExists(proc->socketfile)))
+            break;
+
+        if (virProcessKill(proc->pid, 0) == 0)
+            continue;
+
+        goto error;
+    }
+
+    if (!socketCreated) {
+        virReportError(VIR_ERR_OPERATION_TIMEOUT, "%s",
+                       _("nbdkit socket did not show up"));
+        goto error;
+    }
+
+    return 0;
+
+ error:
+    if (errfd > 0) {
+        g_autofree char *errbuf = g_new0(char, 1024);
+        if (read(errfd, errbuf, 1024) > 0)
+            virReportError(VIR_ERR_OPERATION_FAILED,
+                           _("nbdkit failed to start and reported: %s"), errbuf);
+    }
+    qemuNbdkitProcessStop(proc);
+    return -1;
+}
+
+
+int
+qemuNbdkitProcessStop(qemuNbdkitProcess *proc)
+{
+    if (proc->pid < 0)
+        return 0;
+
+    VIR_DEBUG("Stopping nbdkit process %i", proc->pid);
+    unlink(proc->pidfile);
+    unlink(proc->socketfile);
+
+    if (virProcessKill(proc->pid, SIGTERM) < 0) {
+        virReportSystemError(errno, _("Failed to stop nbdkit process %i"), proc->pid);
+        return -1;
+    }
+
+    proc->pid = -1;
+
+    return 0;
+}
diff --git a/src/qemu/qemu_nbdkit.h b/src/qemu/qemu_nbdkit.h
index 8844bba13c..ccd418b7d3 100644
--- a/src/qemu/qemu_nbdkit.h
+++ b/src/qemu/qemu_nbdkit.h
@@ -38,6 +38,8 @@ typedef enum {
 
 VIR_ENUM_DECL(qemuNbdkitCaps);
 
+typedef struct _virQEMUDriver virQEMUDriver;
+
 qemuNbdkitCaps *
 qemuNbdkitCapsNew(const char *path);
 
@@ -74,6 +76,14 @@ struct _qemuNbdkitProcess {
     pid_t pid;
 };
 
+int
+qemuNbdkitProcessStart(qemuNbdkitProcess *proc,
+                       virDomainObj *vm,
+                       virQEMUDriver *driver);
+
+int
+qemuNbdkitProcessStop(qemuNbdkitProcess *proc);
+
 void
 qemuNbdkitProcessFree(qemuNbdkitProcess *proc);
 
-- 
2.39.0



More information about the libvir-list mailing list