Don't fail if $g->set_program method is not supported.
[import-to-ovirt.git] / import-to-ovirt.pl
index 5eca749..abb557b 100755 (executable)
@@ -38,7 +38,11 @@ import-to-ovirt.pl - Import virtual machine disk image to RHEV or oVirt
 
  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.
 
@@ -195,6 +199,7 @@ GetOptions ("help|?" => \$help,
             "man" => \$man,
             "memory=i" => \$memory_mb,
             "name=s" => \$name,
+            "vcpus=i" => \$vcpus,
             "vmtype=s" => \$vmtype,
     )
     or die "$0: unknown command line option\n";
@@ -224,9 +229,20 @@ if ($vmtype =~ /^Desktop$/i) {
     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 ();
-$g->set_program ("virt-import-to-ovirt");
+eval { $g->set_program ("virt-import-to-ovirt"); };
 $g->add_drive_opts ($_, readonly => 1) foreach (@disks);
 $g->launch ();
 my @roots = $g->inspect_os ();
@@ -442,10 +458,14 @@ for ($i = 0; $i < @disks; ++$i) {
         chmod (0666, $output_file) or die "chmod: $output_file: $!";
     });
     print "Copying $input_file ...\n";
+    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",
-            "-o", "compat=0.10", # for RHEL 6-based ovirt nodes
+            @compat_option,
             $output_file) == 0
                 or die "qemu-img: $input_file: failed (status $?)";
     push @real_sizes, -s $output_file;