Fix detection of guest minor version.
[import-to-ovirt.git] / import-to-ovirt.pl
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015 Richard W.M. Jones <rjones@redhat.com>
3 # Copyright (C) 2015 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 use warnings;
20 use strict;
21 use English;
22
23 use Pod::Usage;
24 use Getopt::Long;
25 use File::Temp qw(tempdir);
26 use POSIX qw(_exit setgid setuid strftime);
27 use XML::Writer;
28
29 use Sys::Guestfs;
30
31 =head1 NAME
32
33 import-to-ovirt.pl - Import virtual machine disk image to RHEV or oVirt
34
35 =head1 SYNOPSIS
36
37  sudo ./import-to-ovirt.pl disk.img server:/esd
38
39  sudo ./import-to-ovirt.pl disk.img /esd_mountpoint
40
41 =head1 IMPORTANT NOTES
42
43 In the latest oVirt/RHEV/RHV there is a GUI option to import disks.
44 You B<do not need to use this script> if you are using a sufficiently
45 new version of oVirt.
46
47 This tool should B<only> be used if the guest can already run on KVM.
48
49 B<If you need to convert the guest from some foreign hypervisor, like VMware, Xen or Hyper-V, you should use L<virt-v2v(1)> instead.>
50
51 =head1 EXAMPLES
52
53 Import a KVM guest to the Export Storage Domain of your RHEV or oVirt
54 system.  The NFS mount of the Export Storage Domain is C<server:/esd>.
55
56  sudo ./import-to-ovirt.pl disk.img server:/esd
57
58 Import a KVM guest to an already-mounted Export Storage Domain:
59
60  sudo ./import-to-ovirt.pl disk.img /esd_mountpoint
61
62 If the single guest has multiple disks, use:
63
64  sudo ./import-to-ovirt.pl disk1.img [disk2.img [...]] server:/esd
65
66 If you are importing multiple guests then you must import each one
67 separately.  Do not use multiple disk parameters if each disk comes
68 from a different guest.
69
70 =head1 DESCRIPTION
71
72 This is a command line script for importing a KVM virtual machine to
73 RHEV or oVirt.  The script assumes that the guest can already run on
74 KVM, ie. that it was previously running on KVM and has the required
75 drivers.  If the guest comes from a foreign hypervisor like VMware,
76 Xen or Hyper-V, use L<virt-v2v(1)> instead.
77
78 This script only imports the guest into the oVirt "Export Storage
79 Domain" (ESD).  After the import is complete, you must then go to the
80 oVirt user interface, go to the C<Storage> tab, select the right ESD,
81 and use C<VM Import> to take the guest from the ESD to the data
82 domain.  This process is outside the scope of this script, but could
83 be automated using the oVirt API.
84
85 =head2 Basic usage
86
87 Basic usage is just:
88
89  ./import-to-ovirt.pl [list of disks] server:/esd
90
91 If you are unclear about the C<server:/esd> parameter, go to the oVirt
92 Storage tab, select the C<Domain Type> C<Export> and look in the
93 C<General> tab under C<Path>.
94
95 If the ESD is already mounted on your machine (or if you are using a
96 non-NFS ESD), then you can supply a direct path to the mountpoint
97 instead:
98
99  ./import-to-ovirt.pl [list of disks] /esd_mountpoint
100
101 The list of disks should all belong to a single guest (most guests
102 will only have a single disk).  If you want to import multiple guests,
103 you must run the script multiple times.
104
105 Importing from OVA etc is not supported.  Try C<ovirt-image-uploader>
106 (if the OVA was exported from oVirt), or L<virt-v2v(1)> (if the OVA
107 was exported from VMware).
108
109 =head2 Permissions
110
111 You probably need to run this script as root, because it has to create
112 files on the ESD as a special C<vdsm> user (UID:GID C<36:36>).
113
114 It may also be possible to run the script as the vdsm user.  But if
115 you run it as some non-root, non-vdsm user, then oVirt won't be able
116 to read the data from the ESD and will give an error.
117
118 NFS "root squash" should be turned off on the NFS server, since it
119 stops us from creating files as the vdsm user.  Also NFSv4 may not
120 work unless you have set up idmap correctly (good luck!)
121
122 =head2 Network card and disk model
123
124 (See also L</TO DO> below)
125
126 Currently this script doesn't add a network card to the guest.  You
127 will need to add one yourself in the C<VM Import> tab when importing
128 the guest.
129
130 Similarly, the script always adds the disks as virtio-blk devices.  If
131 the guest is expecting IDE, SCSI or virtio-scsi, you will need to
132 change the disk type when importing the guest.
133
134 =head1 OPTIONS
135
136 =over 4
137
138 =cut
139
140 my $help;
141
142 =item B<--help>
143
144 Display brief help and exit.
145
146 =cut
147
148 my $man;
149
150 =item B<--man>
151
152 Display the manual page and exit.
153
154 =cut
155
156 my $memory_mb = 1024;
157
158 =item B<--memory> MB
159
160 Set the memory size I<in megabytes>.  The default is 1024.
161
162 =cut
163
164 my $name;
165
166 =item B<--name> name
167
168 Set the guest name.  If not present, a name is made up based on
169 the filename of the first disk.
170
171 =cut
172
173 my $vcpus = 1;
174
175 =item B<--vcpus> N
176
177 Set the number of virtual CPUs.  The default is 1.
178
179 =cut
180
181 my $vmtype = "Desktop";
182
183 =item B<--vmtype> Desktop
184
185 =item B<--vmtype> Server
186
187 Set the VmType field in the OVF.  It must be C<Desktop> or
188 C<Server>.  The default is C<Desktop>.
189
190 =cut
191
192 =back
193
194 =cut
195
196 $| = 1;
197
198 GetOptions ("help|?" => \$help,
199             "man" => \$man,
200             "memory=i" => \$memory_mb,
201             "name=s" => \$name,
202             "vcpus=i" => \$vcpus,
203             "vmtype=s" => \$vmtype,
204     )
205     or die "$0: unknown command line option\n";
206
207 pod2usage (1) if $help;
208 pod2usage (-exitval => 0, -verbose => 2) if $man;
209
210 # Get the parameters.
211 if (@ARGV < 2) {
212     die "Use '$0 --man' to display the manual.\n"
213 }
214
215 my @disks = @ARGV[0 .. $#ARGV-1];
216 my $output = $ARGV[$#ARGV];
217
218 if (!defined $name) {
219     $name = $disks[0];
220     $name =~ s{.*/}{};
221     $name =~ s{\.[^.]+}{};
222 }
223
224 if ($vmtype =~ /^Desktop$/i) {
225     $vmtype = 0;
226 } elsif ($vmtype =~ /^Server$/i) {
227     $vmtype = 1;
228 } else {
229     die "$0: --vmtype parameter must be 'Desktop' or 'Server'\n"
230 }
231
232 # Does qemu-img generally work OK?
233 system ("qemu-img create -f qcow2 .test.qcow2 10M >/dev/null") == 0
234     or die "qemu-img command not installed or not working\n";
235
236 # Does this version of qemu-img support compat=0.10?  RHEL 6
237 # did NOT support it.
238 my $qemu_img_supports_compat = 0;
239 system ("qemu-img create -f qcow2 -o compat=0.10 .test.qcow2 10M >/dev/null 2>&1") == 0
240     and $qemu_img_supports_compat = 1;
241 unlink ".test.qcow2";
242
243 # Open the guest in libguestfs so we can inspect it.
244 my $g = Sys::Guestfs->new ();
245 eval { $g->set_program ("virt-import-to-ovirt"); };
246 $g->add_drive_opts ($_, readonly => 1) foreach (@disks);
247 $g->launch ();
248 my @roots = $g->inspect_os ();
249 if (@roots == 0) {
250     die "$0: no operating system was found on the disk\n"
251 }
252 if (@roots > 1) {
253     die "$0: either this is a multi-OS disk, or you passed multiple unrelated guest disks on the command line\n"
254 }
255 my $root = $roots[0];
256
257 # Save the inspection data.
258 my $type = $g->inspect_get_type ($root);
259 my $distro = $g->inspect_get_distro ($root);
260 my $arch = $g->inspect_get_arch ($root);
261 my $major_version = $g->inspect_get_major_version ($root);
262 my $minor_version = $g->inspect_get_minor_version ($root);
263 my $product_name = $g->inspect_get_product_name ($root);
264 my $product_variant = $g->inspect_get_product_variant ($root);
265
266 # Get the virtual size of each disk.
267 my @virtual_sizes;
268 foreach (@disks) {
269     push @virtual_sizes, $g->disk_virtual_size ($_);
270 }
271
272 $g->close ();
273
274 # Map inspection data to RHEV ostype.
275 my $ostype;
276 if ($type eq "linux" && $distro eq "rhel" && $major_version <= 6) {
277     if ($arch eq "x86_64") {
278         $ostype = "RHEL${major_version}x64"
279     } else {
280         $ostype = "RHEL$major_version"
281     }
282 }
283 elsif ($type eq "linux" && $distro eq "rhel") {
284     if ($arch eq "x86_64") {
285         $ostype = "rhel_${major_version}x64"
286     } else {
287         $ostype = "rhel_$major_version"
288     }
289 }
290 elsif ($type eq "linux") {
291     $ostype = "OtherLinux"
292 }
293 elsif ($type eq "windows" && $major_version == 5 && $minor_version == 1) {
294     $ostype = "WindowsXP"
295 }
296 elsif ($type eq "windows" && $major_version == 5 && $minor_version == 2) {
297     if ($product_name =~ /XP/) {
298         $ostype = "WindowsXP"
299     } elsif ($arch eq "x86_64") {
300         $ostype = "Windows2003x64"
301     } else {
302         $ostype = "Windows2003"
303     }
304 }
305 elsif ($type eq "windows" && $major_version == 6 && $minor_version == 0) {
306     if ($arch eq "x86_64") {
307         $ostype = "Windows2008x64"
308     } else {
309         $ostype = "Windows2008"
310     }
311 }
312 elsif ($type eq "windows" && $major_version == 6 && $minor_version == 1) {
313     if ($product_variant eq "Client") {
314         if ($arch eq "x86_64") {
315             $ostype = "Windows7x64"
316         } else {
317             $ostype = "Windows7"
318         }
319     } else {
320         $ostype = "Windows2008R2x64"
321     }
322 }
323 elsif ($type eq "windows" && $major_version == 6 && $minor_version == 2) {
324    if ($product_variant eq "Client") {
325        if ($arch eq "x86_64") {
326            $ostype = "windows_8x64"
327        } else {
328            $ostype = "windows_8"
329        }
330    } else {
331        $ostype = "windows_2012x64"
332    }
333 }
334 elsif ($type eq "windows" && $major_version == 6 && $minor_version == 3) {
335     $ostype = "windows_2012R2x64"
336 }
337 else {
338     $ostype = "Unassigned"
339 }
340
341 # Mount the ESD if needed (or just check it exists).
342 my $mountpoint;
343 if (-d $output) {
344     $mountpoint = $output;
345 } elsif ($output =~ m{^.*:.*$}) {
346     my $umount;
347     $umount = $mountpoint = tempdir (CLEANUP => 1);
348     system ("mount", "-t", "nfs", $output, $mountpoint) == 0
349         or die "$0: mount $output failed: $?\n";
350     END { system ("umount", $umount) if defined $umount }
351 } else {
352     die "$0: ESD $output is not a directory or an NFS mountpoint\n"
353 }
354
355 # Check the ESD looks like an ESD.
356 my @entries = <$mountpoint/*>;
357 @entries =
358     grep { m{/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$}i }
359     @entries;
360 if (@entries == 0) {
361     die "$0: does $output really point to an oVirt Export Storage Domain?\n"
362 }
363 if (@entries > 1) {
364     die "$0: multiple GUIDs found in oVirt Export Storage Domain\n"
365 }
366 my $esd_uuid_dir = $entries[0];
367 my $esd_uuid = $esd_uuid_dir;
368 $esd_uuid =~ s{.*/}{};
369
370 if (! -d $esd_uuid_dir ||
371     ! -d "$esd_uuid_dir/images" ||
372     ! -d "$esd_uuid_dir/master" ||
373     ! -d "$esd_uuid_dir/master/vms") {
374     die "$0: $output doesn't look like an Export Storage Domain\n"
375 }
376
377 # Start the import.
378 print "Importing $product_name to $output...\n";
379
380 # A helper function that forks and runs some code / a command as
381 # an alternate UID:GID.
382 sub run_as_vdsm
383 {
384     my $fn = shift;
385
386     my $pid = fork ();
387     die "fork: $!" unless defined $pid;
388     if ($pid == 0) {
389         # Child process.
390         if ($EUID == 0) {
391             setgid (36);
392             setuid (36);
393         }
394         eval { &$fn () };
395         if ($@) {
396             print STDERR "$@\n";
397             _exit (1);
398         }
399         _exit (0);
400     }
401     waitpid ($pid, 0) or die "waitpid: $!";
402     if ($? != 0) {
403         die "$0: run_as_vdsm: child process failed (status $?)\n";
404     }
405 }
406
407 # Generate a UUID.
408 sub uuidgen
409 {
410     local $_ = `uuidgen -r`;
411     chomp;
412     die unless length $_ >= 30; # Sanity check.
413     $_;
414 }
415
416 # Generate some random UUIDs.
417 my $vm_uuid = uuidgen ();
418 my @image_uuids;
419 foreach (@disks) {
420     push @image_uuids, uuidgen ();
421 }
422 my @vol_uuids;
423 foreach (@disks) {
424     push @vol_uuids, uuidgen ();
425 }
426
427 # Make sure the output is deleted on unsuccessful exit.  We set
428 # $delete_output_on_exit to false at the end of the script.
429 my $delete_output_on_exit = 1;
430 END {
431     if ($delete_output_on_exit) {
432         # Can't use run_as_vdsm in an END{} block.
433         foreach (@image_uuids) {
434             system ("rm", "-rf", "$esd_uuid_dir/images/$_");
435         }
436         system ("rm", "-rf", "$esd_uuid_dir/master/vms/$vm_uuid");
437     }
438 };
439
440 # Copy and convert the disk images.
441 my $i;
442 my $time = time ();
443 my $iso_time = strftime ("%Y/%m/%d %H:%M:%S", gmtime ());
444 my $imported_by = "Imported by import-to-ovirt.pl";
445 my @real_sizes;
446
447 for ($i = 0; $i < @disks; ++$i) {
448     my $input_file = $disks[$i];
449     my $image_uuid = $image_uuids[$i];
450     run_as_vdsm (sub {
451         my $path = "$esd_uuid_dir/images/$image_uuid";
452         mkdir ($path, 0755) or die "mkdir: $path: $!";
453     });
454     my $output_file = "$esd_uuid_dir/images/$image_uuid/".$vol_uuids[$i];
455     run_as_vdsm (sub {
456         open (my $fh, ">", $output_file) or die "open: $output_file: $!";
457         # Well done NFS root_squash, you make the world less secure.
458         chmod (0666, $output_file) or die "chmod: $output_file: $!";
459     });
460     print "Copying $input_file ...\n";
461     my @compat_option = ();
462     if ($qemu_img_supports_compat) {
463         @compat_option = ("-o", "compat=0.10") # for RHEL 6-based ovirt nodes
464     }
465     system ("qemu-img", "convert", "-p",
466             $input_file,
467             "-O", "qcow2",
468             @compat_option,
469             $output_file) == 0
470                 or die "qemu-img: $input_file: failed (status $?)";
471     push @real_sizes, -s $output_file;
472
473     my $size_in_sectors = $virtual_sizes[$i] / 512;
474
475     # Create .meta files per disk.
476     my $meta = <<"EOF";
477 DOMAIN=$esd_uuid
478 VOLTYPE=LEAF
479 CTIME=$time
480 MTIME=$time
481 IMAGE=$image_uuid
482 DISKTYPE=1
483 PUUID=00000000-0000-0000-0000-000000000000
484 LEGALITY=LEGAL
485 POOL_UUID=
486 SIZE=$size_in_sectors
487 FORMAT=COW
488 TYPE=SPARSE
489 DESCRIPTION=$imported_by
490 EOF
491     my $meta_file = $output_file . ".meta";
492     run_as_vdsm (sub {
493         open (my $fh, ">", $meta_file) or die "open: $meta_file: $!";
494         print $fh $meta
495     });
496 }
497
498 # Create the OVF.
499 print "Creating OVF metadata ...\n";
500
501 my $rasd_ns = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData";
502 my $vssd_ns = "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData";
503 my $xsi_ns = "http://www.w3.org/2001/XMLSchema-instance";
504 my $ovf_ns = "http://schemas.dmtf.org/ovf/envelope/1/";
505 my %prefix_map = (
506     $rasd_ns => "rasd",
507     $vssd_ns => "vssd",
508     $xsi_ns => "xsi",
509     $ovf_ns => "ovf",
510 );
511 my @forced_ns_decls = keys %prefix_map;
512
513 my $ovf = "";
514 my $w = XML::Writer->new (
515     OUTPUT => \$ovf,
516     NAMESPACES => 1,
517     PREFIX_MAP => \%prefix_map,
518     FORCED_NS_DECLS => \@forced_ns_decls,
519     DATA_MODE => 1,
520     DATA_INDENT => 4,
521 );
522
523 $w->startTag ([$ovf_ns, "Envelope"],
524               [$ovf_ns, "version"] => "0.9");
525 $w->comment ($imported_by);
526
527 $w->startTag ("References");
528
529 for ($i = 0; $i < @disks; ++$i)
530 {
531     my $href = $image_uuids[$i] . "/" . $vol_uuids[$i];
532     $w->startTag ("File",
533                   [$ovf_ns, "href"] => $href,
534                   [$ovf_ns, "id"] => $vol_uuids[$i],
535                   [$ovf_ns, "size"] => $virtual_sizes[$i],
536                   [$ovf_ns, "description"] => $imported_by);
537     $w->endTag ();
538 }
539
540 $w->endTag ();
541
542 $w->startTag ("Section",
543               [$xsi_ns, "type"] => "ovf:NetworkSection_Type");
544 $w->startTag ("Info");
545 $w->characters ("List of networks");
546 $w->endTag ();
547 $w->endTag ();
548
549 $w->startTag ("Section",
550               [$xsi_ns, "type"] => "ovf:DiskSection_Type");
551 $w->startTag ("Info");
552 $w->characters ("List of Virtual Disks");
553 $w->endTag ();
554
555 for ($i = 0; $i < @disks; ++$i)
556 {
557     my $virtual_size_in_gb = $virtual_sizes[$i];
558     $virtual_size_in_gb /= 1024;
559     $virtual_size_in_gb /= 1024;
560     $virtual_size_in_gb /= 1024;
561     my $real_size_in_gb = $real_sizes[$i];
562     $real_size_in_gb /= 1024;
563     $real_size_in_gb /= 1024;
564     $real_size_in_gb /= 1024;
565     my $href = $image_uuids[$i] . "/" . $vol_uuids[$i];
566
567     my $boot_drive;
568     if ($i == 0) {
569         $boot_drive = "True";
570     } else {
571         $boot_drive = "False";
572     }
573
574     $w->startTag ("Disk",
575                   [$ovf_ns, "diskId" ] => $vol_uuids[$i],
576                   [$ovf_ns, "actual_size"] =>
577                       sprintf ("%.0f", $real_size_in_gb),
578                   [$ovf_ns, "size"] =>
579                       sprintf ("%.0f", $virtual_size_in_gb),
580                   [$ovf_ns, "fileRef"] => $href,
581                   [$ovf_ns, "parentRef"] => "",
582                   [$ovf_ns, "vm_snapshot_id"] => uuidgen (),
583                   [$ovf_ns, "volume-format"] => "COW",
584                   [$ovf_ns, "volume-type"] => "Sparse",
585                   [$ovf_ns, "format"] => "http://en.wikipedia.org/wiki/Byte",
586                   [$ovf_ns, "disk-interface"] => "VirtIO",
587                   [$ovf_ns, "disk-type"] => "System",
588                   [$ovf_ns, "boot"] => $boot_drive);
589     $w->endTag ();
590 }
591
592 $w->endTag ();
593
594 $w->startTag ("Content",
595               [$ovf_ns, "id"] => "out",
596               [$xsi_ns, "type"] => "ovf:VirtualSystem_Type");
597 $w->startTag ("Name");
598 $w->characters ($name);
599 $w->endTag ();
600 $w->startTag ("TemplateId");
601 $w->characters ("00000000-0000-0000-0000-000000000000");
602 $w->endTag ();
603 $w->startTag ("TemplateName");
604 $w->characters ("Blank");
605 $w->endTag ();
606 $w->startTag ("Description");
607 $w->characters ($imported_by);
608 $w->endTag ();
609 $w->startTag ("Domain");
610 $w->endTag ();
611 $w->startTag ("CreationDate");
612 $w->characters ($iso_time);
613 $w->endTag ();
614 $w->startTag ("IsInitilized"); # sic
615 $w->characters ("True");
616 $w->endTag ();
617 $w->startTag ("IsAutoSuspend");
618 $w->characters ("False");
619 $w->endTag ();
620 $w->startTag ("TimeZone");
621 $w->endTag ();
622 $w->startTag ("IsStateless");
623 $w->characters ("False");
624 $w->endTag ();
625 $w->startTag ("Origin");
626 $w->characters ("0");
627 $w->endTag ();
628 $w->startTag ("VmType");
629 $w->characters ($vmtype);
630 $w->endTag ();
631 $w->startTag ("DefaultDisplayType");
632 $w->characters ("1"); # qxl
633 $w->endTag ();
634
635 $w->startTag ("Section",
636               [$ovf_ns, "id"] => $vm_uuid,
637               [$ovf_ns, "required"] => "false",
638               [$xsi_ns, "type"] => "ovf:OperatingSystemSection_Type");
639 $w->startTag ("Info");
640 $w->characters ($product_name);
641 $w->endTag ();
642 $w->startTag ("Description");
643 $w->characters ($ostype);
644 $w->endTag ();
645 $w->endTag ();
646
647 $w->startTag ("Section",
648               [$xsi_ns, "type"] => "ovf:VirtualHardwareSection_Type");
649 $w->startTag ("Info");
650 $w->characters (sprintf ("%d CPU, %d Memory", $vcpus, $memory_mb));
651 $w->endTag ();
652
653 $w->startTag ("Item");
654 $w->startTag ([$rasd_ns, "Caption"]);
655 $w->characters (sprintf ("%d virtual cpu", $vcpus));
656 $w->endTag ();
657 $w->startTag ([$rasd_ns, "Description"]);
658 $w->characters ("Number of virtual CPU");
659 $w->endTag ();
660 $w->startTag ([$rasd_ns, "InstanceId"]);
661 $w->characters ("1");
662 $w->endTag ();
663 $w->startTag ([$rasd_ns, "ResourceType"]);
664 $w->characters ("3");
665 $w->endTag ();
666 $w->startTag ([$rasd_ns, "num_of_sockets"]);
667 $w->characters ($vcpus);
668 $w->endTag ();
669 $w->startTag ([$rasd_ns, "cpu_per_socket"]);
670 $w->characters (1);
671 $w->endTag ();
672 $w->endTag ("Item");
673
674 $w->startTag ("Item");
675 $w->startTag ([$rasd_ns, "Caption"]);
676 $w->characters (sprintf ("%d MB of memory", $memory_mb));
677 $w->endTag ();
678 $w->startTag ([$rasd_ns, "Description"]);
679 $w->characters ("Memory Size");
680 $w->endTag ();
681 $w->startTag ([$rasd_ns, "InstanceId"]);
682 $w->characters ("2");
683 $w->endTag ();
684 $w->startTag ([$rasd_ns, "ResourceType"]);
685 $w->characters ("4");
686 $w->endTag ();
687 $w->startTag ([$rasd_ns, "AllocationUnits"]);
688 $w->characters ("MegaBytes");
689 $w->endTag ();
690 $w->startTag ([$rasd_ns, "VirtualQuantity"]);
691 $w->characters ($memory_mb);
692 $w->endTag ();
693 $w->endTag ("Item");
694
695 $w->startTag ("Item");
696 $w->startTag ([$rasd_ns, "Caption"]);
697 $w->characters ("USB Controller");
698 $w->endTag ();
699 $w->startTag ([$rasd_ns, "InstanceId"]);
700 $w->characters ("3");
701 $w->endTag ();
702 $w->startTag ([$rasd_ns, "ResourceType"]);
703 $w->characters ("23");
704 $w->endTag ();
705 $w->startTag ([$rasd_ns, "UsbPolicy"]);
706 $w->characters ("Disabled");
707 $w->endTag ();
708 $w->endTag ("Item");
709
710 $w->startTag ("Item");
711 $w->startTag ([$rasd_ns, "Caption"]);
712 $w->characters ("Graphical Controller");
713 $w->endTag ();
714 $w->startTag ([$rasd_ns, "InstanceId"]);
715 $w->characters (uuidgen ());
716 $w->endTag ();
717 $w->startTag ([$rasd_ns, "ResourceType"]);
718 $w->characters ("20");
719 $w->endTag ();
720 $w->startTag ("Type");
721 $w->characters ("video");
722 $w->endTag ();
723 $w->startTag ([$rasd_ns, "VirtualQuantity"]);
724 $w->characters ("1");
725 $w->endTag ();
726 $w->startTag ([$rasd_ns, "Device"]);
727 $w->characters ("qxl");
728 $w->endTag ();
729 $w->endTag ("Item");
730
731 for ($i = 0; $i < @disks; ++$i)
732 {
733     my $href = $image_uuids[$i] . "/" . $vol_uuids[$i];
734
735     $w->startTag ("Item");
736
737     $w->startTag ([$rasd_ns, "Caption"]);
738     $w->characters ("Drive " . ($i+1));
739     $w->endTag ();
740     $w->startTag ([$rasd_ns, "InstanceId"]);
741     $w->characters ($vol_uuids[$i]);
742     $w->endTag ();
743     $w->startTag ([$rasd_ns, "ResourceType"]);
744     $w->characters ("17");
745     $w->endTag ();
746     $w->startTag ("Type");
747     $w->characters ("disk");
748     $w->endTag ();
749     $w->startTag ([$rasd_ns, "HostResource"]);
750     $w->characters ($href);
751     $w->endTag ();
752     $w->startTag ([$rasd_ns, "Parent"]);
753     $w->characters ("00000000-0000-0000-0000-000000000000");
754     $w->endTag ();
755     $w->startTag ([$rasd_ns, "Template"]);
756     $w->characters ("00000000-0000-0000-0000-000000000000");
757     $w->endTag ();
758     $w->startTag ([$rasd_ns, "ApplicationList"]);
759     $w->endTag ();
760     $w->startTag ([$rasd_ns, "StorageId"]);
761     $w->characters ($esd_uuid);
762     $w->endTag ();
763     $w->startTag ([$rasd_ns, "StoragePoolId"]);
764     $w->characters ("00000000-0000-0000-0000-000000000000");
765     $w->endTag ();
766     $w->startTag ([$rasd_ns, "CreationDate"]);
767     $w->characters ($iso_time);
768     $w->endTag ();
769     $w->startTag ([$rasd_ns, "LastModified"]);
770     $w->characters ($iso_time);
771     $w->endTag ();
772     $w->startTag ([$rasd_ns, "last_modified_date"]);
773     $w->characters ($iso_time);
774     $w->endTag ();
775
776     $w->endTag ("Item");
777 }
778
779 $w->endTag ("Section"); # ovf:VirtualHardwareSection_Type
780
781 $w->endTag ("Content");
782
783 $w->endTag ([$ovf_ns, "Envelope"]);
784 $w->end ();
785
786 #print "OVF:\n$ovf\n";
787
788 my $ovf_dir = "$esd_uuid_dir/master/vms/$vm_uuid";
789 run_as_vdsm (sub {
790     mkdir ($ovf_dir, 0755) or die "mkdir: $ovf_dir: $!";
791 });
792 my $ovf_file = "$ovf_dir/$vm_uuid.ovf";
793 run_as_vdsm (sub {
794     open (my $fh, ">", $ovf_file) or die "open: $ovf_file: $!";
795     print $fh $ovf
796 });
797
798 # Finished.
799 $delete_output_on_exit = 0;
800 print "\n";
801 print "OVF written to $ovf_file\n";
802 print "\n";
803 print "Import finished without errors.  Now go to the Storage tab ->\n";
804 print "Export Storage Domain -> VM Import, and import the guest.\n";
805 exit 0;
806
807 __END__
808
809 =head1 TO DO
810
811 =over 4
812
813 =item Network
814
815 Add a network card to the OVF.  The problem is detecting what
816 network devices the guest can support.
817
818 =item Disk model
819
820 Detect what disk models (eg. IDE, virtio-blk, virtio-scsi) the
821 guest can support and add the correct type of disk.
822
823 =back
824
825 =head1 DEBUGGING IMPORT FAILURES
826
827 When you export to the ESD, and then import that guest through the
828 oVirt / RHEV-M UI, you may encounter an import failure.  Diagnosing
829 these failures is infuriatingly difficult as the UI generally hides
830 the true reason for the failure.
831
832 There are two log files of interest.  The first is stored on the oVirt
833 engine / RHEV-M server itself, and is called
834 F</var/log/ovirt-engine/engine.log>
835
836 The second file, which is the most useful, is found on the SPM host
837 (SPM stands for "Storage Pool Manager").  This is a oVirt node that is
838 elected to do all metadata modifications in the data center, such as
839 image or snapshot creation.  You can find out which host is the
840 current SPM from the "Hosts" tab "Spm Status" column.  Once you have
841 located the SPM, log into it and grab the file
842 F</var/log/vdsm/vdsm.log> which will contain detailed error messages
843 from low-level commands.
844
845 =head1 SEE ALSO
846
847 L<https://bugzilla.redhat.com/show_bug.cgi?id=998279>,
848 L<https://bugzilla.redhat.com/show_bug.cgi?id=1049604>,
849 L<virt-v2v(1)>,
850 L<engine-image-uploader(8)>.
851
852 =head1 AUTHOR
853
854 Richard W.M. Jones <rjones@redhat.com>
855
856 =head1 COPYRIGHT
857
858 Copyright (C) 2015 Richard W.M. Jones <rjones@redhat.com>
859
860 Copyright (C) 2015 Red Hat Inc.
861
862 =head1 LICENSE
863
864 This program is free software; you can redistribute it and/or modify it
865 under the terms of the GNU General Public License as published by the
866 Free Software Foundation; either version 2 of the License, or (at your
867 option) any later version.
868
869 This program is distributed in the hope that it will be useful, but
870 WITHOUT ANY WARRANTY; without even the implied warranty of
871 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
872 General Public License for more details.
873
874 You should have received a copy of the GNU General Public License along
875 with this program; if not, write to the Free Software Foundation, Inc.,
876 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.