]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
vmx: Expose datacenter path in domain XML
authorMatthias Bolte <matthias.bolte@googlemail.com>
Fri, 11 Sep 2015 10:00:47 +0000 (12:00 +0200)
committerRichard W.M. Jones <rjones@redhat.com>
Thu, 17 Sep 2015 10:34:54 +0000 (11:34 +0100)
Tool such as libguestfs need the datacenter path to get access to disk
images. The ESX driver knows the correct datacenter path, but this
information cannot be accessed using libvirt API yet. Also, it cannot
be deduced from the connection URI in a robust way.

Expose the datacenter path in the domain XML as <vmware:datacenterpath>
node similar to the way the <qemu:commandline> node works. The new node
is ignored while parsing the domain XML. In contrast to <qemu:commandline>
it is output only.

src/esx/esx_driver.c
src/vmware/vmware_conf.c
src/vmware/vmware_driver.c
src/vmx/vmx.c
src/vmx/vmx.h
tests/vmx2xmldata/vmx2xml-datacenterpath.vmx [new file with mode: 0644]
tests/vmx2xmldata/vmx2xml-datacenterpath.xml [new file with mode: 0644]
tests/vmx2xmltest.c
tests/xml2vmxdata/xml2vmx-datacenterpath.vmx [new file with mode: 0644]
tests/xml2vmxdata/xml2vmx-datacenterpath.xml [new file with mode: 0644]
tests/xml2vmxtest.c

index c304ff355b77274faf790a91078dfea447fc5c69..5944947d93c416b8b5c22a2a9a14ef968cf11579 100644 (file)
@@ -2741,6 +2741,7 @@ esxDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
     ctx.parseFileName = esxParseVMXFileName;
     ctx.formatFileName = NULL;
     ctx.autodetectSCSIControllerModel = NULL;
+    ctx.datacenterPath = priv->primary->datacenterPath;
 
     def = virVMXParseConfig(&ctx, priv->xmlopt, vmx);
 
@@ -2799,6 +2800,7 @@ esxConnectDomainXMLFromNative(virConnectPtr conn, const char *nativeFormat,
     ctx.parseFileName = esxParseVMXFileName;
     ctx.formatFileName = NULL;
     ctx.autodetectSCSIControllerModel = NULL;
+    ctx.datacenterPath = NULL;
 
     def = virVMXParseConfig(&ctx, priv->xmlopt, nativeConfig);
 
@@ -2853,6 +2855,7 @@ esxConnectDomainXMLToNative(virConnectPtr conn, const char *nativeFormat,
     ctx.parseFileName = NULL;
     ctx.formatFileName = esxFormatVMXFileName;
     ctx.autodetectSCSIControllerModel = esxAutodetectSCSIControllerModel;
+    ctx.datacenterPath = NULL;
 
     vmx = virVMXFormatConfig(&ctx, priv->xmlopt, def, virtualHW_version);
 
@@ -3096,6 +3099,7 @@ esxDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
     ctx.parseFileName = NULL;
     ctx.formatFileName = esxFormatVMXFileName;
     ctx.autodetectSCSIControllerModel = esxAutodetectSCSIControllerModel;
+    ctx.datacenterPath = NULL;
 
     vmx = virVMXFormatConfig(&ctx, priv->xmlopt, def, virtualHW_version);
 
index 21cf333a4f3088d7a29c873068254e5f12235516..f3cbbf55547933afddd42bc87908f16454249823 100644 (file)
@@ -145,6 +145,9 @@ vmwareLoadDomains(struct vmware_driver *driver)
     virCommandPtr cmd;
 
     ctx.parseFileName = vmwareCopyVMXFileName;
+    ctx.formatFileName = NULL;
+    ctx.autodetectSCSIControllerModel = NULL;
+    ctx.datacenterPath = NULL;
 
     cmd = virCommandNewArgList(driver->vmrun, "-T",
                                vmwareDriverTypeToString(driver->type),
index ec74fe3831e5ca8018c2bfc22b88fdbd7667a9fd..e228aaa58495fa5feddc4e50721e4332f05b0694 100644 (file)
@@ -381,7 +381,10 @@ vmwareDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int fla
     if (flags & VIR_DOMAIN_DEFINE_VALIDATE)
         parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
 
+    ctx.parseFileName = NULL;
     ctx.formatFileName = vmwareCopyVMXFileName;
+    ctx.autodetectSCSIControllerModel = NULL;
+    ctx.datacenterPath = NULL;
 
     vmwareDriverLock(driver);
     if ((vmdef = virDomainDefParseString(xml, driver->caps, driver->xmlopt,
@@ -671,7 +674,10 @@ vmwareDomainCreateXML(virConnectPtr conn, const char *xml,
     if (flags & VIR_DOMAIN_START_VALIDATE)
         parse_flags |= VIR_DOMAIN_DEF_PARSE_VALIDATE;
 
+    ctx.parseFileName = NULL;
     ctx.formatFileName = vmwareCopyVMXFileName;
+    ctx.autodetectSCSIControllerModel = NULL;
+    ctx.datacenterPath = NULL;
 
     vmwareDriverLock(driver);
 
@@ -1022,6 +1028,9 @@ vmwareConnectDomainXMLFromNative(virConnectPtr conn, const char *nativeFormat,
     }
 
     ctx.parseFileName = vmwareCopyVMXFileName;
+    ctx.formatFileName = NULL;
+    ctx.autodetectSCSIControllerModel = NULL;
+    ctx.datacenterPath = NULL;
 
     def = virVMXParseConfig(&ctx, driver->xmlopt, nativeConfig);
 
index 70cda2aa3c19d7f86ac09f7acfb5edf1dd21c02b..c6d97f840a203a0799486b0041a812a51f95c6e8 100644 (file)
@@ -523,10 +523,11 @@ VIR_ENUM_IMPL(virVMXControllerModelSCSI, VIR_DOMAIN_CONTROLLER_MODEL_SCSI_LAST,
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * Helpers
  */
+
 static int
-vmxDomainDefPostParse(virDomainDefPtr def,
-                      virCapsPtr caps ATTRIBUTE_UNUSED,
-                      void *opaque ATTRIBUTE_UNUSED)
+virVMXDomainDefPostParse(virDomainDefPtr def,
+                         virCapsPtr caps ATTRIBUTE_UNUSED,
+                         void *opaque ATTRIBUTE_UNUSED)
 {
     /* memory hotplug tunables are not supported by this driver */
     if (virDomainDefCheckUnsupportedMemoryHotplug(def) < 0)
@@ -536,27 +537,60 @@ vmxDomainDefPostParse(virDomainDefPtr def,
 }
 
 static int
-vmxDomainDeviceDefPostParse(virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
-                            const virDomainDef *def ATTRIBUTE_UNUSED,
-                            virCapsPtr caps ATTRIBUTE_UNUSED,
-                            void *opaque ATTRIBUTE_UNUSED)
+virVMXDomainDevicesDefPostParse(virDomainDeviceDefPtr dev ATTRIBUTE_UNUSED,
+                                const virDomainDef *def ATTRIBUTE_UNUSED,
+                                virCapsPtr caps ATTRIBUTE_UNUSED,
+                                void *opaque ATTRIBUTE_UNUSED)
 {
     return 0;
 }
 
-virDomainDefParserConfig virVMXDomainDefParserConfig = {
+static virDomainDefParserConfig virVMXDomainDefParserConfig = {
     .hasWideSCSIBus = true,
     .macPrefix = {0x00, 0x0c, 0x29},
-    .devicesPostParseCallback = vmxDomainDeviceDefPostParse,
-    .domainPostParseCallback = vmxDomainDefPostParse,
+    .devicesPostParseCallback = virVMXDomainDevicesDefPostParse,
+    .domainPostParseCallback = virVMXDomainDefPostParse,
 };
 
+static void
+virVMXDomainDefNamespaceFree(void *nsdata)
+{
+    VIR_FREE(nsdata);
+}
+
+static int
+virVMXDomainDefNamespaceFormatXML(virBufferPtr buf, void *nsdata)
+{
+    const char *datacenterPath = nsdata;
+
+    if (!datacenterPath)
+        return 0;
+
+    virBufferAddLit(buf, "<vmware:datacenterpath>");
+    virBufferEscapeString(buf, "%s", datacenterPath);
+    virBufferAddLit(buf, "</vmware:datacenterpath>\n");
+
+    return 0;
+}
+
+static const char *
+virVMXDomainDefNamespaceHref(void)
+{
+    return "xmlns:vmware='http://libvirt.org/schemas/domain/vmware/1.0'";
+}
+
+static virDomainXMLNamespace virVMXDomainXMLNamespace = {
+    .parse = NULL,
+    .free = virVMXDomainDefNamespaceFree,
+    .format = virVMXDomainDefNamespaceFormatXML,
+    .href = virVMXDomainDefNamespaceHref,
+};
 
 virDomainXMLOptionPtr
 virVMXDomainXMLConfInit(void)
 {
-    return virDomainXMLOptionNew(&virVMXDomainDefParserConfig,
-                               NULL, NULL);
+    return virDomainXMLOptionNew(&virVMXDomainDefParserConfig, NULL,
+                                 &virVMXDomainXMLNamespace);
 }
 
 char *
@@ -1268,6 +1302,7 @@ virVMXParseConfig(virVMXContext *ctx,
     bool hgfs_disabled = true;
     long long sharedFolder_maxNum = 0;
     int cpumasklen;
+    char *namespaceData;
 
     if (ctx->parseFileName == NULL) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -1767,6 +1802,15 @@ virVMXParseConfig(virVMXContext *ctx,
             ++def->nparallels;
     }
 
+    /* ctx:datacenterPath -> def:namespaceData */
+    if (ctx->datacenterPath) {
+        if (VIR_STRDUP(namespaceData, ctx->datacenterPath) < 0)
+            goto cleanup;
+
+        def->ns = *virDomainXMLOptionGetNamespace(xmlopt);
+        def->namespaceData = namespaceData;
+    }
+
     success = true;
 
  cleanup:
index e98612498c67ebc7c5c9e5611cc127e85e35a4b9..90bd7479ed81012a84c49fb4362c82235c86e381 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * vmx.h: VMware VMX parsing/formatting functions
  *
- * Copyright (C) 2009-2010 Matthias Bolte <matthias.bolte@googlemail.com>
+ * Copyright (C) 2009-2011, 2015 Matthias Bolte <matthias.bolte@googlemail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -41,15 +41,17 @@ typedef int (*virVMXAutodetectSCSIControllerModel)(virDomainDiskDefPtr def,
                                                    int *model, void *opaque);
 
 /*
- * virVMXParseFileName is only used by virVMXParseConfig.
- * virVMXFormatFileName is only used by virVMXFormatConfig.
- * virVMXAutodetectSCSIControllerModel is optionally used by virVMXFormatConfig.
+ * parseFileName is only used by virVMXParseConfig.
+ * formatFileName is only used by virVMXFormatConfig.
+ * autodetectSCSIControllerModel is optionally used by virVMXFormatConfig.
+ * datacenterPath is only used by virVMXFormatConfig.
  */
 struct _virVMXContext {
     void *opaque;
     virVMXParseFileName parseFileName;
     virVMXFormatFileName formatFileName;
     virVMXAutodetectSCSIControllerModel autodetectSCSIControllerModel;
+    const char *datacenterPath; /* including folders */
 };
 
 
diff --git a/tests/vmx2xmldata/vmx2xml-datacenterpath.vmx b/tests/vmx2xmldata/vmx2xml-datacenterpath.vmx
new file mode 100644 (file)
index 0000000..a8e5db3
--- /dev/null
@@ -0,0 +1,2 @@
+config.version = "8"
+virtualHW.version = "4"
diff --git a/tests/vmx2xmldata/vmx2xml-datacenterpath.xml b/tests/vmx2xmldata/vmx2xml-datacenterpath.xml
new file mode 100644 (file)
index 0000000..a690c0f
--- /dev/null
@@ -0,0 +1,19 @@
+<domain type='vmware' xmlns:vmware='http://libvirt.org/schemas/domain/vmware/1.0'>
+  <uuid>00000000-0000-0000-0000-000000000000</uuid>
+  <memory unit='KiB'>32768</memory>
+  <currentMemory unit='KiB'>32768</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='i686'>hvm</type>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <video>
+      <model type='vmvga' vram='4096'/>
+    </video>
+  </devices>
+  <vmware:datacenterpath>folder1/folder2/datacenter1</vmware:datacenterpath>
+</domain>
index 1d1fe83eda9e62e8378e12d3ea6c695e02ee0f36..2b14804de443a9c00622ec786f5fa4f64d20e599 100644 (file)
@@ -201,6 +201,7 @@ mymain(void)
     ctx.parseFileName = testParseVMXFileName;
     ctx.formatFileName = NULL;
     ctx.autodetectSCSIControllerModel = NULL;
+    ctx.datacenterPath = NULL;
 
     DO_TEST("case-insensitive-1", "case-insensitive-1");
     DO_TEST("case-insensitive-2", "case-insensitive-2");
@@ -280,6 +281,10 @@ mymain(void)
 
     DO_TEST("svga", "svga");
 
+    ctx.datacenterPath = "folder1/folder2/datacenter1";
+
+    DO_TEST("datacenterpath", "datacenterpath");
+
     virObjectUnref(caps);
     virObjectUnref(xmlopt);
 
diff --git a/tests/xml2vmxdata/xml2vmx-datacenterpath.vmx b/tests/xml2vmxdata/xml2vmx-datacenterpath.vmx
new file mode 100644 (file)
index 0000000..59bde03
--- /dev/null
@@ -0,0 +1,10 @@
+.encoding = "UTF-8"
+config.version = "8"
+virtualHW.version = "4"
+guestOS = "other"
+uuid.bios = "56 4d 9b ef ac d9 b4 e0-c8 f0 ae a8 b9 10 35 15"
+displayName = "datacenterpath"
+memsize = "4"
+numvcpus = "1"
+floppy0.present = "false"
+floppy1.present = "false"
diff --git a/tests/xml2vmxdata/xml2vmx-datacenterpath.xml b/tests/xml2vmxdata/xml2vmx-datacenterpath.xml
new file mode 100644 (file)
index 0000000..048e13d
--- /dev/null
@@ -0,0 +1,9 @@
+<domain type='vmware' xmlns:vmware='http://libvirt.org/schemas/domain/vmware/1.0'>
+  <name>datacenterpath</name>
+  <uuid>564d9bef-acd9-b4e0-c8f0-aea8b9103515</uuid>
+  <memory unit='KiB'>4096</memory>
+  <os>
+    <type>hvm</type>
+  </os>
+  <vmware:datacenterpath>folder1/folder2/datacenter1</vmware:datacenterpath>
+</domain>
index 53efe31e99be3193d15f92c6f7c8b6116163282b..32bad5fc98f44428daec0f89f6afc180ce17ce0d 100644 (file)
@@ -221,6 +221,7 @@ mymain(void)
     ctx.parseFileName = NULL;
     ctx.formatFileName = testFormatVMXFileName;
     ctx.autodetectSCSIControllerModel = testAutodetectSCSIControllerModel;
+    ctx.datacenterPath = NULL;
 
     DO_TEST("minimal", "minimal", 4);
     DO_TEST("minimal-64bit", "minimal-64bit", 4);
@@ -293,6 +294,8 @@ mymain(void)
 
     DO_TEST("svga", "svga", 4);
 
+    DO_TEST("datacenterpath", "datacenterpath", 4);
+
     virObjectUnref(caps);
     virObjectUnref(xmlopt);