Co-authored-by: Sri Ramanujam <sramanujam(a)datto.com>
Signed-off-by: Matt Coleman <matt(a)datto.com>
---
src/hyperv/hyperv_driver.c | 91 ++++++++++++++++++++++++++++++++++++++
1 file changed, 91 insertions(+)
diff --git a/src/hyperv/hyperv_driver.c b/src/hyperv/hyperv_driver.c
index 2ac30fa4c6..79b48a9dff 100644
--- a/src/hyperv/hyperv_driver.c
+++ b/src/hyperv/hyperv_driver.c
@@ -1310,6 +1310,96 @@ hypervDomainGetAutostart(virDomainPtr domain, int *autostart)
+static int
+hypervDomainSetAutostart(virDomainPtr domain, int autostart)
+{
+ int result = -1;
+ char uuid_string[VIR_UUID_STRING_BUFLEN];
+ hypervPrivate *priv = domain->conn->privateData;
+ Msvm_VirtualSystemSettingData *vssd = NULL;
+ hypervInvokeParamsListPtr params = NULL;
+ g_auto(virBuffer) eprQuery = VIR_BUFFER_INITIALIZER;
+ virHashTablePtr autostartParam = NULL;
+ hypervWmiClassInfoListPtr embeddedParamClass = NULL;
+ const char *methodName = NULL, *embeddedParamName = NULL;
+ char enabledValue[] = "2", disabledValue[] = "0";
+
+ if (priv->wmiVersion == HYPERV_WMI_VERSION_V1) {
+ methodName = "ModifyVirtualSystem";
+ embeddedParamName = "SystemSettingData";
+ embeddedParamClass = Msvm_VirtualSystemGlobalSettingData_WmiInfo;
+ } else if (priv->wmiVersion == HYPERV_WMI_VERSION_V2) {
+ methodName = "ModifySystemSettings";
+ embeddedParamName = "SystemSettings";
+ embeddedParamClass = Msvm_VirtualSystemSettingData_WmiInfo;
+ enabledValue[0] = '4';
+ disabledValue[0] = '2';
+ }
+
+ virUUIDFormat(domain->uuid, uuid_string);
+
+ if (hypervGetVSSDFromUUID(priv, uuid_string, &vssd) < 0)
+ goto cleanup;
+
+ params = hypervCreateInvokeParamsList(priv, methodName,
+ MSVM_VIRTUALSYSTEMMANAGEMENTSERVICE_SELECTOR,
+ Msvm_VirtualSystemManagementService_WmiInfo);
+
+ if (!params) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("Could not create params"));
+ goto cleanup;
+ }
+
+ if (priv->wmiVersion == HYPERV_WMI_VERSION_V1) {
+ virBufferEscapeSQL(&eprQuery,
+ MSVM_COMPUTERSYSTEM_WQL_SELECT "WHERE Name =
'%s'",
+ uuid_string);
+
+ if (hypervAddEprParam(params, "ComputerSystem", priv, &eprQuery,
+ Msvm_ComputerSystem_WmiInfo) < 0)
+ goto params_cleanup;
+ }
+
+ autostartParam = hypervCreateEmbeddedParam(priv, embeddedParamClass);
+
+ if (hypervSetEmbeddedProperty(autostartParam, "AutomaticStartupAction",
+ autostart ? enabledValue : disabledValue) < 0) {
+ hypervFreeEmbeddedParam(autostartParam);
+ goto params_cleanup;
+ }
+
+ if (hypervSetEmbeddedProperty(autostartParam, "InstanceID",
+ vssd->data.common->InstanceID) < 0) {
+ hypervFreeEmbeddedParam(autostartParam);
+ goto params_cleanup;
+ }
+
+ if (hypervAddEmbeddedParam(params, priv, embeddedParamName, autostartParam,
+ embeddedParamClass) < 0) {
+ hypervFreeEmbeddedParam(autostartParam);
+ goto params_cleanup;
+ }
+
+ if (hypervInvokeMethod(priv, params, NULL) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("Could not set autostart"));
+ goto cleanup;
+ }
+
+ result = 0;
+ goto cleanup;
+
+ params_cleanup:
+ hypervFreeInvokeParams(params);
+ cleanup:
+ hypervFreeObject(priv, (hypervObject *) vssd);
+
+ return result;
+}
+
+
+
static int
hypervConnectIsEncrypted(virConnectPtr conn)
{
@@ -1861,6 +1951,7 @@ static virHypervisorDriver hypervHypervisorDriver = {
.domainCreate = hypervDomainCreate, /* 0.9.5 */
.domainCreateWithFlags = hypervDomainCreateWithFlags, /* 0.9.5 */
.domainGetAutostart = hypervDomainGetAutostart, /* 6.9.0 */
+ .domainSetAutostart = hypervDomainSetAutostart, /* 6.9.0 */
.connectIsEncrypted = hypervConnectIsEncrypted, /* 0.9.5 */
.connectIsSecure = hypervConnectIsSecure, /* 0.9.5 */
.domainIsActive = hypervDomainIsActive, /* 0.9.5 */
--
2.27.0