stages/org.osbuild.wsl.conf: add stage to configure WSL settings

This commit is contained in:
Sanne Raymaekers 2023-04-04 13:44:44 +02:00 committed by Tomáš Hozza
parent 5dbf596ffa
commit 028bf67a1d

65
stages/org.osbuild.wsl.conf Executable file
View file

@ -0,0 +1,65 @@
#!/usr/bin/python3
"""
Configure advanced settings in Windows Subsystem for Linux.
The stage configures the WSL settings on the system.
"""
import sys
import iniparse
import osbuild.api
SCHEMA = """
"description": "WSL configuration.",
"additionalProperties": false,
"minProperties": 1,
"properties": {
"boot": {
"type": "object",
"description": "Configures the [boot] section.",
"additionalProperties": false,
"minProperties": 1,
"properties": {
"systemd": {
"type": "boolean",
"default": false,
"description": "Enable systemd."
}
}
}
}
"""
def main(tree, options):
boot = options.get("boot", {})
if not boot:
return 0
systemd = boot.get("systemd", False)
wsl_conf_path = f"{tree}/etc/wsl.conf"
wsl_config = iniparse.SafeConfigParser()
try:
with open(wsl_conf_path, "r", encoding="utf8") as f:
wsl_config.readfp(f)
except FileNotFoundError:
print(f"WSL configuration file '{wsl_conf_path}' does not exist, will be created.")
if not wsl_config.has_section("boot"):
wsl_config.add_section("boot")
wsl_config.set("boot", "systemd", (f"{systemd}").lower())
with open(wsl_conf_path, mode="w", encoding="utf8") as f:
wsl_config.write(f)
return 0
if __name__ == '__main__':
args = osbuild.api.arguments()
r = main(args["tree"], args["options"])
sys.exit(r)