From d17cd6fc38fb8d3aef05f9191d773c5d8651f82c Mon Sep 17 00:00:00 2001 From: Didier Roche Date: Wed, 16 Mar 2022 12:20:33 +0100 Subject: [PATCH] System-Setup: Add systemd experimental support to WSL Grow a new API for enabling/disabling systemd experimental support. We do this by chaining a specific command= in the boot section. We preserve the user customized command= content when systemd experimental support is not enabled. Co-authored-by: Jean-Baptiste Lallement --- autoinstall-system-setup-schema.json | 3 ++ examples/answers-system-setup-reconf.yaml | 1 + examples/autoinstall-system-setup-full.yaml | 1 + subiquity/common/types.py | 1 + .../client/controllers/wslconfadvanced.py | 3 +- system_setup/common/wsl_conf.py | 40 ++++++++++++++++++- system_setup/models/wslconfadvanced.py | 1 + .../server/controllers/wslconfadvanced.py | 5 ++- .../tests/golden/answers-reconf/wsl.conf | 3 ++ .../tests/golden/autoinstall-full/wsl.conf | 3 ++ system_setup/ui/views/wslconfadvanced.py | 8 ++++ 11 files changed, 66 insertions(+), 3 deletions(-) diff --git a/autoinstall-system-setup-schema.json b/autoinstall-system-setup-schema.json index d2669198..e478bdc9 100644 --- a/autoinstall-system-setup-schema.json +++ b/autoinstall-system-setup-schema.json @@ -99,6 +99,9 @@ }, "automount_mountfstab": { "type": "boolean" + }, + "systemd_enabled": { + "type": "boolean" } }, "additionalProperties": false diff --git a/examples/answers-system-setup-reconf.yaml b/examples/answers-system-setup-reconf.yaml index 1268c545..b526ae85 100644 --- a/examples/answers-system-setup-reconf.yaml +++ b/examples/answers-system-setup-reconf.yaml @@ -8,5 +8,6 @@ WSLConfigurationAdvanced: interop_appendwindowspath: false automount_enabled: false automount_mountfstab: false + systemd_enabled: true Summary: reboot: yes \ No newline at end of file diff --git a/examples/autoinstall-system-setup-full.yaml b/examples/autoinstall-system-setup-full.yaml index 7159bcbe..7fcbc5e1 100644 --- a/examples/autoinstall-system-setup-full.yaml +++ b/examples/autoinstall-system-setup-full.yaml @@ -19,4 +19,5 @@ wslconfadvanced: interop_appendwindowspath: false automount_enabled: false automount_mountfstab: false + systemd_enabled: true shutdown: 'poweroff' \ No newline at end of file diff --git a/subiquity/common/types.py b/subiquity/common/types.py index 1490a6f0..0738b39b 100644 --- a/subiquity/common/types.py +++ b/subiquity/common/types.py @@ -458,3 +458,4 @@ class WSLConfigurationAdvanced: automount_mountfstab: bool = attr.ib(default=True) interop_enabled: bool = attr.ib(default=True) interop_appendwindowspath: bool = attr.ib(default=True) + systemd_enabled: bool = attr.ib(default=False) diff --git a/system_setup/client/controllers/wslconfadvanced.py b/system_setup/client/controllers/wslconfadvanced.py index 342ce0ae..8c4c362d 100644 --- a/system_setup/client/controllers/wslconfadvanced.py +++ b/system_setup/client/controllers/wslconfadvanced.py @@ -32,7 +32,8 @@ class WSLConfigurationAdvancedController(SubiquityTuiController): def run_answers(self): if all(elem in self.answers for elem in ['interop_enabled', 'interop_appendwindowspath', - 'automount_enabled', 'automount_mountfstab']): + 'automount_enabled', 'automount_mountfstab', + 'systemd_enabled']): reconfiguration = WSLConfigurationAdvanced(**self.answers) self.done(reconfiguration) diff --git a/system_setup/common/wsl_conf.py b/system_setup/common/wsl_conf.py index f7bec16b..1c55f3d4 100644 --- a/system_setup/common/wsl_conf.py +++ b/system_setup/common/wsl_conf.py @@ -24,6 +24,8 @@ from configparser import ConfigParser log = logging.getLogger("system_setup.common.wsl_conf") +WSL_SYSTEMD_BOOT_COMMAND = "/usr/libexec/wsl-systemd" + config_base_default = { "wsl": { "automount": { @@ -46,6 +48,9 @@ config_adv_default = { "interop": { "enabled": "true", "appendwindowspath": "true" + }, + "boot": { + "command": "" } } } @@ -74,9 +79,20 @@ def wsl_config_loader(data, config_ref, id): conf_sec_list = config[conf_sec] for conf_item in conf_sec_list: if conf_item in config_ref[id][conf_sec]: + # Handle systemd experimental feature (string -> bool) + if ( + conf_sec.lower() == "boot" and + conf_item.lower() == "command"): + v = conf_sec_list[conf_item] + data["systemd_enabled"] = "false" + if v == WSL_SYSTEMD_BOOT_COMMAND: + data["systemd_enabled"] = "true" + continue data[conf_sec.lower() + "_" + conf_item.lower()] = \ - conf_sec_list[conf_item] + conf_sec_list[conf_item] + # This is the systemd value we converted + data.pop("boot_command", None) return data @@ -128,9 +144,31 @@ def wsl_config_update(config_class, root_dir): config_default_value = config_settings[config_setting] config_api_name = \ config_section.lower() + "_" + config_setting.lower() + + # on systemd, select correct API endpoint + systemd_experimental_section = False + if (config_section.lower() == "boot" and + config_setting.lower() == "command"): + systemd_experimental_section = True + config_api_name = "systemd_enabled" + config_value = config_class.__dict__[config_api_name] if isinstance(config_value, bool): config_value = str(config_value).lower() + + # map systemd bool -> boot command string and keep old value + # if it was not set to wsl-systemd (user customized) + if systemd_experimental_section: + v = "" + # Keep previous value set by user if the option is kept + # disabled and they set their own boot command. + if (config.has_option("boot", "command") and + config["boot"]["command"] != WSL_SYSTEMD_BOOT_COMMAND): + v = config["boot"]["command"] + if config_value == "true": + v = WSL_SYSTEMD_BOOT_COMMAND + config_value = v + # if the value for the setting is default value, drop it if config_default_value == config_value: if config_section in config: diff --git a/system_setup/models/wslconfadvanced.py b/system_setup/models/wslconfadvanced.py index 63c1276f..7d072bcb 100644 --- a/system_setup/models/wslconfadvanced.py +++ b/system_setup/models/wslconfadvanced.py @@ -25,6 +25,7 @@ class WSLConfigurationAdvanced(object): interop_appendwindowspath = attr.ib() automount_enabled = attr.ib() automount_mountfstab = attr.ib() + systemd_enabled = attr.ib() class WSLConfigurationAdvancedModel(object): diff --git a/system_setup/server/controllers/wslconfadvanced.py b/system_setup/server/controllers/wslconfadvanced.py index b9c7e244..baf19c07 100644 --- a/system_setup/server/controllers/wslconfadvanced.py +++ b/system_setup/server/controllers/wslconfadvanced.py @@ -39,7 +39,8 @@ class WSLConfigurationAdvancedController(SubiquityController): 'interop_enabled': {'type': 'boolean'}, 'interop_appendwindowspath': {'type': 'boolean'}, 'automount_enabled': {'type': 'boolean'}, - 'automount_mountfstab': {'type': 'boolean'} + 'automount_mountfstab': {'type': 'boolean'}, + 'systemd_enabled': {'type': 'boolean'} }, 'additionalProperties': False, } @@ -80,6 +81,8 @@ class WSLConfigurationAdvancedController(SubiquityController): self.model.wslconfadvanced.automount_enabled data.automount_mountfstab = \ self.model.wslconfadvanced.automount_mountfstab + data.systemd_enabled = \ + self.model.wslconfadvanced.systemd_enabled return data async def POST(self, data: WSLConfigurationAdvanced): diff --git a/system_setup/tests/golden/answers-reconf/wsl.conf b/system_setup/tests/golden/answers-reconf/wsl.conf index 449819ba..2469d457 100644 --- a/system_setup/tests/golden/answers-reconf/wsl.conf +++ b/system_setup/tests/golden/answers-reconf/wsl.conf @@ -4,6 +4,9 @@ mountfstab = false options = metadata root = /custom_mnt_path +[boot] +command = /usr/libexec/wsl-systemd + [interop] appendwindowspath = false enabled = false diff --git a/system_setup/tests/golden/autoinstall-full/wsl.conf b/system_setup/tests/golden/autoinstall-full/wsl.conf index 22dce10d..f0faca70 100644 --- a/system_setup/tests/golden/autoinstall-full/wsl.conf +++ b/system_setup/tests/golden/autoinstall-full/wsl.conf @@ -4,6 +4,9 @@ mountfstab = false options = metadata root = /custom_mnt_path +[boot] +command = /usr/libexec/wsl-systemd + [interop] appendwindowspath = false enabled = false diff --git a/system_setup/ui/views/wslconfadvanced.py b/system_setup/ui/views/wslconfadvanced.py index 68b61b93..c617f54a 100644 --- a/system_setup/ui/views/wslconfadvanced.py +++ b/system_setup/ui/views/wslconfadvanced.py @@ -59,6 +59,10 @@ class WSLConfigurationAdvancedForm(Form): BooleanField(_("Append Windows Path"), help=_("Whether Windows Path will be append in the" " PATH environment variable in WSL.")) + systemd_enabled = \ + BooleanField(_("Enable Systemd"), + help=_("EXPERIMENTAL - Whether systemd should be" + " activated at boot time.")) class WSLConfigurationAdvancedView(BaseView): @@ -78,6 +82,8 @@ class WSLConfigurationAdvancedView(BaseView): configuration_data.automount_enabled, 'automount_mountfstab': configuration_data.automount_mountfstab, + 'systemd_enabled': + configuration_data.systemd_enabled, } self.form = WSLConfigurationAdvancedForm(initial=initial) @@ -101,4 +107,6 @@ class WSLConfigurationAdvancedView(BaseView): .automount_enabled.value, automount_mountfstab=self.form .automount_mountfstab.value, + systemd_enabled=self.form + .systemd_enabled.value, ))