Co-authored-by: Sri Ramanujam <sramanujam(a)datto.com>
Signed-off-by: Matt Coleman <matt(a)datto.com>
---
src/hyperv/hyperv_driver.c | 92 ++++++++++++++++++++++++++++++++++++++
1 file changed, 92 insertions(+)
diff --git a/src/hyperv/hyperv_driver.c b/src/hyperv/hyperv_driver.c
index 2ac30fa4c6..6b2acbc405 100644
--- a/src/hyperv/hyperv_driver.c
+++ b/src/hyperv/hyperv_driver.c
@@ -1310,6 +1310,97 @@ 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;
+ g_autofree char *enabledValue = NULL, *disabledValue = NULL;
+
+ if (priv->wmiVersion == HYPERV_WMI_VERSION_V1) {
+ methodName = "ModifyVirtualSystem";
+ embeddedParamName = "SystemSettingData";
+ embeddedParamClass = Msvm_VirtualSystemGlobalSettingData_WmiInfo;
+ enabledValue = g_strdup("2");
+ disabledValue = g_strdup("0");
+ } else if (priv->wmiVersion == HYPERV_WMI_VERSION_V2) {
+ methodName = "ModifySystemSettings";
+ embeddedParamName = "SystemSettings";
+ embeddedParamClass = Msvm_VirtualSystemSettingData_WmiInfo;
+ enabledValue = g_strdup("4");
+ disabledValue = g_strdup("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 +1952,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