[libvirt] [PATCH 2/3] qemu: Break out function to check if we can create/define/restore

Cole Robinson crobinso at redhat.com
Mon Nov 2 19:52:38 UTC 2009


Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 src/qemu/qemu_driver.c |  151 +++++++++++++++++++-----------------------------
 1 files changed, 59 insertions(+), 92 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 7eed356..e038887 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -2594,6 +2594,59 @@ cleanup:
     return dom;
 }
 
+static int
+qemudDomainCanCreate(virConnectPtr conn,
+                     virDomainDefPtr def,
+                     unsigned int check_active)
+{
+    struct qemud_driver *driver = conn->privateData;
+    int ret = 0;
+    virDomainObjPtr vm = NULL;
+
+    /* See if a VM with matching UUID already exists */
+    vm = virDomainFindByUUID(&driver->domains, def->uuid);
+    if (vm) {
+        /* UUID matches, but if names don't match, refuse it */
+        if (STRNEQ(vm->def->name, def->name)) {
+            char uuidstr[VIR_UUID_STRING_BUFLEN];
+            virUUIDFormat(vm->def->uuid, uuidstr);
+            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
+                             _("domain '%s' is already defined with uuid %s"),
+                             vm->def->name, uuidstr);
+            goto cleanup;
+        }
+
+        if (check_active) {
+            /* UUID & name match, but if VM is already active, refuse it */
+            if (virDomainObjIsActive(vm)) {
+                qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_INVALID,
+                                 _("domain is already active as '%s'"),
+                                 vm->def->name);
+                goto cleanup;
+            }
+        }
+
+        virDomainObjUnlock(vm);
+    } else {
+        /* UUID does not match, but if a name matches, refuse it */
+        vm = virDomainFindByName(&driver->domains, def->name);
+        if (vm) {
+            char uuidstr[VIR_UUID_STRING_BUFLEN];
+            virUUIDFormat(vm->def->uuid, uuidstr);
+            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
+                             _("domain '%s' already exists with uuid %s"),
+                             def->name, uuidstr);
+            goto cleanup;
+        }
+    }
+
+    ret = 1;
+cleanup:
+    if (vm)
+        virDomainObjUnlock(vm);
+    return ret;
+}
+
 static int qemudGetVersion(virConnectPtr conn, unsigned long *version) {
     struct qemud_driver *driver = conn->privateData;
     int ret = -1;
@@ -2648,38 +2701,8 @@ static virDomainPtr qemudDomainCreate(virConnectPtr conn, const char *xml,
     if (virSecurityDriverVerify(conn, def) < 0)
         goto cleanup;
 
-    /* See if a VM with matching UUID already exists */
-    vm = virDomainFindByUUID(&driver->domains, def->uuid);
-    if (vm) {
-        /* UUID matches, but if names don't match, refuse it */
-        if (STRNEQ(vm->def->name, def->name)) {
-            char uuidstr[VIR_UUID_STRING_BUFLEN];
-            virUUIDFormat(vm->def->uuid, uuidstr);
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
-                             _("domain '%s' is already defined with uuid %s"),
-                             vm->def->name, uuidstr);
-            goto cleanup;
-        }
-
-        /* UUID & name match, but if VM is already active, refuse it */
-        if (virDomainObjIsActive(vm)) {
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
-                             _("domain is already active as '%s'"), vm->def->name);
-            goto cleanup;
-        }
-        virDomainObjUnlock(vm);
-    } else {
-        /* UUID does not match, but if a name matches, refuse it */
-        vm = virDomainFindByName(&driver->domains, def->name);
-        if (vm) {
-            char uuidstr[VIR_UUID_STRING_BUFLEN];
-            virUUIDFormat(vm->def->uuid, uuidstr);
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
-                             _("domain '%s' is already defined with uuid %s"),
-                             def->name, uuidstr);
-            goto cleanup;
-        }
-    }
+    if (!qemudDomainCanCreate(conn, def, 1))
+        goto cleanup;
 
     if (!(vm = virDomainAssignDef(conn,
                                   driver->caps,
@@ -3709,38 +3732,8 @@ static int qemudDomainRestore(virConnectPtr conn,
         goto cleanup;
     }
 
-    /* See if a VM with matching UUID already exists */
-    vm = virDomainFindByUUID(&driver->domains, def->uuid);
-    if (vm) {
-        /* UUID matches, but if names don't match, refuse it */
-        if (STRNEQ(vm->def->name, def->name)) {
-            char uuidstr[VIR_UUID_STRING_BUFLEN];
-            virUUIDFormat(vm->def->uuid, uuidstr);
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
-                             _("domain '%s' is already defined with uuid %s"),
-                             vm->def->name, uuidstr);
-            goto cleanup;
-        }
-
-        /* UUID & name match, but if VM is already active, refuse it */
-        if (virDomainObjIsActive(vm)) {
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_INVALID,
-                             _("domain is already active as '%s'"), vm->def->name);
-            goto cleanup;
-        }
-        virDomainObjUnlock(vm);
-    } else {
-        /* UUID does not match, but if a name matches, refuse it */
-        vm = virDomainFindByName(&driver->domains, def->name);
-        if (vm) {
-            char uuidstr[VIR_UUID_STRING_BUFLEN];
-            virUUIDFormat(vm->def->uuid, uuidstr);
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
-                             _("domain '%s' is already defined with uuid %s"),
-                             def->name, uuidstr);
-            goto cleanup;
-        }
-    }
+    if (!qemudDomainCanCreate(conn, def, 1))
+        goto cleanup;
 
     if (!(vm = virDomainAssignDef(conn,
                                   driver->caps,
@@ -4197,34 +4190,8 @@ static virDomainPtr qemudDomainDefine(virConnectPtr conn, const char *xml) {
     if (virSecurityDriverVerify(conn, def) < 0)
         goto cleanup;
 
-    /* See if a VM with matching UUID already exists */
-    vm = virDomainFindByUUID(&driver->domains, def->uuid);
-    if (vm) {
-        /* UUID matches, but if names don't match, refuse it */
-        if (STRNEQ(vm->def->name, def->name)) {
-            char uuidstr[VIR_UUID_STRING_BUFLEN];
-            virUUIDFormat(vm->def->uuid, uuidstr);
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
-                             _("domain '%s' is already defined with uuid %s"),
-                             vm->def->name, uuidstr);
-            goto cleanup;
-        }
-
-        /* UUID & name match */
-        virDomainObjUnlock(vm);
-        newVM = 0;
-    } else {
-        /* UUID does not match, but if a name matches, refuse it */
-        vm = virDomainFindByName(&driver->domains, def->name);
-        if (vm) {
-            char uuidstr[VIR_UUID_STRING_BUFLEN];
-            virUUIDFormat(vm->def->uuid, uuidstr);
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
-                             _("domain '%s' is already defined with uuid %s"),
-                             def->name, uuidstr);
-            goto cleanup;
-        }
-    }
+    if (!qemudDomainCanCreate(conn, def, 0))
+        goto cleanup;
 
     if (qemudCanonicalizeMachine(driver, def) < 0)
         goto cleanup;
-- 
1.6.5.1




More information about the libvir-list mailing list