fix: preserve indentation in dracut module-setup.sh

Message ID 20251216101936.3259099-1-kumar.rakesh@siemens.com
State Under Review
Headers show
Series fix: preserve indentation in dracut module-setup.sh | expand

Commit Message

Rakesh Kumar Dec. 16, 2025, 10:19 a.m. UTC
The marker replacement logic treated the inserted file content
as plain text, which caused indentation to be lost in generated
scripts (e.g. module-setup.sh).

Update the replacement logic to detect the marker line’s
indentation and apply the same indentation to all non-empty lines
of the inserted content. This preserves the original formatting
and avoids syntax and readability issues in the generated files.

Signed-off-by: Rakesh Kumar <kumar.rakesh@siemens.com>
---
 doc/user_manual.md                        | 19 +++++++++++++---
 meta/classes-recipe/dracut-module.bbclass | 27 +++++++++++++++++------
 2 files changed, 36 insertions(+), 10 deletions(-)

Patch

diff --git a/doc/user_manual.md b/doc/user_manual.md
index e7955c12..731e84f1 100644
--- a/doc/user_manual.md
+++ b/doc/user_manual.md
@@ -1763,9 +1763,22 @@  module in the initrd or as a dependency to other modules. It defaults to
 `${PN}` without the prefix `dracut-`.
 - `DRACUT_MODULE_PATH` contains the path to the installed module. It is set
 to `${D}/usr/lib/dracut/modules.d/${DRACUT_MODULE_NO}${DRACUT_MODULE_NAME}/`
-
-The `install()` function is added by storing the file `install.sh` in the
-files directory of the dracut module.
+- `DRACUT_CHECK_CONTENT_FILE_NAME` contents for check() function
+- `DRACUT_DEPENDS_CONTENT_FILE_NAME` contents for depends() function
+- `DRACUT_CMDLINE_CONTENT_FILE_NAME` contents for cmdline() function
+- `DRACUT_INSTALL_CONTENT_FILE_NAME` contents for install() function
+- `DRACUT_INSTALLKERNEL_CONTENT_FILE_NAME` contents for installkernel() function
+
+Hook functions such as check(), depends(), cmdline(), install(), and
+installkernel() are provided by replacing placeholders in dracut module
+module-setup.sh template with content from individual files, selected via
+the corresponding `DRACUT_<FUNCTION>_CONTENT_FILE_NAME` variables.
+
+Example:
+
+The `install()` function can be provided by placing a file (for example,
+`install.sh`) in the dracut module’s files directory and assigning it to
+`DRACUT_INSTALL_CONTENT_FILE_NAME`.
 
 Other files can by added to the module by coping them to the Module folder
 with:
diff --git a/meta/classes-recipe/dracut-module.bbclass b/meta/classes-recipe/dracut-module.bbclass
index 364fb5b4..eb1e4fb6 100644
--- a/meta/classes-recipe/dracut-module.bbclass
+++ b/meta/classes-recipe/dracut-module.bbclass
@@ -37,15 +37,28 @@  def add_file_if_variable_is_set(d, variable_name, prefix):
     return ''
 
 def replace_marker_with_file_content(template_file, content_file, marker):
-    with open(template_file, 'r') as template_fd:
-        tmpl_content = template_fd.read()
+    import re, bb
 
-    with open(content_file, 'r') as content_fd:
-        content = content_fd.read()
+    tmpl = open(template_file).read()
+    content = open(content_file).read().rstrip('\n')
 
-    new_tpml_content = tmpl_content.replace(marker, content)
-    with open(template_file, 'w') as tmpl_fd:
-        tmpl_fd.write(new_tpml_content)
+    # locate marker and its indentation
+    m = re.search(rf'^(?P<indent>\s*){re.escape(marker)}\s*$', tmpl, re.MULTILINE)
+    if not m:
+        bb.fatal(f"Marker '{marker}' not found in {template_file}")
+
+    indent = m.group('indent')
+
+    # indent all non-blank lines
+    indented = '\n'.join(
+        (indent + line) if line.strip() else ''
+        for line in content.splitlines()
+    )
+
+    # replace the exact marker line
+    new_tmpl = tmpl[:m.start()] + indented + tmpl[m.end():]
+
+    open(template_file, 'w').write(new_tmpl)
 
 SRC_URI:append = " ${@ add_file_if_variable_is_set(d, 'DRACUT_CHECK_CONTENT_FILE_NAME', 'file://')} \
             ${@ add_file_if_variable_is_set(d, 'DRACUT_DEPENDS_CONTENT_FILE_NAME', 'file://')} \