Skip to content

Instantly share code, notes, and snippets.

@wildtruc
Last active February 6, 2024 12:35
Show Gist options
  • Save wildtruc/74c0f2697459d0f25b4ae4503b6a9b06 to your computer and use it in GitHub Desktop.
Save wildtruc/74c0f2697459d0f25b4ae4503b6a9b06 to your computer and use it in GitHub Desktop.
Script to clean old /usr/lib/modules directories in Fedora Work Station.
#! /bin/bash
## ONLY DEDICATED TO FEDORA WORK STATION.
## For an unknown reason as far as I know, Fedora kernels update doesn't
## remove the very old /lib/modules dir from system and then, cumulate
## numbers of modules kernel folders.
## This very small script install it self as a boot service and check
## if the the kernels in /boot directory are the only present in /lib/modules,
## then, clean if needed.
##
## The command had to be run only once :
## sudo kernel-cleaner.sh
##
## Remove is done manually from /lib/systemd/system/.
# set -e
if [ ! -f /lib/systemd/system/obsolete-kernels-cleaner.service ]; then
cat <<-SYS > /lib/systemd/system/obsolete-kernels-cleaner.service
[Unit]
Description=Obsolete KERNEL modules directory removal
Wants=syslog.target
[Service]
Type=forking
ExecStart=/usr/local/sbin/kernel-cleaner.sh
[Install]
WantedBy=multi-user.target
SYS
systemctl daemon-reload
systemctl enable obsolete-kernels-cleaner.service
else
echo "Checking not auto removed modules kernel directories."
available_kernels=$(rpm -q --queryformat "%{VERSION}-%{RELEASE}\n" kernel-core)
removable_kernels=$(ls -1 /usr/lib/modules/| grep -v "${available_kernels[*]}")
if [ ${#removable_kernels[*]} -gt 0 ]; then
for modules_dir in ${removable_kernels[@]}; do
[ -d /usr/lib/modules/$modules_dir ] && { rm -rf /usr/lib/modules/$modules_dir
echo "Removed $modules_dir from /usr/lib/modules."
}
done
else
echo "/usr/lib/modules report clean status."
fi
unset available_kernels removable_kernels
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment