[libvirt] [PATCH 1/4] xml: Add element <title> to allow short description of domains

Peter Krempa pkrempa at redhat.com
Fri Jan 27 17:22:16 UTC 2012


This patch adds a new element <title> to the domain XML. This attribute
can hold a short title defined by the user to ease the identification of
domains. The title is limited to 40 bytes and can't contain newlines.

 *include/libvirt/libvirt.h.in
        - add macro specifying maximal note length
 *docs/formatdomain.html.in
 *docs/schemas/domaincommon.rng
        - add schema grammar for the new element and documentation

  *src/conf/domain_conf.c
  *src/conf/domain_conf.h
        - add field to hold the new attribute
        - add code to parse and create XML with the new attribute
---
This patch is basicaly identical with those in previous versions. I just
added a macro to set the maximal length of the <title> element to ease
future possible changes.

 docs/formatdomain.html.in                          |    6 ++++
 docs/schemas/domaincommon.rng                      |   13 +++++++++-
 include/libvirt/libvirt.h.in                       |    6 ++++
 src/conf/domain_conf.c                             |   19 ++++++++++++++
 src/conf/domain_conf.h                             |    1 +
 .../qemu-simple-description-title.xml              |   27 ++++++++++++++++++++
 tests/qemuxml2argvdata/qemuxml2argv-minimal.xml    |    5 +++
 7 files changed, 76 insertions(+), 1 deletions(-)
 create mode 100644 tests/domainschemadata/qemu-simple-description-title.xml

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index 464c4a3..6ccff06 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -37,6 +37,7 @@
     <app1:foo xmlns:app1="http://app1.org/app1/">..</app1:foo>
     <app2:bar xmlns:app2="http://app1.org/app2/">..</app2:bar>
   </metadata>
+  <title>A short description - title - of the domain</title>
   ...</pre>

     <dl>
@@ -72,6 +73,11 @@
         (if the application needs structure, they should have
         sub-elements to their namespace
         element). <span class="since">Since 0.9.10</span></dd>
+
+      <dt><code>title</code></dt>
+      <dd>The optional element <code>title</code> provides space for a
+      shorter description, capped at 40 bytes and with no newline,
+      <span class="since">since 0.9.10</span>.</dd>
     </dl>

     <h3><a name="elementsOS">Operating system booting</a></h3>
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index 2e53e14..17e7b65 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -6,7 +6,7 @@
   <include href='networkcommon.rng'/>

   <!--
-    description element, may be placed anywhere under the root
+    description and title element, may be placed anywhere under the root
     -->
   <define name="description">
     <element name="description">
@@ -14,6 +14,14 @@
     </element>
   </define>

+  <define name="title">
+    <element name="title">
+      <data type="string">
+        <param name='pattern'>[^\n]{0,40}</param>
+      </data>
+    </element>
+  </define>
+
   <!--
       We handle only document defining a domain
     -->
@@ -29,6 +37,9 @@
           <ref name="metadata"/>
         </optional>
         <optional>
+          <ref name="title"/>
+        </optional>
+        <optional>
           <ref name="cpu"/>
         </optional>
         <optional>
diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
index e99cd00..9a51c4f 100644
--- a/include/libvirt/libvirt.h.in
+++ b/include/libvirt/libvirt.h.in
@@ -1474,6 +1474,12 @@ int                     virDomainGetMaxVcpus    (virDomainPtr domain);
 int                     virDomainGetSecurityLabel (virDomainPtr domain,
                                                    virSecurityLabelPtr seclabel);

+/**
+ * VIR_DOMAIN_TITLE_LENGTH:
+ *
+ * Maximum length of the string in the title field.
+ */
+#define VIR_DOMAIN_TITLE_LENGTH 40
 /*
  * XML domain description
  */
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 978e91c..51a45ff 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1479,6 +1479,7 @@ void virDomainDefFree(virDomainDefPtr def)
     VIR_FREE(def->cpumask);
     VIR_FREE(def->emulator);
     VIR_FREE(def->description);
+    VIR_FREE(def->title);

     virBlkioDeviceWeightArrayClear(def->blkio.devices,
                                    def->blkio.ndevices);
@@ -7093,6 +7094,22 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
         VIR_FREE(tmp);
     }

+    /* Extract short description of domain (title) */
+    def->title = virXPathString("string(./title[1])", ctxt);
+    if (def->title) {
+        if (strchr(def->title, '\n')) {
+            virDomainReportError(VIR_ERR_XML_ERROR,
+                               "%s", _("Domain title can't contain newlines"));
+            goto error;
+        }
+
+        if (strlen(def->title) > VIR_DOMAIN_TITLE_LENGTH) {
+            virDomainReportError(VIR_ERR_XML_ERROR,
+                                 "%s", _("Domain title too long"));
+            goto error;
+        }
+    }
+
     /* Extract documentation if present */
     def->description = virXPathString("string(./description[1])", ctxt);

@@ -11455,6 +11472,8 @@ virDomainDefFormatInternal(virDomainDefPtr def,
         xmlIndentTreeOutput = oldIndentTreeOutput;
     }

+    virBufferEscapeString(buf, "  <title>%s</title>\n", def->title);
+
     virBufferAsprintf(buf, "  <memory>%lu</memory>\n", def->mem.max_balloon);
     virBufferAsprintf(buf, "  <currentMemory>%lu</currentMemory>\n",
                       def->mem.cur_balloon);
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 1d2fb81..46b392e 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1422,6 +1422,7 @@ struct _virDomainDef {
     int id;
     unsigned char uuid[VIR_UUID_BUFLEN];
     char *name;
+    char *title;
     char *description;

     struct {
diff --git a/tests/domainschemadata/qemu-simple-description-title.xml b/tests/domainschemadata/qemu-simple-description-title.xml
new file mode 100644
index 0000000..a8a9cac
--- /dev/null
+++ b/tests/domainschemadata/qemu-simple-description-title.xml
@@ -0,0 +1,27 @@
+<domain type='qemu'>
+  <name>qemu-demo</name>
+  <uuid>603cc28c-9841-864e-0949-8cc7d3bae9f8</uuid>
+  <memory>65536</memory>
+  <currentMemory>65536</currentMemory>
+  <title>A short description of this domain</title>
+  <description>
+      A longer explanation that this domain is a test domain
+      for validating domain schemas.
+  </description>
+  <vcpu>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc-0.14'>hvm</type>
+  </os>
+  <features>
+    <acpi/>
+    <apic/>
+    <pae/>
+  </features>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>restart</on_crash>
+  <devices>
+    <emulator>/usr/bin/qemu-kvm</emulator>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-minimal.xml b/tests/qemuxml2argvdata/qemuxml2argv-minimal.xml
index 2f13d46..6cb0b31 100644
--- a/tests/qemuxml2argvdata/qemuxml2argv-minimal.xml
+++ b/tests/qemuxml2argvdata/qemuxml2argv-minimal.xml
@@ -1,6 +1,11 @@
 <domain type='qemu'>
   <name>QEMUGuest1</name>
   <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <description>
+      A test of qemu's minimal configuration.
+      This test also tests the description and title elements.
+  </description>
+  <title>A description of the test machine.</title>
   <memory>219100</memory>
   <currentMemory>219100</currentMemory>
   <vcpu cpuset='1-4,8-20,525'>1</vcpu>
-- 
1.7.3.4




More information about the libvir-list mailing list