[PATCH v4 11/13] conf: Add support to parse/format <source> for NVRAM

Peter Krempa pkrempa at redhat.com
Fri Jun 3 11:48:43 UTC 2022


From: Rohit Kumar <rohit.kumar3 at nutanix.com>

This patch introduces the logic to format and parse remote NVRAM.

Update NVRAM element schema, and docs for supporting network backed
NVRAM. NVRAM backed over network would give the flexibility to start
the VM on any host without having to worry about where to get the latest
nvram image.

<nvram type='network'>
  <source protocol='iscsi' name='iqn.2013-07.com.example:iscsi-nopool/0'>
    <host name='example.com' port='6000'/>
  </source>
</nvram>

or

<nvram type='file'>
  <source file='/var/lib/libvirt/nvram/guest_VARS.fd'/>
</nvram>

In the qemu driver we will support the new definition only with qemu's
supporting -blockdev.

Signed-off-by: Prerna Saxena <prerna.saxena at nutanix.com>
Signed-off-by: Florian Schmidt <flosch at nutanix.com>
Signed-off-by: Rohit Kumar <rohit.kumar3 at nutanix.com>
Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 docs/formatdomain.rst             |  37 ++++++++++
 src/conf/domain_conf.c            | 116 ++++++++++++++++++++++++------
 src/conf/domain_conf.h            |   1 +
 src/conf/schemas/domaincommon.rng |   9 ++-
 src/qemu/qemu_validate.c          |   7 ++
 5 files changed, 146 insertions(+), 24 deletions(-)

diff --git a/docs/formatdomain.rst b/docs/formatdomain.rst
index 312b605a8b..8a9da07612 100644
--- a/docs/formatdomain.rst
+++ b/docs/formatdomain.rst
@@ -135,6 +135,34 @@ harddisk, cdrom, network) determining where to obtain/find the boot image.
    </os>
    ...

+   <!-- QEMU with UEFI manual firmware, secure boot and with NVRAM type 'file'-->
+   ...
+   <os>
+     <type>hvm</type>
+     <loader readonly='yes' secure='yes' type='pflash'>/usr/share/OVMF/OVMF_CODE.fd</loader>
+     <nvram type='file' template='/usr/share/OVMF/OVMF_VARS.fd'>
+       <source file='/var/lib/libvirt/nvram/guest_VARS.fd'/>
+     <boot dev='hd'/>
+   </os>
+   ...
+
+   <!-- QEMU with UEFI manual firmware, secure boot and with network backed NVRAM'-->
+   ...
+   <os>
+     <type>hvm</type>
+     <loader readonly='yes' secure='yes' type='pflash'>/usr/share/OVMF/OVMF_CODE.fd</loader>
+     <nvram type='network'>
+       <source protocol='iscsi' name='iqn.2013-07.com.example:iscsi-nopool/0'>
+         <host name='example.com' port='6000'/>
+         <auth username='myname'>
+           <secret type='iscsi' usage='mycluster_myname'/>
+         </auth>
+       </source>
+     </nvram>
+     <boot dev='hd'/>
+   </os>
+   ...
+
    <!-- QEMU with automatic UEFI firmware and secure boot -->
    ...
    <os firmware='efi'>
@@ -224,6 +252,15 @@ harddisk, cdrom, network) determining where to obtain/find the boot image.
    if the NVRAM file has been created by libvirt it is left behind and it is
    management application's responsibility to save and remove file (if needed to
    be persistent). :since:`Since 1.2.8`
+
+   :since:`Since 8.5.0`,  it's possible for the element to have ``type`` attribute
+   (accepts values ``file``, ``block`` and ``network``) in that case the NVRAM
+   storage is described by a ``<source>`` sub-element with the same syntax as
+   ``disk``'s source. See `Hard drives, floppy disks, CDROMs`_.
+
+   **Note:** ``network`` backed NVRAM the variables are not instantiated from
+   the ``template`` and it's user's responsibility to provide a valid NVRAM image.
+
 ``boot``
    The ``dev`` attribute takes one of the values "fd", "hd", "cdrom" or
    "network" and is used to specify the next boot device to consider. The
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 05b2518a71..b34513f943 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -17951,6 +17951,51 @@ virDomainLoaderDefParseXML(xmlNodePtr node,
 }


