Skip to contentSkip to conversation

kbuild: add target to build a cpio containing modules

PATCH Superseded Ahmad Fatoum 5 participants 8 messages Nov 15, 2025 – Nov 28, 2025 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

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.

---
 .gitignore               |  5 +++++
 Makefile                 |  2 +-
 scripts/Makefile.package | 17 +++++++++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
.gitignore +5 -0
index 86a1ba0d90353962183b47c0c657ec877e5b2f9f..764d115400d90f0904b8f60ea8851a7860c16411 100644
@@ -96,6 +96,11 @@ modules.order
 #
 /tar-install/
 
+#
+# modules directory (make cpio-modules-pkg)
+#
+/modules-install/
+
 #
 # pacman files (make pacman-pkg)
 #
Makefile +1 -1
index fb4389aa5d5f1776f7bb5988102ed54f92491de7..19c7900d02ed5eee06f00820c138591b42e895e1 100644
@@ -1583,7 +1583,7 @@ CLEAN_FILES += vmlinux.symvers modules-only.symvers \
 # Directories & files removed with 'make mrproper'
 MRPROPER_FILES += include/config include/generated          \
 		  arch/$(SRCARCH)/include/generated .objdiff \
-		  debian snap tar-install PKGBUILD pacman \
+		  debian snap tar-install modules-install PKGBUILD pacman \
 		  .config .config.old .version \
 		  Module.symvers \
 		  certs/signing_key.pem \
scripts/Makefile.package +17 -0
index 74bcb9e7f7a4516473481468a0fcf700c3bead33..20eec9e2dec4dda3fa0ef64a15b80dccdcb55f90 100644
@@ -189,6 +189,22 @@ tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
 tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
 	@:
 
+modules-install: 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 = $(srctree)/usr/gen_initramfs.sh $< > $@
+
+modules-$(KERNELRELEASE)-$(ARCH).cpio: modules-install
+	$(Q)$(MAKE) -f $(srctree)/Makefile $(build)=usr cpio-data= usr/gen_init_cpio
+	$(call cmd,cpio)
+
+PHONY += cpio-modules-pkg
+cpio-modules-pkg: modules-$(KERNELRELEASE)-$(ARCH).cpio
+	@:
+
 # perf-tar*-src-pkg - generate a source tarball with perf source
 # ---------------------------------------------------------------------------
 
@@ -245,6 +261,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 '  cpio-modules-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'

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

Best regards,

Signature · 1 line
Ahmad Fatoum <[email protected]>
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 8 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.

Quoted 9 lines
---
 .gitignore               |  5 +++++
 Makefile                 |  2 +-
 scripts/Makefile.package | 17 +++++++++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
#2 lore raw
In reply to Ahmad Fatoum

Hi Ahmad,

Quoted 6 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.

Yeah, I would say this is pretty useful, I have already used it while triaging another issue.

Quoted 46 lines
---
 .gitignore               |  5 +++++
 Makefile                 |  2 +-
 scripts/Makefile.package | 17 +++++++++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
.gitignore +5 -0
index 86a1ba0d90353962183b47c0c657ec877e5b2f9f..764d115400d90f0904b8f60ea8851a7860c16411 100644
@@ -96,6 +96,11 @@ modules.order
 #
 /tar-install/
 
+#
+# modules directory (make cpio-modules-pkg)
+#
+/modules-install/
+
 #
 # pacman files (make pacman-pkg)
 #
Makefile +1 -1
index fb4389aa5d5f1776f7bb5988102ed54f92491de7..19c7900d02ed5eee06f00820c138591b42e895e1 100644
@@ -1583,7 +1583,7 @@ CLEAN_FILES += vmlinux.symvers modules-only.symvers \
 # Directories & files removed with 'make mrproper'
 MRPROPER_FILES += include/config include/generated          \
 		  arch/$(SRCARCH)/include/generated .objdiff \
-		  debian snap tar-install PKGBUILD pacman \
+		  debian snap tar-install modules-install PKGBUILD pacman \
 		  .config .config.old .version \
 		  Module.symvers \
 		  certs/signing_key.pem \
