Delete old initrds by RPM

After moving to dnf (installonlypkg(kernel)) schema of packaging the kernel,
the %%postun scriptlet stopped working properly because $1 is the number
of packages left after the transaction, and there are multiple
kernels - packages with the same name but of different version/release - left.

Let's just make initrd be %%ghost as Fedora (but increase its size from Fedora's 20 MiB to 65 MiB),
and drop deleting empty directories etc. Recent commit fixed ownership of directories,
now /lib/modules/kernel_version is owned by RPM (it was not), dkms will remove modules
that it built, and, if anything else was left, then let's just not touch it, why must we delete
what we know nothing about?

Also delete scriptlet in the devel subpackage, that scriptlet would probably cause problems
with reinstalling the devel package.
This commit is contained in:
Mikhail Novosyolov 2021-05-24 21:10:00 +03:00
parent 1ad94f7911
commit 4e538a2533

View file

@ -32,7 +32,7 @@
%define sublevel 39
# Release number. Increase this before a rebuild.
%define rpmrel 1
%define rpmrel 2
%define fullrpmrel %{rpmrel}
%define rpmtag %{disttag}
@ -131,9 +131,10 @@
# Common target directories
%define _bootdir /boot
%define _modulesdir /lib/modules
%define devel_root /usr/src/linux-%{kver_full}
%define initrd_path %{_bootdir}/initrd-%{kver_full}.img
# Directories needed for building
%define temp_root %{build_dir}/temp-root
%define temp_boot %{temp_root}%{_bootdir}
@ -473,32 +474,17 @@ input and output, etc. This is a general-purpose kernel.
# This is also the reason the devel package is in Requires(post) for this
# package now: it must be installed completely before we call depmod here.
/sbin/depmod -a %{kver_full}
/sbin/dracut -f /boot/initrd-%{kver_full}.img %{kver_full}
/sbin/dracut -f %{initrd_path} %{kver_full}
# File triggers from grub packages will handle this.
#/usr/sbin/update-grub2
%postun
# Do not remove just installed kernel if package is being reinstalled
if [ "$1" -gt 0 ]; then exit 0; fi
rm -f /boot/initrd-%{kver_full}.img
rm -f /boot/initrd-%{kver_full}_old.img
rm -f /boot/initrd-%{kver_full}kdump.img
rm -f /boot/initramfs-%{kver_full}kdump.img
# Third-party modules might have left something in /lib/modules/.../kernel/.
rm -rf /lib/modules/%{kver_full}/kernel/
rm -rf /lib/modules/%{kver_full}/modules*
# Remove /lib/modules/<...>/ if it is empty (-devel uses it too).
find /lib/modules/%{kver_full} -maxdepth 0 -empty -exec rm -rf {} \; || true
%files -f %{kernel_files}
%{_bootdir}/System.map-%{kver_full}
%{_bootdir}/symvers-%{kver_full}.zst
%{_bootdir}/config-%{kver_full}
%{_bootdir}/vmlinuz-%{kver_full}
%ghost %{initrd_path}
%ifarch %{armx}
%{_bootdir}/dtb-%{kver_full}
%endif
@ -544,6 +530,13 @@ Obsoletes: kernel-%{flavour}-%{kernelversion}.%{patchlevel}-devel-latest < 5.10.
# Have dkms updated/installed before the kernel, scriptlet here checks if dkms exists
OrderWithRequires(post): dkms
# Try to remove the main kernel package after removing this devel package
# because there may be dkms-built kernel modules inside directories owned
# by the main package, try to get rid of such files before RPM starts to
# deal with directories owned in the main package
# (note that the devel package does not explicitly require the main package,
# there is no need to do so, a kernel module may be built using just the devel part).
OrderWithRequires(postun): %{name} = %{EVRD}
%ifarch %{ix86}
Conflicts: arch(x86_64)
@ -580,15 +573,6 @@ for ii in $(/usr/sbin/dkms status -k %{kver_full} | awk '{ print $1 $2; }'); do
/usr/sbin/dkms --rpm_safe_upgrade uninstall -m $mod -v $ver -k %{kver_full} || :
done
%postun devel
rm -rf /usr/src/linux-%{kver_full} >/dev/null
# depmod (called when removing DKMS modules) might have created files in
# /lib/modules/.../. Remove these first.
rm -rf /lib/modules/%{kver_full}/modules*
# Remove the dir if it is already empty.
find /lib/modules/%{kver_full} -maxdepth 0 -empty -exec rm -rf {} \; || true
%files devel
%{devel_root}/Documentation
%dir %{devel_root}
@ -1513,6 +1497,14 @@ find %{target_modules} -name "*.ko" | %kxargs %{zstd_cmd} --rm -T1 #-D dictionar
find %{buildroot}%{_modulesdir}/%{kver_full} -type f -name '*.ko%{kmod_suffix}' | sed -e 's,^%{buildroot},,' | sort -u >> %{kernel_files}
find %{buildroot}%{_modulesdir}/%{kver_full} -type d | sed -e 's,^%{buildroot},%dir ,' | sort -u >> %{kernel_files}
# We estimate the size of the initramfs because rpm needs to take this size
# into consideration when performing disk space calculations (See rhbz#530778)
# 65 MiB is a bit more than needed, but let's be more sure that there is enought space.
# On my PC, zstd-compressed initrds take 58,5 MiB.
# Real size of the RPM package should not increase because RPM compresses the payload.
# This file is %%ghost, so the real initrd will be deleted when uninstalling this package.
dd if=/dev/zero of=%{buildroot}%{initrd_path} bs=1M count=65
%if %build_debug
install -m 644 vmlinux %{buildroot}%{_bootdir}/vmlinux-%{kver_full}
find %{buildroot}%{_modulesdir} -type f -name '*.debug' | sed -e 's,^%{buildroot},,' | sort -u >> %{debuginfo_files}