[Libguestfs] [PATCH 1/8] MetadataReader: Allow different libvirt connections for input and output

Matthew Booth mbooth at redhat.com
Mon Feb 1 17:23:29 UTC 2010


Command line option changes:
-s -> -f
-c -> -ic

-c, --connect now generates an error message.
New option -oc specifies output connection.
---
 lib/Sys/VirtV2V/MetadataReader.pm            |   20 +++--
 lib/Sys/VirtV2V/MetadataReader/LibVirt.pm    |    9 ++-
 lib/Sys/VirtV2V/MetadataReader/LibVirtXML.pm |    5 +-
 v2v/virt-v2v.pl                              |  108 ++++++++++++++------------
 4 files changed, 79 insertions(+), 63 deletions(-)

diff --git a/lib/Sys/VirtV2V/MetadataReader.pm b/lib/Sys/VirtV2V/MetadataReader.pm
index 6a1bec3..7b5e7d5 100644
--- a/lib/Sys/VirtV2V/MetadataReader.pm
+++ b/lib/Sys/VirtV2V/MetadataReader.pm
@@ -36,7 +36,9 @@ Sys::VirtV2V::MetadataReader - Read a variety of guest metadata formats
 
  use Sys::VirtV2V::MetadataReader;
 
- $reader = Sys::VirtV2V::MetadataReader->instantiate("libvirtxml", $vmm, @args);
+ $reader = Sys::VirtV2V::MetadataReader->instantiate("libvirtxml", $uri,
+                                                     $config, @args);
+ exit 1 unless($mdr->is_configured());
  $dom = $reader->get_dom();
 
 =head1 DESCRIPTION
@@ -53,7 +55,7 @@ implements methods to access backends.
 
 =over
 
-=item instantiate(name, vmm, @args)
+=item instantiate(name, $uri, $config, @args)
 
 =over
 
@@ -61,13 +63,13 @@ implements methods to access backends.
 
 The name of the module to instantiate.
 
-=item config
+=item uri
 
-A parsed virt-v2v configuration file.
+A URI describing the target connection.
 
-=item vmm
+=item config
 
-A Sys::Virt connection.
+A parsed virt-v2v configuration file.
 
 =item args
 
