[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gentoo-dev
Subject:    [gentoo-dev] [PATCH] eclass/(dist-)kernel{-utils,-install}: More flexible initrd generation
From:       Andrew Ammerlaan <andrewammerlaan () gentoo ! org>
Date:       2023-11-27 9:17:14
Message-ID: 54e6d0f8-a229-48fb-871e-036dcf75c012 () gentoo ! org
[Download RAW message or body]

Hi all,

This patch is intended to be merged alongside the introduction of 
sys-kernel/installkernel-gentoo-8. Version 8 introduces:
- UKI compatibility
- The ability to use /etc/kernel/preinst.d plugins, to complement the 
existing /etc/kernel/postinst.d plugins
- An optional dracut preinst.d plugin that is intended to be toggle-able 
with USE=dracut, similar to the existing GRUB postinst.d plugin that is 
toggle-able with USE=grub

This last change is what the eclass patch below is for. This patch 
encapsulates the initrd related code from the dist-kernel-utils and 
kernel-install eclasses in a "if has_version 
"<=sys-kernel/installkernel-gentoo-7";".
Offloading the initrd/uki generation to installkernel plugins gives 
users the ability to use initrd generators other than dracut. And also 
allows users of manually configured and compiled kernels to 
automatically generate an accompanying initrd/uki if desired (make 
install calls installkernel which can call an initrd/uki plugin if one 
is installed).

Note that sys-kernel/installkernel-systemd already has a plugin to run 
dracut on kernel installation, installed by sys-kernel/dracut itself, so 
no further changes are required there.

I'm aware some people have strong feelings about what installkernel 
should and should not do, and what should be automagic and what not. 
I've done my very best to accommodate all needs and concerns, and I 
believe the latest iteration[1] should make everyone happy. Nonetheless, 
I'll give everyone some more time to complain, provided there are no 
insurmountable objections I'll merge [1] in about 7-10 days and release 
the new version 8 shortly after.

A follow-up eclass patch that cleans-up the eclasses and drops support 
for <=sys-kernel/installkernel-gentoo-7 will follow later once version 8 
is stable, a sneak-preview of this patch can already be found in my PR[2].

[1] https://github.com/projg2/installkernel-gentoo/pull/10
[2] https://github.com/gentoo/gentoo/pull/33727


 From 948a66a862f29b1356e530f95078c48c5bedbae0 Mon Sep 17 00:00:00 2001
From: Andrew Ammerlaan <andrewammerlaan@gentoo.org>
Date: Wed, 8 Nov 2023 14:46:19 +0100
Subject: [PATCH] eclass/(dist-)kernel{-utils,-install}: More flexible 
initrd generation

We only need to call dracut to generate an initrd/uki if we are using
<=sys-kernel/installkernel-gentoo-7. sys-kernel/installkernel-systemd
already calls dracut via its plugin system if it is installed.
sys-kernel/installkernel-gentoo-8[dracut] now also contains the code
required to generate an initrd or uki.

This change makes it possible for users to use other/custom initrd
generators with our dist kernels.

Signed-off-by: Andrew Ammerlaan <andrewammerlaan@gentoo.org>
---
  eclass/dist-kernel-utils.eclass | 50 ++++++++++++++++++++-------------
  eclass/kernel-install.eclass    | 30 ++++++++++++++------
  2 files changed, 52 insertions(+), 28 deletions(-)

diff --git a/eclass/dist-kernel-utils.eclass 
b/eclass/dist-kernel-utils.eclass
index 76a2f8d48cdf1..62750d1721a2c 100644
--- a/eclass/dist-kernel-utils.eclass
+++ b/eclass/dist-kernel-utils.eclass
@@ -116,21 +116,23 @@ dist-kernel_install_kernel() {
  	local image=${2}
  	local map=${3}

-	# if dracut is used in uefi=yes mode, initrd will actually
-	# be a combined kernel+initramfs UEFI executable.  we can easily
-	# recognize it by PE magic (vs cpio for a regular initramfs)
-	local initrd=${image%/*}/initrd
-	local magic
-	[[ -s ${initrd} ]] && read -n 2 magic < "${initrd}"
-	if [[ ${magic} == MZ ]]; then
-		einfo "Combined UEFI kernel+initramfs executable found"
-		# install the combined executable in place of kernel
-		image=${initrd%/*}/uki.efi
-		mv "${initrd}" "${image}" || die
-
-		if [[ ${KERNEL_IUSE_SECUREBOOT} ]]; then
-			# Ensure the uki is signed if dracut hasn't already done so.
-			secureboot_sign_efi_file "${image}"
+	if has_version "<=sys-kernel/installkernel-gentoo-7"; then
+		# if dracut is used in uefi=yes mode, initrd will actually
+		# be a combined kernel+initramfs UEFI executable.  we can easily
+		# recognize it by PE magic (vs cpio for a regular initramfs)
+		local initrd=${image%/*}/initrd
+		local magic
+		[[ -s ${initrd} ]] && read -n 2 magic < "${initrd}"
+		if [[ ${magic} == MZ ]]; then
+			einfo "Combined UEFI kernel+initramfs executable found"
+			# install the combined executable in place of kernel
+			image=${initrd%/*}/uki.efi
+			mv "${initrd}" "${image}" || die
+
+			if [[ ${KERNEL_IUSE_SECUREBOOT} ]]; then
+				# Ensure the uki is signed if dracut hasn't already done so.
+				secureboot_sign_efi_file "${image}"
+			fi
  		fi
  	fi

@@ -150,6 +152,10 @@ dist-kernel_install_kernel() {
  # The function will determine whether <kernel-dir> is actually
  # a dist-kernel, and whether initramfs was used.
  #
+# With sys-kernel/installkernel-systemd, or version 8 or greater of
+# sys-kernel/installkernel-gentoo, the generation of the initrd via dracut
+# is handled by kernel-install instead.
+#
  # This function is to be used in pkg_postinst() of ebuilds installing
  # kernel modules that are included in the initramfs.
  dist-kernel_reinstall_initramfs() {
@@ -160,19 +166,23 @@ dist-kernel_reinstall_initramfs() {
  	local ver=${2}

  	local image_path=${kernel_dir}/$(dist-kernel_get_image_path)
-	local initramfs_path=${image_path%/*}/initrd
  	if [[ ! -f ${image_path} ]]; then
  		eerror "Kernel install missing, image not found:"
  		eerror "  ${image_path}"
  		eerror "Initramfs will not be updated.  Please reinstall your kernel."
  		return
  	fi
-	if [[ ! -f ${initramfs_path} && ! -f ${initramfs_path%/*}/uki.efi ]]; then
-		einfo "No initramfs or uki found at ${image_path}"
-		return
+
+	if has_version "<=sys-kernel/installkernel-gentoo-7"; then
+		local initramfs_path=${image_path%/*}/initrd
+		if [[ ! -f ${initramfs_path} && ! -f ${initramfs_path%/*}/uki.efi ]]; 
then
+			einfo "No initramfs or uki found at ${image_path}"
+			return
+		fi
+
+		dist-kernel_build_initramfs "${initramfs_path}" "${ver}"
  	fi

-	dist-kernel_build_initramfs "${initramfs_path}" "${ver}"
  	dist-kernel_install_kernel "${ver}" "${image_path}" \
  		"${kernel_dir}/System.map"
  }
diff --git a/eclass/kernel-install.eclass b/eclass/kernel-install.eclass
index a0eceae8280bb..b4d84f3986c0d 100644
--- a/eclass/kernel-install.eclass
+++ b/eclass/kernel-install.eclass
@@ -48,11 +48,21 @@ RESTRICT+="

  # note: we need installkernel with initramfs support!
  IDEPEND="
-	|| (
-		sys-kernel/installkernel-gentoo
-		sys-kernel/installkernel-systemd
+	!initramfs? (
+		|| (
+			sys-kernel/installkernel-gentoo
+			sys-kernel/installkernel-systemd
+		)
  	)
-	initramfs? ( >=sys-kernel/dracut-059-r4 )"
+	initramfs? (
+		>=sys-kernel/dracut-059-r4
+		|| (
+			<=sys-kernel/installkernel-gentoo-7
+			>=sys-kernel/installkernel-gentoo-8[dracut(-)]
+			sys-kernel/installkernel-systemd
+		)
+	)
+"
  # needed by objtool that is installed along with the kernel and used
  # to build external modules
  # NB: linux-mod.eclass also adds this dep but it's cleaner to have
@@ -429,9 +439,13 @@ kernel-install_pkg_preinst() {
  # @FUNCTION: kernel-install_install_all
  # @USAGE: <ver>
  # @DESCRIPTION:
-# Build an initramfs for the kernel and install the kernel.  This is
-# called from pkg_postinst() and pkg_config().  <ver> is the full
-# kernel version.
+# Build an initramfs for the kernel if required and install the kernel.
+# This is called from pkg_postinst() and pkg_config().  <ver> is the
+# full kernel version.
+#
+# With sys-kernel/installkernel-systemd, or version 8 or greater of
+# sys-kernel/installkernel-gentoo, the generation of the initrd via dracut
+# is handled by kernel-install instead.
  kernel-install_install_all() {
  	debug-print-function ${FUNCNAME} "${@}"

@@ -448,7 +462,7 @@ kernel-install_install_all() {
  		nonfatal mount-boot_check_status || break

  		local image_path=$(dist-kernel_get_image_path)
-		if use initramfs; then
+		if use initramfs && has_version 
"<=sys-kernel/installkernel-gentoo-7"; then
  			# putting it alongside kernel image as 'initrd' makes
  			# kernel-install happier
  			nonfatal dist-kernel_build_initramfs \

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic