Richard W.M. Jones
2018-Mar-27 12:13 UTC
[Libguestfs] [PATCH FOR DISCUSSION ONLY] v2v: Add -o kubevirt output mode.
XXX No documentation. Only handles one disk. Network cards? Do we need to escape YAML format? What firmware types does kubevirt support. --- v2v/Makefile.am | 2 + v2v/cmdline.ml | 21 ++++++++++ v2v/output_kubevirt.ml | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ v2v/output_kubevirt.mli | 24 +++++++++++ 4 files changed, 150 insertions(+) diff --git a/v2v/Makefile.am b/v2v/Makefile.am index f36731750..2f6953463 100644 --- a/v2v/Makefile.am +++ b/v2v/Makefile.am @@ -66,6 +66,7 @@ SOURCES_MLI = \ modules_list.mli \ name_from_disk.mli \ output_glance.mli \ + output_kubevirt.mli \ output_libvirt.mli \ output_local.mli \ output_null.mli \ @@ -132,6 +133,7 @@ SOURCES_ML = \ output_rhv_upload_precheck_source.ml \ output_rhv_upload.ml \ output_vdsm.ml \ + output_kubevirt.ml \ inspect_source.ml \ target_bus_assignment.ml \ cmdline.ml \ diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml index a8da63f71..a9608b330 100644 --- a/v2v/cmdline.ml +++ b/v2v/cmdline.ml @@ -131,6 +131,7 @@ let parse_cmdline () error (f_"%s option used more than once on the command line") "-o"; match mode with | "glance" -> output_mode := `Glance + | "kubevirt" -> output_mode := `Kubevirt | "libvirt" -> output_mode := `Libvirt | "disk" | "local" -> output_mode := `Local | "null" -> output_mode := `Null @@ -394,6 +395,7 @@ read the man page virt-v2v(1). match output_mode with | `Not_set -> no_options (); `Not_set | `Glance -> no_options (); `Glance + | `Kubevirt -> no_options (); `Kubevirt | `Libvirt -> no_options (); `Libvirt | `Local -> no_options (); `Local | `Null -> no_options (); `Null @@ -521,6 +523,25 @@ read the man page virt-v2v(1). Output_glance.output_glance (), output_format, output_alloc + | `Kubevirt -> + if output_conn <> None then + error_option_cannot_be_used_in_output_mode "glance" "-oc"; + if output_password <> None then + error_option_cannot_be_used_in_output_mode "glance" "-op"; + let os + match output_storage with + | None -> + error (f_"-o local: output directory was not specified, use '-os /dir'") + | Some d when not (is_directory d) -> + error (f_"-os %s: output directory does not exist or is not a directory") d + | Some d -> d in + if qemu_boot then + error_option_cannot_be_used_in_output_mode "glance" "--qemu-boot"; + if not do_copy then + error_option_cannot_be_used_in_output_mode "libvirt" "--no-copy"; + Output_kubevirt.output_kubevirt os, + output_format, output_alloc + | `Not_set | `Libvirt -> if output_password <> None then diff --git a/v2v/output_kubevirt.ml b/v2v/output_kubevirt.ml new file mode 100644 index 000000000..a6747fd06 --- /dev/null +++ b/v2v/output_kubevirt.ml @@ -0,0 +1,103 @@ +(* virt-v2v + * Copyright (C) 2018 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 Std_utils +open Tools_utils +open Unix_utils +open Common_gettext.Gettext + +open Types +open Utils + +class output_kubevirt dir = object + inherit output + + method as_options + sprintf "-o kubevirt -os %s" dir + + method supported_firmware = [ TargetBIOS; TargetUEFI ] (* XXX GUESS *) + + val mutable name = "" + + method prepare_targets source targets + (* Only one disk is supported at present. XXX *) + if List.length targets <> 1 then + error (f_"-o kubevirt: this output mode only supports a single disk"); + + (* Don't use the source name directly, it must be munged + * for DNS (RFC 1123). + *) + name <- PCRE.replace ~global:true (PCRE.compile "[_.]+") "-" + source.s_name; + + List.mapi ( + fun i t -> + (* This is the PVC name which is generated by the + * script that calls virt-v2v. + *) + let target_file + TargetFile (dir // sprintf "%s-disk-%d" name i) in + { t with target_file } + ) targets + + method create_metadata source targets target_buses + guestcaps inspect target_firmware + (* Create the YAML-format metadata. *) + (* XXX ESCAPING? *) + with_open_out (dir // name ^ ".yaml") ( + fun chan -> + let fpf fs = fprintf chan fs in + fpf "apiVersion: kubevirt.io/v1alpha1\n"; + fpf "kind: OfflineVirtualMachine\n"; + fpf "metadata:\n"; + fpf " name: %s\n" name; + fpf "domain:\n"; + fpf " cpu:\n"; + fpf " cores: %d\n" source.s_vcpu; + fpf " resources:\n"; + fpf " requests:\n"; + fpf " memory: %Ld%s\n" (source.s_memory /^ 1024_L) "KiB"; + fpf " devices:\n"; + + (* virt-v2v (and indeed hardware) doesn't really work this way, + * in that there are several buses which may have multiple disks, + * and we're throwing all that careful mapping away, but here we + * are ... XXX + *) + let disk_bus + match guestcaps.gcaps_block_bus with + | Virtio_blk -> "virtio" + | Virtio_SCSI -> "scsi" + | IDE -> "ide" in + + fpf " disks:\n"; + fpf " - name: disk-1\n"; + fpf " disk:\n"; + fpf " bus: %s\n" disk_bus; + fpf " volumeName: volume-1\n"; + fpf " volumes:\n"; + fpf " - name: volume-1\n"; + fpf " persistentVolumeClaim:\n"; + fpf " name: %s-disk-1\n" name; + ) +end + +let output_kubevirt = new output_kubevirt +let () = Modules_list.register_output_module "kubevirt" diff --git a/v2v/output_kubevirt.mli b/v2v/output_kubevirt.mli new file mode 100644 index 000000000..3ff0041c7 --- /dev/null +++ b/v2v/output_kubevirt.mli @@ -0,0 +1,24 @@ +(* virt-v2v + * Copyright (C) 2018 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. + *) + +(** [-o kubevirt] target. *) + +val output_kubevirt : string -> Types.output +(** [output_kubevirt dir] creates and returns a new + {!Types.output} object specialized for writing output to + KubeVirt. *) -- 2.13.2
Richard W.M. Jones
2018-Mar-27 12:29 UTC
Re: [Libguestfs] [PATCH FOR DISCUSSION ONLY] v2v: Add -o kubevirt output mode.
On Tue, Mar 27, 2018 at 01:13:03PM +0100, Richard W.M. Jones wrote:> + with_open_out (dir // name ^ ".yaml") ( > + fun chan -> > + let fpf fs = fprintf chan fs in > + fpf "apiVersion: kubevirt.io/v1alpha1\n"; > + fpf "kind: OfflineVirtualMachine\n"; > + fpf "metadata:\n"; > + fpf " name: %s\n" name; > + fpf "domain:\n"; > + fpf " cpu:\n"; > + fpf " cores: %d\n" source.s_vcpu; > + fpf " resources:\n"; > + fpf " requests:\n"; > + fpf " memory: %Ld%s\n" (source.s_memory /^ 1024_L) "KiB"; > + fpf " devices:\n";Of course in the process of doing that I forgot all about the osinfo field, but it could be added somewhere under here as a custom field. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Apparently Analagous Threads
- [PATCH FOR DISCUSSION ONLY v2] v2v: Add -o kubevirt output mode.
- Re: [PATCH v7 4/6] v2v: Add general mechanism for input and output options (-io/-oo).
- [PATCH] v2v: Refactor some command line error messages.
- [PATCH v7 4/6] v2v: Add general mechanism for input and output options (-io/-oo).
- Re: [PATCH v7 4/6] v2v: Add general mechanism for input and output options (-io/-oo).