Matthew Booth
2010-May-04 19:07 UTC
[Libguestfs] [PATCH 1/2] Config: NFC: always create and pass round a Config object
We previously wouldn't create a Config object if no config file was specified. This change ensures that a Config object is always created, but will do nothing interesting if there is no config file. Apart from being slightly cleaner, this allows information provided by Config to be later supplied from the command line instead. --- lib/Sys/VirtV2V/Config.pm | 34 ++++++++++++++++++++-------------- lib/Sys/VirtV2V/Converter.pm | 4 ++-- lib/Sys/VirtV2V/GuestOS.pm | 2 +- lib/Sys/VirtV2V/GuestOS/RedHat.pm | 18 +----------------- v2v/virt-v2v.pl | 5 ++--- 5 files changed, 26 insertions(+), 37 deletions(-) diff --git a/lib/Sys/VirtV2V/Config.pm b/lib/Sys/VirtV2V/Config.pm index 57bf24a..50c613c 100644 --- a/lib/Sys/VirtV2V/Config.pm +++ b/lib/Sys/VirtV2V/Config.pm @@ -69,6 +69,9 @@ sub new my $self = {}; bless($self, $class); + # No further config required if no config path was specified + return $self if (!defined($path)); + die(user_message(__x("Config file {path} doesn't exist", path => $path))) unless (-e $path); @@ -99,6 +102,8 @@ sub get_transfer_iso my $dom = $self->{dom}; + return undef unless (defined($dom)); + # path-root doesn't have to be defined my ($root) = $dom->findnodes('/virt-v2v/path-root/text()'); $root = $root->getData() if (defined($root)); @@ -175,15 +180,7 @@ sub get_transfer_iso return $iso_path; } -=item get_app_search(desc, name, arch) - -Return a string describing what v2v is looking for in the config file. The -string is intended to be presented to the user to help improve the configuration -file. - -=cut - -sub get_app_search +sub _get_app_search { my ($desc, $name, $arch) = @_; @@ -216,6 +213,10 @@ sub match_app my $dom = $self->{dom}; + die(user_message(__x("No config specified. No app match for {search}", + search => _get_app_search($desc, $name, $arch)))) + unless (defined($dom)); + my $distro = $desc->{distro}; my $major = $desc->{major_version}; my $minor = $desc->{minor_version}; @@ -248,7 +249,7 @@ sub match_app } die(user_message(__x("No app in config matches {search}", - search => get_app_search($desc, $name, $arch)))) + search => _get_app_search($desc, $name, $arch)))) unless (defined($app)); my %app; @@ -263,7 +264,7 @@ sub match_app die(user_message(__x("Matched local file {path} for {search}. ". "However, this file is not available.", path => $abs, - search => get_app_search($desc, $name, $arch)))) + search => _get_app_search($desc, $name, $arch)))) unless (-r $abs); my @deps; @@ -301,9 +302,14 @@ sub map_network my $self = shift; my ($oldname, $oldtype) = @_; - my ($mapping) = $self->{dom}->findnodes - ("/virt-v2v/network[\@type='$oldtype' and \@name='$oldname']". - "/network"); + my $dom = $self->{dom}; + + my $mapping; + if (defined($dom)) { + my ($mapping) = $dom->findnodes + ("/virt-v2v/network[\@type='$oldtype' and \@name='$oldname']". + "/network"); + } unless (defined($mapping)) { print STDERR user_message(__x("WARNING: No mapping found for ". diff --git a/lib/Sys/VirtV2V/Converter.pm b/lib/Sys/VirtV2V/Converter.pm index 45038ee..a5b7e43 100644 --- a/lib/Sys/VirtV2V/Converter.pm +++ b/lib/Sys/VirtV2V/Converter.pm @@ -137,7 +137,7 @@ sub convert my ($g, $guestos, $config, $dom, $desc, $devices) = @_; carp("convert called without guestos argument") unless defined($guestos); - # config will be undefined if no config was specified + carp("convert called without config argument") unless defined($config); carp("convert called without dom argument") unless defined($dom); carp("convert called without desc argument") unless defined($desc); carp("convert called without devices argument") unless defined($devices); @@ -156,7 +156,7 @@ sub convert unless (defined($guestcaps)); # Map network names from config - _map_networks($dom, $config) if (defined($config)); + _map_networks($dom, $config); # Convert the metadata _convert_metadata($dom, $desc, $devices, $guestcaps); diff --git a/lib/Sys/VirtV2V/GuestOS.pm b/lib/Sys/VirtV2V/GuestOS.pm index fea4ba1..e5b1704 100644 --- a/lib/Sys/VirtV2V/GuestOS.pm +++ b/lib/Sys/VirtV2V/GuestOS.pm @@ -100,7 +100,7 @@ sub new defined($g) or carp("instantiate called without g argument"); defined($desc) or carp("instantiate called without desc argument"); defined($dom) or carp("instantiate called without dom argument"); - # config will be undefined if no config was specified + defined($config) or carp("instantiate called without config argument"); my $self = {}; diff --git a/lib/Sys/VirtV2V/GuestOS/RedHat.pm b/lib/Sys/VirtV2V/GuestOS/RedHat.pm index a4680c5..ee074c1 100644 --- a/lib/Sys/VirtV2V/GuestOS/RedHat.pm +++ b/lib/Sys/VirtV2V/GuestOS/RedHat.pm @@ -475,17 +475,8 @@ sub add_kernel eval { my $desc = $self->{desc}; - my $config = $self->{config}; - unless (defined($config)) { - my $search = Sys::VirtV2V::Config::get_app_search - ($desc, $kernel_pkg, $kernel_arch); - die(user_message(__x("No config specified. No app match for ". - "{search}", - search => $search))); - } - ($app, $depnames) - $config->match_app($desc, $kernel_pkg, $kernel_arch); + $self->{config}->match_app($desc, $kernel_pkg, $kernel_arch); }; # Return undef if we didn't find a kernel if ($@) { @@ -619,13 +610,6 @@ sub add_application my $user_arch = $desc->{arch}; my $config = $self->{config}; - unless (defined($config)) { - my $search = Sys::VirtV2V::Config::get_app_search($desc, $label, - $user_arch); - die(user_message(__x("No config specified. No app match for {search}", - search => $search))); - } - my ($app, $deps) = $config->match_app($self->{desc}, $label, $user_arch); # Nothing to do if it's already installed diff --git a/v2v/virt-v2v.pl b/v2v/virt-v2v.pl index ea89e48..d9f6bb3 100755 --- a/v2v/virt-v2v.pl +++ b/v2v/virt-v2v.pl @@ -217,8 +217,7 @@ GetOptions ("help|?" => sub { ) or pod2usage(2); # Read the config file if one was given -my $config; -$config = Sys::VirtV2V::Config->new($config_file) if defined($config_file); +my $config = Sys::VirtV2V::Config->new($config_file); my $target; if ($output_method eq "libvirt") { @@ -302,7 +301,7 @@ my $storage = $conn->get_storage_paths(); # Create the transfer iso if required my $transferiso; -$transferiso = $config->get_transfer_iso() if (defined($config)); +$transferiso = $config->get_transfer_iso(); if ($output_method eq 'rhev') { $) = "36 36"; -- 1.6.6.1
Matthew Booth
2010-May-04 19:07 UTC
[Libguestfs] [PATCH 2/2] Config: Add --bridge and --network command line options
This change allows a default network mapping to be specified on the command line which will be used if no mapping was found in the configuration file, or if no configuration file was specified. This change makes it possible to convert all guests except Xen PV guests without a config file. Xen PV guests will require a config file to specify replacement kernels. Guests with an old kernel that don't support VirtIO still need the config file to enable VirtIO support. --- lib/Sys/VirtV2V/Config.pm | 20 +++++++++++++++ v2v/virt-v2v.pl | 57 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/lib/Sys/VirtV2V/Config.pm b/lib/Sys/VirtV2V/Config.pm index 50c613c..5aa9917 100644 --- a/lib/Sys/VirtV2V/Config.pm +++ b/lib/Sys/VirtV2V/Config.pm @@ -312,6 +312,10 @@ sub map_network } unless (defined($mapping)) { + # Return the default if it was specified + return @{$self->{default_net_mapping}} + if (defined($self->{default_net_mapping})); + print STDERR user_message(__x("WARNING: No mapping found for ". "{type} interface {name} in config ". "file. The converted guest may not ". @@ -346,6 +350,22 @@ sub map_network return ($newname, $newtype); } +=item set_default_net_mapping(name, type) + +Set the default network name and type which will be used if no specific mapping +can be found in a config file. + +=cut + +sub set_default_net_mapping +{ + my $self = shift; + + my ($name, $type) = @_; + + $self->{default_net_mapping} = [ $name, $type ]; +} + =back =head1 COPYRIGHT diff --git a/v2v/virt-v2v.pl b/v2v/virt-v2v.pl index d9f6bb3..8395d93 100755 --- a/v2v/virt-v2v.pl +++ b/v2v/virt-v2v.pl @@ -178,6 +178,28 @@ my $config_file; Load the virt-v2v configuration from I<file>. There is no default. +=cut + +my $network; + +=item B<-n network> | B<--network network> + +Map all guest bridges or networks which don't have a mapping in the +configuration file to I<network>. + +This option cannot be used in conjunction with I<--bridge>. + +=cut + +my $bridge; + +=item B<-b bridge> | B<--bridge bridge> + +Map all guest bridges or networks which don't have a mapping in the +configuration file to I<bridge>. + +This option cannot be used in conjunction with I<--network>. + =item B<--help> Display brief help. @@ -213,12 +235,34 @@ GetOptions ("help|?" => sub { "oc=s" => \$output_uri, "op=s" => \$output_pool, "osd=s" => \$output_storage_domain, - "f|config=s" => \$config_file + "f|config=s" => \$config_file, + "n|network=s" => sub { + my (undef, $value) = @_; + + pod2usage({ -message => __("--network may only be specified ". + "once"), + -exitval => 1 }) if (defined($network)); + $network = $value; + }, + "b|bridge=s" => sub { + my (undef, $value) = @_; + + pod2usage({ -message => __("--bridge may only be specified ". + "once"), + -exitval => 1 }) if (defined($bridge)); + $bridge = $value; + }, ) or pod2usage(2); # Read the config file if one was given my $config = Sys::VirtV2V::Config->new($config_file); +if (defined($network)) { + $config->set_default_net_mapping($network, 'network'); +} elsif (defined($bridge)) { + $config->set_default_net_mapping($bridge, 'bridge'); +} + my $target; if ($output_method eq "libvirt") { pod2usage({ -message => __("You must specify an output storage pool ". @@ -509,7 +553,8 @@ be created. virt-v2v.conf should specify: =item * -a mapping for the guest's network configuration. +a mapping for the guest's network configuration, unless a default was specified +on the command line with I<--bridge> or I<--network>. =item * @@ -566,8 +611,9 @@ converted. =back -virt-v2v.conf should specify a mapping for the guest's network configuration. -See L<virt-v2v.conf(5)> for details. +virt-v2v.conf should specify a mapping for the guest's network configuration, +unless a default was specified on the command line with I<--bridge> or +I<--network>. See L<virt-v2v.conf(5)> for details. =head3 Authenticating to the ESX server @@ -632,7 +678,8 @@ virt-v2v -f virt-v2v.conf -o rhev -osd <export_sd> <domain> =back Ensure that I<virt-v2v.conf> contains a correct network mapping for your target -RHEV configuration. +RHEV configuration, or that you have specified a default mapping on the command +line with either I<--bridge> or I<--network>. =head1 RUNNING THE CONVERTED GUEST -- 1.6.6.1
Richard W.M. Jones
2010-May-06 10:42 UTC
[Libguestfs] [PATCH 1/2] Config: NFC: always create and pass round a Config object
ACK to both, although it seems like these are already upstream? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
Apparently Analagous Threads
- [PATCH] Move all interaction with the config file into Sys::VirtV2V::Config
- [PATCH] Fix error in Converter::Windows when there is no transfer iso
- [PATCH] Config: Don't require all referenced software to be available
- [PATCH v2v] Pre-convert Windows guests.
- [PATCH] List all missing dependencies at once