From 714886f3e4f0d4f5eb8ec299edcfd137c409c8d0 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Fri, 18 Sep 2026 19:06:56 +0200 Subject: [PATCH] kernel: build the separate initramfs kernel only once With TARGET_ROOTFS_INITRAMFS_SEPARATE, the initramfs kernel differs from the regular kernel only in CONFIG_RD_. Both are built in the same tree, so every build switches the kernel config twice and relinks vmlinux twice, even if nothing changed. Give the regular kernel the same ramdisk support and use its image for the initramfs as is. The initramfs step now only creates the initrd. Remove the separate initramfs branches of Kernel/SetInitramfs, as it is no longer called in this mode. Signed-off-by: John Crispin --- include/kernel-defaults.mk | 62 +++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/include/kernel-defaults.mk b/include/kernel-defaults.mk index eb852a04cab..70e3f3f5946 100644 --- a/include/kernel-defaults.mk +++ b/include/kernel-defaults.mk @@ -45,31 +45,21 @@ else endif ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),y) - ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE),y) + ifeq ($(strip $(CONFIG_EXTERNAL_CPIO)),"") define Kernel/SetInitramfs/PreConfigure - { \ - grep -v -e CONFIG_BLK_DEV_INITRD $(2)/.config.old > $(2)/.config; \ - echo 'CONFIG_BLK_DEV_INITRD=y' >> $(2)/.config; \ - echo 'CONFIG_INITRAMFS_SOURCE=""' >> $(2)/.config; \ - } - endef - else - ifeq ($(strip $(CONFIG_EXTERNAL_CPIO)),"") - define Kernel/SetInitramfs/PreConfigure { \ grep -v -e INITRAMFS -e CONFIG_RD_ -e CONFIG_BLK_DEV_INITRD $(2)/.config.old > $(2)/.config; \ echo 'CONFIG_BLK_DEV_INITRD=y' >> $(2)/.config; \ echo 'CONFIG_INITRAMFS_SOURCE="$(strip $(1) $(INITRAMFS_EXTRA_FILES))"' >> $(2)/.config; \ } - endef - else - define Kernel/SetInitramfs/PreConfigure + endef + else + define Kernel/SetInitramfs/PreConfigure { \ grep -v INITRAMFS $(2)/.config.old > $(2)/.config; \ echo 'CONFIG_INITRAMFS_SOURCE="$(call qstrip,$(CONFIG_EXTERNAL_CPIO))"' >> $(2)/.config; \ } - endef - endif + endef endif define Kernel/SetInitramfs @@ -78,13 +68,11 @@ ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),y) mv $(2)/.config $(2)/.config.old; \ $(call Kernel/SetInitramfs/PreConfigure,$(1),$(2)); \ echo "# CONFIG_INITRAMFS_PRESERVE_MTIME is not set" >> $(2)/.config; \ - $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE),,echo "CONFIG_INITRAMFS_ROOT_UID=$(shell id -u)" >> $(2)/.config;) \ - $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE),,echo "CONFIG_INITRAMFS_ROOT_GID=$(shell id -g)" >> $(2)/.config;) \ - $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE), \ - echo "# CONFIG_INITRAMFS_FORCE is not set" >> $(2)/.config;, \ - $(if $(CONFIG_TARGET_INITRAMFS_FORCE), \ - echo "CONFIG_INITRAMFS_FORCE=y" >> $(2)/.config;, \ - echo "# CONFIG_INITRAMFS_FORCE is not set" >> $(2)/.config;)) \ + echo "CONFIG_INITRAMFS_ROOT_UID=$(shell id -u)" >> $(2)/.config; \ + echo "CONFIG_INITRAMFS_ROOT_GID=$(shell id -g)" >> $(2)/.config; \ + $(if $(CONFIG_TARGET_INITRAMFS_FORCE), \ + echo "CONFIG_INITRAMFS_FORCE=y" >> $(2)/.config;, \ + echo "# CONFIG_INITRAMFS_FORCE is not set" >> $(2)/.config;) \ $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_NONE), \ echo "CONFIG_INITRAMFS_COMPRESSION_NONE=y" >> $(2)/.config;, \ echo "# CONFIG_INITRAMFS_COMPRESSION_NONE is not set" >> $(2)/.config; ) \ @@ -92,21 +80,33 @@ ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),y) $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_$(ALGO)), \ echo "CONFIG_INITRAMFS_COMPRESSION_$(ALGO)=y" >> $(2)/.config; $\, \ echo "# CONFIG_INITRAMFS_COMPRESSION_$(ALGO) is not set" >> $(2)/.config; $\) \ - $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_$(ALGO)), \ - echo "CONFIG_RD_$(ALGO)=y" >> $(2)/.config; $\, \ - echo "# CONFIG_RD_$(ALGO) is not set" >> $(2)/.config; $\) \ ) \ + $(call Kernel/SetRamdiskCompression,$(2)/.config) \ } endef else endif +define Kernel/SetRamdiskCompression + $(foreach ALGO,GZIP BZIP2 LZMA LZO XZ LZ4 ZSTD, \ + $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_$(ALGO)), \ + echo "CONFIG_RD_$(ALGO)=y" >> $(1); $\, \ + echo "# CONFIG_RD_$(ALGO) is not set" >> $(1); $\) \ + ) +endef + +# With a separate initramfs, the initramfs kernel differs from the regular +# kernel only in ramdisk support. Enable it here too, so that one kernel +# build serves both images. define Kernel/SetNoInitramfs mv $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.old grep -v INITRAMFS $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config.set echo 'CONFIG_INITRAMFS_SOURCE=""' >> $(LINUX_DIR)/.config.set echo '# CONFIG_INITRAMFS_FORCE is not set' >> $(LINUX_DIR)/.config.set echo "# CONFIG_INITRAMFS_PRESERVE_MTIME is not set" >> $(LINUX_DIR)/.config.set + $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE), \ + echo 'CONFIG_BLK_DEV_INITRD=y' >> $(LINUX_DIR)/.config.set; \ + $(call Kernel/SetRamdiskCompression,$(LINUX_DIR)/.config.set)) endef define Kernel/Configure/Default @@ -180,15 +180,16 @@ endef ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),) # $1: Custom TARGET_DIR. If omitted TARGET_DIR is used. # $2: If defined Generate Per Rootfs Kernel Directory and use it -# For Separate Initramf with $2 declared, skip kernel compile, it has -# already been done previously on generic image build +# For Separate Initramfs, the regular kernel is used as is, as its config +# already has the ramdisk support (see Kernel/SetNoInitramfs) define Kernel/CompileImage/Initramfs $(call locked,{ \ $(if $(2),$(call Kernel/PrepareConfigPerRootfs,$(LINUX_DIR)$(2));) \ - $(call Kernel/Configure/Initramfs,$(if $(1),$(1),$(TARGET_DIR)),$(LINUX_DIR)$(2)); \ + $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE),, \ + $(call Kernel/Configure/Initramfs,$(if $(1),$(1),$(TARGET_DIR)),$(LINUX_DIR)$(2)); \ + rm -rf $(LINUX_DIR)$(2)/usr/initramfs_data.cpio*;) \ $(CP) $(GENERIC_PLATFORM_DIR)/other-files/init $(if $(1),$(1),$(TARGET_DIR))/init; \ $(if $(SOURCE_DATE_EPOCH),touch -hcd "@$(SOURCE_DATE_EPOCH)" $(if $(1),$(1),$(TARGET_DIR)) $(if $(1),$(1),$(TARGET_DIR))/init;) \ - rm -rf $(LINUX_DIR)$(2)/usr/initramfs_data.cpio*; \ $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE), \ $(call locked,{ \ $(if $(call qstrip,$(CONFIG_EXTERNAL_CPIO)), \ @@ -210,8 +211,7 @@ define Kernel/CompileImage/Initramfs $(STAGING_DIR_HOST)/bin/xz -T$(if $(filter 1,$(NPROC)),2,0) -9 -fz --check=crc32 $(KERNEL_BUILD_DIR)/initrd$(2).cpio;) \ $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_ZSTD), \ $(STAGING_DIR_HOST)/bin/zstd -T0 -f -o $(KERNEL_BUILD_DIR)/initrd$(2).cpio.zstd $(KERNEL_BUILD_DIR)/initrd$(2).cpio;) \ - }, gen-cpio$(2)); \ - $(if $(2),,$(KERNEL_MAKE) $(KERNEL_MAKEOPTS_IMAGE) $(if $(KERNELNAME),$(KERNELNAME),all);),\ + }, gen-cpio$(2));,\ $(KERNEL_MAKE) $(if $(2),-C $(LINUX_DIR)$(2)) $(KERNEL_MAKEOPTS_IMAGE) $(if $(KERNELNAME),$(KERNELNAME),all);) \ $(call Kernel/CopyImage,-initramfs,$(2)); \ $(if $(2),rm -rf $(LINUX_DIR)$(2);) \