[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 1/4] Check that we're not overwriting an existing Libvirt domain



Exit with an error if we would overwrite an existing libvirt domain.

Fixes RHBZ#617110
---
 lib/Sys/VirtV2V/Connection/LibVirt.pm |    4 ++++
 lib/Sys/VirtV2V/Target/LibVirt.pm     |   31 ++++++++++++++++++++++++++++++-
 lib/Sys/VirtV2V/Target/RHEV.pm        |   11 +++++++++++
 3 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/lib/Sys/VirtV2V/Connection/LibVirt.pm b/lib/Sys/VirtV2V/Connection/LibVirt.pm
index b83a10d..51331da 100644
--- a/lib/Sys/VirtV2V/Connection/LibVirt.pm
+++ b/lib/Sys/VirtV2V/Connection/LibVirt.pm
@@ -79,6 +79,10 @@ sub new
     $self->{uri} = URI->new($uri);
     $self->{name} = $name;
 
+    # Check that the guest doesn't already exist on the target
+    die(user_message(__x("Domain {name} already exists on the target.",
+                         name => $name))) if ($target->guest_exists($name));
+
     # Parse uri authority for hostname and username
     $self->{uri}->authority() =~ /^(?:([^:]*)(?::([^ ]*))?@)?(.*)$/
         or die(user_message(__x("Unable to parse URI authority: {auth}",
diff --git a/lib/Sys/VirtV2V/Target/LibVirt.pm b/lib/Sys/VirtV2V/Target/LibVirt.pm
index 574590c..279e93d 100644
--- a/lib/Sys/VirtV2V/Target/LibVirt.pm
+++ b/lib/Sys/VirtV2V/Target/LibVirt.pm
@@ -145,8 +145,10 @@ sub close
 
 package Sys::VirtV2V::Target::LibVirt;
 
-use Sys::VirtV2V::Util qw(user_message);
+use Sys::Virt;
+use Sys::Virt::Error;
 
+use Sys::VirtV2V::Util qw(user_message);
 use Locale::TextDomain 'virt-v2v';
 
 =head1 NAME
@@ -288,6 +290,33 @@ sub get_volume
     return Sys::VirtV2V::Target::LibVirt::Vol->_get($self->{pool}, $name);
 }
 
+=item guest_exists(name)
+
+Return 1 if a guest with I<name> already exists, 0 otherwise.
+
+=cut
+
+sub guest_exists
+{
+    my $self = shift;
+    my ($name) = @_;
+
+    eval {
+        $self->{vmm}->get_domain_by_name($name);
+    };
+
+    if ($@) {
+        if ($ ->code == Sys::Virt::Error::ERR_NO_DOMAIN) {
+            return 0;
+        }
+
+        die(user_message(__x("Error checking for domain: {error}",
+                             error => $ ->stringify())));
+    }
+
+    return 1;
+}
+
 =item create_guest(dom)
 
 Create the guest in the target
diff --git a/lib/Sys/VirtV2V/Target/RHEV.pm b/lib/Sys/VirtV2V/Target/RHEV.pm
index 574b022..218cf63 100644
--- a/lib/Sys/VirtV2V/Target/RHEV.pm
+++ b/lib/Sys/VirtV2V/Target/RHEV.pm
@@ -618,6 +618,17 @@ sub get_volume
     die("Cannot retrieve an existing RHEV storage volume by name");
 }
 
+=item guest_exists(name)
+
+This always returns 0 for a RHEV target.
+
+=cut
+
+sub guest_exists
+{
+    return 0;
+}
+
 =item create_guest(dom)
 
 Create the guest in the target
-- 
1.7.2.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]