>From 8b93d6fa151d3deb8dad69e05ae3730ed7d7fe38 Mon Sep 17 00:00:00 2001 From: Richard W.M. Jones Date: Thu, 24 Mar 2011 11:42:59 +0000 Subject: [PATCH 2/2] Add -ou option for setting output UUID. --- lib/Sys/VirtConvert/Connection/LibVirt.pm | 1 + lib/Sys/VirtConvert/Connection/LibVirtTarget.pm | 41 +++++++++++++++++++++- lib/Sys/VirtConvert/Connection/RHEVTarget.pm | 27 +++++++++++++-- v2v/virt-v2v.pl | 29 +++++++++++++++- 4 files changed, 92 insertions(+), 6 deletions(-) diff --git a/lib/Sys/VirtConvert/Connection/LibVirt.pm b/lib/Sys/VirtConvert/Connection/LibVirt.pm index b351296..dc2127c 100644 --- a/lib/Sys/VirtConvert/Connection/LibVirt.pm +++ b/lib/Sys/VirtConvert/Connection/LibVirt.pm @@ -159,6 +159,7 @@ sub _parse_dom my $root = $dom->getDocumentElement(); $meta{name} = _node_val($root, 'name/text()'); + $meta{uuid} = _node_val($root, 'uuid/text()'); $meta{memory} = _node_val($root, 'memory/text()') * 1024; $meta{cpus} = _node_val($root, 'vcpu/text()'); $meta{arch} = _node_val($root, 'os/type/@arch'); diff --git a/lib/Sys/VirtConvert/Connection/LibVirtTarget.pm b/lib/Sys/VirtConvert/Connection/LibVirtTarget.pm index 5b64278..c175655 100644 --- a/lib/Sys/VirtConvert/Connection/LibVirtTarget.pm +++ b/lib/Sys/VirtConvert/Connection/LibVirtTarget.pm @@ -261,7 +261,34 @@ sub guest_exists return 1; } -=item create_guest(desc, meta, config, guestcaps, output_name) +=item guest_uuid_exists(uuid) + +Return 1 if a guest with I already exists, 0 otherwise. + +=cut + +sub guest_uuid_exists +{ + my $self = shift; + my ($uuid) = @_; + + eval { + $self->{vmm}->get_domain_by_uuid($uuid); + }; + + if ($@) { + if ($@->code == Sys::Virt::Error::ERR_NO_DOMAIN) { + return 0; + } + + v2vdie __x('Error checking for domain: {error}', + error => $@->stringify()); + } + + return 1; +} + +=item create_guest(desc, meta, config, guestcaps, output_name, output_uuid) Create the guest in the target @@ -270,11 +297,20 @@ Create the guest in the target sub create_guest { my $self = shift; - my ($desc, $meta, $config, $guestcaps, $output_name) = @_; + my ($desc, $meta, $config, $guestcaps, $output_name, $output_uuid) = @_; my $vmm = $self->{vmm}; $meta->{name} = $output_name; + + if (defined $output_uuid) { + if ($output_uuid ne "none") { + $meta->{uuid} = $output_uuid; + } else { + delete $meta->{uuid}; + } + } + _configure_capabilities($vmm, $meta, $guestcaps); $vmm->define_domain(_meta_to_domxml($meta, $config, $guestcaps)); @@ -311,6 +347,7 @@ DOM my $root = $dom->getDocumentElement(); _append_elem($root, 'name', $meta->{name}); + _append_elem($root, 'uuid', $meta->{uuid}) if defined $meta->{uuid}; _append_elem($root, 'memory', $meta->{memory} / 1024); _append_elem($root, 'vcpu', $meta->{cpus}); diff --git a/lib/Sys/VirtConvert/Connection/RHEVTarget.pm b/lib/Sys/VirtConvert/Connection/RHEVTarget.pm index 4ebdcb9..42aa869 100644 --- a/lib/Sys/VirtConvert/Connection/RHEVTarget.pm +++ b/lib/Sys/VirtConvert/Connection/RHEVTarget.pm @@ -605,7 +605,18 @@ sub guest_exists return 0; } -=item create_guest(desc, meta, config, guestcaps, output_name) +=item guest_uuid_exists(uuid) + +This always returns 0 for a RHEV target. + +=cut + +sub guest_uuid_exists +{ + return 0; +} + +=item create_guest(desc, meta, config, guestcaps, output_name, output_uuid) Create the guest in the target @@ -614,7 +625,7 @@ Create the guest in the target sub create_guest { my $self = shift; - my ($desc, $meta, $config, $guestcaps, $output_name) = @_; + my ($desc, $meta, $config, $guestcaps, $output_name, $output_uuid) = @_; # Get the number of virtual cpus my $ncpus = $meta->{cpus}; @@ -625,7 +636,17 @@ sub create_guest # Generate a creation date my $vmcreation = _format_time(gmtime()); - my $vmuuid = rhev_util::get_uuid(); + my $vmuuid; + if (defined $output_uuid) { + if ($output_uuid ne "none") { + $vmuuid = $output_uuid; + } else { + $vmuuid = rhev_util::get_uuid(); + } + } else { + $vmuuid = + defined $meta->{uuid} ? $meta->{uuid} : rhev_util::get_uuid(); + } my $ostype = _get_os_type($desc); diff --git a/v2v/virt-v2v.pl b/v2v/virt-v2v.pl index cdf77ab..8bd9c23 100755 --- a/v2v/virt-v2v.pl +++ b/v2v/virt-v2v.pl @@ -198,6 +198,24 @@ as the input name. =cut +my $output_uuid; + +=item B<-ou> I + +=item B<-ou none> + +Change the UUID of the guest. + +If this option is not given, then the output UUID is the same +as the input UUID. + +The I<-ou> option changes the UUID to the one given. + +The I<-ou none> option removes the UUID (this lets the target +hypervisor choose a new random UUID for the guest). + +=cut + my $config_file; $config_file = '/etc/virt-v2v.conf'; @@ -337,6 +355,7 @@ GetOptions ("help|?" => sub { $output_sparse = parse_allocation($value); }, "on=s" => \$output_name, + "ou=s" => \$output_uuid, "f|config=s" => \$config_file, "n|network=s" => sub { my (undef, $value) = @_; @@ -474,6 +493,14 @@ v2vdie __x('Domain {name} already exists on the target.', name => $output_name) if $target->guest_exists($output_name); +# Check that the user is not using -ou option to set a UUID that +# already exists on the target. +v2vdie __x('Domain with UUID {uuid} already exists on the target.', + uuid => $output_uuid) + if defined $output_uuid && + $output_uuid ne "none" && + $target->guest_uuid_exists($output_uuid); + # Copy source storage to target $source->copy_storage($target, $output_format, $output_sparse); @@ -518,7 +545,7 @@ if ($@) { $g->close(); -$target->create_guest($desc, $meta, $config, $guestcaps, $output_name); +$target->create_guest($desc, $meta, $config, $guestcaps, $output_name, $output_uuid); if($guestcaps->{block} eq 'virtio' && $guestcaps->{net} eq 'virtio') { logmsg NOTICE, __x('{name} configured with virtio drivers.', -- 1.7.4.1