From 9b9c8e205fece146a2708bdc553ac926bb6b8e16 Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Tue, 30 Sep 2025 10:54:16 +0000 Subject: [PATCH] OSmorphing: Add windows registry operations --- coriolis/osmorphing/windows.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/coriolis/osmorphing/windows.py b/coriolis/osmorphing/windows.py index 5826ed26..9f303c5b 100644 --- a/coriolis/osmorphing/windows.py +++ b/coriolis/osmorphing/windows.py @@ -26,7 +26,9 @@ SERVICE_START_MANUAL = 3 SERVICE_START_DISABLED = 4 +SERVICES_PATH_FORMAT = "HKLM:\\%s\\ControlSet001\\Services" SERVICE_PATH_FORMAT = "HKLM:\\%s\\ControlSet001\\Services\\%s" +RUN_PATH_FORMAT = "HKLM:\\%s\\\Microsoft\\Windows\\CurrentVersion\\Run" CLOUDBASEINIT_SERVICE_NAME = "cloudbase-init" CLOUDBASE_INIT_DEFAULT_PLUGINS = [ 'cloudbaseinit.plugins.common.mtu.MTUPlugin', @@ -287,6 +289,22 @@ def _set_service_start_mode(self, key_name, service_name, start_mode): "%(start_mode)s" % {"path": registry_path, "start_mode": start_mode}) + def _set_services_start_mode(self, key_name, service_names, start_mode): + LOG.info("Setting service start mode: %(service_names)s, " + "%(start_mode)s", {"service_names": service_names, + "start_mode": start_mode}) + registry_paths = [SERVICE_PATH_FORMAT % (key_name, service) + for service in service_names] + paths_string = ", ".join(f"'{path}'" for path in registry_paths) + self._conn.exec_ps_command( + f""" + $ErrorActionPreference='Stop'; + @({paths_string}) | ForEach-Object {{ + Set-ItemProperty -Path $_ -Name 'Start' -Value {start_mode} + }} + """, + ignore_stdout=True) + def _create_service(self, key_name, service_name, image_path, display_name, description, start_mode=SERVICE_START_AUTO, @@ -321,6 +339,17 @@ def _create_service(self, key_name, service_name, image_path, "start_mode": start_mode}, ignore_stdout=True) + def _delete_startup_entry(self, key_name, service_name): + registry_path = RUN_PATH_FORMAT % key_name + LOG.info("Deleting startup entry: %s", service_name) + + self._conn.exec_ps_command( + "$ErrorActionPreference = 'Stop';" + "Remove-ItemProperty -Path '%(path)s' -Name '%(entry)s' -Force" + % {"path": registry_path, "entry": service_name}, + ignore_stdout=True, + ) + def run_user_script(self, user_script): if len(user_script) == 0: return