If you use the VDDK library to access virtual machines remotely, you
really need to know the Managed Object Reference ("moref") of the VM.
This must be passed each time you connect to the API.
For example nbdkit's VDDK plugin requires a moref to be passed to
mount up a VM's disk remotely:
nbdkit vddk user=root password=+/tmp/rootpw \
server=esxi.example.com thumbprint=xx:xx:xx:... \
vm=moref=2 \
file="[datastore1] Fedora/Fedora.vmdk"
Getting the moref is a huge pain. To get some idea of what it is, why
it is needed, and how much trouble it is to get it, see:
https://blogs.vmware.com/vsphere/2012/02/uniquely-identifying-virtual-machines-in-vsphere-and-vcloud-part-1-overview.html
https://blogs.vmware.com/vsphere/2012/02/uniquely-identifying-virtual-machines-in-vsphere-and-vcloud-part-2-technical.html
However the moref is available conveniently in the internals of the
libvirt VMX driver. This patch exposes it as a custom XML element
using the same "vmware:" namespace which was previously used for the
datacenterpath (see libvirt commit
636a99058758a044).
It appears in the XML like this:
<domain type='vmware' xmlns:vmware='http://libvirt.org/schemas/domain/vmware/1.0'>
<name>Fedora</name>
...
<vmware:datacenterpath>ha-datacenter</vmware:datacenterpath>
<vmware:moref>2</vmware:moref>
</domain>
Note that the moref can appear as either a simple ID (for esx://
connections) or as a "vm-<ID>" (for vpx:// connections). It should be
treated by users as an opaque string.
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
esxVI_ObjectContent *virtualMachine = NULL;
esxVI_VirtualMachinePowerState powerState;
int id;
+ char *moref = NULL;
char *vmPathName = NULL;
char *datastoreName = NULL;
char *directoryName = NULL;
esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
propertyNameList, &virtualMachine,
esxVI_Occurrence_RequiredItem) < 0 ||
+ esxVI_GetVirtualMachineMORef(virtualMachine, &moref) < 0 ||
esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0 ||
esxVI_GetVirtualMachineIdentity(virtualMachine, &id, NULL, NULL) < 0 ||
esxVI_GetStringValue(virtualMachine, "config.files.vmPathName",
ctx.formatFileName = NULL;
ctx.autodetectSCSIControllerModel = NULL;
ctx.datacenterPath = priv->primary->datacenterPath;
+ ctx.moref = moref;
def = virVMXParseConfig(&ctx, priv->xmlopt, priv->caps, vmx);
esxVI_String_Free(&propertyNameList);
esxVI_ObjectContent_Free(&virtualMachine);
+ VIR_FREE(moref);
VIR_FREE(datastoreName);
VIR_FREE(directoryName);
VIR_FREE(directoryAndFileName);
ctx.formatFileName = NULL;
ctx.autodetectSCSIControllerModel = NULL;
ctx.datacenterPath = NULL;
+ ctx.moref = NULL;
def = virVMXParseConfig(&ctx, priv->xmlopt, priv->caps, nativeConfig);
ctx.formatFileName = esxFormatVMXFileName;
ctx.autodetectSCSIControllerModel = esxAutodetectSCSIControllerModel;
ctx.datacenterPath = NULL;
+ ctx.moref = NULL;
vmx = virVMXFormatConfig(&ctx, priv->xmlopt, def, virtualHW_version);
ctx.formatFileName = esxFormatVMXFileName;
ctx.autodetectSCSIControllerModel = esxAutodetectSCSIControllerModel;
ctx.datacenterPath = NULL;
+ ctx.moref = NULL;
vmx = virVMXFormatConfig(&ctx, priv->xmlopt, def, virtualHW_version);
}
+int
+esxVI_GetVirtualMachineMORef(esxVI_ObjectContent *virtualMachine,
+ char **moref)
+{
+ for (; virtualMachine != NULL; virtualMachine = virtualMachine->_next) {
+ if (virtualMachine->obj &&
+ STREQ(virtualMachine->obj->type, "VirtualMachine") &&
+ virtualMachine->obj->value) {
+ return VIR_STRDUP(*moref, virtualMachine->obj->value);
+ }
+ }
+ return -1;
+}
int
esxVI_GetBoolean(esxVI_ObjectContent *objectContent, const char *propertyName,
(esxVI_ObjectContent *virtualMachine,
esxVI_VirtualMachineQuestionInfo **questionInfo);
+int esxVI_GetVirtualMachineMORef
+ (esxVI_ObjectContent *virtualMachine,
+ char **moref);
+
int esxVI_GetBoolean(esxVI_ObjectContent *objectContent,
const char *propertyName,
esxVI_Boolean *value, esxVI_Occurrence occurrence);
VIR_DOMAIN_DEF_FEATURE_NAME_SLASH),
};
+struct virVMXDomainDefNamespaceData {
+ char *datacenterPath;
+ char *moref;
+};
+
static void
virVMXDomainDefNamespaceFree(void *nsdata)
{
- VIR_FREE(nsdata);
+ struct virVMXDomainDefNamespaceData *data = nsdata;
+
+ if (data) {
+ VIR_FREE(data->datacenterPath);
+ VIR_FREE(data->moref);
+ }
+ VIR_FREE(data);
}
static int
virVMXDomainDefNamespaceFormatXML(virBufferPtr buf, void *nsdata)
{
- const char *datacenterPath = nsdata;
+ struct virVMXDomainDefNamespaceData *data = nsdata;
- if (!datacenterPath)
+ if (!data)
return 0;
- virBufferAddLit(buf, "<vmware:datacenterpath>");
- virBufferEscapeString(buf, "%s", datacenterPath);
- virBufferAddLit(buf, "</vmware:datacenterpath>\n");
+ if (data->datacenterPath) {
+ virBufferAddLit(buf, "<vmware:datacenterpath>");
+ virBufferEscapeString(buf, "%s", data->datacenterPath);
+ virBufferAddLit(buf, "</vmware:datacenterpath>\n");
+ }
+ if (data->moref) {
+ virBufferAddLit(buf, "<vmware:moref>");
+ virBufferEscapeString(buf, "%s", data->moref);
+ virBufferAddLit(buf, "</vmware:moref>\n");
+ }
return 0;
}
bool hgfs_disabled = true;
long long sharedFolder_maxNum = 0;
int cpumasklen;
- char *namespaceData;
if (ctx->parseFileName == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
}
/* ctx:datacenterPath -> def:namespaceData */
- if (ctx->datacenterPath) {
- if (VIR_STRDUP(namespaceData, ctx->datacenterPath) < 0)
+ if (ctx->datacenterPath || ctx->moref) {
+ struct virVMXDomainDefNamespaceData *nsdata = NULL;
+
+ if (VIR_ALLOC(nsdata) < 0 ||
+ VIR_STRDUP(nsdata->datacenterPath, ctx->datacenterPath) < 0 ||
+ VIR_STRDUP(nsdata->moref, ctx->moref) < 0) {
+ virVMXDomainDefNamespaceFree(nsdata);
goto cleanup;
+ }
def->ns = *virDomainXMLOptionGetNamespace(xmlopt);
- def->namespaceData = namespaceData;
+ def->namespaceData = nsdata;
}
if (virDomainDefPostParse(def, caps, VIR_DOMAIN_DEF_PARSE_ABI_UPDATE,
* formatFileName is only used by virVMXFormatConfig.
* autodetectSCSIControllerModel is optionally used by virVMXFormatConfig.
* datacenterPath is only used by virVMXFormatConfig.
+ * moref is only used by virVMXFormatConfig.
*/
struct _virVMXContext {
void *opaque;
virVMXFormatFileName formatFileName;
virVMXAutodetectSCSIControllerModel autodetectSCSIControllerModel;
const char *datacenterPath; /* including folders */
+ const char *moref;
};