Don't fail if $g->set_program method is not supported.
[import-to-ovirt.git] / import-to-ovirt.pl
index cb6c1db..abb557b 100755 (executable)
@@ -23,7 +23,7 @@ use English;
 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;
@@ -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";
@@ -216,8 +221,28 @@ if (!defined $name) {
     $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 ();
@@ -363,12 +388,12 @@ sub run_as_vdsm
     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);
@@ -433,14 +458,17 @@ for ($i = 0; $i < @disks; ++$i) {
         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;
 
@@ -544,8 +572,10 @@ for ($i = 0; $i < @disks; ++$i)
 
     $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 (),
@@ -767,7 +797,7 @@ run_as_vdsm (sub {
 });
 
 # Finished.
-#$delete_output_on_exit = 0;
+$delete_output_on_exit = 0;
 print "\n";
 print "OVF written to $ovf_file\n";
 print "\n";
@@ -815,6 +845,8 @@ from low-level commands.
 
 =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)>.