+static int
+virDomainNvramDefParseXML(virDomainLoaderDef *loader,
+                          xmlXPathContextPtr ctxt,
+                          virDomainXMLOption *xmlopt,
+                          unsigned int flags)
+{
+    g_autofree char *nvramType = virXPathString("string(./os/nvram/@type)", ctxt);
+    g_autoptr(virStorageSource) src = virStorageSourceNew();
+
+    src->type = VIR_STORAGE_TYPE_FILE;
+    src->format = VIR_STORAGE_FILE_RAW;
+
+    if (!nvramType) {
+        char *nvramPath = NULL;
+
+        if (!(nvramPath = virXPathString("string(./os/nvram[1])", ctxt)))
+            return 0; /* no nvram */
+
+        src->path = nvramPath;
+    } else {
+        xmlNodePtr sourceNode;
+
+        if ((src->type = virStorageTypeFromString(nvramType)) <= 0) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                           _("unknown disk type '%s'"), nvramType);
+            return -1;
+        }
+
+        if (!(sourceNode = virXPathNode("./os/nvram/source[1]", ctxt))) {
+            virReportError(VIR_ERR_XML_ERROR, "%s",
+                           _("Missing source element for nvram"));
+            return -1;
+        }
+
+        if (virDomainStorageSourceParse(sourceNode, ctxt, src, flags, xmlopt) < 0)
+            return -1;
+
+        loader->newStyleNVRAM = true;
+    }
+
+    loader->nvram = g_steal_pointer(&src);
+    return 0;
+}
+
+
 static int
 virDomainSchedulerParseCommonAttrs(xmlNodePtr node,
                                    virProcessSchedPolicy *policy,
@@ -18336,11 +18381,12 @@ virDomainDefParseBootFirmwareOptions(virDomainDef *def,

 static int
 virDomainDefParseBootLoaderOptions(virDomainDef *def,
-                                   xmlXPathContextPtr ctxt)
+                                   xmlXPathContextPtr ctxt,
+                                   virDomainXMLOption *xmlopt,
+                                   unsigned int flags)
 {
     xmlNodePtr loader_node = virXPathNode("./os/loader[1]", ctxt);
     const bool fwAutoSelect = def->os.firmware != VIR_DOMAIN_OS_DEF_FIRMWARE_NONE;
-    g_autofree char *nvramPath = NULL;

     if (!loader_node)
         return 0;
@@ -18352,12 +18398,8 @@ virDomainDefParseBootLoaderOptions(virDomainDef *def,
                                    fwAutoSelect) < 0)
         return -1;

-    if ((nvramPath = virXPathString("string(./os/nvram[1])", ctxt))) {
-        def->os.loader->nvram = virStorageSourceNew();
-        def->os.loader->nvram->path = g_steal_pointer(&nvramPath);
-        def->os.loader->nvram->type = VIR_STORAGE_TYPE_FILE;
-        def->os.loader->nvram->format = VIR_STORAGE_FILE_RAW;
-    }
+    if (virDomainNvramDefParseXML(def->os.loader, ctxt, xmlopt, flags) < 0)
+        return -1;

     if (!fwAutoSelect)
         def->os.loader->nvramTemplate = virXPathString("string(./os/nvram[1]/@template)", ctxt);
@@ -18412,7 +18454,9 @@ virDomainDefParseBootAcpiOptions(virDomainDef *def,

 static int
 virDomainDefParseBootOptions(virDomainDef *def,
-                             xmlXPathContextPtr ctxt)
+                             xmlXPathContextPtr ctxt,
+                             virDomainXMLOption *xmlopt,
+                             unsigned int flags)
 {
     /*
      * Booting options for different OS types....
@@ -18430,7 +18474,7 @@ virDomainDefParseBootOptions(virDomainDef *def,
         if (virDomainDefParseBootFirmwareOptions(def, ctxt) < 0)
             return -1;

-        if (virDomainDefParseBootLoaderOptions(def, ctxt) < 0)
+        if (virDomainDefParseBootLoaderOptions(def, ctxt, xmlopt, flags) < 0)
             return -1;

         if (virDomainDefParseBootAcpiOptions(def, ctxt) < 0)
@@ -18446,7 +18490,7 @@ virDomainDefParseBootOptions(virDomainDef *def,
     case VIR_DOMAIN_OSTYPE_UML:
         virDomainDefParseBootKernelOptions(def, ctxt);

-        if (virDomainDefParseBootLoaderOptions(def, ctxt) < 0)
+        if (virDomainDefParseBootLoaderOptions(def, ctxt, xmlopt, flags) < 0)
             return -1;

         break;
@@ -19746,7 +19790,7 @@ virDomainDefParseXML(xmlXPathContextPtr ctxt,
     if (virDomainDefClockParse(def, ctxt) < 0)
         return NULL;

-    if (virDomainDefParseBootOptions(def, ctxt) < 0)
+    if (virDomainDefParseBootOptions(def, ctxt, xmlopt, flags) < 0)
         return NULL;

     /* analysis of the disk devices */
@@ -27100,26 +27144,48 @@ virDomainHugepagesFormat(virBuffer *buf,
 }


-static void
+static int
 virDomainLoaderDefFormatNvram(virBuffer *buf,
-                              virDomainLoaderDef *loader)
+                              virDomainLoaderDef *loader,
+                              virDomainXMLOption *xmlopt,
+                              unsigned int flags)
 {
     g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
-    g_auto(virBuffer) childBuf = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) childBufDirect = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) childBufChild = VIR_BUFFER_INIT_CHILD(buf);
+    virBuffer *childBuf = &childBufDirect;
+    bool childNewline = false;

     virBufferEscapeString(&attrBuf, " template='%s'", loader->nvramTemplate);
