mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

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

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