[Pkg-libvirt-commits] [libguestfs] 108/266: v2v: Move libvirt XML generation into common module, and use DOM module.

Hilko Bengen bengen at moszumanska.debian.org
Fri Oct 3 14:41:48 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag debian/1%1.27.35-1
in repository libguestfs.

commit e95a1c75d8a5bb2ef230cd9f13bbe979e2a7457a
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Fri Aug 15 10:08:29 2014 +0100

    v2v: Move libvirt XML generation into common module, and use DOM module.
    
    Since `-o local' and `-o libvirt' both need to generate libvirt XML
    they should share the same code to generate it.
    
    Also generate it using the DOM module instead of printing the XML
    directly.  This is cleaner and avoids quoting issues.
    
    No functional change here.
---
 po/POTFILES-ml         |  1 +
 v2v/Makefile.am        |  2 ++
 v2v/target_libvirt.ml  | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++
 v2v/target_libvirt.mli | 25 ++++++++++++++
 v2v/target_local.ml    | 57 ++----------------------------
 5 files changed, 125 insertions(+), 54 deletions(-)

diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index 2c2b71b..4111d63 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -90,6 +90,7 @@ v2v/lib_linux.ml
 v2v/source_libvirt.ml
 v2v/stringMap.ml
 v2v/target_RHEV.ml
+v2v/target_libvirt.ml
 v2v/target_local.ml
 v2v/types.ml
 v2v/utils.ml
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index 2c1ce77..e2f4635 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -30,6 +30,7 @@ SOURCES_MLI = \
 	DOM.mli \
 	lib_linux.mli \
 	source_libvirt.mli \
+	target_libvirt.mli \
 	target_local.mli \
 	target_RHEV.mli \
 	types.mli \
@@ -46,6 +47,7 @@ SOURCES_ML = \
 	source_libvirt.ml \
 	convert_linux.ml \
 	convert_windows.ml \
+	target_libvirt.ml \
 	target_local.ml \
 	target_RHEV.ml \
 	v2v.ml
