From 28ed8ed2595c2403737881660aac1308d6d91710 Mon Sep 17 00:00:00 2001 From: fiftydinar <65243233+fiftydinar@users.noreply.github.com> Date: Mon, 26 Feb 2024 17:55:06 +0100 Subject: [PATCH] feat(systemd): Add support for including systemd units (#144) * 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 * fix(systemd): Make included systemd unit check better It doesn't fail now if there is systemd folder present without any files. * chore(systemd): Code spacing fixes * chore(systemd): Disable dotglob, as it's not needed for systemd units * docs(systemd): Remove inclusion term --- modules/systemd/README.md | 11 ++++++++++- modules/systemd/systemd.sh | 19 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/modules/systemd/README.md b/modules/systemd/README.md index 32bbf0a..41adbfd 100644 --- a/modules/systemd/README.md +++ b/modules/systemd/README.md @@ -2,4 +2,13 @@ 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. -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/systemd.sh b/modules/systemd/systemd.sh index 1642db9..dc59f2a 100644 --- a/modules/systemd/systemd.sh +++ b/modules/systemd/systemd.sh @@ -3,6 +3,24 @@ # 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" + +if [[ -d "$SYSTEM_UNIT_INCLUDE" ]]; then + if [[ -n $(find "$SYSTEM_UNIT_INCLUDE" -type f) ]]; then + cp -r "$SYSTEM_UNIT_INCLUDE"/* "$SYSTEM_UNIT_DIR" + fi +fi +if [[ -d "$USER_UNIT_INCLUDE" ]]; then + if [[ -n $(find "$USER_UNIT_INCLUDE" -type f) ]]; then + cp -r "$USER_UNIT_INCLUDE"/* "$USER_UNIT_DIR" + fi +fi + +# 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 +30,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")