action_utils: Implement methods to get/set the systemd boot target

- To be used in GNOME app.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2025-01-12 20:46:22 -08:00 committed by Veiko Aasa
parent cb487e1e4c
commit dbb4391b61
No known key found for this signature in database
GPG Key ID: 478539CAE680674E
3 changed files with 33 additions and 5 deletions

View File

@ -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,

View File

@ -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'
]

View File

@ -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."""