From bb7a1b67c7301f85640d2bdca6278b71215a5766 Mon Sep 17 00:00:00 2001 From: "Guilherme G. Piccoli" Date: Wed, 3 Aug 2022 11:03:53 -0300 Subject: [PATCH] kdump-load.sh: Clean-up old/useless kdump initrd files The kdump-steamos tooling creates an initrd file based in the running kernel version for kdump, during package installation (even if kdump is not the default crash collection mechanism). Such file lives in the /home partition. When SteamOS image is upgraded, with a new kernel, there is a mechanism to create a new initrd either manually or automatically, just before the kdump load; in the end, we may have lots of initrds (one per kernel version ever installed), but we don't have currently a way to clear that. Well, until now. We hereby introduce such a simple mechanism, to prevent waste of precious /home space with useless initrd files. It works by comparing installed kernels [0] with the kdump-initrd-* files in /home, and if we have some of these kdump initrds that have no match with any installed kernel, they are removed (and such operation is logged in the systemd journal). [0] Definition: installed kernel in our context is a kernel that has a modules folder in /lib/modules . Signed-off-by: Guilherme G. Piccoli --- kdump-load.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/kdump-load.sh b/kdump-load.sh index 4737929..60be97f 100644 --- a/kdump-load.sh +++ b/kdump-load.sh @@ -64,6 +64,27 @@ create_initrd() { export DRACUT_NO_XATTR=${DRACUT_XATTR} } +# This routine performs a clean-up by deleting the old/useless remaining +# kdump initrd files. +cleanup_unused_initrd() { + INSTALLED_KERNELS="${KDUMP_FOLDER}/.installed_kernels" + + find /lib/modules/* -maxdepth 0 -type d -exec basename {} \;>"${INSTALLED_KERNELS}" + + find "${KDUMP_FOLDER}"/* -name "kdump-initrd*" -type f -print0 | while IFS= read -r -d '' file + do + FNAME="$(basename "${file}" .img)" + KVER="${FNAME#kdump-initrd-}" + if ! grep -q "${KVER}" "${INSTALLED_KERNELS}" ; then + rm -f "${KDUMP_FOLDER}/${FNAME}.img" + logger "kdump-steamos: removed unused file \"${FNAME}.img\"" + fi + done + + rm -f "${INSTALLED_KERNELS}" +} + + if [ ! -s "/usr/share/kdump/kdump.conf" ]; then logger "kdump-steamos: /usr/share/kdump/kdump.conf is missing, aborting." exit 0 @@ -106,6 +127,7 @@ if [ "${USE_PSTORE_RAM}" -eq 1 ]; then if modprobe ramoops mem_address=0x"${MEM_START}" mem_size=${MEM_REQUIRED} record_size=${RECORD_SIZE}; then # If Pstore is set, update grub.cfg to avoid reserving crashkernel memory. logger "kdump-steamos: pstore-RAM was loaded successfully" + cleanup_unused_initrd grub_update pstore exit 0 fi @@ -118,6 +140,7 @@ if [ "${USE_PSTORE_RAM}" -eq 1 ]; then # no point in continuing since kdump cannot work. fi +cleanup_unused_initrd grub_update kdump # Stolen from Debian kdump