Skip to contentSkip to conversation

kbuild: add target to build a cpio containing modules

PATCH Applied Ahmad Fatoum 5 participants 8 messages Nov 25, 2025 – Nov 30, 2025 Kbuild

lore
Share & tools
lore
#0 lore raw

v2 cover kbuild: add target to build a cpio containing modules

---
Changes in v2:
- Add Simon's Tested-by and Reviewed-by
- rename to modules-cpio-pkg (Nathan)
- add separator/doc comment heading (Thomas)
- Make temp directory ignoring and cleanup automatic (Thomas)
- Use CONFIG_SHELL instead of shebang (Thomas)
- Use -o instead of redirecting stdout when creating cpio (Thomas)
- Drop superfluous -f as $(build)= overrides it (Thomas)
- Add extra preparatory patch that makes hostprogs := gen_init_cpio
unconditional
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Ahmad Fatoum (1):
initramfs: add gen_init_cpio to hostprogs unconditionally

Sascha Hauer (1):
kbuild: add target to build a cpio containing modules

 scripts/Makefile.package | 20 ++++++++++++++++++++
 usr/Makefile             |  4 ++--
 2 files changed, 22 insertions(+), 2 deletions(-)

---
base-commit: e9a6fb0bcdd7609be6969112f3fbfcce3b1d4a7c
change-id: 20251115-cpio-modules-pkg-70d85a69892b

Best regards,

Signature · 1 line
Ahmad Fatoum <[email protected]>
#1 lore raw

PATCH v2 1/2 initramfs: add gen_init_cpio to hostprogs unconditionally

gen_init_cpio is currently only needed when an initramfs cpio archive is to be created out of CONFIG_INITRAMFS_SOURCE's contents. In other cases, it's not added to hostprogs and no make target is available.

In preparation to use the host program from Makefile.package, define it unconditionally. The program will still only be built as needed.

---
 usr/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
usr/Makefile +2 -2
index f1779496bca78b4a56651b6af7221675ec7ade7a..e8f42478a0b76540180d191042cd58d3e4381f19 100644
@@ -16,6 +16,8 @@ obj-$(CONFIG_BLK_DEV_INITRD) := initramfs_data.o
 
 $(obj)/initramfs_data.o: $(obj)/initramfs_inc_data
 
+hostprogs := gen_init_cpio
+
 ramfs-input := $(CONFIG_INITRAMFS_SOURCE)
 cpio-data :=
 
@@ -48,8 +50,6 @@ ifeq ($(cpio-data),)
 
 cpio-data := $(obj)/initramfs_data.cpio
 
-hostprogs := gen_init_cpio
-
 # .initramfs_data.cpio.d is used to identify all files included
 # in initramfs and to detect if any files are added/removed.
 # Removed files are identified by directory timestamp being updated
Signature · 1 line
2.47.3
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] initramfs: add gen_init_cpio to hostprogs unconditionally

Quoted 5 lines · Ahmad Fatoum

gen_init_cpio is currently only needed when an initramfs cpio archive is to be created out of CONFIG_INITRAMFS_SOURCE's contents. In other cases, it's not added to hostprogs and no make target is available.

Quoted 7 lines

In preparation to use the host program from Makefile.package, define it unconditionally. The program will still only be built as needed.

---
 usr/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
#3 lore raw

PATCH v2 2/2 kbuild: add target to build a cpio containing modules

In reply to Ahmad Fatoum

From: Sascha Hauer <[email protected]>

Add a new package target to build a cpio archive containing the kernel modules. This is particularly useful to supplement an existing initramfs with the kernel modules so that the root filesystem can be started with all needed kernel modules without modifying it.

---
 scripts/Makefile.package | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
scripts/Makefile.package +20 -0
index 74bcb9e7f7a4516473481468a0fcf700c3bead33..83bfcf7cb09fd2d69b97e0c19a2b99c728835e8d 100644
@@ -189,6 +189,25 @@ tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
 tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
 	@:
 
+# modules-cpio-pkg - generate an initramfs with the modules
+# ---------------------------------------------------------------------------
+
+.tmp_modules_cpio: FORCE
+	$(Q)$(MAKE) -f $(srctree)/Makefile
+	$(Q)rm -rf $@
+	$(Q)$(MAKE) -f $(srctree)/Makefile INSTALL_MOD_PATH=$@ modules_install
+
+quiet_cmd_cpio = CPIO    $@
+      cmd_cpio = $(CONFIG_SHELL) $(srctree)/usr/gen_initramfs.sh -o $@ $<
+
+modules-$(KERNELRELEASE)-$(ARCH).cpio: .tmp_modules_cpio
+	$(Q)$(MAKE) $(build)=usr usr/gen_init_cpio
+	$(call cmd,cpio)
+
+PHONY += modules-cpio-pkg
+modules-cpio-pkg: modules-$(KERNELRELEASE)-$(ARCH).cpio
+	@:
+
 # perf-tar*-src-pkg - generate a source tarball with perf source
 # ---------------------------------------------------------------------------
 
@@ -245,6 +264,7 @@ help:
 	@echo '  tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball'
 	@echo '  tarxz-pkg           - Build the kernel as a xz compressed tarball'
 	@echo '  tarzst-pkg          - Build the kernel as a zstd compressed tarball'
+	@echo '  modules-cpio-pkg    - Build the kernel modules as cpio archive'
 	@echo '  perf-tar-src-pkg    - Build the perf source tarball with no compression'
 	@echo '  perf-targz-src-pkg  - Build the perf source tarball with gzip compression'
 	@echo '  perf-tarbz2-src-pkg - Build the perf source tarball with bz2 compression'