+
     if (loader->nvram) {
-        if (loader->nvram->type == VIR_STORAGE_TYPE_FILE)
-            virBufferEscapeString(&childBuf, "%s", loader->nvram->path);
+        virStorageSource *src = loader->nvram;
+
+        if (!loader->newStyleNVRAM) {
+            virBufferEscapeString(&childBufDirect, "%s", src->path);
+        } else {
+            childNewline = true;
+            childBuf = &childBufChild;
+
+            virBufferAsprintf(&attrBuf, " type='%s'", virStorageTypeToString(src->type));
+
+            if (virDomainDiskSourceFormat(&childBufChild, src, "source", 0,
+                                          false, flags, false, false, xmlopt) < 0)
+                return -1;
+        }
     }

-    virXMLFormatElementInternal(buf, "nvram", &attrBuf, &childBuf, false, false);
+    virXMLFormatElementInternal(buf, "nvram", &attrBuf, childBuf, false, childNewline);
+
+    return 0;
 }


-static void
+static int
 virDomainLoaderDefFormat(virBuffer *buf,
-                         virDomainLoaderDef *loader)
+                         virDomainLoaderDef *loader,
+                         virDomainXMLOption *xmlopt,
+                         unsigned int flags)
 {
     g_auto(virBuffer) loaderAttrBuf = VIR_BUFFER_INITIALIZER;
     g_auto(virBuffer) loaderChildBuf = VIR_BUFFER_INITIALIZER;
@@ -27140,7 +27206,10 @@ virDomainLoaderDefFormat(virBuffer *buf,

     virXMLFormatElementInternal(buf, "loader", &loaderAttrBuf, &loaderChildBuf, false, false);

-    virDomainLoaderDefFormatNvram(buf, loader);
+    if (virDomainLoaderDefFormatNvram(buf, loader, xmlopt, flags) < 0)
+        return -1;
+
+    return 0;
 }

 static void
@@ -28341,8 +28410,9 @@ virDomainDefFormatInternalSetRootName(virDomainDef *def,
     if (def->os.initgroup)
         virBufferAsprintf(buf, "<initgroup>%s</initgroup>\n", def->os.initgroup);

-    if (def->os.loader)
-        virDomainLoaderDefFormat(buf, def->os.loader);
+    if (def->os.loader &&
+        virDomainLoaderDefFormat(buf, def->os.loader, xmlopt, flags) < 0)
+        return -1;
     virBufferEscapeString(buf, "<kernel>%s</kernel>\n",
                           def->os.kernel);
     virBufferEscapeString(buf, "<initrd>%s</initrd>\n",
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 9ec81067c6..1ce6e855b6 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2254,6 +2254,7 @@ struct _virDomainLoaderDef {
     virDomainLoader type;
     virTristateBool secure;
     virStorageSource *nvram;
+    bool newStyleNVRAM;
     char *nvramTemplate;   /* user override of path to master nvram */
 };

diff --git a/src/conf/schemas/domaincommon.rng b/src/conf/schemas/domaincommon.rng
index cc598212a8..bf829f3a65 100644
--- a/src/conf/schemas/domaincommon.rng
+++ b/src/conf/schemas/domaincommon.rng
@@ -333,7 +333,14 @@
               </attribute>
             </optional>
             <optional>
-              <ref name="absFilePath"/>
+              <choice>
+                <group>
+                  <ref name="absFilePath"/>
+                </group>
+                <group>
+                  <ref name="diskSource"/>
+                </group>
+              </choice>
             </optional>
           </element>
         </optional>
diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c
index 48b7d08684..2bbe198a11 100644
--- a/src/qemu/qemu_validate.c
+++ b/src/qemu/qemu_validate.c
@@ -576,6 +576,13 @@ qemuValidateDomainDefNvram(const virDomainDef *def,
     if (!src)
         return 0;

+    if (def->os.loader->newStyleNVRAM &&
+        !virQEMUCapsGet(qemuCaps, QEMU_CAPS_BLOCKDEV)) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("modern nvram specification is not supported by this qemu"));
+        return -1;
+    }
+
     switch (src->type) {
     case VIR_STORAGE_TYPE_FILE:
     case VIR_STORAGE_TYPE_BLOCK:
-- 
2.35.3



More information about the libvir-list mailing list