This document provides a detailed and well-explained guide on how to remove old kernels and associated packages in RHEL 7. The process includes listing installed kernels, removing unnecessary ones, and ensuring the system operates correctly post-cleanup.
1. Checking Installed Kernels
Before removing any kernel, it’s important to list all installed kernels and determine which ones are safe to remove.
cd /lib/modules && ls -d *.el7*
ls -lart
These commands list all installed kernel versions within /lib/modules
and provide details on their creation time.
To verify if weak-modules
is available and execute the removal of a specific kernel version, use:
[ -x /usr/sbin/weak-modules ] && /usr/sbin/weak-modules --remove-kernel <kernel_version>
Example:
[ -x /usr/sbin/weak-modules ] && /usr/sbin/weak-modules --remove-kernel 3.10.0-1160.102.1.el7.x86_64
This ensures weak modules associated with the specified kernel are removed.
2. Removing Kernel Entries from Bootloader
To remove kernel entries from the bootloader:
/bin/kernel-install remove <kernel_version> /lib/modules/<kernel_version>/vmlinuz
Example:
/bin/kernel-install remove 3.10.0-1160.102.1.el7.x86_64 /lib/modules/3.10.0-1160.102.1.el7.x86_64/vmlinuz
This command ensures the specified kernel is uninstalled properly from the system boot entries.
3. Listing Installed Packages Related to EL7
Before removing packages, list all installed EL7 packages:
rpm -qa | grep -e '\.el[67]' | grep -vE '^(gpg-pubkey|libmodulemd|katello-ca-consumer)' | sort
To count the total number of such packages:
rpm -qa | grep -e '\.el[67]' | grep -vE '^(gpg-pubkey|libmodulemd|katello-ca-consumer)' | sort | wc -l
Save the package list for review before removal:
rpm -qa | grep -e '\.el[67]' | grep -vE '^(gpg-pubkey|libmodulemd|katello-ca-consumer)' | sort >> rhel7.txt
cat rhel7.txt
4. Removing EL7 Packages and Old Kernel Modules
To remove all packages listed in rhel7.txt
:
yum remove $(cat rhel7.txt) -y
After package removal, clean up kernel modules:
rm -r /lib/modules/*el7*
Remove outdated bootloader entries:
rm /boot/vmlinuz-*rescue* /boot/initramfs-*rescue*
5. Ensuring System Integrity Post Cleanup
Reinstall the rescue kernel to ensure system recovery options remain intact:
/usr/lib/kernel/install.d/51-dracut-rescue.install add "$(uname -r)" /boot "/boot/vmlinuz-$(uname -r)"
Verify if any old kernels remain in the bootloader:
grubby --info=ALL | grep "\.el7" || echo "Old kernels are not present in the bootloader."
Check if rescue kernel exists:
ls /boot/vmlinuz-*rescue* /boot/initramfs-*rescue*
Verify rescue kernel integrity:
lsinitrd /boot/initramfs-*rescue*.img | grep -qm1 "$(uname -r)/kernel/" && echo "OK" || echo "FAIL"
Get detailed information on the rescue kernel:
grubby --info $(ls /boot/vmlinuz-*rescue*)
6. Summary
By following the steps above, you can:
- List and identify old kernel versions
- Remove unnecessary kernels and weak modules
- Clean up EL7-related packages and modules
- Ensure system stability after kernel removal
- Verify and restore the rescue kernel to avoid boot failures
This guide ensures a safe and effective cleanup process while maintaining system integrity and reducing the risk of boot issues.