[Libguestfs] [PATCH] v2v: adding input -i ova

Shahar Havivi shaharh at redhat.com
Thu Aug 21 12:29:00 UTC 2014


Hi,
This is a partial patch, for now extracting the ova file and verify the SHA1 sum,
TODO: parse vmware ovf to Types.source

Comments will be appreciated,
Thanks,
Shahar Havivi.

Signed-off-by: Shahar Havivi <shaharh at redhat.com>
---
 v2v/Makefile.am    |  2 ++
 v2v/cmdline.ml     | 13 ++++++--
 v2v/source_ova.ml  | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 v2v/source_ova.mli | 22 ++++++++++++++
 v2v/types.ml       |  1 +
 v2v/types.mli      |  3 +-
 v2v/utils.ml       | 17 +++++++++++
 v2v/v2v.ml         |  4 ++-
 8 files changed, 146 insertions(+), 4 deletions(-)
 create mode 100644 v2v/source_ova.ml
 create mode 100644 v2v/source_ova.mli

diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index c4ed313..960cda8 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -32,6 +32,7 @@ SOURCES_MLI = \
 	lib_linux.mli \
 	source_disk.mli \
 	source_libvirt.mli \
+	source_ova.mli \
 	target_libvirt.mli \
 	target_local.mli \
 	target_RHEV.mli \
@@ -49,6 +50,7 @@ SOURCES_ML = \
 	cmdline.ml \
 	source_disk.ml \
 	source_libvirt.ml \
+	source_ova.ml \
 	convert_linux.ml \
 	convert_windows.ml \
 	target_libvirt.ml \
diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
index c2d586a..34788e7 100644
--- a/v2v/cmdline.ml
+++ b/v2v/cmdline.ml
@@ -54,6 +54,7 @@ let parse_cmdline () =
     | "disk" | "local" -> input_mode := `Disk
     | "libvirt" -> input_mode := `Libvirt
     | "libvirtxml" -> input_mode := `LibvirtXML
+    | "ova" -> input_mode := `OVA
     | s ->
       error (f_"unknown -i option: %s") s
   in
