target-bootstrapper-service: add runtime serial tty drop-in generation

Message ID 20260619103944.1303513-1-gouravsingh@siemens.com
State New
Headers show
Series target-bootstrapper-service: add runtime serial tty drop-in generation | expand

Commit Message

Gourav Singh June 19, 2026, 10:39 a.m. UTC
Extend the recipe to support dynamically detected serial consoles without
taking over every getty instance.

The existing loop still installs overrides for statically configured
TARGET_BOOTSTRAPPER_TTY_SERVICES. In addition, a runtime oneshot service
and helper script are installed to detect the first available serial tty
(ttyACM*/ttyUSB*/ttyAMA*/ttyGS*) and create one matching
serial-getty@<tty>.service.d override from a stored template.

This keeps installer behavior on configured ttys, adds support for
runtime-discovered serial devices, and preserves other consoles for
normal login/debug access.

Signed-off-by: Gourav Singh <gouravsingh@siemens.com>
---
 .../generate-target-bootstrapper-dropin.sh    | 47 +++++++++++++++++++
 .../files/postinst.tmpl                       |  3 ++
 ...arget-bootstrapper-generate-dropin.service | 12 +++++
 .../target-bootstrapper-service.bb            | 14 +++++-
 4 files changed, 74 insertions(+), 2 deletions(-)
 create mode 100644 meta-isar/recipes-installer/target-bootstrapper-service/files/generate-target-bootstrapper-dropin.sh
 create mode 100644 meta-isar/recipes-installer/target-bootstrapper-service/files/target-bootstrapper-generate-dropin.service

Patch

diff --git a/meta-isar/recipes-installer/target-bootstrapper-service/files/generate-target-bootstrapper-dropin.sh b/meta-isar/recipes-installer/target-bootstrapper-service/files/generate-target-bootstrapper-dropin.sh
new file mode 100644
index 00000000..50c3ba40
--- /dev/null
+++ b/meta-isar/recipes-installer/target-bootstrapper-service/files/generate-target-bootstrapper-dropin.sh
@@ -0,0 +1,47 @@ 
+#!/bin/sh
+# This software is a part of Isar.
+# Copyright (C) Siemens AG, 2026
+#
+# SPDX-License-Identifier: MIT
+
+set -e
+
+OVERRIDE_DIR="/usr/lib/systemd/system"
+DROP_IN_SUFFIX="d/10-target-bootstrapper.override.conf"
+
+# Check if override template exists
+if [ ! -f "/usr/lib/target-bootstrapper.override.conf" ]; then
+    exit 0
+fi
+
+# Detect first available serial device (ttyACM*, ttyUSB*, ttyAMA*, etc.)
+detect_first_serial_device() {
+    for pattern in ttyACM ttyUSB ttyAMA ttyGS; do
+        for dev in /dev/${pattern}*; do
+            if [ -c "$dev" ]; then
+                basename "$dev"
+                return 0
+            fi
+        done
+    done
+}
+
+DETECTED_TTY=$(detect_first_serial_device 2>/dev/null)
+
+if [ -z "$DETECTED_TTY" ]; then
+    exit 0
+fi
+
+# Map device name to getty service instance
+# e.g. ttyACM0 -> serial-getty@ttyACM0.service
+TTY_SERVICE="serial-getty@${DETECTED_TTY}.service"
+DROP_IN_DIR="${OVERRIDE_DIR}/${TTY_SERVICE}.d"
+
+# Create drop-in directory and install override
+mkdir -p "$DROP_IN_DIR"
+cp "/usr/lib/target-bootstrapper.override.conf" "$DROP_IN_DIR/${DROP_IN_SUFFIX##*/}"
+
+# Reload systemd to pick up new drop-ins
+systemctl daemon-reload || true
+
+exit 0
diff --git a/meta-isar/recipes-installer/target-bootstrapper-service/files/postinst.tmpl b/meta-isar/recipes-installer/target-bootstrapper-service/files/postinst.tmpl
index 7c24af17..57851956 100644
--- a/meta-isar/recipes-installer/target-bootstrapper-service/files/postinst.tmpl
+++ b/meta-isar/recipes-installer/target-bootstrapper-service/files/postinst.tmpl
@@ -5,3 +5,6 @@  set -e
 for tty_service in ${TARGET_BOOTSTRAPPER_TTY_SERVICES}; do
     deb-systemd-helper enable ${tty_service} || true
 done
+
+# Enable the runtime detection service
+deb-systemd-helper enable target-bootstrapper-generate-dropin.service || true
diff --git a/meta-isar/recipes-installer/target-bootstrapper-service/files/target-bootstrapper-generate-dropin.service b/meta-isar/recipes-installer/target-bootstrapper-service/files/target-bootstrapper-generate-dropin.service
new file mode 100644
index 00000000..38457c8d
--- /dev/null
+++ b/meta-isar/recipes-installer/target-bootstrapper-service/files/target-bootstrapper-generate-dropin.service
@@ -0,0 +1,12 @@ 
+[Unit]
+Description=Generate target-bootstrapper drop-in for detected serial devices
+Before=serial-getty@.service getty@.service
+After=dev-*.device
+
+[Service]
+Type=oneshot
+ExecStart=/usr/libexec/target-bootstrapper-service/generate-target-bootstrapper-dropin.sh
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-isar/recipes-installer/target-bootstrapper-service/target-bootstrapper-service.bb b/meta-isar/recipes-installer/target-bootstrapper-service/target-bootstrapper-service.bb
index e7a8b2a1..572cddcd 100644
--- a/meta-isar/recipes-installer/target-bootstrapper-service/target-bootstrapper-service.bb
+++ b/meta-isar/recipes-installer/target-bootstrapper-service/target-bootstrapper-service.bb
@@ -25,6 +25,8 @@  inherit dpkg-raw
 SRC_URI = "\
     file://postinst.tmpl \
     file://target-bootstrapper.override.conf \
+    file://generate-target-bootstrapper-dropin.sh \
+    file://target-bootstrapper-generate-dropin.service \
     "
 
 TEMPLATE_FILES = "postinst.tmpl"
@@ -33,11 +35,19 @@  TEMPLATE_VARS = "TARGET_BOOTSTRAPPER_TTY_SERVICES"
 DEPENDS += " target-bootstrapper"
 DEBIAN_DEPENDS = "target-bootstrapper"
 
-do_install[cleandirs] = "${D}/usr/lib/systemd/system/"
+do_install[cleandirs] = "${D}/usr/lib/systemd/system/ ${D}/usr/libexec"
 do_install() {
     for svc_name in ${TARGET_BOOTSTRAPPER_TTY_SERVICES}
     do
-        mkdir -p ${D}/usr/lib/systemd/system/${svc_name}.service.d/
+        install -d -m 0755 ${D}/usr/lib/systemd/system/${svc_name}.service.d/
         install -m 0644 ${WORKDIR}/target-bootstrapper.override.conf ${D}/usr/lib/systemd/system/${svc_name}.service.d/10-target-bootstrapper.override.conf
     done
+
+    # Install script and service for runtime detection of serial devices
+    install -d -m 0755 ${D}/usr/libexec/${PN}/
+    install -m 0755 ${WORKDIR}/generate-target-bootstrapper-dropin.sh ${D}/usr/libexec/${PN}/
+    install -m 0644 ${WORKDIR}/target-bootstrapper-generate-dropin.service ${D}/usr/lib/systemd/system/
+
+    # Install override template for runtime use by the detection script
+    install -m 0644 ${WORKDIR}/target-bootstrapper.override.conf ${D}/usr/lib/target-bootstrapper.override.conf
 }