diff --git a/v2v/target_libvirt.ml b/v2v/target_libvirt.ml
new file mode 100644
index 0000000..5bc6ae9
--- /dev/null
+++ b/v2v/target_libvirt.ml
@@ -0,0 +1,94 @@
+(* 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
+open DOM
+
+let create_libvirt_xml source overlays guestcaps =
+  let memory_k = source.s_memory /^ 1024L in
+
+  let features =
+    List.filter (
+      fun feature ->
+        (* drop acpi if the guest doesn't support it *)
+        feature <> "acpi" || guestcaps.gcaps_acpi
+    ) source.s_features in
+
+
+  let disks =
+    let block_prefix =
+      if guestcaps.gcaps_block_bus = "virtio" then "vd" else "hd" in
+    List.mapi (
+      fun i ov ->
+        e "disk" ["type", "file"; "device", "disk"] [
+          e "driver" [
+            "name", "qemu";
+            "type", ov.ov_target_format;
+            "cache", "none"
+          ] [];
+          e "source" [
+            "file", ov.ov_target_file;
+          ] [];
+          e "target" [
+            "dev", block_prefix ^ (drive_name i);
+            "bus", guestcaps.gcaps_block_bus;
+          ] [];
+        ]
+    ) overlays in
+
+  (* XXX Missing here from list of devices compared to old virt-v2v:
+     <video/>
+     <graphics/>
+     cdroms and floppies
+     network interfaces
+     See: lib/Sys/VirtConvert/Connection/LibVirtTarget.pm
+  *)
+  let devices = disks @
+  (* Standard devices added to every guest. *) [
+    e "input" ["type", "tablet"; "bus", "usb"] [];
+    e "input" ["type", "mouse"; "bus", "ps2"] [];
+    e "console" ["type", "pty"] [];
+  ] in
+
+  let doc : doc =
+    doc "domain" [
+      "type", "kvm";                (* Always assume target is kvm? *)
+    ] [
+      e "name" [] [PCData source.s_name];
+      e "memory" ["unit", "KiB"] [PCData (Int64.to_string memory_k)];
+      e "currentMemory" ["unit", "KiB"] [PCData (Int64.to_string memory_k)];
+      e "vcpu" [] [PCData (string_of_int source.s_vcpu)];
+      e "os" [] [
+        e "type" ["arch", source.s_arch] [PCData "hvm"];
+      ];
+      e "features" [] (List.map (fun s -> PCData s) features);
+
+      e "on_poweroff" [] [PCData "destroy"];
+      e "on_reboot" [] [PCData "restart"];
+      e "on_crash" [] [PCData "restart"];
+
+      e "devices" [] devices;
+    ] (* /doc *) in
+
+  doc
diff --git a/v2v/target_libvirt.mli b/v2v/target_libvirt.mli
new file mode 100644
index 0000000..de76a1f
--- /dev/null
+++ b/v2v/target_libvirt.mli
@@ -0,0 +1,25 @@
+(* 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.
+ *)
+
+(*
+val initialize : string -> Types.source -> Types.overlay list -> Types.overlay list
+
+val create_metadata : string -> Types.source -> Types.overlay list -> Types.guestcaps -> unit
+*)
+
+val create_libvirt_xml : Types.source -> Types.overlay list -> Types.guestcaps -> DOM.doc
diff --git a/v2v/target_local.ml b/v2v/target_local.ml
index 68a1bfd..63045e5 100644
--- a/v2v/target_local.ml
+++ b/v2v/target_local.ml
@@ -32,62 +32,11 @@ let initialize dir source overlays =
   ) overlays
 
 let create_metadata dir source overlays guestcaps =
+  let doc = Target_libvirt.create_libvirt_xml source overlays guestcaps in
+
   let name = source.s_name in
   let file = dir // name ^ ".xml" in
 
   let chan = open_out file in
-  let p fs = fprintf chan fs in
-
-  p "<domain type='%s'>\n" "kvm"; (* Always assume target is kvm? *)
-  p "  <name>%s</name>\n" name;
-  let memory_k = source.s_memory /^ 1024L in
-  p "  <memory unit='KiB'>%Ld</memory>\n" memory_k;
-  p "  <currentMemory unit='KiB'>%Ld</currentMemory>\n" memory_k;
-  p "  <vcpu>%d</vcpu>\n" source.s_vcpu;
-  p "  <os>\n";
-  p "    <type arch='%s'>hvm</type>\n" source.s_arch;
-  p "  </os>\n";
-  p "  <features>\n";
-  List.iter (
-    fun feature ->
-      if feature = "acpi" && not guestcaps.gcaps_acpi then
-        (* drop acpi if the guest doesn't support it *) ()
-      else
-        (* pass through all other features *)
-        p "    <%s/>\n" feature
-  ) source.s_features;
-  p "  </features>\n";
-
-  p "  <on_poweroff>destroy</on_poweroff>\n";
-  p "  <on_reboot>restart</on_reboot>\n";
-  p "  <on_crash>restart</on_crash>\n";
-  p "  <devices>\n";
-
-  let block_prefix =
-    if guestcaps.gcaps_block_bus = "virtio" then "vd" else "hd" in
-  iteri (
-    fun i ov ->
-      p "    <disk type='file' device='disk'>\n";
-      p "      <driver name='qemu' type='%s' cache='none'/>\n"
-        ov.ov_target_format;
-      p "      <source file='%s'/>\n" (xml_quote_attr ov.ov_target_file);
-      p "      <target dev='%s%s' bus='%s'/>\n"
-        block_prefix (drive_name i) guestcaps.gcaps_block_bus;
-      p "    </disk>\n";
-  ) overlays;
-
-  p "    <input type='tablet' bus='usb'/>\n";
-  p "    <input type='mouse' bus='ps2'/>\n";
-  p "    <console type='pty'/>\n";
-
-  (* XXX Missing here from old virt-v2v:
-     <video/>
-     <graphics/>
-     cdroms and floppies
-     network interfaces
-     See: lib/Sys/VirtConvert/Connection/LibVirtTarget.pm
-  *)
-
-  p "</domain>\n";
-
+  DOM.doc_to_chan chan doc;
   close_out chan

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list