public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: "'Quirin Gylstorff' via isar-users" <isar-users@googlegroups.com>
To: isar-users@googlegroups.com, jan.kiszka@siemens.com,
	felix.moessbauer@siemens.com, cedric.hombourger@siemens.com
Subject: [PATCH v7 11/13] Add dracut module helper
Date: Wed,  5 Nov 2025 13:12:42 +0100	[thread overview]
Message-ID: <20251105121350.114449-12-Quirin.Gylstorff@siemens.com> (raw)
In-Reply-To: <20251105121350.114449-1-Quirin.Gylstorff@siemens.com>

From: Quirin Gylstorff <quirin.gylstorff@siemens.com>

This adds a helper class similar to initramfs-hook to generate a dracut
module based on dpkg-raw.

Signed-off-by: Quirin Gylstorff <quirin.gylstorff@siemens.com>
---
 meta/classes-recipe/dracut-module.bbclass     | 87 +++++++++++++++++++
 .../dracut-module/files/module-setup.sh.tmpl  | 42 +++++++++
 2 files changed, 129 insertions(+)
 create mode 100644 meta/classes-recipe/dracut-module.bbclass
 create mode 100644 meta/recipes-initramfs/dracut-module/files/module-setup.sh.tmpl

diff --git a/meta/classes-recipe/dracut-module.bbclass b/meta/classes-recipe/dracut-module.bbclass
new file mode 100644
index 00000000..364fb5b4
--- /dev/null
+++ b/meta/classes-recipe/dracut-module.bbclass
@@ -0,0 +1,87 @@
+#
+# Copyright (c) Siemens AG, 2025
+#
+# Authors:
+#  Quirin Gylstorff <quirin.gylstorff@siemens.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+inherit dpkg-raw
+
+FILESPATH:append = ":${LAYERDIR_core}/recipes-initramfs/dracut-module/files"
+
+DPKG_ARCH = "all"
+
+DRACUT_MODULE_SETUP = "module-setup.sh"
+SRC_URI:append = "file://${DRACUT_MODULE_SETUP}.tmpl"
+
+DRACUT_MODULE_NO ??= "50"
+DRACUT_MODULE_NAME ?= "${@ d.getVar('PN')[7:] if d.getVar('PN').startswith('dracut-') else d.getVAR('PN')}"
+
+DEBIAN_DEPENDS = "dracut-core"
+DRACUT_MODULE_PATH = "${D}/usr/lib/dracut/modules.d/${DRACUT_MODULE_NO}${DRACUT_MODULE_NAME}/"
+
+DRACUT_REQUIRED_BINARIES ??= ""
+DRACUT_MODULE_DEPENDENCIES ??= ""
+DRACUT_CHECK_CONTENT_FILE_NAME ??= ""
+DRACUT_DEPENDS_CONTENT_FILE_NAME ??= ""
+DRACUT_CMDLINE_CONTENT_FILE_NAME ??= ""
+DRACUT_INSTALL_CONTENT_FILE_NAME ??= ""
+DRACUT_INSTALLKERNEL_CONTENT_FILE_NAME ??= ""
+
+def add_file_if_variable_is_set(d, variable_name, prefix):
+    variable = d.getVar(variable_name) or ''
+    if variable:
+        return f"{prefix}{variable}"
+    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()
+
+    with open(content_file, 'r') as content_fd:
+        content = content_fd.read()
+
+    new_tpml_content = tmpl_content.replace(marker, content)
+    with open(template_file, 'w') as tmpl_fd:
+        tmpl_fd.write(new_tpml_content)
+
+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://')} \
+            ${@ add_file_if_variable_is_set(d, 'DRACUT_CMDLINE_CONTENT_FILE_NAME', 'file://')} \
+            ${@ add_file_if_variable_is_set(d, 'DRACUT_INSTALL_CONTENT_FILE_NAME', 'file://')} \
+            ${@ add_file_if_variable_is_set(d, 'DRACUT_INSTALLKERNEL_CONTENT_FILE_NAME', 'file://')}"
+
+TEMPLATE_FILES:append = " \
+    ${DRACUT_MODULE_SETUP}.tmpl \
+    "
+
+TEMPLATE_VARS:append = " \
+    DRACUT_REQUIRED_BINARIES \
+    DRACUT_MODULE_DEPENDENCIES \
+    "
+python do_add_additional_dracut_configuration() {
+    workdir = os.path.normpath(d.getVar('WORKDIR'))
+    module_setup = d.getVar('DRACUT_MODULE_SETUP')
+    module_setup_tpml = f"{module_setup}.tmpl"
+    content_file_name_to_marker = {
+        "DRACUT_CHECK_CONTENT_FILE_NAME" : "# ISAR_DRACUT_CHECK",
+        "DRACUT_DEPENDS_CONTENT_FILE_NAME" : "# ISAR_DRACUT_DEPENDS",
+        "DRACUT_CMDLINE_CONTENT_FILE_NAME" : "# ISAR_DRACUT_CMDLINE",
+        "DRACUT_INSTALL_CONTENT_FILE_NAME" : "# ISAR_DRACUT_INSTALL",
+        "DRACUT_INSTALLKERNEL_CONTENT_FILE_NAME" : "# ISAR_DRACUT_KERNELINSTALL"
+    }
+
+    for var_name, marker in content_file_name_to_marker.items():
+        file_name = d.getVar(var_name) or ''
+        if file_name:
+            replace_marker_with_file_content(f"{workdir}/{module_setup_tpml}",
+                f"{workdir}/{file_name}", marker)
+}
+addtask add_additional_dracut_configuration before do_transform_template after do_patch
+
+do_install[cleandirs] += "${DRACUT_MODULE_PATH}"
+do_install:append() {
+    install -m 770 ${WORKDIR}/${DRACUT_MODULE_SETUP} ${DRACUT_MODULE_PATH}
+}
diff --git a/meta/recipes-initramfs/dracut-module/files/module-setup.sh.tmpl b/meta/recipes-initramfs/dracut-module/files/module-setup.sh.tmpl
new file mode 100644
index 00000000..be0f4c54
--- /dev/null
+++ b/meta/recipes-initramfs/dracut-module/files/module-setup.sh.tmpl
@@ -0,0 +1,42 @@
+#!/bin/bash
+
+# called by dracut
+check() {
+
+    # If the binary(s) requirements are not fulfilled the module can't be installed.
+    require_binaries \
+        ${DRACUT_REQUIRED_BINARIES} \
+        || return 1
+    # ISAR_DRACUT_CHECK
+    return 0
+
+}
+
+# Module dependency requirements.
+depends() {
+    echo "${DRACUT_MODULE_DEPENDENCIES}"
+    # ISAR_DRACUT_DEPENDS
+    return 0
+
+}
+installkernel() {
+    # ISAR_DRACUT_KERNELINSTALL
+    return 0
+}
+
+cmdline() {
+    # ISAR_DRACUT_CMDLINE
+    return 0
+}
+
+install() {
+    for executable in ${DRACUT_REQUIRED_BINARIES}; do
+        if exec_path=$(command -v $executable 2>/dev/null); then
+            inst_binary "$exec_path"
+        else
+            echo "(ERROR): Unable to copy $executable" >&2
+            exit 1
+        fi
+    done
+    # ISAR_DRACUT_INSTALL
+}
-- 
2.51.0

