Skip to contentSkip to conversation

systemd-systemctl: fix handling of instance unit files

PATCH New Artur Kowalski 1 participant 1 message Jan 31, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 systemd-systemctl: fix handling of instance unit files

This fixes handling of units with instance name in theirs filenames such as [email protected] or [email protected]. Such files cause template file to be ignored (for the specific instance).

Thanks to Markus Volk <[email protected]> for noticing the breakage when building images with gnome-shell:

| Error: Systemctl preset_all issue in [email protected]
| WARNING: /home/flk/poky/build/tmp/work/intel_corei7_64-poky-linux/gnome-image/1.0/temp/run.systemd_preset_all.2011511:158 exit 1 from 'systemctl --root="/home/flk/poky/build/tmp/work/intel_corei7_64-poky-linux/gnome-image/1.0/rootfs" --global --preset-mode=enable-only preset-all'

The problem manifested after cdc3b3028f6d71788b5fdd99436f69fbf18f613e because we enabled preset-all for user units - [email protected] is a user service, so prior to that change the file wasn't processed at all.

The error was triggered because there is no [email protected] file. With this patch applied systemctl script looks for instance unit file and falls back to template unit in case of lack thereof, keeping the same behavior upstream systemctl does.

---
 .../recipes-core/systemd/systemd-systemctl/systemctl | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
meta/recipes-core/systemd/systemd-systemctl/systemctl +11 -1
index 81c246a5b2..65e3157859 100755
@@ -224,7 +224,17 @@ class SystemdUnit():
             instance = None
             unit = self.unit
 
-        path = self._path_for_unit(unit)
+        if instance_unit_name is not None:
+            try:
+                # Try first with instance unit name. Systemd allows to create instance unit files
+                # e.g. `[email protected]` which cause template unit file to be ignored
+                # for the instance for which instance unit file is present. In that case, there may
+                # not be any template file at all.
+                path = self._path_for_unit(instance_unit_name)
+            except SystemdUnitNotFoundError:
+                path = self._path_for_unit(unit)
+        else:
+            path = self._path_for_unit(unit)
 
         if path.is_symlink():
             # ignore aliases
Signature · 1 line
2.47.1