]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
hyperv: ensure we don't define multiple domains with the same name
authorJonathon Jongsma <jjongsma@redhat.com>
Fri, 27 Feb 2026 17:56:00 +0000 (11:56 -0600)
committerJonathon Jongsma <jjongsma@redhat.com>
Fri, 6 Mar 2026 16:21:00 +0000 (10:21 -0600)
Right now if we run `virsh define domain.xml` multiple times, it will
result in multiple domains being defined with the same name. This
violates libvirt assumptions about name uniqueness, so prevent this from
happening by returning an error.

There's not much we can do about vms that may have been created outside
of libvirt that might have the same name (unless we switch to using
something like the UUID as the name for hyperv domains, which would
not be very user-friendly), but at least we can not contribute to the
problem.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
src/hyperv/hyperv_driver.c

index c9f1c11617ce19e65d7446c0f3eeb1b564c1cd81..0dad27f0ecb0e94a4b090268e1d6885739431942 100644 (file)
@@ -2953,6 +2953,13 @@ hypervDomainDefineXML(virConnectPtr conn, const char *xml)
         return NULL;
     }
 
+    /* abort if a domain with this name already exists */
+    if (hypervGetVirtualSystemByName(priv, def->name, &existing) == 0 &&
+        existing != NULL) {
+        virReportError(VIR_ERR_DOM_EXIST, "%s", def->name);
+        return NULL;
+    }
+
     /* prepare params: only set the VM's name for now */
     params = hypervCreateInvokeParamsList("DefineSystem",
                                           MSVM_VIRTUALSYSTEMMANAGEMENTSERVICE_SELECTOR,