diff mbox series

[1/1] scripts/package/builddeb: allow hooks also in /usr/share/kernel

Message ID 20241127105851.1590405-2-josch@mister-muffin.de (mailing list archive)
State New
Headers show
Series scripts/package/builddeb: allow hooks also in /usr/share/kernel | expand

Commit Message

Johannes Schauer Marin Rodrigues Nov. 27, 2024, 10:58 a.m. UTC
By passing an additional directory to run-parts, allow Debian and its
derivatives an easy way to ship maintainer scripts in /usr while at the
same time allowing the local admin to easily override or disable them by
placing hooks of the same name in /etc. This adds support for the
mechanism described in the UAPI Configuration Files Specification for
kernel hooks:
https://uapi-group.org/specifications/specs/configuration_files_specification/

This functionality relies on run-parts 5.21 or later. If run-parts is
lacking support, only scripts in /etc/kernel will be considered. It is
the responsibility of packages installing hooks into /usr/share/kernel
to also declare a Depends: debianutils (>= 5.21).
---
 scripts/package/builddeb | 39 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 5 deletions(-)

Comments

Masahiro Yamada Nov. 28, 2024, 1:38 a.m. UTC | #1
On Wed, Nov 27, 2024 at 7:59 PM Johannes Schauer Marin Rodrigues
<josch@mister-muffin.de> wrote:
>
> By passing an additional directory to run-parts, allow Debian and its
> derivatives an easy way to ship maintainer scripts in /usr while at the
> same time allowing the local admin to easily override or disable them by
> placing hooks of the same name in /etc. This adds support for the
> mechanism described in the UAPI Configuration Files Specification for
> kernel hooks:
> https://uapi-group.org/specifications/specs/configuration_files_specification/
>
> This functionality relies on run-parts 5.21 or later. If run-parts is
> lacking support, only scripts in /etc/kernel will be considered. It is
> the responsibility of packages installing hooks into /usr/share/kernel
> to also declare a Depends: debianutils (>= 5.21).


Signed-off-by: is required for any kernel patch.



> ---
>  scripts/package/builddeb | 39 ++++++++++++++++++++++++++++++++++-----
>  1 file changed, 34 insertions(+), 5 deletions(-)
>
> diff --git a/scripts/package/builddeb b/scripts/package/builddeb
> index 441b0bb66e0d..1fa5228eed0b 100755
> --- a/scripts/package/builddeb
> +++ b/scripts/package/builddeb
> @@ -5,10 +5,14 @@
>  #
>  # Simple script to generate a deb package for a Linux kernel. All the
>  # complexity of what to do with a kernel after it is installed or removed
> -# is left to other scripts and packages: they can install scripts in the
> -# /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
> -# specified in KDEB_HOOKDIR) that will be called on package install and
> -# removal.
> +# is left to other scripts and packages. Scripts can be placed into
> +# the preinst, postinst, prerm and postrm directories in /etc/kernel
> +# (override with KDEB_HOOKDIR) or /usr/share/kernel (override with
> +# KDEB_DISTRO_HOOKDIR). Hooks of the same name in KDEB_HOOKDIR will override
> +# hooks in KDEB_DISTRO_HOOKDIR. This, the former directory (usually in /etc)
> +# can be used by the local admin while the latter directory (usually in /usr)
> +# can be used by packages shipped by the distribution. The preinst, postinst,
> +# prerm and postrm will then be called on package installation and removal.
>
>  set -eu
>
> @@ -69,8 +73,10 @@ install_linux_image () {
>         # make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
>         # so do we; recent versions of dracut and initramfs-tools will obey this.
>         debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
> +       debdistrohookdir=${KDEB_DISTRO_HOOKDIR:-/usr/share/kernel}
>         for script in postinst postrm preinst prerm; do
>                 mkdir -p "${pdir}${debhookdir}/${script}.d"
> +               mkdir -p "${pdir}${debdistrohookdir}/${script}.d"
>
>                 mkdir -p "${pdir}/DEBIAN"
>                 cat <<-EOF > "${pdir}/DEBIAN/${script}"
> @@ -84,7 +90,30 @@ install_linux_image () {
>                 # Tell initramfs builder whether it's wanted
>                 export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)
>
> -               test -d ${debhookdir}/${script}.d && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" ${debhookdir}/${script}.d
> +               if test -d "${debhookdir}/${script}.d" && test -d "${debdistrohookdir}/${script}.d"; then
> +                       if test -n "$(find "${debhookdir}/${script}.d" -maxdepth 0 -empty)" ; then


This must be escaped like "\$(find ...)"
because the 'find' command must be evaluated on the
installed system instead of on the build machine.

However, the added complexities look skeptical to me.

If /usr/share/kernel/postinst.d/ exists on the installed system,
we can assume run-parts>=5.21 exists there.


One more thing, do we need a new env variable
for the second search path?

If a user wants to add more search paths for custom hooks,
they can pass multiple words to KDEB_HOOKDIR.

export KDEB_HOOKDIR="
/home/masahiro/etc/kernel
/etc/kernel
/usr/local/share/kernel
/usr/share/kernel
"


This is compatible with the current "single path for KDEB_HOOKDIR"
usage, and flexible enough for the new extension.








> +                               # KDEB_HOOKDIR is empty, execute run-parts on KDEB_DISTRO_HOOKDIR only
> +                               run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debdistrohookdir}/${script}.d"
> +                       elif test -n "$(find "${debdistrohookdir}/${script}.d" -maxdepth 0 -empty)" ; then
> +                               # KDEB_DISTROHOOKDIR is empty, execute run-parts on KDEB_HOOKDIR only
> +                               run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debhookdir}/${script}.d"
> +                       else
> +                               # Both KDEB_HOOKDIR and KDEB_DISTROHOOKDIR contain files. It is the
> +                               # responsibility of the distribution package that placed files into
> +                               # KDEB_DISTROHOOKDIR to add a Depends: debianutils (>= 5.21)
> +                               if run-parts --help 2>&1 | grep -Fxq "Usage: run-parts [OPTION]... DIRECTORY [DIRECTORY ...]"; then
> +                                       run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debhookdir}/${script}.d" "${debdistrohookdir}/${script}.d"
> +                               else
> +                                       echo "E: Ignoring maintainer scripts in ${debdistrohookdir} because run-parts is too old (5.21 required)" >&2
> +                                       run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debhookdir}/${script}.d"
> +                               fi
> +                       fi
> +               elif test -d "${debhookdir}/${script}.d"; then
> +                       run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debhookdir}/${script}.d"
> +               elif test -d "${debdistrohookdir}/${script}.d"; then
> +                       run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debdistrohookdir}/${script}.d"
> +               fi
> +
>                 exit 0
>                 EOF
>                 chmod 755 "${pdir}/DEBIAN/${script}"
> --
> 2.39.2
>
>
diff mbox series

