[libvirt PATCH v3 12/18] qemu: include nbdkit state in private xml

Jonathon Jongsma jjongsma at redhat.com
Thu Oct 20 21:59:03 UTC 2022


Add xml to the private data for a disk source to represent the nbdkit
process so that the state can be re-created if the libvirt daemon is
restarted. Format:

   <nbdkit>
     <pidfile>/path/to/nbdkit.pid</pidfile>
     <socketfile>/path/to/nbdkit.socket</socketfile>
   </nbdkit>

Signed-off-by: Jonathon Jongsma <jjongsma at redhat.com>
---
 src/qemu/qemu_domain.c | 53 ++++++++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_nbdkit.c | 21 +++++++++++++++++
 src/qemu/qemu_nbdkit.h |  5 ++++
 3 files changed, 79 insertions(+)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index ace7ae4c61..2ae87392cb 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -1849,6 +1849,34 @@ qemuStorageSourcePrivateDataAssignSecinfo(qemuDomainSecretInfo **secinfo,
 }
 
 
+static int
+qemuStorageSourcePrivateDataParseNbdkit(xmlNodePtr node,
+                                        xmlXPathContextPtr ctxt,
+                                        virStorageSource *src)
+{
+    qemuDomainStorageSourcePrivate *srcpriv = qemuDomainStorageSourcePrivateFetch(src);
+    g_autofree char *pidfile = NULL;
+    g_autofree char *socketfile = NULL;
+    VIR_XPATH_NODE_AUTORESTORE(ctxt);
+
+    if (srcpriv->nbdkitProcess)
+        return 0;
+
+    ctxt->node = node;
+
+    if (!(pidfile = virXPathString("string(./pidfile)", ctxt)))
+        return -1;
+
+    if (!(socketfile = virXPathString("string(./socketfile)", ctxt)))
+        return -1;
+
+    if (!(srcpriv->nbdkitProcess = qemuNbdkitProcessLoad(src, pidfile, socketfile)))
+        return -1;
+
+    return 0;
+}
+
+
 static int
 qemuStorageSourcePrivateDataParse(xmlXPathContextPtr ctxt,
                                   virStorageSource *src)
@@ -1859,6 +1887,7 @@ qemuStorageSourcePrivateDataParse(xmlXPathContextPtr ctxt,
     g_autofree char *httpcookiealias = NULL;
     g_autofree char *tlskeyalias = NULL;
     g_autofree char *thresholdEventWithIndex = NULL;
+    xmlNodePtr nbdkitnode = NULL;
 
     src->nodestorage = virXPathString("string(./nodenames/nodename[@type='storage']/@name)", ctxt);
     src->nodeformat = virXPathString("string(./nodenames/nodename[@type='format']/@name)", ctxt);
@@ -1902,6 +1931,10 @@ qemuStorageSourcePrivateDataParse(xmlXPathContextPtr ctxt,
         virTristateBoolTypeFromString(thresholdEventWithIndex) == VIR_TRISTATE_BOOL_YES)
         src->thresholdEventWithIndex = true;
 
+    if ((nbdkitnode = virXPathNode("nbdkit", ctxt))) {
+        if (qemuStorageSourcePrivateDataParseNbdkit(nbdkitnode, ctxt, src) < 0)
+            return -1;
+    }
     return 0;
 }
 
@@ -1919,6 +1952,23 @@ qemuStorageSourcePrivateDataFormatSecinfo(virBuffer *buf,
 }
 
 
+static void
+qemuStorageSourcePrivateDataFormatNbdkit(qemuNbdkitProcess *nbdkit,
+                                         virBuffer *buf)
+{
+    g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf);
+
+    if (!nbdkit)
+        return;
+
+    virBufferEscapeString(&childBuf, "<pidfile>%s</pidfile>\n",
+                          nbdkit->pidfile);
+    virBufferEscapeString(&childBuf, "<socketfile>%s</socketfile>\n",
+                          nbdkit->socketfile);
+    virXMLFormatElement(buf, "nbdkit", NULL, &childBuf);
+}
+
+
 static int
 qemuStorageSourcePrivateDataFormat(virStorageSource *src,
                                    virBuffer *buf)
@@ -1957,6 +2007,9 @@ qemuStorageSourcePrivateDataFormat(virStorageSource *src,
     if (src->thresholdEventWithIndex)
         virBufferAddLit(buf, "<thresholdEvent indexUsed='yes'/>\n");
 
+    if (srcPriv)
+        qemuStorageSourcePrivateDataFormatNbdkit(srcPriv->nbdkitProcess, buf);
+
     return 0;
 }
 
diff --git a/src/qemu/qemu_nbdkit.c b/src/qemu/qemu_nbdkit.c
index 59c452a15e..5b47a15112 100644
--- a/src/qemu/qemu_nbdkit.c
+++ b/src/qemu/qemu_nbdkit.c
@@ -597,6 +597,27 @@ qemuNbdkitProcessNew(virStorageSource *source,
 }
 
 
+qemuNbdkitProcess *
+qemuNbdkitProcessLoad(virStorageSource *source,
+                      const char *pidfile,
+                      const char *socketfile)
+{
+    int rc;
+    g_autoptr(qemuNbdkitProcess) nbdkit = qemuNbdkitProcessNew(source, pidfile, socketfile);
+
+    if ((rc = virPidFileReadPath(nbdkit->pidfile, &nbdkit->pid)) < 0)
+        return NULL;
+
+    if (virProcessKill(nbdkit->pid, 0) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("nbdkit process %i is not alive"), nbdkit->pid);
+        return NULL;
+    }
+
+    return g_steal_pointer(&nbdkit);
+}
+
+
 bool
 qemuNbdkitInitStorageSource(qemuNbdkitCaps *caps,
                             virStorageSource *source,
diff --git a/src/qemu/qemu_nbdkit.h b/src/qemu/qemu_nbdkit.h
index 30cab744b0..ca7f1dcf31 100644
--- a/src/qemu/qemu_nbdkit.h
+++ b/src/qemu/qemu_nbdkit.h
@@ -89,4 +89,9 @@ qemuNbdkitProcessStop(qemuNbdkitProcess *proc);
 void
 qemuNbdkitProcessFree(qemuNbdkitProcess *proc);
 
+qemuNbdkitProcess *
+qemuNbdkitProcessLoad(virStorageSource *source,
+                      const char *pidfile,
+                      const char *socketfile);
+
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuNbdkitProcess, qemuNbdkitProcessFree);
-- 
2.37.3



More information about the libvir-list mailing list