c401cc
From a3b40814f053009f0f40ec7b4ced3ef63be5d082 Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <a3b40814f053009f0f40ec7b4ced3ef63be5d082@dist-git>
c401cc
From: Eric Blake <eblake@redhat.com>
c401cc
Date: Wed, 26 Feb 2014 14:54:19 +0100
c401cc
Subject: [PATCH] storage: expose volume meta-type in XML
c401cc
c401cc
https://bugzilla.redhat.com/show_bug.cgi?id=1032370
c401cc
c401cc
I got annoyed at having to use both 'virsh vol-list $pool --details'
c401cc
AND 'virsh vol-dumpxml $vol $pool' to learn if I had populated
c401cc
the volume correctly.  Since two-thirds of the data present in
c401cc
virStorageVolGetInfo() already appears in virStorageVolGetXMLDesc(),
c401cc
this just adds the remaining piece of information, as:
c401cc
c401cc
<volume type='...'>
c401cc
  ...
c401cc
</volume>
c401cc
c401cc
* docs/formatstorage.html.in: Document new <volume type=...>.
c401cc
* docs/schemas/storagevol.rng (vol): Add it to RelaxNG.
c401cc
* src/conf/storage_conf.h (virStorageVolTypeToString): Declare.
c401cc
* src/conf/storage_conf.c (virStorageVolTargetDefFormat): Output
c401cc
the metatype.
c401cc
(virStorageVolDefParseXML): Parse it, for unit tests.
c401cc
* tests/storagevolxml2xmlout/vol-*.xml: Update tests to match.
c401cc
c401cc
Signed-off-by: Eric Blake <eblake@redhat.com>
c401cc
(cherry picked from commit 1b5c8d4cbcedc25f70837f9a0aff3dbbf8899c68)
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 docs/formatstorage.html.in                         | 10 +++++++---
c401cc
 docs/schemas/storagevol.rng                        | 10 ++++++++++
c401cc
 src/conf/storage_conf.c                            | 19 ++++++++++++++++++-
c401cc
 src/conf/storage_conf.h                            |  1 +
c401cc
 tests/storagevolxml2xmlin/vol-logical-backing.xml  |  2 +-
c401cc
 tests/storagevolxml2xmlin/vol-logical.xml          |  2 +-
c401cc
 tests/storagevolxml2xmlin/vol-partition.xml        |  2 +-
c401cc
 tests/storagevolxml2xmlin/vol-sheepdog.xml         |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-file-backing.xml    |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-file-naming.xml     |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-file.xml            |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-logical-backing.xml |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-logical.xml         |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-partition.xml       |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-qcow2-0.10-lazy.xml |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-qcow2-1.1.xml       |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-qcow2-lazy.xml      |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-qcow2-nobacking.xml |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-qcow2.xml           |  2 +-
c401cc
 tests/storagevolxml2xmlout/vol-sheepdog.xml        |  2 +-
c401cc
 20 files changed, 52 insertions(+), 20 deletions(-)
c401cc
c401cc
diff --git a/docs/formatstorage.html.in b/docs/formatstorage.html.in
c401cc
index 278f89b..30ae538 100644
c401cc
--- a/docs/formatstorage.html.in
c401cc
+++ b/docs/formatstorage.html.in
c401cc
@@ -267,14 +267,18 @@
c401cc
 
c401cc
     

Storage volume XML

c401cc
     

c401cc
-      A storage volume will be either a file or a device node.
c401cc
-      The storage volume XML format is available since 0.4.1
c401cc
+      A storage volume will generally be either a file or a device
c401cc
+      node; since 1.2.0, an optional
c401cc
+      output-only attribute type lists the actual type
c401cc
+      (file, block, dir, or network), which is also available
c401cc
+      from virStorageVolGetInfo().  The storage volume
c401cc
+      XML format is available since 0.4.1
c401cc
     

c401cc
 
c401cc
     

General metadata

c401cc
 
c401cc
     
c401cc
-      <volume>
c401cc
+      <volume type='file'>
c401cc
         <name>sparse.img</name>
c401cc
         <key>/var/lib/xen/images/sparse.img</key>
c401cc
         <allocation>0</allocation>
c401cc
diff --git a/docs/schemas/storagevol.rng b/docs/schemas/storagevol.rng
c401cc
index e79bc35..f8081d9 100644
c401cc
--- a/docs/schemas/storagevol.rng
c401cc
+++ b/docs/schemas/storagevol.rng
c401cc
@@ -13,6 +13,16 @@
c401cc
 
c401cc
   <define name='vol'>
c401cc
     <element name='volume'>
c401cc
+      <optional>
c401cc
+        <attribute name='type'>
c401cc
+          <choice>
c401cc
+            <value>file</value>
c401cc
+            <value>block</value>
c401cc
+            <value>dir</value>
c401cc
+            <value>network</value>
c401cc
+          </choice>
c401cc
+        </attribute>
c401cc
+      </optional>
c401cc
       <interleave>
c401cc
         <element name='name'>
