From b965a31853b200a9851ea09ee6e0d20e10a6ffed Mon Sep 17 00:00:00 2001 From: fiftydinar <65243233+fiftydinar@users.noreply.github.com> Date: Sun, 25 Feb 2024 14:01:16 +0100 Subject: [PATCH] feat(systemd): Add support for including systemd units This also gets rid of `files` module dependency & exposes this function better to the users. Related issue: https://github.com/ublue-os/bling/issues/143 --- modules/systemd/README.md | 13 +++++++++++-- modules/systemd/module.yml | 4 ++-- modules/systemd/systemd.sh | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/modules/systemd/README.md b/modules/systemd/README.md index 32bbf0a..21cadff 100644 --- a/modules/systemd/README.md +++ b/modules/systemd/README.md @@ -1,5 +1,14 @@ # `systemd` -The `systemd` module streamlines the management of systemd units during image building. Units are divided into `system` and `user` categories, with `system` units managed directly using `systemctl` and `user` units using `systemctl --global`. You can specify which units to enable/disable or unmask/mask under each category. +The `systemd` module streamlines the inclusion & management of systemd units during image building. Units are divided into `system` and `user` categories, with `system` units managed directly using `systemctl` and `user` units using `systemctl --global`. You can specify which units to enable/disable or unmask/mask under each category. -Supported operations are [enabling](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#enable%20UNIT%E2%80%A6), [disabling](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#disable%20UNIT%E2%80%A6), [masking](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#mask%20UNIT%E2%80%A6%E2%80%A6) and [unmasking](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#unmask%20UNIT%E2%80%A6). \ No newline at end of file +You can also include your systemd units to be copied into system directories into these locations, +depending if your unit is `system` or `user` based: +`config/systemd/system` +`config/systemd/user` + +Those units are then copied into these folders (depending on unit base): +`/usr/lib/systemd/system` +`/usr/lib/systemd/user` + +Supported management operations are [enabling](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#enable%20UNIT%E2%80%A6), [disabling](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#disable%20UNIT%E2%80%A6), [masking](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#mask%20UNIT%E2%80%A6%E2%80%A6) and [unmasking](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#unmask%20UNIT%E2%80%A6). diff --git a/modules/systemd/module.yml b/modules/systemd/module.yml index 9a0a16e..449521d 100644 --- a/modules/systemd/module.yml +++ b/modules/systemd/module.yml @@ -1,5 +1,5 @@ name: systemd -shortdesc: The systemd module streamlines the management of systemd units during image building. +shortdesc: The systemd module streamlines the inclusion & management of systemd units during image building readme: https://raw.githubusercontent.com/ublue-os/bling/main/modules/systemd/README.md example: | type: systemd @@ -20,4 +20,4 @@ example: | masked: - example.service # Masked (does not run for the user, under any circumstances) unmasked: - - example.service # Unmasked (runs for the user, even if previously masked) \ No newline at end of file + - example.service # Unmasked (runs for the user, even if previously masked) diff --git a/modules/systemd/systemd.sh b/modules/systemd/systemd.sh index 1642db9..bd7c8bc 100644 --- a/modules/systemd/systemd.sh +++ b/modules/systemd/systemd.sh @@ -3,6 +3,22 @@ # Tell build process to exit if there are any errors. set -euo pipefail +# Copy included systemd unit files to system +SYSTEM_UNIT_INCLUDE="$CONFIG_DIRECTORY"/systemd/system +USER_UNIT_INCLUDE="$CONFIG_DIRECTORY"/systemd/user +SYSTEM_UNIT_DIR="/usr/lib/systemd/system" +USER_UNIT_DIR="/usr/lib/systemd/user" + +shopt -s dotglob +if [[ -d "$SYSTEM_UNIT_INCLUDE" ]]; then + cp -r "$SYSTEM_UNIT_INCLUDE"/* "$SYSTEM_UNIT_DIR" +fi +if [[ -d "$USER_UNIT_INCLUDE" ]]; then + cp -r "$USER_UNIT_INCLUDE"/* "$USER_UNIT_DIR" +fi +shopt -u dotglob + +# Systemd units configuration (enable, disable, unmask & mask) get_yaml_array ENABLED '.system.enabled[]' "$1" get_yaml_array DISABLED '.system.disabled[]' "$1" get_yaml_array UNMASKED '.system.unmasked[]' "$1" @@ -12,7 +28,6 @@ get_yaml_array USER_DISABLED '.user.disabled[]' "$1" get_yaml_array USER_UNMASKED '.user.unmasked[]' "$1" get_yaml_array USER_MASKED '.user.masked[]' "$1" - if [[ ${#ENABLED[@]} -gt 0 ]]; then for unit in "${ENABLED[@]}"; do unit=$(printf "$unit")