ed5979
From b1970c733dc46b2a8f648997a7e1c5d12900ff54 Mon Sep 17 00:00:00 2001
ed5979
From: Hanna Reitz <hreitz@redhat.com>
ed5979
Date: Mon, 20 Jun 2022 18:27:04 +0200
ed5979
Subject: [PATCH 17/20] qemu-img: Change info key names for protocol nodes
ed5979
ed5979
RH-Author: Hanna Czenczek <hreitz@redhat.com>
ed5979
RH-MergeRequest: 145: Show protocol-level information in qemu-img info
ed5979
RH-Bugzilla: 1860292
ed5979
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
ed5979
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ed5979
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
ed5979
RH-Commit: [12/12] 67c260aaa05466410503fecee6210bf9d47e8c7c (hreitz/qemu-kvm-c-9-s)
ed5979
ed5979
Currently, when querying a qcow2 image, qemu-img info reports something
ed5979
like this:
ed5979
ed5979
image: test.qcow2
ed5979
file format: qcow2
ed5979
virtual size: 64 MiB (67108864 bytes)
ed5979
disk size: 196 KiB
ed5979
cluster_size: 65536
ed5979
Format specific information:
ed5979
    compat: 1.1
ed5979
    compression type: zlib
ed5979
    lazy refcounts: false
ed5979
    refcount bits: 16
ed5979
    corrupt: false
ed5979
    extended l2: false
ed5979
Child node '/file':
ed5979
    image: test.qcow2
ed5979
    file format: file
ed5979
    virtual size: 192 KiB (197120 bytes)
ed5979
    disk size: 196 KiB
ed5979
    Format specific information:
ed5979
        extent size hint: 1048576
ed5979
ed5979
Notably, the way the keys are named is specific for image files: The
ed5979
filename is shown under "image", the BDS driver under "file format", and
ed5979
the BDS length under "virtual size".  This does not make much sense for
ed5979
nodes that are not actually supposed to be guest images, like the /file
ed5979
child node shown above.
ed5979
ed5979
Give bdrv_node_info_dump() a @protocol parameter that gives a hint that
ed5979
the respective node is probably just used for data storage and does not
ed5979
necessarily present the data for a VM guest disk.  This renames the keys
ed5979
so that with this patch, the output becomes:
ed5979
ed5979
image: test.qcow2
ed5979
[...]
ed5979
Child node '/file':
ed5979
    filename: test.qcow2
ed5979
    protocol type: file
ed5979
    file length: 192 KiB (197120 bytes)
ed5979
    disk size: 196 KiB
ed5979
    Format specific information:
ed5979
        extent size hint: 1048576
ed5979
ed5979
(Perhaps we should also rename "Format specific information", but I
ed5979
could not come up with anything better that will not become problematic
ed5979
if we guess wrong with the protocol "heuristic".)
ed5979
ed5979
This change affects iotest 302, which has protocol node information in
ed5979
its reference output.
ed5979
ed5979
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
ed5979
Message-Id: <20220620162704.80987-13-hreitz@redhat.com>
ed5979
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
ed5979
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ed5979
(cherry picked from commit d570177b50c389f379f93183155a27d44856ab46)
ed5979
Signed-off-by: Hanna Czenczek <hreitz@redhat.com>
ed5979
---
ed5979
 block/monitor/block-hmp-cmds.c |  2 +-
ed5979
 block/qapi.c                   | 39 ++++++++++++++++++++++++++++------
ed5979
 include/block/qapi.h           |  2 +-
ed5979
 qemu-img.c                     |  3 ++-
ed5979
 tests/qemu-iotests/302.out     |  6 +++---
ed5979
 5 files changed, 39 insertions(+), 13 deletions(-)
ed5979
ed5979
diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
ed5979
index 72824d4e2e..4d83339a5d 100644
ed5979
--- a/block/monitor/block-hmp-cmds.c
ed5979
+++ b/block/monitor/block-hmp-cmds.c
ed5979
@@ -734,7 +734,7 @@ static void print_block_info(Monitor *mon, BlockInfo *info,
ed5979
         monitor_printf(mon, "\nImages:\n");
ed5979
         image_info = inserted->image;
ed5979
         while (1) {
ed5979
-            bdrv_node_info_dump(qapi_ImageInfo_base(image_info), 0);
ed5979
+            bdrv_node_info_dump(qapi_ImageInfo_base(image_info), 0, false);
ed5979
             if (image_info->has_backing_image) {
ed5979
                 image_info = image_info->backing_image;
ed5979
             } else {
ed5979
diff --git a/block/qapi.c b/block/qapi.c
ed5979
index 3e35603f0c..56f398c500 100644
ed5979
--- a/block/qapi.c
ed5979
+++ b/block/qapi.c
ed5979
@@ -934,24 +934,49 @@ void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
ed5979
     visit_free(v);
ed5979
 }
ed5979
 
ed5979
-void bdrv_node_info_dump(BlockNodeInfo *info, int indentation)
ed5979
+/**
ed5979
+ * Print the given @info object in human-readable form.  Every field is indented
ed5979
+ * using the given @indentation (four spaces per indentation level).
ed5979
+ *
ed5979
+ * When using this to print a whole block graph, @protocol can be set to true to
ed5979
+ * signify that the given information is associated with a protocol node, i.e.
ed5979
+ * just data storage for an image, such that the data it presents is not really
ed5979
+ * a full VM disk.  If so, several fields change name: For example, "virtual
ed5979
+ * size" is printed as "file length".
ed5979
+ * (Consider a qcow2 image, which is represented by a qcow2 node and a file
ed5979
+ * node.  Printing a "virtual size" for the file node does not make sense,
ed5979
+ * because without the qcow2 node, it is not really a guest disk, so it does not
ed5979
+ * have a "virtual size".  Therefore, we call it "file length" instead.)
ed5979
+ *
ed5979
+ * @protocol is ignored when @indentation is 0, because we take that to mean
ed5979
+ * that the associated node is the root node in the queried block graph, and
ed5979
+ * thus is always to be interpreted as a standalone guest disk.
ed5979
+ */
ed5979
+void bdrv_node_info_dump(BlockNodeInfo *info, int indentation, bool protocol)
ed5979
 {
ed5979
     char *size_buf, *dsize_buf;
ed5979
     g_autofree char *ind_s = g_strdup_printf("%*s", indentation * 4, "");
ed5979
 
ed5979
+    if (indentation == 0) {
ed5979
+        /* Top level, consider this a normal image */
ed5979
+        protocol = false;
ed5979
+    }
ed5979
+
ed5979
     if (!info->has_actual_size) {
ed5979
         dsize_buf = g_strdup("unavailable");
ed5979
     } else {
ed5979
         dsize_buf = size_to_str(info->actual_size);
ed5979
     }
ed5979
     size_buf = size_to_str(info->virtual_size);
ed5979
-    qemu_printf("%simage: %s\n"
ed5979
-                "%sfile format: %s\n"
ed5979
-                "%svirtual size: %s (%" PRId64 " bytes)\n"
ed5979
+    qemu_printf("%s%s: %s\n"
ed5979
+                "%s%s: %s\n"
ed5979
+                "%s%s: %s (%" PRId64 " bytes)\n"
ed5979
                 "%sdisk size: %s\n",
ed5979
-                ind_s, info->filename,
ed5979
-                ind_s, info->format,
ed5979
-                ind_s, size_buf, info->virtual_size,
ed5979
+                ind_s, protocol ? "filename" : "image", info->filename,
ed5979
+                ind_s, protocol ? "protocol type" : "file format",
ed5979
+                info->format,
ed5979
+                ind_s, protocol ? "file length" : "virtual size",
ed5979
+                size_buf, info->virtual_size,
ed5979
                 ind_s, dsize_buf);
ed5979
     g_free(size_buf);
ed5979
     g_free(dsize_buf);
ed5979
diff --git a/include/block/qapi.h b/include/block/qapi.h
ed5979
index 38855f2ae9..26113da21a 100644
ed5979
--- a/include/block/qapi.h
ed5979
+++ b/include/block/qapi.h
ed5979
@@ -51,5 +51,5 @@ void bdrv_snapshot_dump(QEMUSnapshotInfo *sn);
ed5979
 void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
ed5979
                                    const char *prefix,
ed5979
                                    int indentation);
ed5979
-void bdrv_node_info_dump(BlockNodeInfo *info, int indentation);
ed5979
+void bdrv_node_info_dump(BlockNodeInfo *info, int indentation, bool protocol);
ed5979
 #endif
ed5979
diff --git a/qemu-img.c b/qemu-img.c
ed5979
index e281011245..2943625c67 100644
ed5979
--- a/qemu-img.c
ed5979
+++ b/qemu-img.c
ed5979
@@ -2853,7 +2853,8 @@ static void dump_human_image_info(BlockGraphInfo *info, int indentation,
ed5979
 {
ed5979
     BlockChildInfoList *children_list;
ed5979
 
ed5979
-    bdrv_node_info_dump(qapi_BlockGraphInfo_base(info), indentation);
ed5979
+    bdrv_node_info_dump(qapi_BlockGraphInfo_base(info), indentation,
ed5979
+                        info->children == NULL);
ed5979
 
ed5979
     for (children_list = info->children; children_list;
ed5979
          children_list = children_list->next)
ed5979
diff --git a/tests/qemu-iotests/302.out b/tests/qemu-iotests/302.out
ed5979
index edfa1c4f05..7b5014cdd8 100644
ed5979
--- a/tests/qemu-iotests/302.out
ed5979
+++ b/tests/qemu-iotests/302.out
ed5979
@@ -5,9 +5,9 @@ file format: raw
ed5979
 virtual size: 448 KiB (458752 bytes)
ed5979
 disk size: unavailable
ed5979
 Child node '/file':
ed5979
-    image: nbd+unix:///exp?socket=SOCK_DIR/PID-nbd-sock
ed5979
-    file format: nbd
ed5979
-    virtual size: 448 KiB (458752 bytes)
ed5979
+    filename: nbd+unix:///exp?socket=SOCK_DIR/PID-nbd-sock
ed5979
+    protocol type: nbd
ed5979
+    file length: 448 KiB (458752 bytes)
ed5979
     disk size: unavailable
ed5979
 
ed5979
 === Converted image info ===
ed5979
-- 
ed5979
2.31.1
ed5979