[PATCH v2 01/10] hyperv: ambiguous VM names will throw an error

Matt Coleman mcoleman at datto.com
Thu Jan 14 13:03:32 UTC 2021


Since Hyper-V allows multiple VMs to be created with the same name,
some commands produce unpredictable results due to
hypervDomainLookupByName's WMI query selecting the wrong domain.

For example, this prevents `virsh dumpxml` from outputting XML for the
wrong domain.

Signed-off-by: Matt Coleman <matt at datto.com>
---
 include/libvirt/virterror.h | 1 +
 src/hyperv/hyperv_driver.c  | 7 +++++++
 src/util/virerror.c         | 6 ++++--
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h
index b96fe250aa..524a7bf9e8 100644
--- a/include/libvirt/virterror.h
+++ b/include/libvirt/virterror.h
@@ -333,6 +333,7 @@ typedef enum {
     VIR_ERR_NO_NETWORK_PORT = 107,      /* network port not found */
     VIR_ERR_NO_HOSTNAME = 108,          /* no domain's hostname found */
     VIR_ERR_CHECKPOINT_INCONSISTENT = 109, /* checkpoint can't be used */
+    VIR_ERR_MULTIPLE_DOMAINS = 110,     /* more than one matching domain found */
 
 # ifdef VIR_ENUM_SENTINELS
     VIR_ERR_NUMBER_LAST
diff --git a/src/hyperv/hyperv_driver.c b/src/hyperv/hyperv_driver.c
index 1200bf02ff..2e18ef6691 100644
--- a/src/hyperv/hyperv_driver.c
+++ b/src/hyperv/hyperv_driver.c
@@ -1118,6 +1118,13 @@ hypervDomainLookupByName(virConnectPtr conn, const char *name)
     if (hypervGetVirtualSystemByName(priv, name, &computerSystem) < 0)
         goto cleanup;
 
+    if (computerSystem->next) {
+        virReportError(VIR_ERR_MULTIPLE_DOMAINS,
+                       _("Multiple domains exist with the name '%s': repeat the request using a UUID"),
+                       name);
+        goto cleanup;
+    }
+
     hypervMsvmComputerSystemToDomain(conn, computerSystem, &domain);
 
  cleanup:
diff --git a/src/util/virerror.c b/src/util/virerror.c
index 9e3bad97eb..14054add41 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -1229,8 +1229,10 @@ static const virErrorMsgTuple virErrorMsgStrings[] = {
         N_("no hostname found: %s") },
     [VIR_ERR_CHECKPOINT_INCONSISTENT] = {
         N_("checkpoint inconsistent"),
-        N_("checkpoint inconsistent: %s")
-    },
+        N_("checkpoint inconsistent: %s") },
+    [VIR_ERR_MULTIPLE_DOMAINS] = {
+        N_("multiple matching domains found"),
+        N_("multiple matching domains found: %s") },
 };
 
 G_STATIC_ASSERT(G_N_ELEMENTS(virErrorMsgStrings) == VIR_ERR_NUMBER_LAST);
-- 
2.27.0





More information about the libvir-list mailing list