@@ -83,15 +85,15 @@ sub instantiate
 {
     my $class = shift;
 
-    my ($name, $config, $vmm, @args) = @_;
+    my ($name, $uri, $config, @args) = @_;
 
     defined($name) or carp("instantiate called without name argument");
+    defined($uri)  or carp("instantiate called without uri argument");
     defined($config) or carp("instantiate called without config argument");
-    defined($vmm) or carp("instantiate called without vmm argument");
 
     foreach my $module ($class->modules()) {
         if($module->get_name() eq $name) {
-            return $module->_new($config->{$name}, $vmm, @args);
+            return $module->_new($uri, $config->{$name}, @args);
         }
     }
 
diff --git a/lib/Sys/VirtV2V/MetadataReader/LibVirt.pm b/lib/Sys/VirtV2V/MetadataReader/LibVirt.pm
index d18f432..e6fcfff 100644
--- a/lib/Sys/VirtV2V/MetadataReader/LibVirt.pm
+++ b/lib/Sys/VirtV2V/MetadataReader/LibVirt.pm
@@ -38,7 +38,8 @@ Sys::VirtV2V::MetadataReader::LibVirt - Read libvirt metadata from libvirtd
 
  use Sys::VirtV2V::MetadataReader;
 
- $reader = Sys::VirtV2V::MetadataReader->instantiate("libvirt", $vmm, @args);
+ $reader = Sys::VirtV2V::MetadataReader->instantiate
+    ("libvirt", "xen+ssh://xenserver.example.com/", $config, @args);
  $dom = $reader->get_dom();
 
 =head1 DESCRIPTION
@@ -62,12 +63,16 @@ sub _new
 {
     my $class = shift;
 
-    my ($config, $vmm, @args) = @_;
+    my ($uri, $config, @args) = @_;
 
     my $self = {};
 
     bless($self, $class);
 
+    my @vmm_params = (auth => 1);
+    push(@vmm_params, url => $uri) if defined($uri);
+    my $vmm = Sys::Virt->new(@vmm_params);
+
     $self->{vmm} = $vmm;
     $self->_handle_args(@args);
 
diff --git a/lib/Sys/VirtV2V/MetadataReader/LibVirtXML.pm b/lib/Sys/VirtV2V/MetadataReader/LibVirtXML.pm
index 877b7e5..191210f 100644
--- a/lib/Sys/VirtV2V/MetadataReader/LibVirtXML.pm
+++ b/lib/Sys/VirtV2V/MetadataReader/LibVirtXML.pm
@@ -37,7 +37,8 @@ Sys::VirtV2V::MetadataReader::LibVirtXML - Read libvirt XML from a file
 
  use Sys::VirtV2V::MetadataReader;
 
- $reader = Sys::VirtV2V::MetadataReader->instantiate("libvirtxml", $vmm, @args);
+ $reader = Sys::VirtV2V::MetadataReader->instantiate("libvirtxml", undef,
+                                                     $config, @args);
  $dom = $reader->get_dom();
 
 =head1 DESCRIPTION
@@ -61,7 +62,7 @@ sub _new
 {
     my $class = shift;
 
-    my ($config, $vmm, @args) = @_;
+    my ($uri, $config, @args) = @_;
 
     my %obj = ();
     my $self = \%obj;
diff --git a/v2v/virt-v2v.pl b/v2v/virt-v2v.pl
index 0ad3c6e..ad1610f 100755
--- a/v2v/virt-v2v.pl
+++ b/v2v/virt-v2v.pl
@@ -46,9 +46,9 @@ virt-v2v - Convert a guest to use KVM
 
  virt-v2v guest-domain.xml
 
- virt-v2v -s virt-v2v.conf guest-domain.xml
+ virt-v2v -f virt-v2v.conf guest-domain.xml
 
- virt-v2v --connect qemu:///system guest-domain.xml
+ virt-v2v -ic qemu:///system guest-domain
 
 =head1 DESCRIPTION
 
@@ -112,58 +112,59 @@ table below.
 
 =cut
 
-my $help;
+my $input_method = "libvirt";
 
-=item B<--help>
-
-Display brief help.
-
-=cut
+=item B<-i input>
 
-my $version;
+Specifies how the conversion source metadata can be obtained. The default is
+C<libvirt>.  Supported options are:
 
-=item B<--version>
+=over
 
-Display version number and exit.
+=item I<libvirt>
 
-=cut
+Guest argument is the name of a libvirt domain.
 
-my $uri;
+=item I<libvirtxml>
 
-=item B<--connect URI> | B<-c URI>
+Guest argument is the path to an XML file describing a libvirt domain.
 
-Connect to libvirt using the given I<URI>. If omitted, then we connect to the
-default libvirt hypervisor.
+=back
 
 =cut
 
-my $input = "libvirt";
+my $input_uri;
 
-=item B<--input input> | B<-i input>
+=item B<-ic URI>
 
-The specified guest description uses the given I<input format>. The default is
-C<libvirt>. Supported options are:
+Specifies the connection to use when using the libvirt input method. If omitted,
+then we connect to the default libvirt hypervisor.
 
-=over
-
-=item I<libvirt>
-
-Guest argument is the name of a libvirt domain.
+=cut
 
-=item I<libvirtxml>
+my $output_uri = "qemu:///system";
 
-Guest argument is the path to an XML file describing a libvirt domain.
+=item B<-oc URI>
 
-=back
+Specifies the libvirt connection to use to create the converted guest. If
+ommitted, this defaults to qemu:///system.
 
 =cut
 
 my $config_file;
 
-=item B<--config file> | B<-s file>
+=item B<-f file>, B<--config file>
 
 Load the virt-v2v configuration from I<file>. There is no default.
 
+=item B<--help>
+
+Display brief help.
+
+=item B<--version>
+
+Display version number and exit.
+
 =back
 
 =cut
@@ -171,26 +172,28 @@ Load the virt-v2v configuration from I<file>. There is no default.
 # Initialise the message output prefix
 Sys::VirtV2V::UserMessage->set_identifier('virt-v2v');
 
-GetOptions ("help|?"      => \$help,
-            "version"     => \$version,
-            "connect|c=s" => \$uri,
-            "input|i=s"   => \$input,
-            "config|s=s"  => \$config_file
-    ) or pod2usage(2);
-pod2usage(0) if($help);
-
-if ($version) {
-    print "$Sys::VirtV2V::VERSION\n";
-    exit(0);
-}
+GetOptions ("help|?"      => sub {
+                pod2usage(0);
+            },
+            "version"     => sub {
+                print "$Sys::VirtV2V::VERSION\n";
+                exit(0);
+            },
+            "c|connect"   => sub {
+                # -c|--connect is the default for other virt tools. Be nice to
+                # the user and point out that virt-v2v is different.
+                pod2usage({ -message => __("Use -ic or -oc to specify an ".
+                                           "input or an output connection"),
+                            -exitval => 1 });
+            },
+            "i=s"         => \$input_method,
+            "ic=s"        => \$input_uri,
+            "oc=s"        => \$output_uri,
+            "f|config=s"  => \$config_file
+) or pod2usage(2);
 
 pod2usage(user_message(__"no guest argument given")) if @ARGV == 0;
 
-# Connect to libvirt
-my @vmm_params = (auth => 1);
-push(@vmm_params, uri => $uri) if(defined($uri));
-my $vmm = Sys::Virt->new(@vmm_params);
-
 # Read the config file if one was given
 my $config = {};
 if(defined($config_file)) {
@@ -206,11 +209,11 @@ if(defined($config_file)) {
 }
 
 # Get an appropriate MetadataReader
-my $mdr = Sys::VirtV2V::MetadataReader->instantiate($input, $config,
-                                                    $vmm, @ARGV);
+my $mdr = Sys::VirtV2V::MetadataReader->instantiate($input_method, $input_uri,
+                                                    $config, @ARGV);
 if(!defined($mdr)) {
-    print STDERR user_message __x("{input} is not a valid metadata format",
-                                  input => $input);
+    print STDERR user_message __x("{input} is not a valid input method",
+                                  input => $input_method);
     exit(1);
 }
 
@@ -239,6 +242,11 @@ my $os = inspect_guest($g);
 # Instantiate a GuestOS instance to manipulate the guest
 my $guestos = Sys::VirtV2V::GuestOS->instantiate($g, $os);
 
+# Connect to target libvirt
+my @vmm_params = (auth => 1);
+push(@vmm_params, uri => $output_uri) if(defined($output_uri));
+my $vmm = Sys::Virt->new(@vmm_params);
+
 # Modify the guest and its metadata for the target hypervisor
 Sys::VirtV2V::Converter->convert($vmm, $guestos, $dom, $os);
 
-- 
1.6.6




More information about the Libguestfs mailing list