[Libguestfs] [PATCH v2 5/5] v2v: Add --machine-readable output for --print-target.

Richard W.M. Jones rjones at redhat.com
Fri Mar 16 13:30:43 UTC 2018


The output will look similar to this:

__MACHINEBEGIN__
{
  "targets": [
    {
      "qemu_uri": "json:{ \"file.driver\": \"nbd\", \"file.path\": \"/home/rjones/d/libguestfs/tmp/rhvupload.VVGewv/nbdkit0.sock\", \"file.export\": \"/\" }",
      "format": "raw",
      "estimated_size": 2274060540
    }
  ],
  "overlays": [
    {
      "file": "/home/rjones/d/libguestfs/tmp/v2vovl827733.qcow2",
      "sd": "sda",
      "virtual_size": 6442450944
    }
  ]
}
__MACHINEEND__
---
 v2v/cmdline.ml   |  4 +++-
 v2v/cmdline.mli  |  1 +
 v2v/types.ml     | 18 ++++++++++++++++++
 v2v/types.mli    |  2 ++
 v2v/v2v.ml       | 30 +++++++++++++++++++++++-------
 v2v/virt-v2v.pod |  6 ++++++
 6 files changed, 53 insertions(+), 8 deletions(-)

diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
index fdd0f2614..25d26b946 100644
--- a/v2v/cmdline.ml
+++ b/v2v/cmdline.ml
@@ -39,6 +39,7 @@ type cmdline = {
   debug_overlays : bool;
   do_copy : bool;
   in_place : bool;
+  machine_readable : bool;
   network_map : string NetworkMap.t;
   output_alloc : output_allocation;
   output_format : string option;
@@ -627,7 +628,8 @@ read the man page virt-v2v(1).
       output_format, output_alloc in
 
   {
-    compressed; debug_overlays; do_copy; in_place; network_map;
+    compressed; debug_overlays; do_copy; in_place;
+    machine_readable; network_map;
     output_alloc; output_format; output_name;
     print_source; print_target;
     root_choice;
diff --git a/v2v/cmdline.mli b/v2v/cmdline.mli
index 0e1d54f40..765f63a2d 100644
--- a/v2v/cmdline.mli
+++ b/v2v/cmdline.mli
@@ -37,6 +37,7 @@ type cmdline = {
   debug_overlays : bool;
   do_copy : bool;
   in_place : bool;
+  machine_readable : bool;
   network_map : string NetworkMap.t;
   output_alloc : Types.output_allocation;
   output_format : string option;
diff --git a/v2v/types.ml b/v2v/types.ml
index c1f36fdb1..eeec077ee 100644
--- a/v2v/types.ml
+++ b/v2v/types.ml
@@ -291,6 +291,11 @@ overlay virtual disk size: %Ld
     ov.ov_virtual_size
     ov.ov_source.s_qemu_uri
 
+let json_of_overlay ov =
+  [ "file", JSON.String ov.ov_overlay_file;
+    "sd", JSON.String ov.ov_sd;
+    "virtual_size", JSON.Int64 ov.ov_virtual_size ]
+
 type target = {
   target_file : target_file;
   target_format : string;
@@ -314,6 +319,19 @@ target estimated size: %s
     (match t.target_estimated_size with
     | None -> "None" | Some i -> Int64.to_string i)
 
+let json_of_target t =
+  let json = ref [] in
+  (match t.target_file with
+   | TargetFile s ->
+      List.push_back json ("file", JSON.String s)
+   | TargetURI s ->
+      List.push_back json ("qemu_uri", JSON.String s)
+  );
+  List.push_back json ("format", JSON.String t.target_format);
+  Option.may (fun i -> List.push_back json ("estimated_size", JSON.Int64 i))
+             t.target_estimated_size;
+  !json
+
 type target_firmware = TargetBIOS | TargetUEFI
 
 let string_of_target_firmware = function
diff --git a/v2v/types.mli b/v2v/types.mli
index a432e167c..d3dfebb7f 100644
--- a/v2v/types.mli
+++ b/v2v/types.mli
@@ -185,6 +185,7 @@ type overlay = {
 (** Overlay disk. *)
 
 val string_of_overlay : overlay -> string
+val json_of_overlay : overlay -> JSON.doc
 
 (** {2 Target disks} *)
 
@@ -208,6 +209,7 @@ and target_file =
   | TargetURI of string      (** Target is a QEMU URI. *)
 
 val string_of_target : target -> string
+val json_of_target : target -> JSON.doc
 
 (** {2 Guest firmware} *)
 
diff --git a/v2v/v2v.ml b/v2v/v2v.ml
index abb531c6f..63879a469 100644
--- a/v2v/v2v.ml
+++ b/v2v/v2v.ml
@@ -155,13 +155,28 @@ let rec main () =
    | Copying (overlays, targets) ->
       (* Print overlays/targets and stop. *)
       if cmdline.print_target then (
-        printf (f_"Overlay and Target information (--print-target option):\n");
-        printf "\n";
-        List.iter (
-          fun (ov, t) ->
-            printf "%s\n" (string_of_overlay ov);
-            printf "%s\n" (string_of_target t)
-        ) (List.combine overlays targets);
+        if not cmdline.machine_readable then (
+          printf (f_"Overlay and Target information (--print-target option):\n");
+          printf "\n";
+          List.iter (
+            fun (ov, t) ->
+              printf "%s\n" (string_of_overlay ov);
+              printf "%s\n" (string_of_target t)
+          ) (List.combine overlays targets);
+        )
+        else (
+          let json = [
+            "targets",
+              JSON.List (
+                List.map (fun t -> JSON.Dict (json_of_target t)) targets
+              );
+            "overlays",
+              JSON.List (
+                List.map (fun ov -> JSON.Dict (json_of_overlay ov)) overlays
+              );
+          ] in
+          machine_output "%s" (JSON.string_of_doc ~fmt:JSON.Indented json)
+        );
         exit 0
       )
   );
@@ -200,6 +215,7 @@ and open_source cmdline input =
 
   (* Print source and stop. *)
   if cmdline.print_source then (
+    (* XXX Implement machine-readable version. *)
     printf (f_"Source guest information (--print-source option):\n");
     printf "\n";
     printf "%s\n" (string_of_source source);
diff --git a/v2v/virt-v2v.pod b/v2v/virt-v2v.pod
index bbd6985e4..5c7e563bf 100644
--- a/v2v/virt-v2v.pod
+++ b/v2v/virt-v2v.pod
@@ -636,6 +636,12 @@ See L</NETWORKS AND BRIDGES>.
 Print information about the target disk(s) and overlay file(s), and
 stop.
 
+To obtain machine-readable JSON output use:
+
+ virt-v2v ... --print-target --machine-readable
+
+and parse the lines between C<__MACHINEBEGIN__> and C<__MACHINEEND__>.
+
 =item B<--qemu-boot>
 
 When using I<-o qemu> only, this boots the guest immediately after
-- 
2.13.2




More information about the Libguestfs mailing list