scripts/Makefile.package +0 -0
index 74bcb9e7f7a4516473481468a0fcf700c3bead33..20eec9e2dec4dda3fa0ef64a15b80dccdcb55f90 100644
@@ -189,6 +189,22 @@ tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
 tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
 	@:
 

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

Quoted 10 lines
scripts/Makefile.package +10 -0
+modules-install: 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 = $(srctree)/usr/gen_initramfs.sh $< > $@
+
+modules-$(KERNELRELEASE)-$(ARCH).cpio: modules-install
+	$(Q)$(MAKE) -f $(srctree)/Makefile $(build)=usr cpio-data= usr/gen_init_cpio

This seems like a bit of a hack to generate gen_init_cpio this way but I am not sure it is worth moving usr/gen_init_cpio.c and usr/gen_initramfs.sh elsewhere (scripts?) to make it a most accessible hostprog right now... Nicolas, do you have any opinions on this?

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

Quoted 4 lines
scripts/Makefile.package +4 -0
+	$(call cmd,cpio)
+
+PHONY += cpio-modules-pkg
+cpio-modules-pkg: modules-$(KERNELRELEASE)-$(ARCH).cpio

I think it would make a little more sense (and match the style of the perf package targets as well) to name this target modules-cpio-pkg.

Quoted 22 lines
scripts/Makefile.package +3 -1
+	@:
+
 # perf-tar*-src-pkg - generate a source tarball with perf source
 # ---------------------------------------------------------------------------
 
@@ -245,6 +261,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 '  cpio-modules-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'
 
---

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

Best regards,

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

Hi Nathan,

Quoted 17 lines · Nathan Chancellor

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

Quoted 11 lines · Ahmad Fatoum

From: Sascha Hauer <[email protected]>

scripts/Makefile.package +10 -0
+modules-install: 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 = $(srctree)/usr/gen_initramfs.sh $< > $@
+
+modules-$(KERNELRELEASE)-$(ARCH).cpio: modules-install
+	$(Q)$(MAKE) -f $(srctree)/Makefile $(build)=usr cpio-data= usr/gen_init_cpio

This seems like a bit of a hack to generate gen_init_cpio this way but I am not sure it is worth moving usr/gen_init_cpio.c and usr/gen_initramfs.sh elsewhere (scripts?) to make it a most accessible hostprog right now... Nicolas, do you have any opinions on this?

After incorporating Thomas' feedback, this is simplified to:

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

which looks much better.

Quoted 7 lines

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

Quoted 4 lines
scripts/Makefile.package +4 -0
+	$(call cmd,cpio)
+
+PHONY += cpio-modules-pkg
+cpio-modules-pkg: modules-$(KERNELRELEASE)-$(ARCH).cpio

I think it would make a little more sense (and match the style of the perf package targets as well) to name this target modules-cpio-pkg.

It currently matches the style of the tar packages, which start with tar-, but I can follow the perf- order. Makes more sense if it's extended to support compressed cpios in future too.

Cheers,
Ahmad

Quoted 24 lines
Quoted 23 lines
scripts/Makefile.package +3 -1
+	@:
+
 # perf-tar*-src-pkg - generate a source tarball with perf source
 # ---------------------------------------------------------------------------
@@ -245,6 +261,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 '  cpio-modules-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'
 
---

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

Best regards,

Signature · 1 line
Ahmad Fatoum <[email protected]>
Signature · 4 lines
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |
maintainer
#4 lore raw
In reply to Nathan Chancellor

On Wed, Nov 19, 2025 at 11:39:36PM -0700, Nathan Chancellor wrote:
...

Quoted 8 lines
Quoted 8 lines
scripts/Makefile.package +0 -0
index 74bcb9e7f7a4516473481468a0fcf700c3bead33..20eec9e2dec4dda3fa0ef64a15b80dccdcb55f90 100644
@@ -189,6 +189,22 @@ tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
 tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
 	@:
 
Quoted 15 lines

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

