diff mbox series

systemd.bbclass: deduplicate template and instance lines in preset file

Message ID 20250820025524.1361942-1-khangng@os.amperecomputing.com
State New
Headers show
Series systemd.bbclass: deduplicate template and instance lines in preset file | expand

Commit Message

Khang D Nguyen Aug. 20, 2025, 2:55 a.m. UTC
If SYSTEMD_SERVICE contains both template and instance names, the
preset file will contain two lines: one describing the template name
and one describing the instance names.

This is problematic because systemd.preset only use the first matching
one [1], leading to the instances not getting enabled.

For example, openbmc's obmc-console recipe has the following
final SYSTEMD_SERVICE variable:

```
SYSTEMD_SERVICE:obmc-console = " \
obmc-console@.service \
obmc-console-ssh@.service \
obmc-console-ssh@2200.service \
"
```

The resulting preset file will contain lines with the same name:

```
enable obmc-console@.service
enable obmc-console-ssh@.service
enable obmc-console-ssh@.service 2200
```

Fix this by interpreting the template name as a special case of empty
instances.

Tested: preset files are generated correctly:

```
enable obmc-console@.service
enable obmc-console-ssh@.service 2200
```

[1]: https://www.freedesktop.org/software/systemd/man/257/systemd.preset.html#Preset%20File%20Format

Fixes: f33d9b1f434e ("systemd.bbclass: generate preset for templates")
Signed-off-by: Khang D Nguyen <khangng@os.amperecomputing.com>
---
 meta/classes-recipe/systemd.bbclass | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-recipe/systemd.bbclass b/meta/classes-recipe/systemd.bbclass
index 12c59647be..0e9f7cfa33 100644
--- a/meta/classes-recipe/systemd.bbclass
+++ b/meta/classes-recipe/systemd.bbclass
@@ -249,9 +249,12 @@  python systemd_populate_packages() {
                     (servicename, instance, service_type) = re.split('[@.]', service)
                     template_services.setdefault(servicename + '@.' + service_type, []).append(instance)
                 else:
-                    fd.write("%s %s\n" % (action,service))
+                    template_services.setdefault(service, [])
             for template, instances in template_services.items():
-                fd.write("%s %s %s\n" % (action, template, ' '.join(instances)))
+                if instances:
+                    fd.write("%s %s %s\n" % (action, template, ' '.join(instances)))
+                else:
+                    fd.write("%s %s\n" % (action, template))
         d.appendVar("FILES:%s" % pkg, ' ' + oe.path.join(d.getVar("systemd_unitdir"), "%s-preset/98-%s.preset" % (prefix, pkg)))
 
     # Run all modifications once when creating package