mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0003-v2v-add-o-json-output-mode.patch

46b2f6
From 480c7169c341fc2f86609a13100c42e10f599b83 Mon Sep 17 00:00:00 2001
46b2f6
From: Pino Toscano <ptoscano@redhat.com>
46b2f6
Date: Mon, 25 Feb 2019 13:14:43 +0100
46b2f6
Subject: [PATCH] v2v: add -o json output mode
46b2f6
46b2f6
Add a new output mode to virt-v2v: similar to -o local, the written
46b2f6
metadata is a JSON file with the majority of the data that virt-v2v
46b2f6
knowns about (or collects) during the conversion.
46b2f6
46b2f6
This is meant to be used only when no existing output mode is usable,
46b2f6
and a guest needs to be converted to run on KVM anyway.  The user of
46b2f6
this mode is supposed to use all the data in the JSON, as they contain
46b2f6
important details on how even run the guest (e.g. w.r.t. firmware,
46b2f6
drivers of disks/NICs, etc).
46b2f6
46b2f6
(cherry picked from commit f190e08d85556dac293ef15bfeee38e54471570f)
46b2f6
---
46b2f6
 v2v/Makefile.am               |   4 +
46b2f6
 v2v/cmdline.ml                |  29 +++
46b2f6
 v2v/create_json.ml            | 348 ++++++++++++++++++++++++++++++++++
46b2f6
 v2v/create_json.mli           |  29 +++
46b2f6
 v2v/output_json.ml            | 116 ++++++++++++
46b2f6
 v2v/output_json.mli           |  31 +++
46b2f6
 v2v/virt-v2v-output-local.pod |  55 ++++++
46b2f6
 v2v/virt-v2v.pod              |  15 +-
46b2f6
 8 files changed, 625 insertions(+), 2 deletions(-)
46b2f6
 create mode 100644 v2v/create_json.ml
46b2f6
 create mode 100644 v2v/create_json.mli
46b2f6
 create mode 100644 v2v/output_json.ml
46b2f6
 create mode 100644 v2v/output_json.mli
46b2f6
46b2f6
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
46b2f6
index f196be81d..53c137fc6 100644
46b2f6
--- a/v2v/Makefile.am
46b2f6
+++ b/v2v/Makefile.am
46b2f6
@@ -52,6 +52,7 @@ SOURCES_MLI = \
46b2f6
 	config.mli \
46b2f6
 	convert_linux.mli \
46b2f6
 	convert_windows.mli \
46b2f6
+	create_json.mli \
46b2f6
 	create_libvirt_xml.mli \
46b2f6
 	create_ovf.mli \
46b2f6
 	DOM.mli \
46b2f6
@@ -75,6 +76,7 @@ SOURCES_MLI = \
46b2f6
 	networks.mli \
46b2f6
 	openstack_image_properties.mli \
46b2f6
 	output_glance.mli \
46b2f6
+	output_json.mli \
46b2f6
 	output_libvirt.mli \
46b2f6
 	output_local.mli \
46b2f6
 	output_null.mli \
46b2f6
@@ -117,6 +119,7 @@ SOURCES_ML = \
46b2f6
 	parse_ovf_from_ova.ml \
46b2f6
 	parse_ova.ml \
46b2f6
 	create_ovf.ml \
46b2f6
+	create_json.ml \
46b2f6
 	linux.ml \
46b2f6
 	windows.ml \
46b2f6
 	windows_virtio.ml \
46b2f6
@@ -141,6 +144,7 @@ SOURCES_ML = \
46b2f6
 	convert_windows.ml \
46b2f6
 	output_null.ml \
46b2f6
 	output_glance.ml \
46b2f6
+	output_json.ml \
46b2f6
 	output_libvirt.ml \
46b2f6
 	output_local.ml \
46b2f6
 	output_qemu.ml \
46b2f6
diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
46b2f6
index 46f6910d0..4d390f249 100644
46b2f6
--- a/v2v/cmdline.ml
46b2f6
+++ b/v2v/cmdline.ml
46b2f6
@@ -138,6 +138,7 @@ let parse_cmdline () =
46b2f6
     | "glance" -> output_mode := `Glance
46b2f6
     | "libvirt" -> output_mode := `Libvirt
46b2f6
     | "disk" | "local" -> output_mode := `Local
46b2f6
+    | "json" -> output_mode := `JSON
46b2f6
     | "null" -> output_mode := `Null
46b2f6
     | "openstack" | "osp" | "rhosp" -> output_mode := `Openstack
