kbuild: add target to build a cpio containing modules
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
@@ -96,6 +96,11 @@ modules.order # /tar-install/ +# +# modules directory (make cpio-modules-pkg) +# +/modules-install/ + # # pacman files (make pacman-pkg) #
Makefile +1 -1
@@ -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
@@ -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]>
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
Signed-off-by Sascha Hauer <[email protected]>Co-developed-by Ahmad Fatoum <[email protected]>Signed-off-by Ahmad Fatoum <[email protected]>--- .gitignore | 5 +++++ Makefile | 2 +- scripts/Makefile.package | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-)
Nathan Chancellor
maintainerHi 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
Signed-off-by Sascha Hauer <[email protected]>Co-developed-by Ahmad Fatoum <[email protected]>Signed-off-by Ahmad Fatoum <[email protected]>--- .gitignore | 5 +++++ Makefile | 2 +- scripts/Makefile.package | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-).gitignore +5
-0@@ -96,6 +96,11 @@ modules.order # /tar-install/ +# +# modules directory (make cpio-modules-pkg) +# +/modules-install/ + # # pacman files (make pacman-pkg) #Makefile +1
-1@@ -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@@ -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-70d85a69892bBest regards,
Signature · 1 line
Ahmad Fatoum <[email protected]>
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_cpioThis seems like a bit of a hack to generate
gen_init_cpiothis way but I am not sure it is worth movingusr/gen_init_cpio.candusr/gen_initramfs.shelsewhere (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).cpioI 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-70d85a69892bBest 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 |
Nicolas Schier
maintainerOn Wed, Nov 19, 2025 at 11:39:36PM -0700, Nathan Chancellor wrote:
...
Quoted 8 lines
Quoted 8 lines
scripts/Makefile.package +0
-0@@ -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_cpioThis seems like a bit of a hack to generate
gen_init_cpiothis way but I am not sure it is worth movingusr/gen_init_cpio.candusr/gen_initramfs.shelsewhere (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
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.
Signed-off-by Sascha Hauer <[email protected]>Co-developed-by Ahmad Fatoum <[email protected]>Signed-off-by Ahmad Fatoum <[email protected]>--- .gitignore | 5 +++++ Makefile | 2 +- scripts/Makefile.package | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-).gitignore +5
-0@@ -96,6 +96,11 @@ modules.order # /tar-install/ +# +# modules directory (make cpio-modules-pkg) +# +/modules-install/ + # # pacman files (make pacman-pkg) #Makefile +1
-1@@ -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
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-70d85a69892bBest regards,
Signature · 1 line
Ahmad Fatoum <[email protected]>
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@@ -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 ingen_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_cpioDrop 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 |
Quoted 5 lines · Ahmad Fatoum
Quoted 4 lines · Thomas Weißschuh
Quoted 4 lines
scripts/Makefile.package +0
-0
(...)
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_SHELLfrom 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_cpiounconditional. 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