diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 4f78b7b43d..413c44ac61 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -27806,27 +27806,8 @@ virDomainDefFormatInternalSetRootName(virDomainDef *def,
virBufferEscapeString(buf, "%s\n",
def->description);
- if (def->metadata) {
- g_autoptr(xmlBuffer) xmlbuf = NULL;
- int oldIndentTreeOutput = xmlIndentTreeOutput;
-
- /* Indentation on output requires that we previously set
- * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2
- * spaces per level of indentation of intermediate elements,
- * but no leading indentation before the starting element.
- * Thankfully, libxml maps what looks like globals into
- * thread-local uses, so we are thread-safe. */
- xmlIndentTreeOutput = 1;
- xmlbuf = virXMLBufferCreate();
-
- if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
- virBufferGetIndent(buf) / 2, 1) < 0) {
- xmlIndentTreeOutput = oldIndentTreeOutput;
- return -1;
- }
- virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
- xmlIndentTreeOutput = oldIndentTreeOutput;
- }
+ if (virXMLFormatMetadata(buf, def->metadata) < 0)
+ return -1;
if (virDomainDefHasMemoryHotplug(def)) {
virBufferAsprintf(buf,
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index a9eadff29c..b10ff5c7a8 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -2486,27 +2486,8 @@ virNetworkDefFormatBuf(virBuffer *buf,
virUUIDFormat(uuid, uuidstr);
virBufferAsprintf(buf, "%s\n", uuidstr);
- if (def->metadata) {
- g_autoptr(xmlBuffer) xmlbuf = NULL;
- int oldIndentTreeOutput = xmlIndentTreeOutput;
-
- /* Indentation on output requires that we previously set
- * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2
- * spaces per level of indentation of intermediate elements,
- * but no leading indentation before the starting element.
- * Thankfully, libxml maps what looks like globals into
- * thread-local uses, so we are thread-safe. */
- xmlIndentTreeOutput = 1;
- xmlbuf = virXMLBufferCreate();
-
- if (xmlNodeDump(xmlbuf, def->metadata->doc, def->metadata,
- virBufferGetIndent(buf) / 2, 1) < 0) {
- xmlIndentTreeOutput = oldIndentTreeOutput;
- return -1;
- }
- virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
- xmlIndentTreeOutput = oldIndentTreeOutput;
- }
+ if (virXMLFormatMetadata(buf, def->metadata) < 0)
+ return -1;
if (def->forward.type != VIR_NETWORK_FORWARD_NONE) {
const char *dev = NULL;
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e1aef5267e..37515f80ec 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3555,6 +3555,7 @@ virXMLCheckIllegalChars;
virXMLExtractNamespaceXML;
virXMLFormatElement;
virXMLFormatElementEmpty;
+virXMLFormatMetadata;
virXMLNewNode;
virXMLNodeContentString;
virXMLNodeNameEqual;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 8dcece704a..062a5402f6 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -1707,6 +1707,49 @@ virXMLFormatElement(virBuffer *buf,
}
+/**
+ * virXMLFormatMetadata:
+ * @buf: the parent buffer where the element will be placed
+ * @metadata: pointer to metadata node
+ *
+ * Helper to format metadata element. If @metadata is NULL then
+ * this function is a NOP.
+ *
+ * Returns: 0 on success,
+ * -1 otherwise.
+ */
+int
+virXMLFormatMetadata(virBuffer *buf,
+ xmlNodePtr metadata)
+{
+ g_autoptr(xmlBuffer) xmlbuf = NULL;
+ int oldIndentTreeOutput = xmlIndentTreeOutput;
+
+ if (!metadata)
+ return 0;
+
+ /* Indentation on output requires that we previously set
+ * xmlKeepBlanksDefault to 0 when parsing; also, libxml does 2
+ * spaces per level of indentation of intermediate elements,
+ * but no leading indentation before the starting element.
+ * Thankfully, libxml maps what looks like globals into
+ * thread-local uses, so we are thread-safe. */
+ xmlIndentTreeOutput = 1;
+ xmlbuf = virXMLBufferCreate();
+
+ if (xmlNodeDump(xmlbuf, metadata->doc, metadata,
+ virBufferGetIndent(buf) / 2, 1) < 0) {
+ xmlIndentTreeOutput = oldIndentTreeOutput;
+ return -1;
+ }
+
+ virBufferAsprintf(buf, "%s\n", (char *) xmlBufferContent(xmlbuf));
+ xmlIndentTreeOutput = oldIndentTreeOutput;
+
+ return 0;
+}
+
+
void
virXPathContextNodeRestore(virXPathContextNodeSave *save)
{
diff --git a/src/util/virxml.h b/src/util/virxml.h
index ed02abd2e9..0bb0d1c118 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -333,6 +333,9 @@ virXMLFormatElementEmpty(virBuffer *buf,
virBuffer *attrBuf,
virBuffer *childBuf);
+int
+virXMLFormatMetadata(virBuffer *buf,
+ xmlNodePtr metadata);
struct _virXPathContextNodeSave {
xmlXPathContextPtr ctxt;