Skip to content

Commit 82c4907

Browse files
kernel_remove: init script to remove a specified kernel
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.
1 parent ab098bb commit 82c4907

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,34 @@ This is the full script that end with the engineer ready to reboot the VM into t
8080

8181
### kernel_recompile_kabi.sh
8282
Will only recompile the kernel and check the KABI.
83+
84+
### kernel_remove.sh
85+
This script is intended to clean up "development" kernels. By
86+
"development" kernels, the author means such a kernel that was built
87+
locally for testing if patch(es) for a CVE or customer request apply
88+
correctly and test them. Mainly, it means kernels installed using
89+
`make`, instead of as an RPM package.
90+
91+
The script accepts **only one** `kernel release` (`uname -r`) to
92+
remove. Like so:
93+
```bash
94+
$ ./kernel_remove.sh 4.18.0-ciqlts8_6-83208b00c+
95+
+ requested_kernel_to_remove=4.18.0-ciqlts8_6-83208b00c+
96+
++ dnf repoquery --latest-limit=1 --queryformat '%{version}-%{release}' kernel-core
97+
+ latest_rpm_kernel=4.18.0-372.32.1.el8_6.86ciq_lts.12.1
98+
++ uname -r
99+
+ current_kernel=4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64
100+
+ '[' -z 4.18.0-ciqlts8_6-83208b00c+ ']'
101+
+ '[' 4.18.0-ciqlts8_6-83208b00c+ == 4.18.0-372.32.1.el8_6.86ciq_lts.12.1 ']'
102+
++ uname -m
103+
+ '[' 4.18.0-ciqlts8_6-83208b00c+ == 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64 ']'
104+
+ '[' 4.18.0-ciqlts8_6-83208b00c+ == 4.18.0-372.32.1.el8_6.86ciq_lts.12.1.x86_64 ']'
105+
+ '[' -d /lib/modules/4.18.0-ciqlts8_6-83208b00c+ ']'
106+
+ sudo rm -fr /lib/modules/4.18.0-ciqlts8_6-83208b00c+
107+
+ '[' -d /boot/dtb-4.18.0-ciqlts8_6-83208b00c+ ']'
108+
+ '[' -f /boot/config-4.18.0-ciqlts8_6-83208b00c+ ']'
109+
+ '[' -f /boot/System-4.18.0-ciqlts8_6-83208b00c+ ']'
110+
+ '[' -f /boot/vmlinuz-4.18.0-ciqlts8_6-83208b00c+ ']'
111+
+ sudo rm -f /boot/vmlinuz-4.18.0-ciqlts8_6-83208b00c+
112+
+ sudo grubby --remove-kernel=/boot/vmlinuz-4.18.0-ciqlts8_6-83208b00c+
113+
```

kernel_remove.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
set -xeuf -o pipefail
3+
4+
requested_kernel_to_remove="${1:-}"
5+
latest_rpm_kernel="$(dnf repoquery --latest-limit=1 --queryformat '%{version}-%{release}' kernel-core 2>/dev/null)"
6+
current_kernel="$(uname -r)"
7+
8+
if [ -z "${requested_kernel_to_remove}" ]; then
9+
echo 'Specify the kernel version'
10+
echo 'Look under /lib/modules/ for "correct" versions'
11+
exit 1
12+
fi
13+
14+
if [ "${requested_kernel_to_remove}" == "${latest_rpm_kernel}" ] || [ "${requested_kernel_to_remove}" == "${latest_rpm_kernel}.$(uname -m)" ]; then
15+
echo 'ERROR: Will not remove the *latest kernel* installed as an RPM package'
16+
exit 1
17+
elif [ "${requested_kernel_to_remove}" == "${current_kernel}" ]; then
18+
echo 'ERROR: Will not remove the current kernel'
19+
exit 1
20+
else
21+
# The checks exist to prevent "no such [file|directory] found" "errors" on console
22+
# Also helps with error management
23+
[ -d "/lib/modules/${requested_kernel_to_remove}" ] && sudo rm -fr "/lib/modules/${requested_kernel_to_remove}"
24+
[ -d "/boot/dtb-${requested_kernel_to_remove}" ] && sudo rm -fr "/boot/dtb-${requested_kernel_to_remove}"
25+
[ -f "/boot/config-${requested_kernel_to_remove}" ] && sudo rm -f "/boot/config-${requested_kernel_to_remove}"
26+
[ -f "/boot/System-${requested_kernel_to_remove}" ] && sudo rm -f "/boot/System-${requested_kernel_to_remove}"
27+
[ -f "/boot/vmlinuz-${requested_kernel_to_remove}" ] && sudo rm -f "/boot/vmlinuz-${requested_kernel_to_remove}"
28+
# If $requested_kernel_to_remove is current default kernel, grubby will make the next-in-line kernel default
29+
sudo grubby --remove-kernel="/boot/vmlinuz-${requested_kernel_to_remove}"
30+
fi

0 commit comments

Comments
 (0)