Skip to contentSkip to conversation

Move tool for generating initramfs cpio to scripts/

PATCH Superseded Nicolas Schier 2 participants 5 messages Jul 20 – Aug 1 Kbuild

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Nicolas Schier

author maintainer
#0 lore raw

v2 cover Move tool for generating initramfs cpio to scripts/

initramfs cpio tools had been subject of several recent discussions on linux-kbuild. Let's get two minor steps forward by enabling a top-level target for building gen_init_cpio w/o the need of a valid kbuild configuration [1] and move the tools for generating the builtin initramfs from usr/ to scripts/ [2].

---
Changes in v2:
* Build gen_init_cpio only if CONFIG_BLK_DEV_INITRD (Thomas)
* Keep underscore top-level target name (scripts_gen_init_cpio)
(Thomas)
* Link to v1: https://patch.msgid.link/20260309-move-gen_init_cpio-to-scripts-v1-0-0c5059b1ec5b@kernel.org

---
Nicolas Schier (2):
kbuild: Mark usr_gen_init_cpio as no-dot-config-target
kbuild: Move gen_init_cpio and gen_initramfs.sh to scripts/

 MAINTAINERS                       | 1 +
 Makefile                          | 9 +++++----
 scripts/.gitignore                | 1 +
 scripts/Makefile                  | 3 +++
 {usr => scripts}/gen_init_cpio.c  | 0
 {usr => scripts}/gen_initramfs.sh | 2 +-
 scripts/remove-stale-files        | 2 ++
 usr/.gitignore                    | 4 +++-
 usr/Makefile                      | 4 +---
 9 files changed, 17 insertions(+), 9 deletions(-)

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260225-move-gen_init_cpio-to-scripts-9e89ef99e51a

Best regards,

Signature · 1 line
Nicolas
author maintainer
#1 lore raw

PATCH v2 1/2 kbuild: Mark usr_gen_init_cpio as no-dot-config-target

Let usr/gen_init_cpio be built also if there is no kernel configuration.

Askar Safin reported that gen_init_cpio may be of use before any kernel configuration is available. Add usr_gen_init_cpio to list of targets that do not require a .config file.

Fixes f8e05c10631e ("kbuild: Add top-level target for building gen_init_cpio")
---
 Makefile | 1 +
 1 file changed, 1 insertion(+)
Makefile +1 -0
index b9c5792c79e0160b3c787f8a686bd60d9f8066d7..41f6a1917866119c472a8876134824aded20a616 100644
@@ -298,6 +298,7 @@ no-dot-config-targets := $(clean-targets) \
 			 %asm-generic kernelversion %src-pkg dt_binding_check \
 			 dt_style_selftest \
 			 outputmakefile rustavailable rustfmt rustfmtcheck \
+			 usr_gen_init_cpio \
 			 run-command
 no-sync-config-targets := $(no-dot-config-targets) %install modules_sign kernelrelease \
 			  image_name
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH v2 2/2 kbuild: Move gen_init_cpio and gen_initramfs.sh to scripts/

In reply to Nicolas Schier

gen_init_cpio and gen_initramfs.sh are part of kbuild and required for all kernel builds w/ CONFIG_BLK_DEV_INITRD. Move both to scripts/ to be more clear about their importance.

---
 MAINTAINERS                       |  1 +
 Makefile                          | 10 +++++-----
 scripts/.gitignore                |  1 +
 scripts/Makefile                  |  3 +++
 {usr => scripts}/gen_init_cpio.c  |  0
 {usr => scripts}/gen_initramfs.sh |  2 +-
 scripts/remove-stale-files        |  2 ++
 usr/.gitignore                    |  4 +++-
 usr/Makefile                      |  4 +---
 9 files changed, 17 insertions(+), 10 deletions(-)
MAINTAINERS +1 -0
index 15011f5752a994cf1b354f490d6c4e411588df88..ec6d525177c79ce870d232d425bd830ef09bde42 100644
@@ -14038,6 +14038,7 @@ F:	scripts/basic/
 F:	scripts/clang-tools/
 F:	scripts/container
 F:	scripts/dummy-tools/
+F:	scripts/gen_init*
 F:	scripts/include/
 F:	scripts/install.sh
 F:	scripts/mk*
Makefile +5 -5
index 41f6a1917866119c472a8876134824aded20a616..9be54ded7131df69d558a03029f19d9bb70ab1f5 100644
@@ -298,7 +298,7 @@ no-dot-config-targets := $(clean-targets) \
 			 %asm-generic kernelversion %src-pkg dt_binding_check \
 			 dt_style_selftest \
 			 outputmakefile rustavailable rustfmt rustfmtcheck \
-			 usr_gen_init_cpio \
+			 scripts_gen_init_cpio \
 			 run-command
 no-sync-config-targets := $(no-dot-config-targets) %install modules_sign kernelrelease \
 			  image_name
@@ -1509,9 +1509,9 @@ ifdef CONFIG_HEADERS_INSTALL
 prepare: headers
 endif
 
