On 2/27/26 1:07 PM, Jonathon Jongsma via Devel wrote:
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 <[email protected]>
---
src/hyperv/hyperv_driver.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/src/hyperv/hyperv_driver.c b/src/hyperv/hyperv_driver.c
index 6e9917f92a..f717499ba9 100644
--- a/src/hyperv/hyperv_driver.c
+++ b/src/hyperv/hyperv_driver.c
@@ -2952,6 +2952,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);
HA!! So in your new use of VIR_ERR_DOM_EXIST you've used it correctly
(just sending the name of the domain in the string). But now you've got
two cases where the error condition is that a conflicting domain already
exists, but one uses the specific error code and the other uses a
generic error code. So what's the use of the error code then, if some
cases of a "domain already exists" error don't use it?
Anyway, that's a more bike shed discussion, and your patch does fix the
problem at hand with the provided tools, so:
Reviewed-by: Laine Stump <[email protected]>
(I do think the whole VIR_ERR_* thing should be revisited at some point
though)
+ return NULL;
+ }
+
/* prepare params: only set the VM's name for now */
params = hypervCreateInvokeParamsList("DefineSystem",
MSVM_VIRTUALSYSTEMMANAGEMENTSERVICE_SELECTOR,