c401cc
           <ref name='volName'/>
c401cc
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
c401cc
index c9127b0..a5f03c3 100644
c401cc
--- a/src/conf/storage_conf.c
c401cc
+++ b/src/conf/storage_conf.c
c401cc
@@ -51,6 +51,10 @@
c401cc
 #define DEFAULT_POOL_PERM_MODE 0755
c401cc
 #define DEFAULT_VOL_PERM_MODE  0600
c401cc
 
c401cc
+VIR_ENUM_IMPL(virStorageVol,
c401cc
+              VIR_STORAGE_VOL_LAST,
c401cc
+              "file", "block", "dir", "network")
c401cc
+
c401cc
 VIR_ENUM_IMPL(virStoragePool,
c401cc
               VIR_STORAGE_POOL_LAST,
c401cc
               "dir", "fs", "netfs",
c401cc
@@ -1253,6 +1257,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
c401cc
 {
c401cc
     virStorageVolDefPtr ret;
c401cc
     virStorageVolOptionsPtr options;
c401cc
+    char *type = NULL;
c401cc
     char *allocation = NULL;
c401cc
     char *capacity = NULL;
c401cc
     char *unit = NULL;
c401cc
@@ -1278,6 +1283,16 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
c401cc
     /* Normally generated by pool refresh, but useful for unit tests */
c401cc
     ret->key = virXPathString("string(./key)", ctxt);
c401cc
 
c401cc
+    /* Technically overridden by pool refresh, but useful for unit tests */
c401cc
+    type = virXPathString("string(./@type)", ctxt);
c401cc
+    if (type) {
c401cc
+        if ((ret->type = virStorageVolTypeFromString(type)) < 0) {
c401cc
+            virReportError(VIR_ERR_XML_ERROR,
c401cc
+                           _("unknown volume type '%s'"), type);
c401cc
+            goto error;
c401cc
+        }
c401cc
+    }
c401cc
+
c401cc
     capacity = virXPathString("string(./capacity)", ctxt);
c401cc
     unit = virXPathString("string(./capacity/@unit)", ctxt);
c401cc
     if (capacity == NULL) {
c401cc
@@ -1394,6 +1409,7 @@ cleanup:
c401cc
     VIR_FREE(allocation);
c401cc
     VIR_FREE(capacity);
c401cc
     VIR_FREE(unit);
c401cc
+    VIR_FREE(type);
c401cc
     return ret;
c401cc
 
c401cc
 error:
c401cc
@@ -1563,7 +1579,8 @@ virStorageVolDefFormat(virStoragePoolDefPtr pool,
c401cc
     if (options == NULL)
c401cc
         return NULL;
c401cc
 
c401cc
-    virBufferAddLit(&buf, "<volume>\n");
c401cc
+    virBufferAsprintf(&buf, "<volume type='%s'>\n",
c401cc
+                      virStorageVolTypeToString(def->type));
c401cc
     virBufferEscapeString(&buf, "  <name>%s</name>\n", def->name);
c401cc
     virBufferEscapeString(&buf, "  <key>%s</key>\n", def->key);
c401cc
     virBufferAddLit(&buf, "  <source>\n");
c401cc
diff --git a/src/conf/storage_conf.h b/src/conf/storage_conf.h
c401cc
index 62ff1fd..4ed800c 100644
c401cc
--- a/src/conf/storage_conf.h
c401cc
+++ b/src/conf/storage_conf.h
c401cc
@@ -116,6 +116,7 @@ struct _virStorageVolDefList {
c401cc
     virStorageVolDefPtr *objs;
c401cc
 };
c401cc
 
c401cc
+VIR_ENUM_DECL(virStorageVol)
c401cc
 
c401cc
 enum virStoragePoolType {
c401cc
     VIR_STORAGE_POOL_DIR,      /* Local directory */
c401cc
diff --git a/tests/storagevolxml2xmlin/vol-logical-backing.xml b/tests/storagevolxml2xmlin/vol-logical-backing.xml
c401cc
index b4141a5..38ff8c5 100644
c401cc
--- a/tests/storagevolxml2xmlin/vol-logical-backing.xml
c401cc
+++ b/tests/storagevolxml2xmlin/vol-logical-backing.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='block'>
c401cc
   <name>Swap</name>
c401cc
   <key>r4xkCv-MQhr-WKIT-R66x-Epn2-e8hG-1Z5gY0</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlin/vol-logical.xml b/tests/storagevolxml2xmlin/vol-logical.xml
c401cc
index cd4d3f7..46a607a 100644
c401cc
--- a/tests/storagevolxml2xmlin/vol-logical.xml
c401cc
+++ b/tests/storagevolxml2xmlin/vol-logical.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='block'>
c401cc
   <name>Swap</name>
c401cc
   <key>r4xkCv-MQhr-WKIT-R66x-Epn2-e8hG-1Z5gY0</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlin/vol-partition.xml b/tests/storagevolxml2xmlin/vol-partition.xml
c401cc
index 6990bb5..d810fff 100644
c401cc
--- a/tests/storagevolxml2xmlin/vol-partition.xml
c401cc
+++ b/tests/storagevolxml2xmlin/vol-partition.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='block'>
c401cc
   <name>sda1</name>
c401cc
   <key>/dev/sda1</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlin/vol-sheepdog.xml b/tests/storagevolxml2xmlin/vol-sheepdog.xml
c401cc
index 49e221c..d6e920b 100644
c401cc
--- a/tests/storagevolxml2xmlin/vol-sheepdog.xml
c401cc
+++ b/tests/storagevolxml2xmlin/vol-sheepdog.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='network'>
c401cc
   <name>test2</name>
c401cc
   <source>
c401cc
   </source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-file-backing.xml b/tests/storagevolxml2xmlout/vol-file-backing.xml
c401cc
index 8d2fb57..cd33bee 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-file-backing.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-file-backing.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='file'>
c401cc
   <name>sparse.img</name>
c401cc
   <key>/var/lib/libvirt/images/sparse.img</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-file-naming.xml b/tests/storagevolxml2xmlout/vol-file-naming.xml
c401cc
index 7022b02..e515502 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-file-naming.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-file-naming.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='file'>
c401cc
   <name><sparse>.img</name>
c401cc
   <source>
c401cc
   </source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-file.xml b/tests/storagevolxml2xmlout/vol-file.xml
c401cc
index b97dd50..2923188 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-file.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-file.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='file'>
c401cc
   <name>sparse.img</name>
c401cc
   <source>
c401cc
   </source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-logical-backing.xml b/tests/storagevolxml2xmlout/vol-logical-backing.xml
c401cc
index bf34b08..07fe277 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-logical-backing.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-logical-backing.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='block'>
c401cc
   <name>Swap</name>
c401cc
   <key>r4xkCv-MQhr-WKIT-R66x-Epn2-e8hG-1Z5gY0</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-logical.xml b/tests/storagevolxml2xmlout/vol-logical.xml
c401cc
index e9b4e4b..0df5cc0 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-logical.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-logical.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='block'>
c401cc
   <name>Swap</name>
c401cc
   <key>r4xkCv-MQhr-WKIT-R66x-Epn2-e8hG-1Z5gY0</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-partition.xml b/tests/storagevolxml2xmlout/vol-partition.xml
c401cc
index 9be1cf1..147899e 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-partition.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-partition.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='block'>
c401cc
   <name>sda1</name>
c401cc
   <key>/dev/sda1</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-qcow2-0.10-lazy.xml b/tests/storagevolxml2xmlout/vol-qcow2-0.10-lazy.xml
c401cc
index fd3d606..1f799da 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-qcow2-0.10-lazy.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-qcow2-0.10-lazy.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='file'>
c401cc
   <name>OtherDemo.img</name>
c401cc
   <key>/var/lib/libvirt/images/OtherDemo.img</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-qcow2-1.1.xml b/tests/storagevolxml2xmlout/vol-qcow2-1.1.xml
c401cc
index 99fb5ac..14f805f 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-qcow2-1.1.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-qcow2-1.1.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='file'>
c401cc
   <name>OtherDemo.img</name>
c401cc
   <key>/var/lib/libvirt/images/OtherDemo.img</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-qcow2-lazy.xml b/tests/storagevolxml2xmlout/vol-qcow2-lazy.xml
c401cc
index 3708ea7..68a9756 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-qcow2-lazy.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-qcow2-lazy.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='file'>
c401cc
   <name>OtherDemo.img</name>
c401cc
   <key>/var/lib/libvirt/images/OtherDemo.img</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-qcow2-nobacking.xml b/tests/storagevolxml2xmlout/vol-qcow2-nobacking.xml
c401cc
index f6a2e21..075dc69 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-qcow2-nobacking.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-qcow2-nobacking.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='file'>
c401cc
   <name>OtherDemo.img</name>
c401cc
   <key>/var/lib/libvirt/images/OtherDemo.img</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-qcow2.xml b/tests/storagevolxml2xmlout/vol-qcow2.xml
c401cc
index b9adcb4..31dc578 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-qcow2.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-qcow2.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='file'>
c401cc
   <name>OtherDemo.img</name>
c401cc
   <key>/var/lib/libvirt/images/OtherDemo.img</key>
c401cc
   <source>
c401cc
diff --git a/tests/storagevolxml2xmlout/vol-sheepdog.xml b/tests/storagevolxml2xmlout/vol-sheepdog.xml
c401cc
index bd5d6d8..e08e36c 100644
c401cc
--- a/tests/storagevolxml2xmlout/vol-sheepdog.xml
c401cc
+++ b/tests/storagevolxml2xmlout/vol-sheepdog.xml
c401cc
@@ -1,4 +1,4 @@
c401cc
-<volume>
c401cc
+<volume type='network'>
c401cc
   <name>test2</name>
c401cc
   <source>
c401cc
   </source>
c401cc
-- 
c401cc
1.9.0
c401cc