-PHONY += usr_gen_init_cpio
-usr_gen_init_cpio: scripts_basic
-	$(Q)$(MAKE) $(build)=usr usr/gen_init_cpio
+PHONY += scripts_gen_init_cpio
+scripts_gen_init_cpio: scripts_basic
+	$(Q)$(MAKE) $(build)=scripts scripts/gen_init_cpio
 
 PHONY += scripts_unifdef
 scripts_unifdef: scripts_basic
@@ -1787,7 +1787,7 @@ distclean: mrproper
 # Packaging of the kernel to various formats
 # ---------------------------------------------------------------------------
 
-modules-cpio-pkg: usr_gen_init_cpio
+modules-cpio-pkg: scripts_gen_init_cpio
 
 %src-pkg: FORCE
 	$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.package $@
scripts/.gitignore +1 -0
index 4215c2208f7e4100e8047a6ac0407e3aea9a92f3..70045654391074295ba88aa8abf7507f7b7c8ae3 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 /asn1_compiler
+/gen_init_cpio
 /gen_packed_field_checks
 /generate_rust_target
 /insert-sys-cert
scripts/Makefile +3 -0
index 3434a82a119f090864ecb7c6444053d84f54461e..3d50805b0885a814ee58023c34ddf875ee0dfbe8 100644
@@ -53,6 +53,9 @@ ifdef CONFIG_BUILDTIME_MCOUNT_SORT
 HOSTCFLAGS_sorttable.o += -DMCOUNT_SORT_ENABLED
 endif
 
+hostprogs += gen_init_cpio
+always-$(CONFIG_BLK_DEV_INITRD) += gen_init_cpio
+
 # The following programs are only built on demand
 hostprogs += unifdef gen_packed_field_checks
 
scripts/gen_init_cpio.c renamed from usr/gen_init_cpio.c +0 -0
similarity index 100%
rename from usr/gen_init_cpio.c
rename to scripts/gen_init_cpio.c
scripts/gen_initramfs.sh renamed from usr/gen_initramfs.sh +1 -1
similarity index 99%
rename from usr/gen_initramfs.sh
rename to scripts/gen_initramfs.sh
index 7eba2fddf0ef2e4dd3a9000fe6203e16892d1207..27ac1fd48001aec0fb50440478a261f7a7f945f3 100755
@@ -247,4 +247,4 @@ done
 
 # If output_file is set we will generate cpio archive
 # we are careful to delete tmp files
-usr/gen_init_cpio $output $timestamp $cpio_list
+scripts/gen_init_cpio $output $timestamp $cpio_list
scripts/remove-stale-files +2 -0
index 6e39fa8540df2f6da9d8a1d1e90e105243b495a2..e46337e4bfb8f836e2afbbfe6770ae3c6d5fe73e 100755
@@ -26,3 +26,5 @@ rm -f scripts/selinux/genheaders/genheaders
 rm -f *.spec
 
 rm -f lib/test_fortify.log
+
+rm -f usr/gen_init_cpio
usr/.gitignore +3 -1
index 8996e7a889024fbb13511a22743a2bff01c896f1..13bf7a38355ce5a5ffc53479c573ddbf5853a0d8 100644
@@ -1,4 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
-/gen_init_cpio
 /initramfs_data.cpio
 /initramfs_inc_data
+
+# Keep around until cleanup in scripts/remove-stale-files
+/gen_init_cpio
usr/Makefile +1 -3
index e8f42478a0b76540180d191042cd58d3e4381f19..5b97ee66c00a02e32c4383c42df6370e331f5451 100644
@@ -16,8 +16,6 @@ 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 :=
 
@@ -72,7 +70,7 @@ quiet_cmd_initfs = GEN     $@
 # 2) There are changes in which files are included (added or deleted)
 # 3) If gen_init_cpio are newer than initramfs_data.cpio
 # 4) Arguments to gen_initramfs.sh changes
-$(obj)/initramfs_data.cpio: $(src)/gen_initramfs.sh $(obj)/gen_init_cpio $(deps_initramfs) FORCE
+$(obj)/initramfs_data.cpio: $(srctree)/scripts/gen_initramfs.sh scripts/gen_init_cpio $(deps_initramfs) FORCE
 	$(call if_changed,initfs)
 
 endif
Signature · 1 line
2.47.3
maintainer Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] kbuild: Move gen_init_cpio and gen_initramfs.sh to scripts/

scripts/gen_initramfs.sh

Quoted 11 lines
scripts/gen_init_cpio.c renamed from usr/gen_init_cpio.c +0 -0
similarity index 100%
rename from usr/gen_init_cpio.c
rename to scripts/gen_init_cpio.c
scripts/gen_initramfs.sh renamed from usr/gen_initramfs.sh +0 -0
similarity index 99%
rename from usr/gen_initramfs.sh
rename to scripts/gen_initramfs.sh
index 7eba2fddf0ef..27ac1fd48001 100755

