From dbb4391b611db90f19c7f92e468ad686001a06f4 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 12 Jan 2025 20:46:22 -0800 Subject: [PATCH] action_utils: Implement methods to get/set the systemd boot target - To be used in GNOME app. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- plinth/action_utils.py | 18 ++++++++++++++++++ plinth/privileged/__init__.py | 11 ++++++----- plinth/privileged/service.py | 9 +++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/plinth/action_utils.py b/plinth/action_utils.py index 51ba3e866..337c45cd7 100644 --- a/plinth/action_utils.py +++ b/plinth/action_utils.py @@ -29,6 +29,24 @@ def is_systemd_running(): return os.path.exists('/run/systemd') +def systemd_get_default() -> str: + """Return the default target that systemd will boot into.""" + process = subprocess.run(['systemctl', 'get-default'], + stdout=subprocess.PIPE, check=True) + return process.stdout.decode().strip() + + +def systemd_set_default(target: str, isolate: bool = True): + """Set the default target that systemd will boot into. + + If 'isolate' is True, then immediate switch the current system target to + newly set target resulting in starting/stopping of services. + """ + subprocess.run(['systemctl', 'set-default', target], check=True) + if isolate: + subprocess.run(['systemctl', 'isolate', target], check=True) + + def service_daemon_reload(): """Reload systemd to ensure that newer unit files are read.""" subprocess.run(['systemctl', 'daemon-reload'], check=True, diff --git a/plinth/privileged/__init__.py b/plinth/privileged/__init__.py index 9c1093b9d..fa2c5f88d 100644 --- a/plinth/privileged/__init__.py +++ b/plinth/privileged/__init__.py @@ -5,12 +5,13 @@ from .config import dropin_is_valid, dropin_link, dropin_unlink from .packages import (filter_conffile_packages, install, is_package_manager_busy, remove, update) from .service import (disable, enable, is_enabled, is_running, mask, reload, - restart, start, stop, try_reload_or_restart, try_restart, - unmask) + restart, start, stop, systemd_set_default, + try_reload_or_restart, try_restart, unmask) __all__ = [ 'filter_conffile_packages', 'install', 'is_package_manager_busy', 'remove', - 'update', 'disable', 'enable', 'is_enabled', 'is_running', 'mask', - 'reload', 'restart', 'start', 'stop', 'try_reload_or_restart', - 'try_restart', 'unmask', 'dropin_is_valid', 'dropin_link', 'dropin_unlink' + 'update', 'systemd_set_default', 'disable', 'enable', 'is_enabled', + 'is_running', 'mask', 'reload', 'restart', 'start', 'stop', + 'try_reload_or_restart', 'try_restart', 'unmask', 'dropin_is_valid', + 'dropin_link', 'dropin_unlink' ] diff --git a/plinth/privileged/service.py b/plinth/privileged/service.py index d9c5450e3..657ec9c76 100644 --- a/plinth/privileged/service.py +++ b/plinth/privileged/service.py @@ -8,6 +8,15 @@ from plinth.actions import privileged from plinth.daemon import Daemon, RelatedDaemon +@privileged +def systemd_set_default(target: str, isolate: bool = True): + """Set the default target that systemd will boot into.""" + if target not in ['graphical.target', 'multi-user.target']: + raise ValueError('Invalid target') + + action_utils.systemd_set_default(target, isolate) + + @privileged def start(service: str): """Start a service."""