Patch

diff --git a/scripts/package/builddeb b/scripts/package/builddeb
index 441b0bb66e0d..1fa5228eed0b 100755
--- a/scripts/package/builddeb
+++ b/scripts/package/builddeb
@@ -5,10 +5,14 @@ 
 #
 # Simple script to generate a deb package for a Linux kernel. All the
 # complexity of what to do with a kernel after it is installed or removed
-# is left to other scripts and packages: they can install scripts in the
-# /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
-# specified in KDEB_HOOKDIR) that will be called on package install and
-# removal.
+# is left to other scripts and packages. Scripts can be placed into
+# the preinst, postinst, prerm and postrm directories in /etc/kernel
+# (override with KDEB_HOOKDIR) or /usr/share/kernel (override with
+# KDEB_DISTRO_HOOKDIR). Hooks of the same name in KDEB_HOOKDIR will override
+# hooks in KDEB_DISTRO_HOOKDIR. This, the former directory (usually in /etc)
+# can be used by the local admin while the latter directory (usually in /usr)
+# can be used by packages shipped by the distribution. The preinst, postinst,
+# prerm and postrm will then be called on package installation and removal.
 
 set -eu
 
@@ -69,8 +73,10 @@  install_linux_image () {
 	# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
 	# so do we; recent versions of dracut and initramfs-tools will obey this.
 	debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
+	debdistrohookdir=${KDEB_DISTRO_HOOKDIR:-/usr/share/kernel}
 	for script in postinst postrm preinst prerm; do
 		mkdir -p "${pdir}${debhookdir}/${script}.d"
+		mkdir -p "${pdir}${debdistrohookdir}/${script}.d"
 
 		mkdir -p "${pdir}/DEBIAN"
 		cat <<-EOF > "${pdir}/DEBIAN/${script}"
@@ -84,7 +90,30 @@  install_linux_image () {
 		# Tell initramfs builder whether it's wanted
 		export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)
 
-		test -d ${debhookdir}/${script}.d && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" ${debhookdir}/${script}.d
+		if test -d "${debhookdir}/${script}.d" && test -d "${debdistrohookdir}/${script}.d"; then
+			if test -n "$(find "${debhookdir}/${script}.d" -maxdepth 0 -empty)" ; then
+				# KDEB_HOOKDIR is empty, execute run-parts on KDEB_DISTRO_HOOKDIR only
+				run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debdistrohookdir}/${script}.d"
+			elif test -n "$(find "${debdistrohookdir}/${script}.d" -maxdepth 0 -empty)" ; then
+				# KDEB_DISTROHOOKDIR is empty, execute run-parts on KDEB_HOOKDIR only
+				run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debhookdir}/${script}.d"
+			else
+				# Both KDEB_HOOKDIR and KDEB_DISTROHOOKDIR contain files. It is the
+				# responsibility of the distribution package that placed files into
+				# KDEB_DISTROHOOKDIR to add a Depends: debianutils (>= 5.21)
+				if run-parts --help 2>&1 | grep -Fxq "Usage: run-parts [OPTION]... DIRECTORY [DIRECTORY ...]"; then
+					run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debhookdir}/${script}.d" "${debdistrohookdir}/${script}.d"
+				else
+					echo "E: Ignoring maintainer scripts in ${debdistrohookdir} because run-parts is too old (5.21 required)" >&2
+					run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debhookdir}/${script}.d"
+				fi
+			fi
+		elif test -d "${debhookdir}/${script}.d"; then
+			run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debhookdir}/${script}.d"
+		elif test -d "${debdistrohookdir}/${script}.d"; then
+			run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" "${debdistrohookdir}/${script}.d"
+		fi
+
 		exit 0
 		EOF
 		chmod 755 "${pdir}/DEBIAN/${script}"