[v5,07/12] Add class to generate custom dracut initramfs

Message ID 20251030094451.1303871-8-Quirin.Gylstorff@siemens.com
State New
Headers show
Series Add support for dracut | expand

Commit Message

Quirin Gylstorff Oct. 30, 2025, 9:44 a.m. UTC
From: Quirin Gylstorff <quirin.gylstorff@siemens.com>

This class allows to customize a dracut initramfs by using
configuration files add addition modules and drivers.

It is recommended to use the addition of modules and drivers
sparely and prefer dracut configuration files.

This class has the option to add custom modules automatically to
the initramfs if:
 - The modules are provided by the ISAR build system
 - The module name is part of the package name, valid names are
   - dracut-<module-name>
   - <module-name>-dracut
   - <something>-dracut-<module-name>

Signed-off-by: Quirin Gylstorff <quirin.gylstorff@siemens.com>
---
 meta/classes/initrd-dracut.bbclass | 58 ++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 meta/classes/initrd-dracut.bbclass

Patch

diff --git a/meta/classes/initrd-dracut.bbclass b/meta/classes/initrd-dracut.bbclass
new file mode 100644
index 00000000..0602c364
--- /dev/null
+++ b/meta/classes/initrd-dracut.bbclass
@@ -0,0 +1,58 @@ 
+# This software is a part of ISAR.
+# This class provides the necessary options to
+# customize a dracut based initramfs.
+#
+# This class should not provide every dracut cmdline
+# option possible. Use the dracut configuration files.
+
+INITRAMFS_GENERATOR_PKG = "dracut"
+
+# The preferred way to configure dracut is to
+# provide dracut-config-<your-config> package which
+# contains all necessary config options
+DRACUT_CONFIG_PATH ??= ""
+
+# Variable to add additional kernel driver to the initrd
+DRACUT_EXTRA_DRIVERS ??= ""
+
+# Variable to add additional dracut modules to the initrd
+DRACUT_EXTRA_MODULES ??= ""
+
+# This option does not work with some of the dracut modules in Debian
+# as there is no standardized mapping between module name and package name
+DRACUT_EXTRACT_MODULES_FROM_PACKAGE_NAMES ??= "False"
+
+def extend_dracut_cmdline(d):
+    config_path = d.getVar('DRACUT_CONFIG_PATH') or ''
+    extra_drivers = d.getVar('DRACUT_EXTRA_DRIVERS') or ''
+    extra_modules = d.getVar('DRACUT_EXTRA_MODULES') or ''
+    enable_module_extraction = bb.utils.to_boolean(d.getVar('DRACUT_EXTRACT_MODULES_FROM_PACKAGE_NAMES'))
+    pkg_list = d.getVar('INITRAMFS_INSTALL') or ''
+
+    cmdline = []
+    modules_from_pkg_names = []
+    if enable_module_extraction:
+        for pkg in pkg_list.split():
+            # Skip dracut-config-* packages
+            if pkg.startswith('dracut-config-'):
+                continue
+            elif pkg.startswith('dracut-'):
+                modules_from_pkg_names.append(pkg[7:])
+            elif pkg.endswith('-dracut'):
+                modules_from_pkg_names.append(pkg[:-7])
+            elif '-dracut-' in pkg:
+                _, module_name = pkg.split('-dracut-', 1)
+                modules_from_pkg_names.append(module_name)
+        extra_modules = extra_modules + ' ' +' '.join(modules_from_pkg_names)
+
+    if config_path:
+        cmdline.append(f"--conf {config_path}")
+    if extra_drivers:
+        cmdline.append(f"--add-drivers {extra_drivers}")
+    if extra_modules:
+        cmdline.append(f"--add {extra_modules}")
+    return ' '.join(cmdline)
+
+ROOTFS_INITRAMFS_GENERATOR_CMDLINE:append = " ${@ extend_dracut_cmdline(d)}"
+
+inherit initramfs