Skip to content

kernel_remove: init script to remove a specified kernel #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: mainline
Choose a base branch
from

Conversation

thefossguy-ciq
Copy link
Contributor

This script basically cleans up all the possible paths that might be populated from a make modules_install and make install. Given that the kernel is removed, the grub entry is also removed with the help of grubby.

This is an ad-hoc script created to quickly remove a kernel without much fuss about it.


The script only accepts the kernel version and is used like following:

$ ./kernel_remove.sh $KERNEL_VERSION

$ ./kernel_remove.sh 4.18.0-ciqlts8_8-6a8fa3030700+

And that's all. It will remove all installed files and even remove the grub entry.

@PlaidCat
Copy link
Collaborator

Could you add a check for making sure you're not deleting the running kernel?

Additionally could you add an Entry to the ReadMe explaining this and the expected use case and limitations?

Otherwise thanks this will be helpful for full systems or those that don't treat their VM's as disposable (i know this is important to some)

@thefossguy-ciq thefossguy-ciq force-pushed the {ppatel}-add-script-to-cleanup-kernels branch from 32285f0 to 82c4907 Compare July 14, 2025 16:38
@thefossguy-ciq
Copy link
Contributor Author

Some extra documentation that I am not sure goes in the README.

Trying to remove a kernel installed as an RPM

$ uname -r
4.18.0-ciqlts8_6-83208b00c+

$ ls -alh /lib/modules/
total 16K
drwxr-xr-x.  6 root root  161 Jul 14 21:56 .
dr-xr-xr-x. 40 root root 4.0K Jul 14 17:59 ..
drwxr-xr-x.  6 root root 4.0K Jul 14 21:57 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64
drwxr-xr-x.  6 root root 4.0K Jul 14 21:57 4.18.0-372.32.1.el8_6.x86_64
drwxr-xr-x.  3 root root   19 Jul 14 17:42 4.18.0-372.9.1.el8.x86_64
drwxr-xr-x.  3 root root 4.0K Jul 14 20:56 4.18.0-ciqlts8_6-83208b00c+

$ ./kernel_remove.sh 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64
+ requested_kernel_to_remove=4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64
++ dnf repoquery --latest-limit=1 --queryformat '%{version}-%{release}' kernel-core
+ latest_rpm_kernel=4.18.0-372.32.1.el8_6.86ciq_lts.12.1
++ uname -r
+ current_kernel=4.18.0-ciqlts8_6-83208b00c+
+ '[' -z 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64 ']'
+ '[' 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64 == 4.18.0-372.32.1.el8_6.86ciq_lts.12.1 ']'
++ uname -m
+ '[' 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64 == 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64 ']'
+ echo 'ERROR: Will not remove the *latest kernel* installed as an RPM package'
ERROR: Will not remove the *latest kernel* installed as an RPM package
+ exit 1

Trying to remove the current kernel

$ uname -r
4.18.0-ciqlts8_6-83208b00c+

$ ./kernel_remove.sh 4.18.0-ciqlts8_6-83208b00c+
+ requested_kernel_to_remove=4.18.0-ciqlts8_6-83208b00c+
++ dnf repoquery --latest-limit=1 --queryformat '%{version}-%{release}' kernel-core
+ latest_rpm_kernel=4.18.0-372.32.1.el8_6.86ciq_lts.12.1
++ uname -r
+ current_kernel=4.18.0-ciqlts8_6-83208b00c+
+ '[' -z 4.18.0-ciqlts8_6-83208b00c+ ']'
+ '[' 4.18.0-ciqlts8_6-83208b00c+ == 4.18.0-372.32.1.el8_6.86ciq_lts.12.1 ']'
++ uname -m
+ '[' 4.18.0-ciqlts8_6-83208b00c+ == 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64 ']'
+ '[' 4.18.0-ciqlts8_6-83208b00c+ == 4.18.0-ciqlts8_6-83208b00c+ ']'
+ echo 'ERROR: Will not remove the current kernel'
ERROR: Will not remove the current kernel
+ exit 1

@thefossguy-ciq thefossguy-ciq requested a review from PlaidCat July 14, 2025 16:39
@thefossguy-ciq thefossguy-ciq marked this pull request as draft July 14, 2025 16:55
@thefossguy-ciq thefossguy-ciq force-pushed the {ppatel}-add-script-to-cleanup-kernels branch from 82c4907 to 3d2ed68 Compare July 15, 2025 04:27
@thefossguy-ciq thefossguy-ciq marked this pull request as ready for review July 15, 2025 04:27
@Copilot Copilot AI review requested due to automatic review settings July 15, 2025 04:27
Copilot

This comment was marked as outdated.

This script basically cleans up all the possible paths that might be
populated from a `make modules_install` and `make install`. Given that
the kernel is removed, the grub entry is also removed with the help of
`grubby`.

This is an ad-hoc script created to quickly remove a kernel without much
fuss about it.
@thefossguy-ciq thefossguy-ciq force-pushed the {ppatel}-add-script-to-cleanup-kernels branch from 3d2ed68 to 7f763d4 Compare July 15, 2025 05:31
@thefossguy-ciq thefossguy-ciq requested a review from Copilot July 15, 2025 11:16
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

A new Bash script to remove a specified kernel version by cleaning up its modules, boot files, and grub entry, along with documentation of its usage.

  • Added kernel_remove.sh to automate removal of development and older RPM kernels
  • Updated README.md with a usage section and example trace output

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
kernel_remove.sh New script to delete /lib/modules, /boot artifacts for a given kernel and update grub
README.md Documentation for kernel_remove.sh, including description of development kernels and example usage
Comments suppressed due to low confidence (1)

README.md:84

  • Add a note that this script requires sudo privileges to remove files and update grub entries, since it runs sudo rm and sudo grubby commands.
### kernel_remove.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants