[libvirt] [PATCH 7/8] Implement basic virDomainGetState in all drivers

Matthias Bolte matthias.bolte at googlemail.com
Fri May 6 06:37:09 UTC 2011


2011/5/4 Jiri Denemark <jdenemar at redhat.com>:
> Reason is currently always set to 0 (i.e., *_UNKNOWN).
> ---
>  src/esx/esx_driver.c       |   54 ++++++++++++++++++++++++-
>  src/libxl/libxl_driver.c   |   31 +++++++++++++-
>  src/lxc/lxc_driver.c       |   33 ++++++++++++++-
>  src/openvz/openvz_driver.c |   32 ++++++++++++++-
>  src/phyp/phyp_driver.c     |   12 +++++-
>  src/qemu/qemu_driver.c     |   33 ++++++++++++++-
>  src/test/test_driver.c     |   31 +++++++++++++-
>  src/uml/uml_driver.c       |   32 ++++++++++++++-
>  src/vbox/vbox_tmpl.c       |   56 +++++++++++++++++++++++++-
>  src/vmware/vmware_driver.c |   31 +++++++++++++-
>  src/xen/xen_driver.c       |   18 ++++++++-
>  src/xen/xen_hypervisor.c   |   34 +++++++++++++++-
>  src/xen/xen_hypervisor.h   |    4 ++
>  src/xen/xend_internal.c    |   97 ++++++++++++++++++++++++++++++++-----------
>  src/xen/xend_internal.h    |    1 +
>  src/xen/xm_internal.c      |   19 ++++++++-
>  src/xen/xm_internal.h      |    1 +
>  src/xen/xs_internal.c      |   32 ++++++++++++++-
>  src/xen/xs_internal.h      |    3 +
>  src/xenapi/xenapi_driver.c |   45 ++++++++++++++++++++-
>  20 files changed, 559 insertions(+), 40 deletions(-)
>
> diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
> index 543ebe6..94b0121 100644
> --- a/src/esx/esx_driver.c
> +++ b/src/esx/esx_driver.c
> @@ -2425,6 +2425,58 @@ esxDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
>
>
>  static int
> +esxDomainGetState(virDomainPtr domain, int *state, int *reason)
> +{
> +    int result = -1;
> +    esxPrivate *priv = domain->conn->privateData;
> +    esxVI_String *propertyNameList = NULL;
> +    esxVI_ObjectContent *virtualMachine = NULL;
> +    esxVI_DynamicProperty *dynamicProperty = NULL;
> +    esxVI_VirtualMachinePowerState powerState;
> +
> +    if (esxVI_EnsureSession(priv->primary) < 0) {
> +        return -1;
> +    }
> +
> +    if (esxVI_String_AppendValueListToList(&propertyNameList,
> +                                           "runtime.powerState\0") < 0 ||
> +        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
> +                                         propertyNameList, &virtualMachine,
> +                                         esxVI_Occurrence_RequiredItem) < 0) {
> +        goto cleanup;
> +    }
> +
> +    *state = VIR_DOMAIN_NOSTATE;
> +    if (reason)
> +        *reason = 0;
> +
> +    for (dynamicProperty = virtualMachine->propSet; dynamicProperty != NULL;
> +         dynamicProperty = dynamicProperty->_next) {
> +        if (STREQ(dynamicProperty->name, "runtime.powerState")) {
> +            if (esxVI_VirtualMachinePowerState_CastFromAnyType
> +                  (dynamicProperty->val, &powerState) < 0) {
> +                goto cleanup;
> +            }
> +
> +            *state = esxVI_VirtualMachinePowerState_ConvertToLibvirt
> +                       (powerState);
> +        } else {
> +            VIR_WARN("Unexpected '%s' property", dynamicProperty->name);
> +        }
> +    }
> +
> +    result = 0;
> +
> +  cleanup:
> +    esxVI_String_Free(&propertyNameList);
> +    esxVI_ObjectContent_Free(&virtualMachine);
> +
> +    return result;
> +}
> +

This can be simplified to:

static int
esxDomainGetState(virDomainPtr domain, int *state, int *reason)
{
    int result = -1;
    esxPrivate *priv = domain->conn->privateData;
    esxVI_String *propertyNameList = NULL;
    esxVI_ObjectContent *virtualMachine = NULL;
    esxVI_VirtualMachinePowerState powerState;

    if (esxVI_EnsureSession(priv->primary) < 0) {
        return -1;
    }

    if (esxVI_String_AppendValueToList(&propertyNameList,
                                       "runtime.powerState") < 0 ||
        esxVI_LookupVirtualMachineByUuid(priv->primary, domain->uuid,
                                         propertyNameList, &virtualMachine,
                                         esxVI_Occurrence_RequiredItem) < 0 ||
        esxVI_GetVirtualMachinePowerState(virtualMachine, &powerState) < 0) {
        goto cleanup;
    }

    *state = esxVI_VirtualMachinePowerState_ConvertToLibvirt(powerState);

    if (reason) {
        *reason = 0;
    }

    result = 0;

  cleanup:
    esxVI_String_Free(&propertyNameList);
    esxVI_ObjectContent_Free(&virtualMachine);

    return result;
}

Matthias




More information about the libvir-list mailing list