@@ -105,7 +106,7 @@ let parse_cmdline () =
   let argspec = Arg.align [
     "--bridge",  Arg.String add_bridge,     "in:out " ^ s_"Map bridge 'in' to 'out'";
     "--debug-gc",Arg.Set debug_gc,          " " ^ s_"Debug GC and memory allocations";
-    "-i",        Arg.String set_input_mode, "disk|libvirt|libvirtxml " ^ s_"Set input mode (default: libvirt)";
+    "-i",        Arg.String set_input_mode, "disk|libvirt|libvirtxml|ova " ^ s_"Set input mode (default: libvirt)";
     "-ic",       Arg.Set_string input_conn, "uri " ^ s_"Libvirt URI";
     "-if",       Arg.Set_string input_format,
                                             "format " ^ s_"Input format (for -i disk)";
@@ -231,7 +232,15 @@ read the man page virt-v2v(1).
         | [filename] -> filename
         | _ ->
           error (f_"expecting a libvirt XML file name on the command line") in
-      InputLibvirtXML filename in
+      InputLibvirtXML filename
+    | `OVA ->
+      (* -i ova: Expecting an ova filename (tar file). *)
+      let filename =
+        match args with
+        | [filename] -> filename
+        | _ ->
+          error (f_"expecting a libvirt XML file name on the command line") in
+      InputOVA filename in
 
   (* Parse the output mode. *)
   let output =
diff --git a/v2v/source_ova.ml b/v2v/source_ova.ml
new file mode 100644
index 0000000..f268de0
--- /dev/null
+++ b/v2v/source_ova.ml
@@ -0,0 +1,88 @@
+(* virt-v2v
+ * Copyright (C) 2009-2014 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+open Printf
+
+open Common_gettext.Gettext
+open Common_utils
+
+open Types
+open Utils
+
+let contains =
+  true
+
+let parse_ovf dir ovf =
+  let source = {
+    s_dom_type = "kvm";
+    s_name = "";
+    s_orig_name = "";
+    s_memory = 0L;
+    s_vcpu = 1;
+    s_arch = "";
+    s_features = [""];
+    s_display = None;
+    s_disks = [];
+    s_removables = [];
+    s_nics = [];
+  } in
+  source
+
+let create ova =
+  (* get ova directory *)
+  let dir = Filename.dirname (absolute_path ova) in
+  (* extract ova (tar) file *)
+  let cmd = sprintf ("tar -xf %s -C %s") (ova) (dir) in
+
+  if Sys.command (cmd) <> 0 then
+    error (f_"error running command: %s") cmd
+    exit 1;
+
+  let files = Sys.readdir(dir) in
+  let mf = ref "" in
+  let ovf = ref "" in
+  (* search for the ovf file *)
+   Array.iter (fun file ->
+      let len = String.length file in
+      if len >= 4 && String.lowercase (String.sub file (len-4) 4) = ".ovf" then
+          ovf := file
+      else if len >= 3 && String.lowercase (String.sub file (len-3) 3) = ".mf" then
+          mf := file
+        ) files;
+
+  (* verify sha1 from manifest file *)
+  let mf = dir // !mf in
+  let rex = Str.regexp "SHA1(\\(.*\\))= *\\(.*?\\).*$" in
+  let lines = read_file mf in
+  List.iter (
+    fun line -> 
+      if Str.string_match rex line 0 then
+        let file = Str.matched_group 1 line in
+        let sha1 = Str.matched_group 2 line in
+
+        let cmd = sprintf "sha1sum %s" (dir // file) in
+        let out = external_command ~prog cmd in
+        if List.exists (fun line -> string_contains line sha1) out == false then
+          error (f_"Checksum of %s does not match manifes sha1 %s") (file) (sha1)
+          exit 1;
+      else
+        error (f_"error cant parse mf: %s, line: %s") (mf) (line)
+        exit 1;
+  ) lines;
+
+  parse_ovf dir ovf
diff --git a/v2v/source_ova.mli b/v2v/source_ova.mli
new file mode 100644
index 0000000..6b5f831
--- /dev/null
+++ b/v2v/source_ova.mli
@@ -0,0 +1,22 @@
+(* virt-v2v
+ * Copyright (C) 2009-2014 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(** [-i ova] source. *)
+
+val create : string -> Types.source
+(** [create_from_ova filename] reads the source ovf from vmware ova file. *)
diff --git a/v2v/types.ml b/v2v/types.ml
index ffc99ce..6eff7f8 100644
--- a/v2v/types.ml
+++ b/v2v/types.ml
@@ -24,6 +24,7 @@ type input =
 | InputDisk of string option * string
 | InputLibvirt of string option * string
 | InputLibvirtXML of string
+| InputOVA of string
 
 type output =
 | OutputLibvirt of string option * string
diff --git a/v2v/types.mli b/v2v/types.mli
index 09af68e..9342721 100644
--- a/v2v/types.mli
+++ b/v2v/types.mli
@@ -22,6 +22,7 @@ type input =
 | InputDisk of string option * string   (* -i disk: format + file name *)
 | InputLibvirt of string option * string (* -i libvirt: -ic + guest name *)
 | InputLibvirtXML of string         (* -i libvirtxml: XML file name *)
+| InputOVA of string         (* -i ova: OVA file name *)
 (** The input arguments as specified on the command line. *)
 
 type output =
@@ -90,7 +91,7 @@ val string_of_source : source -> string
 val string_of_source_disk : source_disk -> string
 
 type overlay = {
-  ov_overlay : string;       (** Local overlay file (qcow2 format). *)
+  ov_overlay : string;       (** Local overlgy file (qcow2 format). *)
   ov_target_file : string;   (** Destination file. *)
   ov_target_format : string; (** Destination format (eg. -of option). *)
   ov_sd : string;            (** sdX libguestfs name of disk. *)
diff --git a/v2v/utils.ml b/v2v/utils.ml
index b541b1d..c4a370b 100644
--- a/v2v/utils.ml
+++ b/v2v/utils.ml
@@ -90,3 +90,20 @@ let remove_duplicates xs =
     | x :: xs -> Hashtbl.add h x true; x :: loop xs
   in
   loop xs
+
+let string_contains s1 s2 =
+  let re = Str.regexp_string s2 in
+  try ignore (Str.string_match re s1 0); true
+  with Not_found ->
+    false
+
+let read_file filename =
+  let lines = ref [] in
+  let chan = open_in filename in
+  try
+    while true; do
+      lines := input_line chan :: !lines
+        done; []
+  with End_of_file ->
+    close_in chan;
+  List.rev !lines
diff --git a/v2v/v2v.ml b/v2v/v2v.ml
index 63bc162..de11fe3 100644
--- a/v2v/v2v.ml
+++ b/v2v/v2v.ml
@@ -46,7 +46,9 @@ let rec main () =
     | InputLibvirt (libvirt_uri, guest) ->
       Source_libvirt.create libvirt_uri guest
     | InputLibvirtXML filename ->
-      Source_libvirt.create_from_xml filename in
+      Source_libvirt.create_from_xml filename
+    | InputOVA filename ->
+      Source_ova.create filename in
 
   (* Print source and stop. *)
   if print_source then (
-- 
1.9.3




More information about the Libguestfs mailing list