234 lines
8.1 KiB
Diff
234 lines
8.1 KiB
Diff
From 71d68738920971f0216cfe77f6873a518369045f Mon Sep 17 00:00:00 2001
|
||
From: "Richard W.M. Jones" <rjones@redhat.com>
|
||
Date: Tue, 8 Jul 2025 10:40:48 +0100
|
||
Subject: [PATCH] -o kubevirt: Add -oo disk to allow disk names to be
|
||
overridden
|
||
|
||
Fixes: https://issues.redhat.com/browse/RHEL-101599
|
||
---
|
||
docs/virt-v2v.pod | 15 +++++++--
|
||
output/output_kubevirt.ml | 58 ++++++++++++++++++++++++++------
|
||
tests/Makefile.am | 2 ++
|
||
tests/test-o-kubevirt-oo-disk.sh | 52 ++++++++++++++++++++++++++++
|
||
4 files changed, 115 insertions(+), 12 deletions(-)
|
||
create mode 100755 tests/test-o-kubevirt-oo-disk.sh
|
||
|
||
diff --git a/docs/virt-v2v.pod b/docs/virt-v2v.pod
|
||
index 3eefe404..5020d179 100644
|
||
--- a/docs/virt-v2v.pod
|
||
+++ b/docs/virt-v2v.pod
|
||
@@ -466,8 +466,9 @@ Set the output method to I<kubevirt>. B<Note the way this mode works
|
||
is experimental and will change in future.>
|
||
|
||
In this mode, the converted guest is written to a local directory
|
||
-specified by I<-os /dir> (the directory must exist). The converted
|
||
-guest’s disks are written to:
|
||
+specified by I<-os /dir> (the directory must exist).
|
||
+
|
||
+By default the converted guest’s disks are written to:
|
||
|
||
/dir/name-sda
|
||
/dir/name-sdb
|
||
@@ -479,6 +480,8 @@ and guest metadata is created in the associated YAML file:
|
||
|
||
where C<name> is the guest name.
|
||
|
||
+You can override the disk paths by using I<-oo disk=...> option(s).
|
||
+
|
||
=item B<-o> B<libvirt>
|
||
|
||
Set the output method to I<libvirt>. This is the default.
|
||
@@ -600,6 +603,14 @@ For outputs which support qcow2 format (I<-of qcow2>), this writes a
|
||
compressed qcow2 file. It is the equivalent to the I<-c> option of
|
||
L<qemu-img(1)>.
|
||
|
||
+=item B<-oo disk=>DISK
|
||
+
|
||
+For I<-o kubevirt> this overrides the path to each output disk,
|
||
+instead of using the default which is C<DISK-sda> etc in the output
|
||
+storage (I<-os>) path. If you use this option at all, you must repeat
|
||
+it once for each guest disk. If you don't use it, then the default
|
||
+method of putting disks in the output storage path is used.
|
||
+
|
||
=item B<-oo guest-id=>C<ID>
|
||
|
||
For I<-o openstack> (L<virt-v2v-output-openstack(1)>) only, set a guest ID
|
||
diff --git a/output/output_kubevirt.ml b/output/output_kubevirt.ml
|
||
index 76ecd39d..eae9c049 100644
|
||
--- a/output/output_kubevirt.ml
|
||
+++ b/output/output_kubevirt.ml
|
||
@@ -35,7 +35,8 @@ let rfc1123_re =
|
||
"^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$"
|
||
|
||
module Kubevirt = struct
|
||
- type poptions = bool * output_allocation * string * string * string
|
||
+ type poptions =
|
||
+ bool * string list option * output_allocation * string * string * string
|
||
|
||
type t = unit
|
||
|
||
@@ -49,15 +50,18 @@ module Kubevirt = struct
|
||
printf (f_"Output options that can be used with -o kubevirt:
|
||
|
||
-oo compressed Compress the output file (used only with -of qcow2)
|
||
+ -oo disk=disk1 Specify filename of output disk (if used, must be
|
||
+ given once for each disk, else -os path is used)
|
||
")
|
||
|
||
-
|
||
let parse_options options source =
|
||
let compressed = ref false in
|
||
+ let disks = ref [] in
|
||
List.iter (
|
||
function
|
||
| "compressed", "" -> compressed := true
|
||
| "compressed", v -> compressed := bool_of_string v
|
||
+ | "disk", v -> List.push_back disks v
|
||
| k, _ ->
|
||
error (f_"-o kubevirt: unknown output option ‘-oo %s’") k
|
||
) options.output_options;
|
||
@@ -84,23 +88,57 @@ module Kubevirt = struct
|
||
end with an alphanumeric character. Rerun virt-v2v with \
|
||
the '-on name' option to rename it.");
|
||
|
||
- !compressed, options.output_alloc, options.output_format,
|
||
+ let disks = match !disks with [] -> None | disks -> Some disks in
|
||
+
|
||
+ !compressed, disks,
|
||
+ options.output_alloc, options.output_format,
|
||
output_name, output_storage
|
||
|
||
let setup dir options source input_disks =
|
||
- let compressed, output_alloc, output_format, output_name, output_storage =
|
||
- options in
|
||
+ let compressed, disks,
|
||
+ output_alloc, output_format, output_name, output_storage = options in
|
||
|
||
let uris =
|
||
- create_local_output_disks dir ~compressed output_alloc output_format
|
||
- output_name output_storage input_disks in
|
||
+ match disks with
|
||
+ | None ->
|
||
+ create_local_output_disks dir ~compressed
|
||
+ output_alloc output_format output_name output_storage input_disks
|
||
+ | Some disks ->
|
||
+ (* -oo disk specified, so create the disks by hand. *)
|
||
+ let nr_input_disks = List.length input_disks
|
||
+ and nr_output_disks = List.length disks in
|
||
+ if nr_input_disks <> nr_output_disks then
|
||
+ error (f_"incorrect number of '-oo disk' parameters. This guest \
|
||
+ has %d disks, but the parameter was used %d times.")
|
||
+ nr_input_disks nr_output_disks;
|
||
+
|
||
+ let input_sizes = get_disk_sizes input_disks in
|
||
+ List.mapi (
|
||
+ fun i (disk, size) ->
|
||
+ let socket = sprintf "%s/out%d" dir i in
|
||
+ On_exit.unlink socket;
|
||
+
|
||
+ output_to_local_file ~compressed
|
||
+ output_alloc output_format disk size socket;
|
||
+
|
||
+ NBD_URI.Unix (socket, None)
|
||
+ ) (List.combine disks input_sizes)
|
||
+ in
|
||
+
|
||
(), uris
|
||
|
||
let finalize dir options () output_disks source inspect target_meta =
|
||
- let _, output_alloc, output_format, output_name, output_storage = options in
|
||
+ let _, disks,
|
||
+ output_alloc, output_format, output_name, output_storage = options in
|
||
|
||
- let doc = create_kubevirt_yaml source inspect target_meta
|
||
- (disk_path output_storage output_name)
|
||
+ (* This function will return the disk path for the i'th disk. *)
|
||
+ let disk_path =
|
||
+ match disks with
|
||
+ | None -> Output.disk_path output_storage output_name
|
||
+ | Some disks -> List.nth disks
|
||
+ in
|
||
+
|
||
+ let doc = create_kubevirt_yaml source inspect target_meta disk_path
|
||
output_format output_name in
|
||
|
||
let file = output_storage // output_name ^ ".yaml" in
|
||
diff --git a/tests/Makefile.am b/tests/Makefile.am
|
||
index 62237092..3a8bab3c 100644
|
||
--- a/tests/Makefile.am
|
||
+++ b/tests/Makefile.am
|
||
@@ -95,6 +95,7 @@ TESTS = \
|
||
test-networks-and-bridges.sh \
|
||
test-o-glance.sh \
|
||
test-o-kubevirt-fedora.sh \
|
||
+ test-o-kubevirt-oo-disk.sh \
|
||
test-o-kubevirt-windows.sh \
|
||
test-o-libvirt.sh \
|
||
test-o-local-qcow2-compressed.sh \
|
||
@@ -288,6 +289,7 @@ EXTRA_DIST += \
|
||
test-o-glance.sh \
|
||
test-o-kubevirt-fedora.sh \
|
||
test-o-kubevirt-fedora.yaml.expected \
|
||
+ test-o-kubevirt-oo-disk.sh \
|
||
test-o-kubevirt-windows.sh \
|
||
test-o-kubevirt-windows.yaml.expected \
|
||
test-o-libvirt.sh \
|
||
diff --git a/tests/test-o-kubevirt-oo-disk.sh b/tests/test-o-kubevirt-oo-disk.sh
|
||
new file mode 100755
|
||
index 00000000..e1e6fd61
|
||
--- /dev/null
|
||
+++ b/tests/test-o-kubevirt-oo-disk.sh
|
||
@@ -0,0 +1,52 @@
|
||
+#!/bin/bash -
|
||
+# libguestfs virt-v2v test script
|
||
+# Copyright (C) 2018-2025 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.
|
||
+
|
||
+# Test -o kubevirt + -oo disk option.
|
||
+
|
||
+set -e
|
||
+set -x
|
||
+
|
||
+source ./functions.sh
|
||
+set -e
|
||
+set -x
|
||
+
|
||
+skip_if_skipped
|
||
+requires test -f ../test-data/phony-guests/fedora.img
|
||
+
|
||
+libvirt_uri="test://$abs_top_builddir/test-data/phony-guests/guests.xml"
|
||
+fedora=../test-data/phony-guests/fedora.img
|
||
+
|
||
+d=test-o-kubevirt-oo-disk.d
|
||
+rm -rf $d
|
||
+cleanup_fn rm -r $d
|
||
+mkdir $d
|
||
+
|
||
+# Run virt-v2v -o kubevirt.
|
||
+$VG virt-v2v --debug-gc \
|
||
+ -i libvirt -ic "$libvirt_uri" fedora \
|
||
+ -o kubevirt -oo disk=$d/disk.img -os $d
|
||
+
|
||
+ls -l $d
|
||
+
|
||
+cat $d/fedora.yaml
|
||
+
|
||
+# Check the disk was created.
|
||
+test -f $d/disk.img
|
||
+
|
||
+# Check the default path was _not_ created.
|
||
+! test -f $d/fedora-sda
|