Quoted 10 lines
scripts/Makefile.package +10 -0
+modules-install: 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 = $(srctree)/usr/gen_initramfs.sh $< > $@
+
+modules-$(KERNELRELEASE)-$(ARCH).cpio: modules-install
+	$(Q)$(MAKE) -f $(srctree)/Makefile $(build)=usr cpio-data= usr/gen_init_cpio

This seems like a bit of a hack to generate gen_init_cpio this way but I am not sure it is worth moving usr/gen_init_cpio.c and usr/gen_initramfs.sh elsewhere (scripts?) to make it a most accessible hostprog right now... Nicolas, do you have any opinions on this?

Yes, it feels hacky to me, too. After digging into this a bit, I am in favor of moving at least gen_init* and friends to scripts/. I can prepare a patch in a few days.

Kind regards,
Nicolas

#5 lore raw
In reply to Ahmad Fatoum
Quoted 50 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.

---
 .gitignore               |  5 +++++
 Makefile                 |  2 +-
 scripts/Makefile.package | 17 +++++++++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
.gitignore +5 -0
index 86a1ba0d90353962183b47c0c657ec877e5b2f9f..764d115400d90f0904b8f60ea8851a7860c16411 100644
@@ -96,6 +96,11 @@ modules.order
 #
 /tar-install/
 
+#
+# modules directory (make cpio-modules-pkg)
+#
+/modules-install/
+
 #
 # pacman files (make pacman-pkg)
 #
Makefile +1 -1
index fb4389aa5d5f1776f7bb5988102ed54f92491de7..19c7900d02ed5eee06f00820c138591b42e895e1 100644
@@ -1583,7 +1583,7 @@ CLEAN_FILES += vmlinux.symvers modules-only.symvers \
 # Directories & files removed with 'make mrproper'
 MRPROPER_FILES += include/config include/generated          \
 		  arch/$(SRCARCH)/include/generated .objdiff \
-		  debian snap tar-install PKGBUILD pacman \
+		  debian snap tar-install modules-install PKGBUILD pacman \
 		  .config .config.old .version \
 		  Module.symvers \
 		  certs/signing_key.pem \
scripts/Makefile.package +0 -0
index 74bcb9e7f7a4516473481468a0fcf700c3bead33..20eec9e2dec4dda3fa0ef64a15b80dccdcb55f90 100644

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

Quoted 2 lines
scripts/Makefile.package +0 -0
@@ -189,6 +189,22 @@ tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
 tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
 	@:

The other package targets have a separator and documentation comment.

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

Quoted 4 lines
scripts/Makefile.package +3 -0
+modules-install: FORCE
+	$(Q)$(MAKE) -f $(srctree)/Makefile
+	$(Q)rm -rf $@

We have the filename prefix ".tmp_" for temporary files and directories. These will be automatically ignored by git and cleaned up by "make clean".

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

Quoted 4 lines
scripts/Makefile.package +4 -0
+	$(Q)$(MAKE) -f $(srctree)/Makefile INSTALL_MOD_PATH=$@ modules_install
+
+quiet_cmd_cpio = CPIO    $@
+      cmd_cpio = $(srctree)/usr/gen_initramfs.sh $< > $@

Use $(CONFIG_SHELL).

Using '-o' instead of redirecting stdout to the output file should enable the usage of the recent copy_file_range() optimizations in gen_init_cpio.

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

Quoted 3 lines
scripts/Makefile.package +3 -0
+
+modules-$(KERNELRELEASE)-$(ARCH).cpio: modules-install
+	$(Q)$(MAKE) -f $(srctree)/Makefile $(build)=usr cpio-data= usr/gen_init_cpio

Drop the explicit '-f'; $(build) overwrites it.

Is the cpio-data= intended to make sure the line 'hostprogs := gen_init_cpio' is executed? I don't think this works as usr/Makefile will overwrite 'cpio-data' in any case. But it should be fine to make hostprogs definition unconditional.

Quoted 26 lines
scripts/Makefile.package +7 -1
+	$(call cmd,cpio)
+
+PHONY += cpio-modules-pkg
+cpio-modules-pkg: modules-$(KERNELRELEASE)-$(ARCH).cpio
+	@:
+
 # perf-tar*-src-pkg - generate a source tarball with perf source
 # ---------------------------------------------------------------------------
 
