]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Add uniqness checking for LXC define/create methods
authorDaniel P. Berrange <berrange@redhat.com>
Fri, 31 Jul 2009 14:25:03 +0000 (15:25 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 5 Aug 2009 11:01:04 +0000 (12:01 +0100)
* src/lxc_driver.c: Check for name & UUID uniqueness when
  defining or creating domains

src/lxc_driver.c

index d62c2d7e5495cb6012d0ef7657492c0d48c4f7c1..a9c4f79ca8ebc39ba8a63ef59aaca4fbcb0649e5 100644 (file)
@@ -311,6 +311,35 @@ static virDomainPtr lxcDomainDefine(virConnectPtr conn, const char *xml)
                                         VIR_DOMAIN_XML_INACTIVE)))
         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);
+            lxcError(conn, 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);
+            lxcError(conn, NULL, VIR_ERR_OPERATION_FAILED,
+                     _("domain '%s' is already defined with uuid %s"),
+                     def->name, uuidstr);
+            goto cleanup;
+        }
+    }
+
     if ((def->nets != NULL) && !(driver->have_netns)) {
         lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
                  "%s", _("System lacks NETNS support"));
@@ -1082,6 +1111,39 @@ lxcDomainCreateAndStart(virConnectPtr conn,
                                         VIR_DOMAIN_XML_INACTIVE)))
         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);
+            lxcError(conn, 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 (virDomainIsActive(vm)) {
+            lxcError(conn, 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);
+            lxcError(conn, NULL, VIR_ERR_OPERATION_FAILED,
+                     _("domain '%s' is already defined with uuid %s"),
+                     def->name, uuidstr);
+            goto cleanup;
+        }
+    }
+
     if ((def->nets != NULL) && !(driver->have_netns)) {
         lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
                  "%s", _("System lacks NETNS support"));