The cpio commmand in scripts/Makefile.package needs an update for this move, otherwise modules-cpio-pkg will be broken.

Additionally, I see a number of references to "usr/gen_init" even after this is applied:

$ git grep usr/gen_init
Documentation/driver-api/early-userspace/early_userspace_support.rst:usr/gen_init_cpio utility (run 'usr/gen_init_cpio -h' to get the
Documentation/driver-api/early-userspace/early_userspace_support.rst:usr/gen_init_cpio.
Documentation/driver-api/early-userspace/early_userspace_support.rst:usr/gen_initramfs.sh.  This means that CONFIG_INITRAMFS_SOURCE
Documentation/driver-api/early-userspace/early_userspace_support.rst:usr/gen_init_cpio file directives are output.  If a file is
Documentation/driver-api/early-userspace/early_userspace_support.rst:specified as an argument to usr/gen_initramfs.sh then the
Documentation/driver-api/early-userspace/early_userspace_support.rst:processed by usr/gen_init_cpio.
Documentation/driver-api/early-userspace/early_userspace_support.rst:See also 'usr/gen_initramfs.sh -h'.
Documentation/driver-api/early-userspace/early_userspace_support.rst:   into initramfs either via modifying usr/gen_init_cpio.c or via the new
Documentation/filesystems/ramfs-rootfs-initramfs.rst:Run "usr/gen_init_cpio" (after the kernel build) to get a usage message
Documentation/filesystems/ramfs-rootfs-initramfs.rst:usr/gen_initramfs.sh), and proceeds to package up that directory
Documentation/filesystems/ramfs-rootfs-initramfs.rst:using the config file (by feeding it to usr/gen_init_cpio, which is created
Documentation/filesystems/ramfs-rootfs-initramfs.rst:from usr/gen_init_cpio.c).  The kernel's build-time cpio creation code is
Documentation/filesystems/ramfs-rootfs-initramfs.rst:   explained in buffer-format.rst, created in usr/gen_init_cpio.c, and
scripts/remove-stale-files:rm -f usr/gen_init_cpio
scripts/sbom/tests/cmd_graph/test_savedcmd_parser.py:        cmd = "sh ../usr/gen_initramfs.sh -o usr/initramfs_data.cpio -l usr/.initramfs_data.cpio.d    ../usr/default_cpio_list"
tools/testing/selftests/kho/vmtest.sh:        "$build_dir/usr/gen_init_cpio" "$tmp_dir/cpio_list" > "$initrd"
tools/testing/selftests/nolibc/Makefile.nolibc:        $(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(objtree)/usr/gen_init_cpio - > initramfs.cpio
usr/Kconfig:          "usr/gen_init_cpio" program in the kernel tree.

Otherwise, this seems reasonable to me. Feel free to carry

on the respin.

Signature · 2 lines
Cheers,
Nathan
author maintainer
#4 lore raw

Re: [PATCH v2 2/2] kbuild: Move gen_init_cpio and gen_initramfs.sh to scripts/

Quoted 6 lines · Nathan Chancellor
Quoted 6 lines
scripts/gen_init_cpio.c renamed from usr/gen_init_cpio.c +0 -0
similarity index 100%
rename from usr/gen_init_cpio.c
rename to scripts/gen_init_cpio.c
scripts/gen_initramfs.sh renamed from usr/gen_initramfs.sh +0 -0
similarity index 99%
Quoted 8 lines

scripts/gen_initramfs.sh

Quoted 5 lines

rename from usr/gen_initramfs.sh
rename to scripts/gen_initramfs.sh
index 7eba2fddf0ef..27ac1fd48001 100755

scripts/gen_initramfs.sh renamed from usr/gen_initramfs.sh +0 -0

The cpio commmand in scripts/Makefile.package needs an update for this move, otherwise modules-cpio-pkg will be broken.

Good catch, thanks!

Quoted 6 lines

Additionally, I see a number of references to "usr/gen_init" even after this is applied:

$ git grep usr/gen_init
Documentation/driver-api/early-userspace/early_userspace_support.rst:usr/gen_init_cpio utility (run 'usr/gen_init_cpio -h' to get the
Documentation/driver-api/early-userspace/early_userspace_support.rst:usr/gen_init_cpio.

[...]

Quoted 3 lines

tools/testing/selftests/kho/vmtest.sh: "$build_dir/usr/gen_init_cpio" "$tmp_dir/cpio_list" > "$initrd"
tools/testing/selftests/nolibc/Makefile.nolibc: $(QUIET_GEN)echo 'file /init nolibc-test 755 0 0' | $(objtree)/usr/gen_init_cpio - > initramfs.cpio
usr/Kconfig: "usr/gen_init_cpio" program in the kernel tree.

oh well, thanks! I completely forgot checking for references.

Quoted 5 lines

Otherwise, this seems reasonable to me. Feel free to carry

on the respin.

Will do, thank for the review!

Nicolas