Signature · 1 line
2.47.3
maintainer Tested-by on v2 2/2
#4 lore raw
Quoted 21 lines · Ahmad Fatoum

From: Sascha Hauer <[email protected]>

Add a new package target to build a cpio archive containing the kernel modules. This is particularly useful to supplement an existing initramfs with the kernel modules so that the root filesystem can be started with all needed kernel modules without modifying it.

---
 scripts/Makefile.package | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
scripts/Makefile.package +0 -0
index 74bcb9e7f7a4516473481468a0fcf700c3bead33..83bfcf7cb09fd2d69b97e0c19a2b99c728835e8d 100644

scripts/Makefile.package tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar · line 198

Quoted 10 lines
scripts/Makefile.package +7 -0
@@ -189,6 +189,25 @@ tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
 tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
 	@:
 
+# modules-cpio-pkg - generate an initramfs with the modules
+# ---------------------------------------------------------------------------
+
+.tmp_modules_cpio: FORCE
+	$(Q)$(MAKE) -f $(srctree)/Makefile
+	$(Q)rm -rf $@
+	$(Q)$(MAKE) -f $(srctree)/Makefile INSTALL_MOD_PATH=$@ modules_install

Do you know why '-f $(srctree)/Makefile' is necessary here? As this and the make call two lines above call common top-level targets, I do not see the reason for explicitly adding the Makefile path. Am I missing something; or have you observed problems without '-f'?

scripts/Makefile.package tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar · line 205

Quoted 7 lines
scripts/Makefile.package +7 -0
+
+quiet_cmd_cpio = CPIO    $@
+      cmd_cpio = $(CONFIG_SHELL) $(srctree)/usr/gen_initramfs.sh -o $@ $<
+
+modules-$(KERNELRELEASE)-$(ARCH).cpio: .tmp_modules_cpio
+	$(Q)$(MAKE) $(build)=usr usr/gen_init_cpio
+	$(call cmd,cpio)

Removing the '$(MAKE) $(build)=usr usr/gen_init_cpio' line and adding

PHONY += usr_gen_init_cpio
usr_gen_init_cpio: scripts_basic

	$(Q)$(MAKE) $(build)=usr usr/gen_init_cpio

modules-cpio-pkg: usr_gen_init_cpio

to top-level Makefile could be used to allow make to compile gen_init_cpio in parallel instead of sequential. And it simplifies a possible move of gen_init_cpio from usr/ to scripts/, as Nathan suggested.

What do you think? (Top-level Makefile addition should probably better be integrated into patch 1.) I can also send this as a follow-up patch after merging your patches.

Nevertheless,

If there are no further remarks, I am going to merge the patch set before the weekend.

Thanks and kind regards,
Nicolas

maintainer Reviewed-by Tested-by on the v2 series
#5 lore raw
In reply to Ahmad Fatoum
Quoted 21 lines · Ahmad Fatoum

---
Changes in v2:
- Add Simon's Tested-by and Reviewed-by
- rename to modules-cpio-pkg (Nathan)
- add separator/doc comment heading (Thomas)
- Make temp directory ignoring and cleanup automatic (Thomas)
- Use CONFIG_SHELL instead of shebang (Thomas)
- Use -o instead of redirecting stdout when creating cpio (Thomas)
- Drop superfluous -f as $(build)= overrides it (Thomas)
- Add extra preparatory patch that makes hostprogs := gen_init_cpio
unconditional
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Ahmad Fatoum (1):
initramfs: add gen_init_cpio to hostprogs unconditionally

Sascha Hauer (1):
kbuild: add target to build a cpio containing modules

Quoted 11 lines
 scripts/Makefile.package | 20 ++++++++++++++++++++
 usr/Makefile             |  4 ++--
 2 files changed, 22 insertions(+), 2 deletions(-)

---
base-commit: e9a6fb0bcdd7609be6969112f3fbfcce3b1d4a7c
change-id: 20251115-cpio-modules-pkg-70d85a69892b

Best regards,

Signature · 1 line
Ahmad Fatoum <[email protected]>

Thanks a lot for implementing all of Thomas's suggestions (and thanks to him for providing them). This series looks good to me and I can imagine this being quite useful for testing.

Cheers,
Nathan

Reviewed-by on the v2 series
#6 lore raw
In reply to Ahmad Fatoum
Quoted 14 lines · Ahmad Fatoum

---
Changes in v2:
- Add Simon's Tested-by and Reviewed-by
- rename to modules-cpio-pkg (Nathan)
- add separator/doc comment heading (Thomas)
- Make temp directory ignoring and cleanup automatic (Thomas)
- Use CONFIG_SHELL instead of shebang (Thomas)
- Use -o instead of redirecting stdout when creating cpio (Thomas)
- Drop superfluous -f as $(build)= overrides it (Thomas)
- Add extra preparatory patch that makes hostprogs := gen_init_cpio
unconditional
- Link to v1: https://lore.kernel.org/r/[email protected]

Quoted 6 lines

---
Ahmad Fatoum (1):
initramfs: add gen_init_cpio to hostprogs unconditionally

Sascha Hauer (1):
kbuild: add target to build a cpio containing modules

For the series:

Quoted 12 lines
 scripts/Makefile.package | 20 ++++++++++++++++++++
 usr/Makefile             |  4 ++--
 2 files changed, 22 insertions(+), 2 deletions(-)

---
base-commit: e9a6fb0bcdd7609be6969112f3fbfcce3b1d4a7c
change-id: 20251115-cpio-modules-pkg-70d85a69892b

Best regards,

Signature · 1 line
Ahmad Fatoum <[email protected]>