[PATCH v3 6/9] hyperv: implement domainShutdown and domainShutdownFlags

Michal Privoznik mprivozn at redhat.com
Wed Oct 21 11:01:41 UTC 2020


On 10/21/20 10:46 AM, Matt Coleman wrote:
> Co-authored-by: Sri Ramanujam <sramanujam at datto.com>
> Signed-off-by: Matt Coleman <matt at datto.com>
> ---
>   src/hyperv/hyperv_driver.c            | 77 ++++++++++++++++++++++++++
>   src/hyperv/hyperv_wmi_generator.input | 78 +++++++++++++++++++++++++++
>   2 files changed, 155 insertions(+)
> 
> diff --git a/src/hyperv/hyperv_driver.c b/src/hyperv/hyperv_driver.c
> index 4bddc86273..791f34444d 100644
> --- a/src/hyperv/hyperv_driver.c
> +++ b/src/hyperv/hyperv_driver.c
> @@ -941,6 +941,81 @@ hypervDomainResume(virDomainPtr domain)
>   
>   
>   
> +static int
> +hypervDomainShutdownFlags(virDomainPtr domain, unsigned int flags)
> +{
> +    int result = -1;
> +    hypervPrivate *priv = domain->conn->privateData;
> +    Msvm_ComputerSystem *computerSystem = NULL;
> +    Msvm_ShutdownComponent *shutdown = NULL;
> +    bool in_transition = false;
> +    char uuid[VIR_UUID_STRING_BUFLEN];
> +    g_auto(virBuffer) query = VIR_BUFFER_INITIALIZER;
> +    g_autoptr(hypervInvokeParamsList) params = NULL;
> +    g_autofree char *selector = NULL;
> +
> +    virCheckFlags(0, -1);
> +
> +    virUUIDFormat(domain->uuid, uuid);
> +
> +    if (hypervMsvmComputerSystemFromDomain(domain, &computerSystem) < 0)
> +        goto cleanup;
> +
> +    if (!hypervIsMsvmComputerSystemActive(computerSystem, &in_transition) ||
> +        in_transition) {
> +        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
> +                       _("Domain is not active or in state transition"));
> +        goto cleanup;
> +    }
> +
> +    virBufferEscapeSQL(&query, MSVM_SHUTDOWNCOMPONENT_WQL_SELECT "WHERE SystemName = '%s'", uuid);
> +
> +    if (hypervGetWmiClass(Msvm_ShutdownComponent, &shutdown) < 0 ||
> +        !shutdown) {
> +        virReportError(VIR_ERR_OPERATION_FAILED,
> +                       _("Could not get Msvm_ShutdownComponent for domain with UUID '%s'"),
> +                       uuid);
> +        goto cleanup;
> +    }
> +
> +    selector = g_strdup_printf("CreationClassName=\"Msvm_ShutdownComponent\"&DeviceID=\"%s\"&"
> +                               "SystemCreationClassName=\"Msvm_ComputerSystem\"&SystemName=\"%s\"",
> +                               shutdown->data.common->DeviceID, uuid);
> +
> +    params = hypervCreateInvokeParamsList(priv, "InitiateShutdown", selector,
> +                                          Msvm_ShutdownComponent_WmiInfo);
> +
> +    hypervAddSimpleParam(params, "Force", "False");
> +
> +    /* "Reason" is not translated because the Hyper-V administrator may not
> +     * know the libvirt user's language. They may not know English, either,
> +     * but this makes it consistent, at least. */
> +    hypervAddSimpleParam(params, "Reason", "Planned shutdown via libvirt");
> +
> +    if (hypervInvokeMethod(priv, &params, NULL) < 0) {
> +        virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not shutdown domain with UUID '%s'"), uuid);

Again, no need to overwrite the error message.

> +        goto cleanup;
> +    }
> +
> +    result = 0;
> +
> + cleanup:
> +    hypervFreeObject(priv, (hypervObject *) computerSystem);
> +    hypervFreeObject(priv, (hypervObject *) shutdown);
> +
> +    return result;
> +}

Michal




More information about the libvir-list mailing list