Re: [libvirt] [Patch v2] vmware: detect when a domain was shut down from the inside

2012-07-04 Thread Michal Privoznik
On 02.04.2012 15:59, Jean-Baptiste Rouault wrote:
 This patch adds an internal function vmwareUpdateVMStatus to
 update the real state of the domain. This function is used in
 various places in the driver, in particular to detect when
 the domain has been shut down by the user with the halt
 command.
 ---
 v2:
  - Replace internal function vmwareGetVMStatus by vmwareUpdateVMStatus
  - Improve vmrun list output parsing
  - variable initialization and coding-style fixes
  
  src/vmware/vmware_driver.c |   95 
 
  1 files changed, 95 insertions(+), 0 deletions(-)
 
 diff --git a/src/vmware/vmware_driver.c b/src/vmware/vmware_driver.c
 index 8f9d922..53e28e7 100644
 --- a/src/vmware/vmware_driver.c
 +++ b/src/vmware/vmware_driver.c
 @@ -28,6 +28,7 @@
  #include datatypes.h
  #include virfile.h
  #include memory.h
 +#include util.h
  #include uuid.h
  #include command.h
  #include vmx.h
 @@ -181,6 +182,64 @@ vmwareGetVersion(virConnectPtr conn, unsigned long 
 *version)
  }
  
  static int
 +vmwareUpdateVMStatus(struct vmware_driver *driver, virDomainObjPtr vm)
 +{
 +virCommandPtr cmd;
 +char *outbuf = NULL;
 +char *vmxAbsolutePath = NULL;
 +char *parsedVmxPath = NULL;
 +char *str;
 +char *saveptr = NULL;
 +bool found = false;
 +int oldState = virDomainObjGetState(vm, NULL);
 +int newState;
 +int ret = -1;
 +
 +cmd = virCommandNewArgList(VMRUN, -T, vmw_types[driver-type],
 +   list, NULL);
 +virCommandSetOutputBuffer(cmd, outbuf);
 +if (virCommandRun(cmd, NULL)  0)
 +goto cleanup;
 +
 +if (virFileResolveAllLinks(((vmwareDomainPtr) vm-privateData)-vmxPath,
 +   vmxAbsolutePath)  0)
 +goto cleanup;
 +
 +for(str = outbuf ; (parsedVmxPath = strtok_r(str, \n, saveptr)) != 
 NULL;
 +str = NULL) {
 +
 +if (parsedVmxPath[0] != '/')
 +continue;
 +
 +if (STREQ(parsedVmxPath, vmxAbsolutePath)) {
 +found = true;
 +/* If the vmx path is in the output, the domain is running or
 + * is paused but we have no way to detect if it is paused or 
 not. */
 +if (oldState == VIR_DOMAIN_PAUSED)
 +newState = oldState;
 +else
 +newState = VIR_DOMAIN_RUNNING;
 +break;
 +}
 +}
 +
 +if (!found) {
 +vm-def-id = -1;
 +newState = VIR_DOMAIN_SHUTOFF;
 +}
 +
 +virDomainObjSetState(vm, newState, 0);
 +
 +ret = 0;
 +
 +cleanup:
 +virCommandFree(cmd);
 +VIR_FREE(outbuf);
 +VIR_FREE(vmxAbsolutePath);
 +return ret;
 +}
 +
 +static int
  vmwareStopVM(struct vmware_driver *driver,
   virDomainObjPtr vm,
   virDomainShutoffReason reason)
 @@ -331,6 +390,9 @@ vmwareDomainShutdownFlags(virDomainPtr dom,
  goto cleanup;
  }
  
 +if (vmwareUpdateVMStatus(driver, vm)  0)
 +goto cleanup;
 +
  if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_RUNNING) {
  vmwareError(VIR_ERR_INTERNAL_ERROR, %s,
  _(domain is not in running state));
 @@ -485,6 +547,8 @@ vmwareDomainReboot(virDomainPtr dom, unsigned int flags)
  vmwareSetSentinal(cmd, vmw_types[driver-type]);
  vmwareSetSentinal(cmd, vmxPath);
  
 +if (vmwareUpdateVMStatus(driver, vm)  0)
 +goto cleanup;
  
  if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_RUNNING) {
  vmwareError(VIR_ERR_INTERNAL_ERROR, %s,
 @@ -596,6 +660,9 @@ vmwareDomainCreateWithFlags(virDomainPtr dom,
  goto cleanup;
  }
  
 +if (vmwareUpdateVMStatus(driver, vm)  0)
 +goto cleanup;
 +
  if (virDomainObjIsActive(vm)) {
  vmwareError(VIR_ERR_OPERATION_INVALID,
  %s, _(Domain is already running));
 @@ -645,6 +712,9 @@ vmwareDomainUndefineFlags(virDomainPtr dom,
  goto cleanup;
  }
  
 +if (vmwareUpdateVMStatus(driver, vm)  0)
 +goto cleanup;
 +
  if (virDomainObjIsActive(vm)) {
  vm-persistent = 0;
  } else {
 @@ -874,6 +944,21 @@ vmwareDomainXMLFromNative(virConnectPtr conn, const char 
 *nativeFormat,
  return xml;
  }
  
 +static void vmwareDomainObjListUpdateDomain(void *payload, const void *name 
 ATTRIBUTE_UNUSED, void *data)
 +{
 +struct vmware_driver *driver = data;
 +virDomainObjPtr vm = payload;
 +virDomainObjLock(vm);
 +vmwareUpdateVMStatus(driver, vm);
 +virDomainObjUnlock(vm);
 +}
 +
 +static void
 +vmwareDomainObjListUpdateAll(virDomainObjListPtr doms, struct vmware_driver 
 *driver)
 +{
 +virHashForEach(doms-objs, vmwareDomainObjListUpdateDomain, driver);
 +}
 +
  static int
  vmwareNumDefinedDomains(virConnectPtr conn)
  {
 @@ -881,6 +966,7 @@ vmwareNumDefinedDomains(virConnectPtr conn)
  int n;
  
  vmwareDriverLock(driver);
 +vmwareDomainObjListUpdateAll(driver-domains, driver);
  n = 

Re: [libvirt] [Patch v2] vmware: detect when a domain was shut down from the inside

2012-07-02 Thread Jean-Baptiste Rouault
On Monday 02 April 2012 15:59:32 Jean-Baptiste Rouault wrote:
 This patch adds an internal function vmwareUpdateVMStatus to
 update the real state of the domain. This function is used in
 various places in the driver, in particular to detect when
 the domain has been shut down by the user with the halt
 command.

Hi,

Bumping the thread because it seems that it was forgotten.

Regards,
Jean-Baptiste

-- 
Jean-Baptiste ROUAULT
Ingénieur RD - diateam : Architectes de l'information
Phone : +33 (0)2 98 050 050 Fax : +33 (0)2 98 050 051

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [Patch v2] vmware: detect when a domain was shut down from the inside

2012-04-02 Thread Jean-Baptiste Rouault
This patch adds an internal function vmwareUpdateVMStatus to
update the real state of the domain. This function is used in
various places in the driver, in particular to detect when
the domain has been shut down by the user with the halt
command.
---
v2:
 - Replace internal function vmwareGetVMStatus by vmwareUpdateVMStatus
 - Improve vmrun list output parsing
 - variable initialization and coding-style fixes
 
 src/vmware/vmware_driver.c |   95 
 1 files changed, 95 insertions(+), 0 deletions(-)

diff --git a/src/vmware/vmware_driver.c b/src/vmware/vmware_driver.c
index 8f9d922..53e28e7 100644
--- a/src/vmware/vmware_driver.c
+++ b/src/vmware/vmware_driver.c
@@ -28,6 +28,7 @@
 #include datatypes.h
 #include virfile.h
 #include memory.h
+#include util.h
 #include uuid.h
 #include command.h
 #include vmx.h
@@ -181,6 +182,64 @@ vmwareGetVersion(virConnectPtr conn, unsigned long 
*version)
 }
 
 static int
+vmwareUpdateVMStatus(struct vmware_driver *driver, virDomainObjPtr vm)
+{
+virCommandPtr cmd;
+char *outbuf = NULL;
+char *vmxAbsolutePath = NULL;
+char *parsedVmxPath = NULL;
+char *str;
+char *saveptr = NULL;
+bool found = false;
+int oldState = virDomainObjGetState(vm, NULL);
+int newState;
+int ret = -1;
+
+cmd = virCommandNewArgList(VMRUN, -T, vmw_types[driver-type],
+   list, NULL);
+virCommandSetOutputBuffer(cmd, outbuf);
+if (virCommandRun(cmd, NULL)  0)
+goto cleanup;
+
+if (virFileResolveAllLinks(((vmwareDomainPtr) vm-privateData)-vmxPath,
+   vmxAbsolutePath)  0)
+goto cleanup;
+
+for(str = outbuf ; (parsedVmxPath = strtok_r(str, \n, saveptr)) != NULL;
+str = NULL) {
+
+if (parsedVmxPath[0] != '/')
+continue;
+
+if (STREQ(parsedVmxPath, vmxAbsolutePath)) {
+found = true;
+/* If the vmx path is in the output, the domain is running or
+ * is paused but we have no way to detect if it is paused or not. 
*/
+if (oldState == VIR_DOMAIN_PAUSED)
+newState = oldState;
+else
+newState = VIR_DOMAIN_RUNNING;
+break;
+}
+}
+
+if (!found) {
+vm-def-id = -1;
+newState = VIR_DOMAIN_SHUTOFF;
+}
+
+virDomainObjSetState(vm, newState, 0);
+
+ret = 0;
+
+cleanup:
+virCommandFree(cmd);
+VIR_FREE(outbuf);
+VIR_FREE(vmxAbsolutePath);
+return ret;
+}
+
+static int
 vmwareStopVM(struct vmware_driver *driver,
  virDomainObjPtr vm,
  virDomainShutoffReason reason)
@@ -331,6 +390,9 @@ vmwareDomainShutdownFlags(virDomainPtr dom,
 goto cleanup;
 }
 
+if (vmwareUpdateVMStatus(driver, vm)  0)
+goto cleanup;
+
 if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_RUNNING) {
 vmwareError(VIR_ERR_INTERNAL_ERROR, %s,
 _(domain is not in running state));
@@ -485,6 +547,8 @@ vmwareDomainReboot(virDomainPtr dom, unsigned int flags)
 vmwareSetSentinal(cmd, vmw_types[driver-type]);
 vmwareSetSentinal(cmd, vmxPath);
 
+if (vmwareUpdateVMStatus(driver, vm)  0)
+goto cleanup;
 
 if (virDomainObjGetState(vm, NULL) != VIR_DOMAIN_RUNNING) {
 vmwareError(VIR_ERR_INTERNAL_ERROR, %s,
@@ -596,6 +660,9 @@ vmwareDomainCreateWithFlags(virDomainPtr dom,
 goto cleanup;
 }
 
+if (vmwareUpdateVMStatus(driver, vm)  0)
+goto cleanup;
+
 if (virDomainObjIsActive(vm)) {
 vmwareError(VIR_ERR_OPERATION_INVALID,
 %s, _(Domain is already running));
@@ -645,6 +712,9 @@ vmwareDomainUndefineFlags(virDomainPtr dom,
 goto cleanup;
 }
 
+if (vmwareUpdateVMStatus(driver, vm)  0)
+goto cleanup;
+
 if (virDomainObjIsActive(vm)) {
 vm-persistent = 0;
 } else {
@@ -874,6 +944,21 @@ vmwareDomainXMLFromNative(virConnectPtr conn, const char 
*nativeFormat,
 return xml;
 }
 
+static void vmwareDomainObjListUpdateDomain(void *payload, const void *name 
ATTRIBUTE_UNUSED, void *data)
+{
+struct vmware_driver *driver = data;
+virDomainObjPtr vm = payload;
+virDomainObjLock(vm);
+vmwareUpdateVMStatus(driver, vm);
+virDomainObjUnlock(vm);
+}
+
+static void
+vmwareDomainObjListUpdateAll(virDomainObjListPtr doms, struct vmware_driver 
*driver)
+{
+virHashForEach(doms-objs, vmwareDomainObjListUpdateDomain, driver);
+}
+
 static int
 vmwareNumDefinedDomains(virConnectPtr conn)
 {
@@ -881,6 +966,7 @@ vmwareNumDefinedDomains(virConnectPtr conn)
 int n;
 
 vmwareDriverLock(driver);
+vmwareDomainObjListUpdateAll(driver-domains, driver);
 n = virDomainObjListNumOfDomains(driver-domains, 0);
 vmwareDriverUnlock(driver);
 
@@ -894,6 +980,7 @@ vmwareNumDomains(virConnectPtr conn)
 int n;
 
 vmwareDriverLock(driver);
+vmwareDomainObjListUpdateAll(driver-domains,