@@ -245,6 +261,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 '  cpio-modules-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'
 
---

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

Best regards,

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

Hello Thomas,

Quoted 9 lines · Thomas Weißschuh

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

Quoted 7 lines
scripts/Makefile.package +0 -0
index 74bcb9e7f7a4516473481468a0fcf700c3bead33..20eec9e2dec4dda3fa0ef64a15b80dccdcb55f90 100644
@@ -189,6 +189,22 @@ tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
 tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
 	@:

The other package targets have a separator and documentation comment.

Ack.

Quoted 6 lines

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

Quoted 3 lines
scripts/Makefile.package +3 -0
+modules-install: FORCE
+	$(Q)$(MAKE) -f $(srctree)/Makefile
+	$(Q)rm -rf $@

We have the filename prefix ".tmp_" for temporary files and directories. These will be automatically ignored by git and cleaned up by "make clean".

The commit originally followed what the tar target did, but it seems doing it like perf is the better template. Will adapt.

Quoted 4 lines

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

Quoted 2 lines
scripts/Makefile.package +2 -0
+quiet_cmd_cpio = CPIO    $@
+      cmd_cpio = $(srctree)/usr/gen_initramfs.sh $< > $@

Use $(CONFIG_SHELL).

Can do. Is this to allow users to override CONFIG_SHELL from outside? If so, why not go one step further and remove shebangs from scripts altogether to enforce that they are called with a $(VARIABLE) that can be overridden?

Quoted 2 lines

Using '-o' instead of redirecting stdout to the output file should enable the usage of the recent copy_file_range() optimizations in gen_init_cpio.

Will give it a try, thanks.

Quoted 4 lines

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

Quoted 2 lines
scripts/Makefile.package +2 -0
+modules-$(KERNELRELEASE)-$(ARCH).cpio: modules-install
+	$(Q)$(MAKE) -f $(srctree)/Makefile $(build)=usr cpio-data= usr/gen_init_cpio

Drop the explicit '-f'; $(build) overwrites it.

Indeed, will drop.

Quoted 3 lines

Is the cpio-data= intended to make sure the line 'hostprogs := gen_init_cpio' is executed? I don't think this works as usr/Makefile will overwrite 'cpio-data' in any case. But it should be fine to make hostprogs definition unconditional.

Will add a commit making hostprogs := gen_init_cpio unconditional. The current approach worked though, because specifying the variable on the command line overrides cpio-data := assignments even if there are multiple.

Thanks,
Ahmad

Signature · 4 lines
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |
#7 lore raw
Quoted 5 lines · Ahmad Fatoum
Quoted 4 lines · Thomas Weißschuh
Quoted 4 lines
scripts/Makefile.package +0 -0
index 74bcb9e7f7a4516473481468a0fcf700c3bead33..20eec9e2dec4dda3fa0ef64a15b80dccdcb55f90 100644

(...)

Quoted 6 lines
Quoted 4 lines

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

Quoted 2 lines
scripts/Makefile.package +2 -0
+quiet_cmd_cpio = CPIO    $@
+      cmd_cpio = $(srctree)/usr/gen_initramfs.sh $< > $@

Use $(CONFIG_SHELL).

Can do. Is this to allow users to override CONFIG_SHELL from outside?

I think so. Some distributions do weird things.

Quoted 3 lines

If so, why not go one step further and remove shebangs from scripts altogether to enforce that they are called with a $(VARIABLE) that can be overridden?

The shebang is also useful to enable syntax highlighting in editors, tell linters which shell dialect (POSIX/bash) is expected and to run the script without kbuild. I would just leave it as is.

(...)

Quoted 8 lines
Quoted 3 lines

Is the cpio-data= intended to make sure the line 'hostprogs := gen_init_cpio' is executed? I don't think this works as usr/Makefile will overwrite 'cpio-data' in any case. But it should be fine to make hostprogs definition unconditional.

Will add a commit making hostprogs := gen_init_cpio unconditional. The current approach worked though, because specifying the variable on the command line overrides cpio-data := assignments even if there are multiple.

Indeed. Good to know, thanks!

Thomas