1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
(* helper-v2v-convert
* Copyright (C) 2009-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.
*)
open Printf
open Unix
open Std_utils
open Tools_utils
open Unix_utils
open Common_gettext.Gettext
open Getopt.OptionName
open Types
open Utils
module G = Guestfs
type options = {
block_driver : guestcaps_block_type;
keep_serial_console : bool;
ks : key_store;
network_map : Networks.t;
root_choice : root_choice;
static_ips : static_ip list;
customize_ops : Customize_cmdline.ops;
}
(* Mountpoint stats, used for free space estimation. *)
type mpstat = {
mp_dev : string; (* Filesystem device (eg. /dev/sda1) *)
mp_path : string; (* Guest mountpoint (eg. /boot) *)
mp_statvfs : Guestfs.statvfs; (* Free space stats. *)
mp_vfs : string; (* VFS type (eg. "ext4") *)
}
let rec convert input_disks options source =
let target_nics = List.map (Networks.map options.network_map) source.s_nics in
message (f_"Opening the source");
let g = open_guestfs ~identifier:"v2v" () in
g#set_memsize (g#get_memsize () * 2);
(* Setting the number of vCPUs allows parallel mkinitrd, but make
* sure this is not too large because each vCPU consumes guest RAM.
*)
g#set_smp (min 8 (Sysconf.nr_processors_online ()));
(* The network is used by the unconfigure_vmware () function, and the "--key
* ID:clevis" command line options (if any). *)
g#set_network true;
List.iter (
fun uri ->
(* NB: Old virt-v2v used copyonread here, when it was using a
* qcow2 file as overlay. We MUST NOT use copyonread! It
* doesn't do anything if there is no backing chain, but worse
* than that I observed a huge (33x!) slow down.
*)
let socket, export =
match uri with NBD_URI.Unix (socket, export) ->
sprintf "unix:%s" socket, Option.value export ~default:"" in
g#add_drive_opts export
~format:"raw" ~protocol:"nbd" ~server:[| socket |]
~cachemode:"unsafe" ~discard:"besteffort"
) input_disks;
g#launch ();
(* Decrypt the disks. *)
inspect_decrypt g options.ks;
(* Check (fsck) the filesystems before conversion. *)
message (f_"Checking filesystem integrity before conversion");
do_fsck ~before:true g;
(* Detect firmware. *)
message (f_"Detecting if this guest uses BIOS or UEFI to boot");
let i_firmware = Firmware.detect_firmware g in
(* Inspect the source, choose root and mount up the filesystems. *)
message (f_"Inspecting the source");
let root = Choose_root.choose_root options.root_choice g in
let inspect = Mount_filesystems.mount_filesystems g root in
let mpstats = get_mpstats g in
check_guest_free_space inspect mpstats;
(* Conversion. *)
let guestcaps =
do_convert g source inspect i_firmware
options.block_driver options.keep_serial_console options.static_ips in
(* Run virt-customize options. *)
Customize_run.run g inspect.i_root options.customize_ops;
g#umount_all ();
(* Doing fstrim on all the filesystems reduces the transfer size
* because unused blocks are marked in the overlay and thus do
* not have to be copied.
*)
message (f_"Mapping filesystem data to avoid copying unused and blank areas");
do_fstrim g inspect;
(* Check (fsck) the filesystems after conversion. *)
g#umount_all ();
message (f_"Checking filesystem integrity after conversion");
do_fsck g;
message (f_"Closing the overlay");
g#umount_all ();
g#shutdown ();
g#close ();
(* Prepare the target metadata. *)
message (f_"Assigning disks to buses");
let target_buses =
Target_bus_assignment.target_bus_assignment
source.s_disks source.s_removables guestcaps in
debug "%s" (string_of_target_buses target_buses);
let target_firmware =
get_target_firmware i_firmware guestcaps source output in
(* Create target metadata file. *)
let target_meta = { guestcaps; target_buses; target_firmware; target_nics } in
(* This is a good place to dump everything we know about the guest. *)
if verbose () then debug_info source inspect target_meta mpstats;
(* Return inspection data and target metadata. *)
inspect, target_meta
(* Collect statvfs information from the guest mountpoints. *)
and get_mpstats g =
let mpstats = List.map (
fun (dev, path) ->
let statvfs = g#statvfs path in
let vfs = g#vfs_type dev in
{ mp_dev = dev; mp_path = path; mp_statvfs = statvfs; mp_vfs = vfs }
) (g#mountpoints ()) in
mpstats
(* Conversion can fail if there is no space on the guest filesystems
* (RHBZ#1139543). To avoid this situation, check there is some
* headroom. Mainly we care about the root filesystem.
*
* Also make sure filesystems have available inodes. (RHBZ#1764569)
*)
and check_guest_free_space inspect mpstats =
message (f_"Checking for sufficient free disk space in the guest");
(* Check whether /boot has its own mount point. *)
let has_boot = List.exists (fun { mp_path } -> mp_path = "/boot") mpstats in
let is_windows = inspect.i_distro = "windows" in
let needed_megabytes_for_mp = function
(* We usually regenerate the initramfs, which has a
* typical size of 20-30MB. Hence:
*)
| "/boot" | "/" when not has_boot && not is_windows -> 50
(* Both Linux and Windows require installation of files,
* device drivers and guest agents.
* https://bugzilla.redhat.com/1949147
* https://bugzilla.redhat.com/1764569#c16
*)
| "/" -> 100
(* For everything else, just make sure there is some free space. *)
| _ -> 10
in
(* Reasonable headroom for conversion operations. *)
let needed_inodes = 100L in
List.iter (
fun { mp_path; mp_statvfs = { G.bfree; bsize; files; ffree } } ->
(* bfree = free blocks for root user *)
let free_bytes = bfree *^ bsize in
let needed_megabytes = needed_megabytes_for_mp mp_path in
let needed_bytes = Int64.of_int needed_megabytes *^ 1024L *^ 1024L in
if free_bytes < needed_bytes then (
let mb i = Int64.to_float i /. 1024. /. 1024. in
error (f_"not enough free space for conversion on filesystem ā%sā. \
%.1f MB free < %d MB needed")
mp_path (mb free_bytes) needed_megabytes
);
(* Not all the filesystems have inode counts. *)
if files > 0L && ffree < needed_inodes then
error (f_"not enough available inodes for conversion on \
filesystem ā%sā. %Ld inodes available < %Ld inodes needed")
mp_path ffree needed_inodes
) mpstats
(* Perform the fstrim. *)
and do_fstrim g inspect =
(* Get all filesystems. *)
let fses = g#list_filesystems () in
let fses = List.filter_map (
function (_, ("unknown"|"swap")) -> None | (dev, _) -> Some dev
) fses in
(* Trim the filesystems. *)
List.iter (
fun dev ->
g#umount_all ();
let mounted =
try g#mount_options "discard" dev "/"; true
with G.Error _ -> false in
if mounted then (
debug "info: trimming %s" dev;
try g#fstrim "/"
with G.Error msg ->
warning (f_"fstrim on guest filesystem %s failed. Usually you \
can ignore this message. To find out more read \
\"Trimming\" in virt-v2v(1).\n\n\
Original message: %s") dev msg
)
) fses
(* Perform fsck before and after conversion.
*
* If fsck returns an error then the conversion will fail. We do not
* attempt to do any repairs.
*)
and do_fsck ?(before=false) g =
let fses = g#list_filesystems () in
List.iter (function
| dev, _ when String.starts_with "btrfsvol:" dev ->
(* Ignore btrfs volumes, since we should see the btrfs device
* somewhere else in the list and checking that is sufficient.
*)
()
| dev, "btrfs" ->
(* btrfs-check would be the obvious thing, but the general
* opinion seems to be that it's broken, and you should use
* btrfs scrub instead.
*)
Fun.protect ~finally:g#umount_all (
fun () ->
g#mount_ro dev "/";
g#btrfs_scrub_full "/" ~readonly:true;
);
| dev, "ext4" ->
if before then (
(* Replay and hence repair a dirty log (RHEL-97600) *)
Fun.protect ~finally:g#umount_all (fun () -> g#mount_ro dev "/");
);
g#e2fsck ~forceno:true dev
| dev, "xfs" ->
if before then (
(* xfs_repair cannot replay the dirty log, only the kernel can,
* so we must first mount then unmount the filesystem, and then
* we can run xfs_repair. Unlike what is documented, xfs_repair
* doesn't return 2 in this case. Mount r/o is fine as that
* will still replay the log (RHEL-95365)
*)
Fun.protect ~finally:g#umount_all (fun () -> g#mount_ro dev "/");
);
(* Must specify the -n flag because we are not attempting to
* fix the filesystem here.
*)
let nomodify = true
(* xfs_repair runs out of memory in the low memory environment
* of the appliance unless we limit the amount of memory it will
* use here.
*)
and noprefetch = true
and maxmem = Int64.of_int (g#get_memsize () / 2) in
if g#xfs_repair ~maxmem ~noprefetch ~nomodify dev <> 0 then
error (f_"detected errors on the XFS filesystem on %s") dev
| _, _ ->
(* Ignore other filesystem types. *)
()
) fses
(* Conversion. *)
and do_convert g source inspect i_firmware
block_driver keep_serial_console interfaces =
(* Create the "Converting..." message. Complicated! *)
let () =
let what_guest =
match inspect.i_product_name, inspect.i_osinfo with
| "unknown", "unknown" -> s_"the guest"
| "unknown", osinfo -> sprintf (f_"%s guest") osinfo
| prod, "unknown" -> prod
| prod, osinfo -> sprintf "%s (%s)" prod osinfo in
message (f_"Converting %s to run on KVM") what_guest in
let convert, conversion_name =
match inspect with
| { i_type = "linux";
i_distro = ("fedora"
| "rhel" | "centos" | "circle" | "scientificlinux"
| "redhat-based" | "oraclelinux" | "rocky"
| "sles" | "suse-based" | "opensuse"
| "altlinux"
| "debian" | "ubuntu" | "linuxmint" | "kalilinux") } ->
Convert_linux.convert, "linux"
| { i_type = "windows" } ->
Convert_windows.convert, "windows"
| _ ->
error (f_"virt-v2v is unable to convert this guest type (%s/%s)")
inspect.i_type inspect.i_distro in
debug "picked conversion module %s" conversion_name;
let guestcaps =
convert g source inspect i_firmware
block_driver keep_serial_console interfaces in
debug "%s" (string_of_guestcaps guestcaps);
(* Did we manage to install virtio drivers? *)
if not (quiet ()) then (
match guestcaps.gcaps_block_bus with
| Virtio_blk | Virtio_SCSI ->
info (f_"This guest has virtio drivers installed.")
| IDE ->
info (f_"This guest does not have virtio drivers installed.")
);
guestcaps
(* Does the guest require UEFI on the target? *)
and get_target_firmware i_firmware guestcaps source output =
message (f_"Checking if the guest needs BIOS or UEFI to boot");
let target_firmware =
match source.s_firmware with
| BIOS -> TargetBIOS
| UEFI -> TargetUEFI
| UnknownFirmware ->
match i_firmware with
| I_BIOS -> TargetBIOS
| I_UEFI _ -> TargetUEFI
in
(match target_firmware with
| TargetBIOS -> ()
| TargetUEFI -> info (f_"This guest requires UEFI on the target to boot."));
target_firmware
(* After conversion we dump as much information about the guest
* as we can in one place. Note this is only called when verbose
* is enabled.
*)
and debug_info source inspect
{ guestcaps; target_buses; target_firmware; target_nics }
mpstats =
eprintf "info:\n";
eprintf "%s\n" (string_of_source source);
eprintf "%s\n" (string_of_inspect inspect);
eprintf "%s\n" (string_of_guestcaps guestcaps);
eprintf "%s\n" (string_of_target_buses target_buses);
eprintf "target firmware: %s\n" (string_of_target_firmware target_firmware);
eprintf "target NICs:\n";
List.iter (fun nic -> eprintf "%s\n" (string_of_source_nic nic))
target_nics;
eprintf "mountpoint stats:\n";
eprintf "%20s %-16s %-16s %-16s %s\n" "" "Size" "Used" "Available" "Use%";
List.iter debug_mpstat mpstats;
flush Stdlib.stderr
(* The calculations here are similar to virt-df df/output.c *)
and debug_mpstat { mp_dev = dev; mp_path = path;
mp_statvfs = { G.bsize; G.blocks; G.bfree; G.bavail };
mp_vfs = vfs } =
let label = sprintf "%s %s (%s):" dev path vfs
and size = blocks *^ bsize
and used = (blocks -^ bfree) *^ bsize
and avail = bavail *^ bsize
and percent =
if blocks <> 0_L then
100. -. 100. *. (Int64.to_float bfree /. Int64.to_float blocks)
else
0. in
if String.length label > 20 then
eprintf "%s\n%20s " label ""
else
eprintf "%-20s " label;
eprintf "%-16Ld %-16Ld %-16Ld\n" size used avail;
eprintf "%20s %-16s %-16s %-16s %.1f%%\n"
"" (human_size size) (human_size used) (human_size avail) percent
|