-- 
You received this message because you are subscribed to the Google Groups "isar-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to isar-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/isar-users/20251105121350.114449-12-Quirin.Gylstorff%40siemens.com.

  parent reply	other threads:[~2025-11-05 12:14 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-05 12:12 [PATCH v7 00/13] Add support for dracut 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 01/13] Add dracut to custom kernel builds 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 02/13] rootfs: Allow to overwrite the initramfs generation cmds 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 03/13] rootfs: Add isar-work directory to rootfs mounts 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 04/13] rootfs: Copy the newly created initrd.img to the work directory 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 05/13] rootfs: Add dracut to initramfs generator 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 06/13] rootfs: exit immediately if INITRAMFS_GENERATOR_CMD fails 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 07/13] initramfs: allow to set the generator command 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 08/13] Add class to generate custom dracut initramfs 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 09/13] rootfs: add flag to use dracut if it is not part of the package list 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 10/13] Add example dracut initramfs 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` 'Quirin Gylstorff' via isar-users [this message]
2025-11-05 12:12 ` [PATCH v7 12/13] Use lighttpd as a example how to add a dracut module 'Quirin Gylstorff' via isar-users
2025-11-05 12:12 ` [PATCH v7 13/13] user_manual: Add dracut for initramfs generation 'Quirin Gylstorff' via isar-users

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251105121350.114449-12-Quirin.Gylstorff@siemens.com \
    --to=isar-users@googlegroups.com \
    --cc=Quirin.Gylstorff@siemens.com \
    --cc=cedric.hombourger@siemens.com \
    --cc=felix.moessbauer@siemens.com \
    --cc=jan.kiszka@siemens.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox