Skip to content

Commit

Permalink
v2v: Refactor Xen and vCenter code.
Browse files Browse the repository at this point in the history
This refactors the code into more logical units, based on the previous
commits.  So Xen + the input_libvirt Xen code is grouped and moved
into a new module called Input_libvirt_xen_ssh.  Similarly VCenter +
input_libvirt vCenter code is moved to Input_libvirt_vcenter_https.

There is no change here, purely code motion.
  • Loading branch information
rwmjones committed Oct 20, 2014
1 parent 9281dc7 commit a468fde
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 154 deletions.
5 changes: 3 additions & 2 deletions po/POTFILES-ml
Expand Up @@ -91,6 +91,9 @@ v2v/convert_windows.ml
v2v/domainxml.ml
v2v/input_disk.ml
v2v/input_libvirt.ml
v2v/input_libvirt_other.ml
v2v/input_libvirt_vcenter_https.ml
v2v/input_libvirt_xen_ssh.ml
v2v/input_libvirtxml.ml
v2v/input_ova.ml
v2v/kvmuid.ml
Expand All @@ -107,6 +110,4 @@ v2v/stringMap.ml
v2v/types.ml
v2v/utils.ml
v2v/v2v.ml
v2v/vCenter.ml
v2v/xen.ml
v2v/xml.ml
10 changes: 6 additions & 4 deletions v2v/Makefile.am
Expand Up @@ -42,6 +42,9 @@ SOURCES_MLI = \
domainxml.mli \
input_disk.mli \
input_libvirt.mli \
input_libvirt_other.mli \
input_libvirt_vcenter_https.mli \
input_libvirt_xen_ssh.mli \
input_libvirtxml.mli \
input_ova.mli \
kvmuid.mli \
Expand All @@ -57,8 +60,6 @@ SOURCES_MLI = \
OVF.mli \
stringMap.mli \
types.mli \
vCenter.mli \
xen.mli \
xml.mli

SOURCES_ML = \
Expand All @@ -69,13 +70,14 @@ SOURCES_ML = \
domainxml.ml \
DOM.ml \
kvmuid.ml \
vCenter.ml \
xen.ml \
OVF.ml \
linux.ml \
modules_list.ml \
input_disk.ml \
input_libvirtxml.ml \
input_libvirt_other.ml \
input_libvirt_vcenter_https.ml \
input_libvirt_xen_ssh.ml \
input_libvirt.ml \
input_ova.ml \
convert_linux.ml \
Expand Down
145 changes: 7 additions & 138 deletions v2v/input_libvirt.ml
Expand Up @@ -16,6 +16,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*)

(** [-i libvirt] source. *)

open Printf

open Common_gettext.Gettext
Expand All @@ -24,144 +26,11 @@ open Common_utils
open Types
open Utils

(* Check the backend is not libvirt. Works around a libvirt bug
* (RHBZ#1134592). This can be removed once the libvirt bug is fixed.
*)
let error_if_libvirt_backend () =
let libguestfs_backend = (new Guestfs.guestfs ())#get_backend () in
if libguestfs_backend = "libvirt" then (
error (f_"because of libvirt bug https://bugzilla.redhat.com/show_bug.cgi?id=1134592 you must set this environment variable:\n\nexport LIBGUESTFS_BACKEND=direct\n\nand then rerun the virt-v2v command.")
)

(* xen+ssh URLs use the SSH driver in CURL. Currently this requires
* ssh-agent authentication. Give a clear error if this hasn't been
* set up (RHBZ#1139973).
*)
let error_if_no_ssh_agent () =
try ignore (Sys.getenv "SSH_AUTH_SOCK")
with Not_found ->
error (f_"ssh-agent authentication has not been set up ($SSH_AUTH_SOCK is not set). Please read \"INPUT FROM RHEL 5 XEN\" in the virt-v2v(1) man page.")

(* Superclass. *)
class virtual input_libvirt verbose libvirt_uri guest =
object
inherit input verbose

method as_options =
sprintf "-i libvirt%s %s"
(match libvirt_uri with
| None -> ""
| Some uri -> " -ic " ^ uri)
guest
end

(* Subclass specialized for handling anything that's *not* VMware vCenter
* or Xen.
*)
class input_libvirt_other verbose libvirt_uri guest =
object
inherit input_libvirt verbose libvirt_uri guest

method source () =
if verbose then printf "input_libvirt_other: source()\n%!";

(* Get the libvirt XML. This also checks (as a side-effect)
* that the domain is not running. (RHBZ#1138586)
*)
let xml = Domainxml.dumpxml ?conn:libvirt_uri guest in

Input_libvirtxml.parse_libvirt_xml ~verbose xml
end

(* Subclass specialized for handling VMware vCenter over https. *)
class input_libvirt_vcenter_https
verbose libvirt_uri parsed_uri scheme server guest =
object
inherit input_libvirt verbose libvirt_uri guest

val mutable mapf = fun ?readahead uri format -> uri, format
val saved_uri = Hashtbl.create 13

method source () =
if verbose then printf "input_libvirt_vcenter_https: source()\n%!";

error_if_libvirt_backend ();

(* Get the libvirt XML. This also checks (as a side-effect)
* that the domain is not running. (RHBZ#1138586)
*)
let xml = Domainxml.dumpxml ?conn:libvirt_uri guest in
let { s_disks = disks } as source =
Input_libvirtxml.parse_libvirt_xml ~verbose xml in

(* Save the mapf function and the original s_qemu_uri fields, so
* we can get them in the adjust_overlay_parameters method below.
*)
mapf <- VCenter.map_path_to_uri verbose parsed_uri scheme server;
List.iter (
fun disk ->
Hashtbl.add saved_uri disk.s_disk_id (disk.s_qemu_uri, disk.s_format)
) disks;

let disks = List.map (
fun ({ s_qemu_uri = uri; s_format = format } as disk) ->
let uri, format = mapf uri format in
{ disk with s_qemu_uri = uri; s_format = format }
) disks in

{ source with s_disks = disks }

(* See RHBZ#1151033 and RHBZ#1153589 for why this is necessary. *)
method adjust_overlay_parameters overlay =
let orig_uri, orig_format =
try Hashtbl.find saved_uri overlay.ov_source.s_disk_id
with Not_found -> failwith "internal error in adjust_overlay_parameters" in
let backing_file, _ =
mapf ~readahead:(64 * 1024 * 1024) orig_uri orig_format in

(* Rebase the qcow2 overlay to adjust the readahead parameter. *)
let cmd =
sprintf "qemu-img rebase -u -b %s %s"
(quote backing_file) (quote overlay.ov_overlay_file) in
if verbose then printf "%s\n%!" cmd;
if Sys.command cmd <> 0 then
warning ~prog (f_"qemu-img rebase failed, see earlier errors")
end

(* Subclass specialized for handling Xen over SSH. *)
class input_libvirt_xen_ssh
verbose libvirt_uri parsed_uri scheme server guest =
object
inherit input_libvirt verbose libvirt_uri guest

method source () =
if verbose then printf "input_libvirt_xen_ssh: source()\n%!";

error_if_libvirt_backend ();
error_if_no_ssh_agent ();

(* Get the libvirt XML. This also checks (as a side-effect)
* that the domain is not running. (RHBZ#1138586)
*)
let xml = Domainxml.dumpxml ?conn:libvirt_uri guest in
let { s_disks = disks } as source =
Input_libvirtxml.parse_libvirt_xml ~verbose xml in

let mapf = Xen.map_path_to_uri verbose parsed_uri scheme server in
let disks = List.map (
fun ({ s_qemu_uri = uri; s_format = format } as disk) ->
let uri, format = mapf uri format in
{ disk with s_qemu_uri = uri; s_format = format }
) disks in

{ source with s_disks = disks }
end

(* Choose the right subclass based on the URI. *)
let input_libvirt verbose libvirt_uri guest =
match libvirt_uri with
| None ->
new input_libvirt_other verbose libvirt_uri guest
Input_libvirt_other.input_libvirt_other verbose libvirt_uri guest

| Some orig_uri ->
let { Xml.uri_server = server; uri_scheme = scheme } as parsed_uri =
Expand All @@ -176,14 +45,14 @@ let input_libvirt verbose libvirt_uri guest =

| Some _, None (* No scheme? *)
| Some _, Some "" ->
new input_libvirt_other verbose libvirt_uri guest
Input_libvirt_other.input_libvirt_other verbose libvirt_uri guest

| Some server, Some ("esx"|"gsx"|"vpx" as scheme) -> (* vCenter over https *)
new input_libvirt_vcenter_https
Input_libvirt_vcenter_https.input_libvirt_vcenter_https
verbose libvirt_uri parsed_uri scheme server guest

| Some server, Some ("xen+ssh" as scheme) -> (* Xen over SSH *)
new input_libvirt_xen_ssh
Input_libvirt_xen_ssh.input_libvirt_xen_ssh
verbose libvirt_uri parsed_uri scheme server guest

(* Old virt-v2v also supported qemu+ssh://. However I am
Expand All @@ -194,6 +63,6 @@ let input_libvirt verbose libvirt_uri guest =
| Some _, Some _ -> (* Unknown remote scheme. *)
warning ~prog (f_"no support for remote libvirt connections to '-ic %s'. The conversion may fail when it tries to read the source disks.")
orig_uri;
new input_libvirt_other verbose libvirt_uri guest
Input_libvirt_other.input_libvirt_other verbose libvirt_uri guest

let () = Modules_list.register_input_module "libvirt"
76 changes: 76 additions & 0 deletions v2v/input_libvirt_other.ml
@@ -0,0 +1,76 @@
(* 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

(* Check the backend is not libvirt. Works around a libvirt bug
* (RHBZ#1134592). This can be removed once the libvirt bug is fixed.
*)
let error_if_libvirt_backend () =
let libguestfs_backend = (new Guestfs.guestfs ())#get_backend () in
if libguestfs_backend = "libvirt" then (
error (f_"because of libvirt bug https://bugzilla.redhat.com/show_bug.cgi?id=1134592 you must set this environment variable:\n\nexport LIBGUESTFS_BACKEND=direct\n\nand then rerun the virt-v2v command.")
)

(* xen+ssh URLs use the SSH driver in CURL. Currently this requires
* ssh-agent authentication. Give a clear error if this hasn't been
* set up (RHBZ#1139973).
*)
let error_if_no_ssh_agent () =
try ignore (Sys.getenv "SSH_AUTH_SOCK")
with Not_found ->
error (f_"ssh-agent authentication has not been set up ($SSH_AUTH_SOCK is not set). Please read \"INPUT FROM RHEL 5 XEN\" in the virt-v2v(1) man page.")

(* Superclass. *)
class virtual input_libvirt verbose libvirt_uri guest =
object
inherit input verbose

method as_options =
sprintf "-i libvirt%s %s"
(match libvirt_uri with
| None -> ""
| Some uri -> " -ic " ^ uri)
guest
end

(* Subclass specialized for handling anything that's *not* VMware vCenter
* or Xen.
*)
class input_libvirt_other verbose libvirt_uri guest =
object
inherit input_libvirt verbose libvirt_uri guest

method source () =
if verbose then printf "input_libvirt_other: source()\n%!";

(* Get the libvirt XML. This also checks (as a side-effect)
* that the domain is not running. (RHBZ#1138586)
*)
let xml = Domainxml.dumpxml ?conn:libvirt_uri guest in

Input_libvirtxml.parse_libvirt_xml ~verbose xml
end

let input_libvirt_other = new input_libvirt_other
30 changes: 30 additions & 0 deletions v2v/input_libvirt_other.mli
@@ -0,0 +1,30 @@
(* 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 libvirt] source. *)

val error_if_libvirt_backend : unit -> unit
val error_if_no_ssh_agent : unit -> unit

class virtual input_libvirt : bool -> string option -> string -> object
method as_options : string
method virtual source : unit -> Types.source
method adjust_overlay_parameters : Types.overlay -> unit
end

val input_libvirt_other : bool -> string option -> string -> Types.input

0 comments on commit a468fde

Please sign in to comment.