[Libguestfs] [PATCH v2v] Add -on (output name) option to allow renaming of guests.

Richard W.M. Jones rjones at redhat.com
Tue Mar 22 15:31:03 UTC 2011


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://et.redhat.com/~rjones/virt-df/
-------------- next part --------------
>From d1844239f8ad7f437713e29b4224748a62de78f5 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Tue, 22 Mar 2011 15:27:42 +0000
Subject: [PATCH 1/3] Add -on (output name) option to allow renaming of guests.

---
 lib/Sys/VirtV2V/Connection/LibVirtTarget.pm |   14 +++++++++-
 lib/Sys/VirtV2V/Connection/RHEVTarget.pm    |   10 ++-----
 v2v/virt-v2v.pl                             |   34 +++++++++++++++++++-------
 3 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/lib/Sys/VirtV2V/Connection/LibVirtTarget.pm b/lib/Sys/VirtV2V/Connection/LibVirtTarget.pm
index a74c978..b2caa83 100644
--- a/lib/Sys/VirtV2V/Connection/LibVirtTarget.pm
+++ b/lib/Sys/VirtV2V/Connection/LibVirtTarget.pm
@@ -261,7 +261,7 @@ sub guest_exists
     return 1;
 }
 
-=item create_guest(desc, dom, guestcaps)
+=item create_guest(desc, dom, guestcaps, output_name)
 
 Create the guest in the target
 
@@ -270,10 +270,11 @@ Create the guest in the target
 sub create_guest
 {
     my $self = shift;
-    my ($desc, $dom, $guestcaps) = @_;
+    my ($desc, $dom, $guestcaps, $output_name) = @_;
 
     my $vmm = $self->{vmm};
 
+    _change_name ($dom, $output_name);
     _unconfigure_incompatible_devices($dom);
     _configure_capabilities($vmm, $dom, $guestcaps);
 
@@ -303,6 +304,15 @@ sub DESTROY
     }
 }
 
+sub _change_name
+{
+    my $dom = shift;
+    my $output_name = shift;
+
+    my ($name) = $dom->findnodes ('/domain/name/text()');
+    $name->setNodeValue ($output_name);
+}
+
 sub _unconfigure_incompatible_devices
 {
     my ($dom) = @_;
diff --git a/lib/Sys/VirtV2V/Connection/RHEVTarget.pm b/lib/Sys/VirtV2V/Connection/RHEVTarget.pm
index 5fde58b..f7e281a 100644
--- a/lib/Sys/VirtV2V/Connection/RHEVTarget.pm
+++ b/lib/Sys/VirtV2V/Connection/RHEVTarget.pm
@@ -606,7 +606,7 @@ sub guest_exists
     return 0;
 }
 
-=item create_guest(dom)
+=item create_guest(desc, dom, guestcaps, output_name)
 
 Create the guest in the target
 
@@ -615,11 +615,7 @@ Create the guest in the target
 sub create_guest
 {
     my $self = shift;
-    my ($desc, $dom, $guestcaps) = @_;
-
-    # Get the name of the guest
-    my ($name) = $dom->findnodes('/domain/name/text()');
-    $name = $name->getNodeValue();
+    my ($desc, $dom, $guestcaps, $output_name) = @_;
 
     # Get the number of virtual cpus
     my ($ncpus) = $dom->findnodes('/domain/vcpu/text()');
@@ -656,7 +652,7 @@ sub create_guest
     </Section>
 
     <Content ovf:id="out" xsi:type="ovf:VirtualSystem_Type">
-        <Name>$name</Name>
+        <Name>$output_name</Name>
         <TemplateId>00000000-0000-0000-0000-000000000000</TemplateId>
         <TemplateName>Blank</TemplateName>
         <Description>Imported with virt-v2v</Description>
diff --git a/v2v/virt-v2v.pl b/v2v/virt-v2v.pl
index 5212fab..a499924 100755
--- a/v2v/virt-v2v.pl
+++ b/v2v/virt-v2v.pl
@@ -191,6 +191,19 @@ source.
 
 =cut
 
+my $output_name;
+
+=item B<-on outputname>
+
+Rename the guest.
+
+If this option is not given, then the output name is the same
+as the input name.
+
+Guest names cannot contain some illegal characters.
+
+=cut
+
 my $config_file;
 $config_file = '/etc/virt-v2v.conf';
 
@@ -290,6 +303,7 @@ GetOptions ("help|?"      => sub {
                 my (undef, $value) = @_;
                 $output_sparse = parse_allocation($value);
             },
+            "on=s"        => \$output_name,
             "f|config=s"  => \$config_file,
             "n|network=s" => sub {
                 my (undef, $value) = @_;
@@ -417,10 +431,13 @@ else {
 ###############################################################################
 ## Start of processing
 
+# Decide the name of the guest target.
+$output_name = $source->get_name() unless defined $output_name;
+
 # Check that the guest doesn't already exist on the target
 v2vdie __x('Domain {name} already exists on the target.',
-           name => $source->get_name)
-    if $target->guest_exists($source->get_name());
+           name => $output_name)
+    if $target->guest_exists($output_name);
 
 # Copy source storage to target
 $source->copy_storage($target, $output_format, $output_sparse);
@@ -462,21 +479,20 @@ if ($@) {
 
 $g->close();
 
-$target->create_guest($os, $dom, $guestcaps);
+$target->create_guest($os, $dom, $guestcaps, $output_name);
 
-my ($name) = $dom->findnodes('/domain/name/text()');
-$name = $name->getNodeValue();
 if($guestcaps->{block} eq 'virtio' && $guestcaps->{net} eq 'virtio') {
-    logmsg NOTICE, __x('{name} configured with virtio drivers.', name => $name);
+    logmsg NOTICE, __x('{name} configured with virtio drivers.',
+                       name => $output_name);
 } elsif ($guestcaps->{block} eq 'virtio') {
     logmsg NOTICE, __x('{name} configured with virtio storage only.',
-                       name => $name);
+                       name => $output_name);
 } elsif ($guestcaps->{net} eq 'virtio') {
     logmsg NOTICE, __x('{name} configured with virtio networking only.',
-                       name => $name);
+                       name => $output_name);
 } else {
     logmsg NOTICE, __x('{name} configured without virtio drivers.',
-                       name => $name);
+                       name => $output_name);
 }
 
 exit(0);
-- 
1.7.4.1



More information about the Libguestfs mailing list