From: Mark Cave-Ayland Date: Fri, 18 Jul 2025 08:44:38 +0000 (+0100) Subject: conf: introduce hardware UUID (hwuuid) element X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5d2235ff45103f75a650313a7e7824f105cb8397;p=thirdparty%2Flibvirt.git conf: introduce hardware UUID (hwuuid) element The hardware UUID (hwuuid) element provides a mechanism to supply an external UUID to the guest, as opposed to the libvirt domain UUID. This is to allow for the scenario whereby a domain can be stopped, cloned and then started as a new domain without altering the guest-visible UUID. Add the element, documentation and core code for the hwuuid feature along with an implementation for the QEMU driver. Reviewed-by: Daniel P. Berrangé Signed-off-by: Mark Cave-Ayland --- diff --git a/docs/formatdomain.rst b/docs/formatdomain.rst index 976746e292..6e9b8c84a2 100644 --- a/docs/formatdomain.rst +++ b/docs/formatdomain.rst @@ -54,6 +54,13 @@ General metadata :since:`Since 0.8.7`, it is also possible to provide the UUID via a `SMBIOS System Information`_ specification. +``hwuuid`` + The optional ``hwuuid`` element can be used to supply an alternative UUID for + identifying the virtual machine from the domain ``uuid`` above. The difference + between using the ``hwuuid`` element and simply providing an alternative UUID + via a `SMBIOS System Information`_ specification is that the ``hwuuid`` affects + all devices that expose the UUID to the guest. + :since:`Since 11.6.0 QEMU/KVM only` ``genid`` :since:`Since 4.4.0`, the ``genid`` element can be used to add a Virtual Machine Generation ID which exposes a 128-bit, cryptographically random, diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 59958c2f08..cb096f2e1e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -13079,6 +13079,7 @@ static int virSysinfoSystemParseXML(xmlNodePtr node, xmlXPathContextPtr ctxt, virSysinfoSystemDef **sysdef, + unsigned char *hwUUID, unsigned char *domUUID, bool uuid_generated) { @@ -13103,10 +13104,18 @@ virSysinfoSystemParseXML(xmlNodePtr node, } if (uuid_generated) { memcpy(domUUID, uuidbuf, VIR_UUID_BUFLEN); - } else if (memcmp(domUUID, uuidbuf, VIR_UUID_BUFLEN) != 0) { - virReportError(VIR_ERR_XML_DETAIL, "%s", + } else if (virUUIDIsValid(hwUUID)) { + if (memcmp(hwUUID, uuidbuf, VIR_UUID_BUFLEN) != 0) { + virReportError(VIR_ERR_XML_DETAIL, "%s", + _("UUID mismatch between and ")); + return -1; + } + } else { + if (memcmp(domUUID, uuidbuf, VIR_UUID_BUFLEN) != 0) { + virReportError(VIR_ERR_XML_DETAIL, "%s", _("UUID mismatch between and ")); - return -1; + return -1; + } } /* Although we've validated the UUID as good, virUUIDParse() is * lax with respect to allowing extraneous "-" and " ", but the @@ -13244,6 +13253,7 @@ virSysinfoChassisParseXML(xmlNodePtr node, static int virSysinfoParseSMBIOSDef(virSysinfoDef *def, xmlXPathContextPtr ctxt, + unsigned char *hwUUID, unsigned char *domUUID, bool uuid_generated) { @@ -13257,7 +13267,7 @@ virSysinfoParseSMBIOSDef(virSysinfoDef *def, /* Extract system related metadata */ if ((tmpnode = virXPathNode("./system[1]", ctxt)) != NULL) { - if (virSysinfoSystemParseXML(tmpnode, ctxt, &def->system, + if (virSysinfoSystemParseXML(tmpnode, ctxt, &def->system, hwUUID, domUUID, uuid_generated) < 0) return -1; } @@ -13344,6 +13354,7 @@ virSysinfoParseFWCfgDef(virSysinfoDef *def, static virSysinfoDef * virSysinfoParseXML(xmlNodePtr node, xmlXPathContextPtr ctxt, + unsigned char *hwUUID, unsigned char *domUUID, bool uuid_generated) { @@ -13358,7 +13369,8 @@ virSysinfoParseXML(xmlNodePtr node, switch (def->type) { case VIR_SYSINFO_SMBIOS: - if (virSysinfoParseSMBIOSDef(def, ctxt, domUUID, uuid_generated) < 0) + if (virSysinfoParseSMBIOSDef(def, ctxt, hwUUID, domUUID, + uuid_generated) < 0) return NULL; break; @@ -18702,6 +18714,21 @@ virDomainDefParseIDs(virDomainDef *def, VIR_FREE(tmp); } + /* Extract hardware uuid (optional). For some use cases e.g. cloning a + * domain from a snapshot, the hardware uuid must remain constant and + * separate from the domain uuid. */ + tmp = virXPathString("string(./hwuuid[1])", ctxt); + if (tmp) { + if (virUUIDParse(tmp, def->hw_uuid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("malformed hwuuid element")); + return -1; + } + VIR_FREE(tmp); + } else { + memset(def->hw_uuid, 0, VIR_UUID_BUFLEN); + } + /* Extract domain genid - a genid can either be provided or generated */ if ((n = virXPathNodeSet("./genid", ctxt, &nodes)) < 0) return -1; @@ -20167,6 +20194,7 @@ virDomainDefParseXML(xmlXPathContextPtr ctxt, for (i = 0; i < n; i++) { virSysinfoDef *sysinfo = virSysinfoParseXML(nodes[i], ctxt, + def->hw_uuid, def->uuid, uuid_generated); if (!sysinfo) @@ -28955,6 +28983,11 @@ virDomainDefFormatInternalSetRootName(virDomainDef *def, virUUIDFormat(uuid, uuidstr); virBufferAsprintf(buf, "%s\n", uuidstr); + if (virUUIDIsValid(def->hw_uuid)) { + virUUIDFormat(def->hw_uuid, uuidstr); + virBufferAsprintf(buf, "%s\n", uuidstr); + } + if (def->genidRequested) { char genidstr[VIR_UUID_STRING_BUFLEN]; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 596d138973..984e5bfc76 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3137,6 +3137,7 @@ struct _virDomainDef { int virtType; /* enum virDomainVirtType */ int id; unsigned char uuid[VIR_UUID_BUFLEN]; + unsigned char hw_uuid[VIR_UUID_BUFLEN]; unsigned char genid[VIR_UUID_BUFLEN]; bool genidRequested; diff --git a/src/conf/schemas/domaincommon.rng b/src/conf/schemas/domaincommon.rng index a714c3fcc5..9782dca147 100644 --- a/src/conf/schemas/domaincommon.rng +++ b/src/conf/schemas/domaincommon.rng @@ -47,6 +47,11 @@ + + + + + diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 457dee7029..4e4f1e87eb 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -10768,7 +10768,11 @@ qemuBuildCommandLine(virDomainObj *vm, qemuBuildNumaCommandLine(cfg, def, cmd, priv) < 0) return NULL; - virUUIDFormat(def->uuid, uuid); + if (virUUIDIsValid(def->hw_uuid)) { + virUUIDFormat(def->hw_uuid, uuid); + } else { + virUUIDFormat(def->uuid, uuid); + } virCommandAddArgList(cmd, "-uuid", uuid, NULL); if (qemuBuildSmbiosCommandLine(cmd, driver, def) < 0)