use Pod::Usage;
use Getopt::Long;
use File::Temp qw(tempdir);
-use POSIX qw(_exit strftime);
+use POSIX qw(_exit setgid setuid strftime);
use XML::Writer;
use Sys::Guestfs;
sudo ./import-to-ovirt.pl disk.img /esd_mountpoint
-=head1 IMPORTANT NOTE
+=head1 IMPORTANT NOTES
+
+In the latest oVirt/RHEV/RHV there is a GUI option to import disks.
+You B<do not need to use this script> if you are using a sufficiently
+new version of oVirt.
This tool should B<only> be used if the guest can already run on KVM.
"man" => \$man,
"memory=i" => \$memory_mb,
"name=s" => \$name,
+ "vcpus=i" => \$vcpus,
"vmtype=s" => \$vmtype,
)
or die "$0: unknown command line option\n";
$name =~ s{\.[^.]+}{};
}
+if ($vmtype =~ /^Desktop$/i) {
+ $vmtype = 0;
+} elsif ($vmtype =~ /^Server$/i) {
+ $vmtype = 1;
+} else {
+ die "$0: --vmtype parameter must be 'Desktop' or 'Server'\n"
+}
+
+# Does qemu-img generally work OK?
+system ("qemu-img create -f qcow2 .test.qcow2 10M >/dev/null") == 0
+ or die "qemu-img command not installed or not working\n";
+
+# Does this version of qemu-img support compat=0.10? RHEL 6
+# did NOT support it.
+my $qemu_img_supports_compat = 0;
+system ("qemu-img create -f qcow2 -o compat=0.10 .test.qcow2 10M >/dev/null") == 0
+ and $qemu_img_supports_compat = 1;
+unlink ".test.qcow2";
+
# Open the guest in libguestfs so we can inspect it.
my $g = Sys::Guestfs->new ();
+eval { $g->set_program ("virt-import-to-ovirt"); };
$g->add_drive_opts ($_, readonly => 1) foreach (@disks);
$g->launch ();
my @roots = $g->inspect_os ();
if ($pid == 0) {
# Child process.
if ($EUID == 0) {
- $GID = 36;
- $UID = 36;
+ setgid (36);
+ setuid (36);
}
eval { &$fn () };
if ($@) {
- print STDERR $@, "\n";
+ print STDERR "$@\n";
_exit (1);
}
_exit (0);
chmod (0666, $output_file) or die "chmod: $output_file: $!";
});
print "Copying $input_file ...\n";
-# system ("qemu-img", "convert", "-p",
-# $input_file,
-# "-O", "qcow2",
-# "-o", "compat=0.10", # for RHEL 6-based ovirt nodes
-# $output_file) == 0
-# or die "qemu-img: $input_file: failed (status $?)";
-# push @real_sizes, -s $output_file;
- push @real_sizes, 0;
+ my @compat_option = ();
+ if ($qemu_img_supports_compat) {
+ @compat_option = ("-o", "compat=0.10") # for RHEL 6-based ovirt nodes
+ }
+ system ("qemu-img", "convert", "-p",
+ $input_file,
+ "-O", "qcow2",
+ @compat_option,
+ $output_file) == 0
+ or die "qemu-img: $input_file: failed (status $?)";
+ push @real_sizes, -s $output_file;
my $size_in_sectors = $virtual_sizes[$i] / 512;
$w->startTag ("Disk",
[$ovf_ns, "diskId" ] => $vol_uuids[$i],
- [$ovf_ns, "actual_size"] => $real_size_in_gb,
- [$ovf_ns, "size"] => $virtual_size_in_gb,
+ [$ovf_ns, "actual_size"] =>
+ sprintf ("%.0f", $real_size_in_gb),
+ [$ovf_ns, "size"] =>
+ sprintf ("%.0f", $virtual_size_in_gb),
[$ovf_ns, "fileRef"] => $href,
[$ovf_ns, "parentRef"] => "",
[$ovf_ns, "vm_snapshot_id"] => uuidgen (),
});
# Finished.
-#$delete_output_on_exit = 0;
+$delete_output_on_exit = 0;
print "\n";
print "OVF written to $ovf_file\n";
print "\n";
=head1 SEE ALSO
+L<https://bugzilla.redhat.com/show_bug.cgi?id=998279>,
+L<https://bugzilla.redhat.com/show_bug.cgi?id=1049604>,
L<virt-v2v(1)>,
L<engine-image-uploader(8)>.