@@ -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}"