From 4b9af8625a642339526b47eff718fab12c4b7cea Mon Sep 17 00:00:00 2001
From: Julio Faracco <jcfaracco@gmail.com>
Date: Mon, 1 May 2017 19:35:55 -0300
Subject: [PATCH] virsh-domain-monitor: add human readable output for
 'domblkinfo'.

https://bugzilla.redhat.com/show_bug.cgi?id=1330940

The virsh command 'domblkinfo' returns the capacity, allocation and phisycal
size of the devices attached in a domain. Usually, this sizes are very big
and hard to understand and calculate. This commits introduce a human readable
support to check the size of each field easilly.

For example, the command before:

virsh # domblkinfo my_domain hda
Capacity:       21474836480
Allocation:     14875545600
Physical:       21474836480

and after this patch:

virsh # domblkinfo my_domain hda --human
Capacity:       20.000G
Allocation:     13.900G
Physical:       20.000G

Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
---
 tools/virsh-domain-monitor.c | 25 ++++++++++++++++++++++---
 tools/virsh.pod              |  5 +++--
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 3db4795d28..0ca53e438e 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -396,6 +396,10 @@ static const vshCmdOptDef opts_domblkinfo[] = {
      .flags = VSH_OFLAG_REQ,
      .help = N_("block device")
     },
+    {.name = "human",
+     .type = VSH_OT_BOOL,
+     .help = N_("Human readable output")
+    },
     {.name = NULL}
 };
 
@@ -405,6 +409,7 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
     virDomainBlockInfo info;
     virDomainPtr dom;
     bool ret = false;
+    bool human = false;
     const char *device = NULL;
 
     if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
@@ -416,9 +421,23 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
     if (virDomainGetBlockInfo(dom, device, &info, 0) < 0)
         goto cleanup;
 
-    vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
-    vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
-    vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
+    human = vshCommandOptBool(cmd, "human");
+
+    if (!human) {
+        vshPrint(ctl, "%-15s %llu\n", _("Capacity:"), info.capacity);
+        vshPrint(ctl, "%-15s %llu\n", _("Allocation:"), info.allocation);
+        vshPrint(ctl, "%-15s %llu\n", _("Physical:"), info.physical);
+    } else {
+        double val;
+        const char *unit;
+
+        val = vshPrettyCapacity(info.capacity, &unit);
+        vshPrint(ctl, "%-15s %-.3lf %s\n", _("Capacity:"), val, unit);
+        val = vshPrettyCapacity(info.allocation, &unit);
+        vshPrint(ctl, "%-15s %-.3lf %s\n", _("Allocation:"), val, unit);
+        val = vshPrettyCapacity(info.physical, &unit);
+        vshPrint(ctl, "%-15s %-.3lf %s\n", _("Physical:"), val, unit);
+    }
 
     ret = true;
 
diff --git a/tools/virsh.pod b/tools/virsh.pod
index bba5fed375..cd1f25fdf4 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -841,12 +841,13 @@ B<domstate> command says that a domain was paused due to I/O error.
 The B<domblkerror> command lists all block devices in error state and
 the error seen on each of them.
 
-=item B<domblkinfo> I<domain> I<block-device>
+=item B<domblkinfo> I<domain> I<block-device> [I<--human>]
 
 Get block device size info for a domain.  A I<block-device> corresponds
 to a unique target name (<target dev='name'/>) or source file (<source
 file='name'/>) for one of the disk devices attached to I<domain> (see
-also B<domblklist> for listing these names).
+also B<domblklist> for listing these names). If I<--human> is set, the
+output will have a human readable output.
 
 =item B<domblklist> I<domain> [I<--inactive>] [I<--details>]