46b2f6
     | "ovirt" | "rhv" | "rhev" -> output_mode := `RHV
46b2f6
@@ -413,6 +414,17 @@ read the man page virt-v2v(1).
46b2f6
     | `RHV -> no_options (); `RHV
46b2f6
     | `QEmu -> no_options (); `QEmu
46b2f6
 
46b2f6
+    | `JSON ->
46b2f6
+       if is_query then (
46b2f6
+         Output_json.print_output_options ();
46b2f6
+         exit 0
46b2f6
+       )
46b2f6
+       else (
46b2f6
+         let json_options =
46b2f6
+           Output_json.parse_output_options output_options in
46b2f6
+         `JSON json_options
46b2f6
+       )
46b2f6
+
46b2f6
     | `Openstack ->
46b2f6
        if is_query then (
46b2f6
          Output_openstack.print_output_options ();
46b2f6
@@ -546,6 +558,23 @@ read the man page virt-v2v(1).
46b2f6
       Output_libvirt.output_libvirt output_conn output_storage,
46b2f6
       output_format, output_alloc
46b2f6
 
46b2f6
+    | `JSON json_options ->
46b2f6
+      if output_password <> None then
46b2f6
+        error_option_cannot_be_used_in_output_mode "json" "-op";
46b2f6
+      if output_conn <> None then
46b2f6
+        error_option_cannot_be_used_in_output_mode "json" "-oc";
46b2f6
+      let os =
46b2f6
+        match output_storage with
46b2f6
+        | None ->
46b2f6
+           error (f_"-o json: output directory was not specified, use '-os /dir'")
46b2f6
+        | Some d when not (is_directory d) ->
46b2f6
+           error (f_"-os %s: output directory does not exist or is not a directory") d
46b2f6
+        | Some d -> d in
46b2f6
+      if qemu_boot then
46b2f6
+        error_option_cannot_be_used_in_output_mode "json" "--qemu-boot";
46b2f6
+      Output_json.output_json os json_options,
46b2f6
+      output_format, output_alloc
46b2f6
+
46b2f6
     | `Local ->
46b2f6
       if output_password <> None then
46b2f6
         error_option_cannot_be_used_in_output_mode "local" "-op";
46b2f6
diff --git a/v2v/create_json.ml b/v2v/create_json.ml
46b2f6
new file mode 100644
46b2f6
index 000000000..fdf7b12f5
46b2f6
--- /dev/null
46b2f6
+++ b/v2v/create_json.ml
46b2f6
@@ -0,0 +1,348 @@
46b2f6
+(* virt-v2v
46b2f6
+ * Copyright (C) 2019 Red Hat Inc.
46b2f6
+ *
46b2f6
+ * This program is free software; you can redistribute it and/or modify
46b2f6
+ * it under the terms of the GNU General Public License as published by
46b2f6
+ * the Free Software Foundation; either version 2 of the License, or
46b2f6
+ * (at your option) any later version.
46b2f6
+ *
46b2f6
+ * This program is distributed in the hope that it will be useful,
46b2f6
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
46b2f6
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46b2f6
+ * GNU General Public License for more details.
46b2f6
+ *
46b2f6
+ * You should have received a copy of the GNU General Public License along
46b2f6
+ * with this program; if not, write to the Free Software Foundation, Inc.,
46b2f6
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46b2f6
+ *)
46b2f6
+
46b2f6
+open Std_utils
46b2f6
+open C_utils
46b2f6
+open Tools_utils
46b2f6
+
46b2f6
+open Types
46b2f6
+open Utils
46b2f6
+
46b2f6
+module G = Guestfs
46b2f6
+
46b2f6
+let json_list_of_string_list =
46b2f6
+  List.map (fun x -> JSON.String x)
46b2f6
+
46b2f6
+let json_list_of_string_string_list =
46b2f6
+  List.map (fun (x, y) -> x, JSON.String y)
46b2f6
+
46b2f6
+let push_optional_string lst name = function
46b2f6
+  | None -> ()
46b2f6
+  | Some v -> List.push_back lst (name, JSON.String v)
46b2f6
+
46b2f6
+let push_optional_int lst name = function
46b2f6
+  | None -> ()
46b2f6
+  | Some v -> List.push_back lst (name, JSON.Int (Int64.of_int v))
46b2f6
+
46b2f6
+let json_unknown_string = function
46b2f6
+  | "unknown" -> JSON.Null
46b2f6
+  | v -> JSON.String v
46b2f6
+
46b2f6
+let find_target_disk targets { s_disk_id = id } =
46b2f6
+  try List.find (fun t -> t.target_overlay.ov_source.s_disk_id = id) targets
46b2f6
+  with Not_found -> assert false
46b2f6
+
46b2f6
+let create_json_metadata source targets target_buses
46b2f6
+                         guestcaps inspect target_firmware =
46b2f6
+  let doc = ref [
46b2f6
+    "version", JSON.Int 1L;
46b2f6
+    "name", JSON.String source.s_name;
46b2f6
+    "memory", JSON.Int source.s_memory;
46b2f6
+    "vcpu", JSON.Int (Int64.of_int source.s_vcpu);
46b2f6
+  ] in
46b2f6
+
46b2f6
+  (match source.s_genid with
46b2f6
+   | None -> ()
46b2f6
+   | Some genid -> List.push_back doc ("genid", JSON.String genid)
46b2f6
+  );
46b2f6
+
46b2f6
+  if source.s_cpu_vendor <> None || source.s_cpu_model <> None ||
46b2f6
+     source.s_cpu_topology <> None then (
46b2f6
+    let cpu = ref [] in
46b2f6
+
46b2f6
+    push_optional_string cpu "vendor" source.s_cpu_vendor;
46b2f6
+    push_optional_string cpu "model" source.s_cpu_model;
46b2f6
+    (match source.s_cpu_topology with
46b2f6
+     | None -> ()
46b2f6
+     | Some { s_cpu_sockets; s_cpu_cores; s_cpu_threads } ->
46b2f6
+        let attrs = [
46b2f6
+          "sockets", JSON.Int (Int64.of_int s_cpu_sockets);
46b2f6
+          "cores", JSON.Int (Int64.of_int s_cpu_cores);
46b2f6
+          "threads", JSON.Int (Int64.of_int s_cpu_threads);
46b2f6
+        ] in
46b2f6
+        List.push_back cpu ("topology", JSON.Dict attrs)
46b2f6
+    );
46b2f6
+
46b2f6
+    List.push_back doc ("cpu", JSON.Dict !cpu);
46b2f6
+  );
46b2f6
+
46b2f6
+  let firmware =
46b2f6
+    let firmware_type =
46b2f6
+      match target_firmware with
46b2f6
+      | TargetBIOS -> "bios"
46b2f6
+      | TargetUEFI -> "uefi" in
46b2f6
+
46b2f6
+    let fw = ref [
46b2f6
+      "type", JSON.String firmware_type;
46b2f6
+    ] in
46b2f6
+
46b2f6
+    (match target_firmware with
46b2f6
+     | TargetBIOS -> ()
46b2f6
+     | TargetUEFI ->
46b2f6
+       let uefi_firmware = find_uefi_firmware guestcaps.gcaps_arch in
46b2f6
+       let flags =
46b2f6
+         List.map (
46b2f6
+           function
46b2f6
+           | Uefi.UEFI_FLAG_SECURE_BOOT_REQUIRED -> "secure_boot_required"
46b2f6
+         ) uefi_firmware.Uefi.flags in
46b2f6
+
46b2f6
+       let uefi = ref [
46b2f6
+         "code", JSON.String uefi_firmware.Uefi.code;
46b2f6
+         "vars", JSON.String uefi_firmware.Uefi.vars;
46b2f6
+         "flags", JSON.List (json_list_of_string_list flags);
46b2f6
+       ] in
46b2f6
+
46b2f6
+       push_optional_string uefi "code-debug" uefi_firmware.Uefi.code_debug;
46b2f6
+
46b2f6
+       List.push_back fw ("uefi", JSON.Dict !uefi)
46b2f6
+    );
46b2f6
+
46b2f6
+    !fw in
46b2f6
+  List.push_back doc ("firmware", JSON.Dict firmware);
46b2f6
+
46b2f6
+  List.push_back doc ("features",
46b2f6
+                      JSON.List (json_list_of_string_list source.s_features));
46b2f6
+
46b2f6
+  let machine =
46b2f6
+    match guestcaps.gcaps_machine with
46b2f6
+    | I440FX -> "pc"
46b2f6
+    | Q35 -> "q35"
46b2f6
+    | Virt -> "virt" in
46b2f6
+  List.push_back doc ("machine", JSON.String machine);
46b2f6
+
46b2f6
+  let disks, removables =
46b2f6
+    let disks = ref []
46b2f6
+    and removables = ref [] in
46b2f6
+
46b2f6
+    let iter_bus bus_name drive_prefix i = function
46b2f6
+    | BusSlotEmpty -> ()
46b2f6
+    | BusSlotDisk d ->
46b2f6
+       (* Find the corresponding target disk. *)
46b2f6
+       let t = find_target_disk targets d in
46b2f6
+
46b2f6
+       let target_file =
46b2f6
+         match t.target_file with
46b2f6
+         | TargetFile s -> s
46b2f6
+         | TargetURI _ -> assert false in
46b2f6
+
46b2f6
+       let disk = [
46b2f6
+         "dev", JSON.String (drive_prefix ^ drive_name i);
46b2f6
+         "bus", JSON.String bus_name;
46b2f6
+         "format", JSON.String t.target_format;
46b2f6
+         "file", JSON.String (absolute_path target_file);
46b2f6
+       ] in
46b2f6
+
46b2f6
+       List.push_back disks (JSON.Dict disk)
46b2f6
+
46b2f6
+    | BusSlotRemovable { s_removable_type = CDROM } ->
46b2f6
+       let cdrom = [
46b2f6
+         "type", JSON.String "cdrom";
46b2f6
+         "dev", JSON.String (drive_prefix ^ drive_name i);
46b2f6
+         "bus", JSON.String bus_name;
46b2f6
+       ] in
46b2f6
+
46b2f6
+       List.push_back removables (JSON.Dict cdrom)
46b2f6
+
46b2f6
+    | BusSlotRemovable { s_removable_type = Floppy } ->
46b2f6
+       let floppy = [
46b2f6
+         "type", JSON.String "floppy";
46b2f6
+         "dev", JSON.String (drive_prefix ^ drive_name i);
46b2f6
+       ] in
46b2f6
+
46b2f6
+       List.push_back removables (JSON.Dict floppy)
46b2f6
+    in
46b2f6
+
46b2f6
+    Array.iteri (iter_bus "virtio" "vd") target_buses.target_virtio_blk_bus;
46b2f6
+    Array.iteri (iter_bus "ide" "hd") target_buses.target_ide_bus;
46b2f6
+    Array.iteri (iter_bus "scsi" "sd") target_buses.target_scsi_bus;
46b2f6
+    Array.iteri (iter_bus "floppy" "fd") target_buses.target_floppy_bus;
46b2f6
+
46b2f6
+    !disks, !removables in
46b2f6
+  List.push_back doc ("disks", JSON.List disks);
46b2f6
+  List.push_back doc ("removables", JSON.List removables);
46b2f6
+
46b2f6
+  let nics =
46b2f6
+    List.map (
46b2f6
+      fun { s_mac = mac; s_vnet_type = vnet_type; s_nic_model = nic_model;
46b2f6
+            s_vnet = vnet; } ->
46b2f6
+        let vnet_type_str =
46b2f6
+          match vnet_type with
46b2f6
+          | Bridge -> "bridge"
46b2f6
+          | Network -> "network" in
46b2f6
+
46b2f6
+        let nic = ref [
46b2f6
+          "vnet", JSON.String vnet;
46b2f6
+          "vnet-type", JSON.String vnet_type_str;
46b2f6
+        ] in
46b2f6
+
46b2f6
+        let nic_model_str = Option.map string_of_nic_model nic_model in
46b2f6
+        push_optional_string nic "model" nic_model_str;
46b2f6
+
46b2f6
+        push_optional_string nic "mac" mac;
46b2f6
+
46b2f6
+        JSON.Dict !nic
46b2f6
+    ) source.s_nics in
46b2f6
+  List.push_back doc ("nics", JSON.List nics);
46b2f6
+
46b2f6
+  let guestcaps_dict =
46b2f6
+    let block_bus =
46b2f6
+      match guestcaps.gcaps_block_bus with
46b2f6
+      | Virtio_blk -> "virtio-blk"
46b2f6
+      | Virtio_SCSI -> "virtio-scsi"
46b2f6
+      | IDE -> "ide" in
46b2f6
+    let net_bus =
46b2f6
+      match guestcaps.gcaps_net_bus with
46b2f6
+      | Virtio_net -> "virtio-net"
46b2f6
+      | E1000 -> "e1000"
46b2f6
+      | RTL8139 -> "rtl8139" in
46b2f6
+    let video =
46b2f6
+      match guestcaps.gcaps_video with
46b2f6
+      | QXL -> "qxl"
46b2f6
+      | Cirrus -> "cirrus" in
46b2f6
+    let machine =
46b2f6
+      match guestcaps.gcaps_machine with
46b2f6
+      | I440FX -> "i440fx"
46b2f6
+      | Q35 -> "q35"
46b2f6
+      | Virt -> "virt" in
46b2f6
+
46b2f6
+    [
46b2f6
+      "block-bus", JSON.String block_bus;
46b2f6
+      "net-bus", JSON.String net_bus;
46b2f6
+      "video", JSON.String video;
46b2f6
+      "machine", JSON.String machine;
46b2f6
+      "arch", JSON.String guestcaps.gcaps_arch;
46b2f6
+      "virtio-rng", JSON.Bool guestcaps.gcaps_virtio_rng;
46b2f6
+      "virtio-balloon", JSON.Bool guestcaps.gcaps_virtio_balloon;
46b2f6
+      "isa-pvpanic", JSON.Bool guestcaps.gcaps_isa_pvpanic;
46b2f6
+      "acpi", JSON.Bool guestcaps.gcaps_acpi;
46b2f6
+    ] in
46b2f6
+  List.push_back doc ("guestcaps", JSON.Dict guestcaps_dict);
46b2f6
+
46b2f6
+  (match source.s_sound with
46b2f6
+   | None -> ()
46b2f6
+   | Some { s_sound_model = model } ->
46b2f6
+     let sound = [
46b2f6
+       "model", JSON.String (string_of_source_sound_model model);
46b2f6
+     ] in
46b2f6
+     List.push_back doc ("sound", JSON.Dict sound)
46b2f6
+   );
46b2f6
+
46b2f6
+  (match source.s_display with
46b2f6
+   | None -> ()
46b2f6
+   | Some d ->
46b2f6
+     let display_type =
46b2f6
+       match d.s_display_type with
46b2f6
+       | Window -> "window"
46b2f6
+       | VNC -> "vnc"
46b2f6
+       | Spice -> "spice" in
46b2f6
+
46b2f6
+     let display = ref [
46b2f6
+       "type", JSON.String display_type;
46b2f6
+     ] in
46b2f6
+
46b2f6
+     push_optional_string display "keymap" d.s_keymap;
46b2f6
+     push_optional_string display "password" d.s_password;
46b2f6
+
46b2f6
+     let listen =
46b2f6
+       match d.s_listen with
46b2f6
+       | LNoListen -> None
46b2f6
+       | LAddress address ->
46b2f6
+         Some [
46b2f6
+           "type", JSON.String "address";
46b2f6
+           "address", JSON.String address;
46b2f6
+         ]
46b2f6
+       | LNetwork network ->
46b2f6
+         Some [
46b2f6
+           "type", JSON.String "network";
46b2f6
+           "network", JSON.String network;
46b2f6
+         ]
46b2f6
+       | LSocket None ->
46b2f6
+         Some [
46b2f6
+           "type", JSON.String "socket";
46b2f6
+           "socket", JSON.Null;
46b2f6
+         ]
46b2f6
+       | LSocket (Some socket) ->
46b2f6
+         Some [
46b2f6
+           "type", JSON.String "socket";
46b2f6
+           "socket", JSON.String socket;
46b2f6
+         ]
46b2f6
+       | LNone ->
46b2f6
+         Some [
46b2f6
+           "type", JSON.String "none";
46b2f6
+         ] in
46b2f6
+     (match listen with
46b2f6
+      | None -> ()
46b2f6
+      | Some l -> List.push_back display ("listen", JSON.Dict l)
46b2f6
+     );
46b2f6
+
46b2f6
+     push_optional_int display "port" d.s_port;
46b2f6
+
46b2f6
+     List.push_back doc ("display", JSON.Dict !display)
46b2f6
+  );
46b2f6
+
46b2f6
+  let inspect_dict =
46b2f6
+    let apps =
46b2f6
+      List.map (
46b2f6
+        fun { G.app2_name = name; app2_display_name = display_name;
46b2f6
+              app2_epoch = epoch; app2_version = version;
46b2f6
+              app2_release = release; app2_arch = arch; } ->
46b2f6
+          JSON.Dict [
46b2f6
+            "name", JSON.String name;
46b2f6
+            "display-name", JSON.String display_name;
46b2f6
+            "epoch", JSON.Int (Int64.of_int32 epoch);
46b2f6
+            "version", JSON.String version;
46b2f6
+            "release", JSON.String release;
46b2f6
+            "arch", JSON.String arch;
46b2f6
+          ]
46b2f6
+      ) inspect.i_apps in
46b2f6
+
46b2f6
+    let firmware_dict =
46b2f6
+      match inspect.i_firmware with
46b2f6
+      | I_BIOS ->
46b2f6
+        [
46b2f6
+          "type", JSON.String "bios";
46b2f6
+        ]
46b2f6
+      | I_UEFI devices ->
46b2f6
+        [
46b2f6
+          "type", JSON.String "uefi";
46b2f6
+          "devices", JSON.List (json_list_of_string_list devices);
46b2f6
+        ] in
46b2f6
+
46b2f6
+    [
46b2f6
+      "root", JSON.String inspect.i_root;
46b2f6
+      "type", JSON.String inspect.i_type;
46b2f6
+      "distro", json_unknown_string inspect.i_distro;
46b2f6
+      "osinfo", json_unknown_string inspect.i_osinfo;
46b2f6
+      "arch", JSON.String inspect.i_arch;
46b2f6
+      "major-version", JSON.Int (Int64.of_int inspect.i_major_version);
46b2f6
+      "minor-version", JSON.Int (Int64.of_int inspect.i_minor_version);
46b2f6
+      "package-format", json_unknown_string inspect.i_package_format;
46b2f6
+      "package-management", json_unknown_string inspect.i_package_management;
46b2f6
+      "product-name", json_unknown_string inspect.i_product_name;
46b2f6
+      "product-variant", json_unknown_string inspect.i_product_variant;
46b2f6
+      "mountpoints", JSON.Dict (json_list_of_string_string_list inspect.i_mountpoints);
46b2f6
+      "applications", JSON.List apps;
46b2f6
+      "windows-systemroot", JSON.String inspect.i_windows_systemroot;
46b2f6
+      "windows-software-hive", JSON.String inspect.i_windows_software_hive;
46b2f6
+      "windows-system-hive", JSON.String inspect.i_windows_system_hive;
46b2f6
+      "windows-current-control-set", JSON.String inspect.i_windows_current_control_set;
46b2f6
+      "firmware", JSON.Dict firmware_dict;
46b2f6
+    ] in
46b2f6
+  List.push_back doc ("inspect", JSON.Dict inspect_dict);
46b2f6
+
46b2f6
+  !doc
46b2f6
diff --git a/v2v/create_json.mli b/v2v/create_json.mli
46b2f6
new file mode 100644
46b2f6
index 000000000..6dbb6e48b
46b2f6
--- /dev/null
46b2f6
+++ b/v2v/create_json.mli
46b2f6
@@ -0,0 +1,29 @@
46b2f6
+(* virt-v2v
46b2f6
+ * Copyright (C) 2019 Red Hat Inc.
46b2f6
+ *
46b2f6
+ * This program is free software; you can redistribute it and/or modify
46b2f6
+ * it under the terms of the GNU General Public License as published by
46b2f6
+ * the Free Software Foundation; either version 2 of the License, or
46b2f6
+ * (at your option) any later version.
46b2f6
+ *
46b2f6
+ * This program is distributed in the hope that it will be useful,
46b2f6
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
46b2f6
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46b2f6
+ * GNU General Public License for more details.
46b2f6
+ *
46b2f6
+ * You should have received a copy of the GNU General Public License along
46b2f6
+ * with this program; if not, write to the Free Software Foundation, Inc.,
46b2f6
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46b2f6
+ *)
46b2f6
+
46b2f6
+(** Create JSON metadata for [-o json]. *)
46b2f6
+
46b2f6
+val create_json_metadata : Types.source -> Types.target list ->
46b2f6
+                           Types.target_buses ->
46b2f6
+                           Types.guestcaps ->
46b2f6
+                           Types.inspect ->
46b2f6
+                           Types.target_firmware ->
46b2f6
+                           JSON.doc
46b2f6
+(** [create_json_metadata source targets target_buses guestcaps
46b2f6
+    inspect target_firmware] creates the JSON with the majority
46b2f6
+    of the data that virt-v2v used for the conversion. *)
46b2f6
diff --git a/v2v/output_json.ml b/v2v/output_json.ml
46b2f6
new file mode 100644
46b2f6
index 000000000..ca0bda978
46b2f6
--- /dev/null
46b2f6
+++ b/v2v/output_json.ml
46b2f6
@@ -0,0 +1,116 @@
46b2f6
+(* virt-v2v
46b2f6
+ * Copyright (C) 2019 Red Hat Inc.
46b2f6
+ *
46b2f6
+ * This program is free software; you can redistribute it and/or modify
46b2f6
+ * it under the terms of the GNU General Public License as published by
46b2f6
+ * the Free Software Foundation; either version 2 of the License, or
46b2f6
+ * (at your option) any later version.
46b2f6
+ *
46b2f6
+ * This program is distributed in the hope that it will be useful,
46b2f6
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
46b2f6
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46b2f6
+ * GNU General Public License for more details.
46b2f6
+ *
46b2f6
+ * You should have received a copy of the GNU General Public License along
46b2f6
+ * with this program; if not, write to the Free Software Foundation, Inc.,
46b2f6
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46b2f6
+ *)
46b2f6
+
46b2f6
+open Printf
46b2f6
+
46b2f6
+open Std_utils
46b2f6
+open Tools_utils
46b2f6
+open Common_gettext.Gettext
46b2f6
+
46b2f6
+open Types
46b2f6
+open Utils
46b2f6
+
46b2f6
+type json_options = {
46b2f6
+  json_disks_pattern : string;
46b2f6
+}
46b2f6
+
46b2f6
+let print_output_options () =
46b2f6
+  printf (f_"Output options (-oo) which can be used with -o json:
46b2f6
+
46b2f6
+  -oo json-disks-pattern=PATTERN   Pattern for the disks.
46b2f6
+")
46b2f6
+
46b2f6
+let known_pattern_variables = ["DiskNo"; "DiskDeviceName"; "GuestName"]
46b2f6
+
46b2f6
+let parse_output_options options =
46b2f6
+  let json_disks_pattern = ref None in
46b2f6
+
46b2f6
+  List.iter (
46b2f6
+    function
46b2f6
+    | "json-disks-pattern", v ->
46b2f6
+       if !json_disks_pattern <> None then
46b2f6
+         error (f_"-o json: -oo json-disks-pattern set more than once");
46b2f6
+       let vars =
46b2f6
+         try Var_expander.scan_variables v
46b2f6
+         with Var_expander.Invalid_variable var ->
46b2f6
+           error (f_"-o json: -oo json-disks-pattern: invalid variable %%{%s}")
46b2f6
+             var in
46b2f6
+       List.iter (
46b2f6
+         fun var ->
46b2f6
+           if not (List.mem var known_pattern_variables) then
46b2f6
+             error (f_"-o json: -oo json-disks-pattern: unhandled variable %%{%s}")
46b2f6
+               var
46b2f6
+       ) vars;
46b2f6
+       json_disks_pattern := Some v
46b2f6
+    | k, _ ->
46b2f6
+       error (f_"-o json: unknown output option ‘-oo %s’") k
46b2f6
+  ) options;
46b2f6
+
46b2f6
+  let json_disks_pattern =
46b2f6
+    Option.default "%{GuestName}-%{DiskDeviceName}" !json_disks_pattern in
46b2f6
+
46b2f6
+  { json_disks_pattern }
46b2f6
+
46b2f6
+class output_json dir json_options = object
46b2f6
+  inherit output
46b2f6
+
46b2f6
+  method as_options = sprintf "-o json -os %s" dir
46b2f6
+
46b2f6
+  method prepare_targets source overlays _ _ _ _ =
46b2f6
+    List.mapi (
46b2f6
+      fun i (_, ov) ->
46b2f6
+        let outname =
46b2f6
+          let vars_fn = function
46b2f6
+            | "DiskNo" -> Some (string_of_int (i+1))
46b2f6
+            | "DiskDeviceName" -> Some ov.ov_sd
46b2f6
+            | "GuestName" -> Some source.s_name
46b2f6
+            | _ -> assert false
46b2f6
+          in
46b2f6
+          Var_expander.replace_fn json_options.json_disks_pattern vars_fn in
46b2f6
+        let destname = dir // outname in
46b2f6
+        mkdir_p (Filename.dirname destname) 0o755;
46b2f6
+        TargetFile destname
46b2f6
+    ) overlays
46b2f6
+
46b2f6
+  method supported_firmware = [ TargetBIOS; TargetUEFI ]
46b2f6
+
46b2f6
+  method create_metadata source targets
46b2f6
+                         target_buses guestcaps inspect target_firmware =
46b2f6
+    let doc =
46b2f6
+      Create_json.create_json_metadata source targets target_buses
46b2f6
+                                       guestcaps inspect target_firmware in
46b2f6
+    let doc_string = JSON.string_of_doc ~fmt:JSON.Indented doc in
46b2f6
+
46b2f6
+    if verbose () then (
46b2f6
+      eprintf "resulting JSON:\n";
46b2f6
+      output_string stderr doc_string;
46b2f6
+      eprintf "\n\n%!";
46b2f6
+    );
46b2f6
+
46b2f6
+    let name = source.s_name in
46b2f6
+    let file = dir // name ^ ".json" in
46b2f6
+
46b2f6
+    with_open_out file (
46b2f6
+      fun chan ->
46b2f6
+        output_string chan doc_string;
46b2f6
+        output_char chan '\n'
46b2f6
+    )
46b2f6
+end
46b2f6
+
46b2f6
+let output_json = new output_json
46b2f6
+let () = Modules_list.register_output_module "json"
46b2f6
diff --git a/v2v/output_json.mli b/v2v/output_json.mli
46b2f6
new file mode 100644
46b2f6
index 000000000..52f58f2d1
46b2f6
--- /dev/null
46b2f6
+++ b/v2v/output_json.mli
46b2f6
@@ -0,0 +1,31 @@
46b2f6
+(* virt-v2v
46b2f6
+ * Copyright (C) 2019 Red Hat Inc.
46b2f6
+ *
46b2f6
+ * This program is free software; you can redistribute it and/or modify
46b2f6
+ * it under the terms of the GNU General Public License as published by
46b2f6
+ * the Free Software Foundation; either version 2 of the License, or
46b2f6
+ * (at your option) any later version.
46b2f6
+ *
46b2f6
+ * This program is distributed in the hope that it will be useful,
46b2f6
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
46b2f6
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46b2f6
+ * GNU General Public License for more details.
46b2f6
+ *
46b2f6
+ * You should have received a copy of the GNU General Public License along
46b2f6
+ * with this program; if not, write to the Free Software Foundation, Inc.,
46b2f6
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46b2f6
+ *)
46b2f6
+
46b2f6
+(** [-o json] target. *)
46b2f6
+
46b2f6
+type json_options
46b2f6
+(** Miscellaneous extra command line parameters used by json. *)
46b2f6
+
46b2f6
+val print_output_options : unit -> unit
46b2f6
+val parse_output_options : (string * string) list -> json_options
46b2f6
+(** Print and parse json -oo options. *)
46b2f6
+
46b2f6
+val output_json : string -> json_options -> Types.output
46b2f6
+(** [output_json directory json_options] creates and returns a new
46b2f6
+    {!Types.output} object specialized for writing output to local
46b2f6
+    files with JSON metadata. *)
46b2f6
diff --git a/v2v/virt-v2v-output-local.pod b/v2v/virt-v2v-output-local.pod
46b2f6
index 7427b1ed7..7c397c0a4 100644
46b2f6
--- a/v2v/virt-v2v-output-local.pod
46b2f6
+++ b/v2v/virt-v2v-output-local.pod
46b2f6
@@ -11,6 +11,9 @@ or libvirt
46b2f6
 
46b2f6
  virt-v2v [-i* options] -o qemu -os DIRECTORY [--qemu-boot]
46b2f6
 
46b2f6
+ virt-v2v [-i* options] -o json -os DIRECTORY
46b2f6
+                        [-oo json-disks-pattern=PATTERN]
46b2f6
+
46b2f6
  virt-v2v [-i* options] -o null
46b2f6
 
46b2f6
 =head1 DESCRIPTION
46b2f6
@@ -54,6 +57,13 @@ above, a shell script is created which contains the raw qemu command
46b2f6
 you would need to boot the guest.  However the shell script is not
46b2f6
 run, I<unless> you also add the I<--qemu-boot> option.
46b2f6
 
46b2f6
+=item B<-o json -os> C<DIRECTORY>
46b2f6
+
46b2f6
+This converts the guest to files in C<DIRECTORY>.  The metadata
46b2f6
+produced is a JSON file containing the majority of the data virt-v2v
46b2f6
+gathers during the conversion.
46b2f6
+See L</OUTPUT TO JSON> below.
46b2f6
+
46b2f6
 =item B<-o null>
46b2f6
 
46b2f6
 The guest is converted, but the final result is thrown away and no
46b2f6
@@ -140,6 +150,51 @@ Define the final guest in libvirt:
46b2f6
 
46b2f6
 =back
46b2f6
 
46b2f6
+=head1 OUTPUT TO JSON
46b2f6
+
46b2f6
+The I<-o json> option produces the following files by default:
46b2f6
+
46b2f6
+ NAME.json                     JSON metadata.
46b2f6
+ NAME-sda, NAME-sdb, etc.      Guest disk(s).
46b2f6
+
46b2f6
+where C<NAME> is the guest name.
46b2f6
+
46b2f6
+It is possible to change the pattern of the disks using the
46b2f6
+I<-oo json-disks-pattern=...> option: it allows parameters in form of
46b2f6
+C<%{...}> variables, for example:
46b2f6
+
46b2f6
+ -oo json-disks-pattern=disk%{DiskNo}.img
46b2f6
+
46b2f6
+Recognized variables are:
46b2f6
+
46b2f6
+=over 4
46b2f6
+
46b2f6
+=item C<%{DiskNo}>
46b2f6
+
46b2f6
+The index of the disk, starting from 1.
46b2f6
+
46b2f6
+=item C<%{DiskDeviceName}>
46b2f6
+
46b2f6
+The destination device of the disk, e.g. C<sda>, C<sdb>, etc.
46b2f6
+
46b2f6
+=item C<%{GuestName}>
46b2f6
+
46b2f6
+The name of the guest.
46b2f6
+
46b2f6
+=back
46b2f6
+
46b2f6
+Using a pattern it is possible use subdirectories for the disks,
46b2f6
+even with names depending on variables; for example:
46b2f6
+
46b2f6
+ -oo json-disks-pattern=%{GuestName}-%{DiskNo}/disk.img
46b2f6
+
46b2f6
+The default pattern is C<%{GuestName}-%{DiskDeviceName}>.
46b2f6
+
46b2f6
+If the literal C<%{...}> text is needed, it is possible to avoid the
46b2f6
+escape it with a leading C<%>; for example,
46b2f6
+C<%%{GuestName}-%{DiskNo}.img> will create file names for the
46b2f6
+disks like C<%%{GuestName}-1.img>, C<%%{GuestName}-2.img>, etc.
46b2f6
+
46b2f6
 =head1 SEE ALSO
46b2f6
 
46b2f6
 L<virt-v2v(1)>.
46b2f6
diff --git a/v2v/virt-v2v.pod b/v2v/virt-v2v.pod
46b2f6
index cf9464834..9a555c3be 100644
46b2f6
--- a/v2v/virt-v2v.pod
46b2f6
+++ b/v2v/virt-v2v.pod
46b2f6
@@ -425,6 +425,17 @@ instead.
46b2f6
 Set the output method to OpenStack Glance.  In this mode the converted
46b2f6
 guest is uploaded to Glance.  See L<virt-v2v-output-openstack(1)>.
46b2f6
 
46b2f6
+=item B<-o> B<json>
46b2f6
+
46b2f6
+Set the output method to I<json>.
46b2f6
+
46b2f6
+In this mode, the converted guest is written to a local directory
46b2f6
+specified by I<-os /dir> (the directory must exist), with a JSON file
46b2f6
+containing the majority of the metadata that virt-v2v gathered during
46b2f6
+the conversion.
46b2f6
+
46b2f6
+See L<virt-v2v-output-local(1)>.
46b2f6
+
46b2f6
 =item B<-o> B<libvirt>
46b2f6
 
46b2f6
 Set the output method to I<libvirt>.  This is the default.
46b2f6
@@ -696,8 +707,8 @@ The location of the storage for the converted guest.
46b2f6
 For I<-o libvirt>, this is a libvirt directory pool
46b2f6
 (see S<C<virsh pool-list>>) or pool UUID.
46b2f6
 
46b2f6
-For I<-o local> and I<-o qemu>, this is a directory name.  The
46b2f6
-directory must exist.
46b2f6
+For I<-o json>, I<-o local> and I<-o qemu>, this is a directory name.
46b2f6
+The directory must exist.
46b2f6
 
46b2f6
 For I<-o rhv-upload>, this is the name of the destination Storage
46b2f6
 Domain.
46b2f6
-- 
46b2f6
2.24.1
46b2f6