How to remove a stuck kernel from GRUB

0

Linux updates usually run without issue on my desktop system at home. Except for recently, when a kernel update didn’t remove an older kernel from my GRUB boot menu. Maybe this was because my /boot filesystem was full, I’m not sure. I just know the boot entry was still there in the menu, but it pointed to a nonexistent kernel.

In these rare instances when something goes wrong, it’s important to know how to manually remove a stuck kernel without breaking your boot manager. Here’s how to remove a kernel entry from GRUB:

List all kernels

Let’s start by looking at what kernels are defined in the GRUB menu. The grubby program is the command line interface to query and manage the GRUB boot manager. The most basic action is to list the kernels defined in GRUB. For that, use the --info option with the ALL argument:

# grubby --info ALL
index=0
kernel="/boot/vmlinuz-0-rescue-ffffffffffffffffffffffffffffffff"
args="ro ro"
root="UUID=ab0c20c2-c002-42ed-903a-d92aeb83d7b4"
initrd="/boot/initramfs-0-rescue-ffffffffffffffffffffffffffffffff.img"
title="Fedora Linux (0-rescue-ffffffffffffffffffffffffffffffff) 42 (Adams)"
id="ffffffffffffffffffffffffffffffff-0-rescue"
index=1
kernel="/boot/vmlinuz-6.14.8-300.fc42.aarch64"
args="ro ro"
root="UUID=ab0c20c2-c002-42ed-903a-d92aeb83d7b4"
initrd="/boot/initramfs-6.14.8-300.fc42.aarch64.img"
title="Fedora Linux (6.14.8-300.fc42.aarch64) 42 (Adams)"
id="52e1582928754799aa40aab822f5eb19-6.14.8-300.fc42.aarch64"
index=2
kernel="/boot/vmlinuz-6.14.6-300.fc42.aarch64"
args="ro ro"
root="UUID=ab0c20c2-c002-42ed-903a-d92aeb83d7b4"
initrd="/boot/initramfs-6.14.6-300.fc42.aarch64.img"
title="Fedora Linux (6.14.6-300.fc42.aarch64) 42 (Adams)"
id="52e1582928754799aa40aab822f5eb19-6.14.6-300.fc42.aarch64"
index=3
kernel="/boot/vmlinuz-0-rescue-52e1582928754799aa40aab822f5eb19"
args="ro ro"
root="UUID=ab0c20c2-c002-42ed-903a-d92aeb83d7b4"
initrd="/boot/initramfs-0-rescue-52e1582928754799aa40aab822f5eb19.img"
title="Fedora Linux (0-rescue-52e1582928754799aa40aab822f5eb19) 42 (Adams)"
id="52e1582928754799aa40aab822f5eb19-0-rescue"

On my Raspberry Pi, that’s 4 entries, labeled from index=0 to index=3. The first and last entries are rescue kernels. The two kernels in the middle are regular kernels (6.14.6-300 and 6.14.8-300) that I would use to boot my system.

Remove a kernel

Let’s remove one of those kernels. The oldest regular kernel is 6.14.6-300, so we can remove that one. I need to provide the full kernel path to grubby, but that’s a long path that I’m sure to mistype along the way. I’d rather just insert the path automatically. Since I’m running this system entirely at the command line, I can’t copy and paste text from a terminal; instead, I need to extract the kernel path using the command line. The grep command isolates all grubby entries that contain kernel= and the awk command picks apart that line to just print the contents between the quotes:

# grubby --info ALL | grep kernel= | grep 6.14.6 | awk -F\" '{print $2}' > /tmp/k 

# cat /tmp/k
/boot/vmlinuz-6.14.6-300.fc42.aarch64

The --remove-kernel option will delete a GRUB entry that matches a full kernel path, so we can use Bash expansion with grubby to remove that kernel version:

# grubby --remove-kernel=$(cat /tmp/k)

And one more grubby command will display all entries, to confirm that we’ve removed the kernel from the GRUB menu:

# grubby --info ALL
index=0
kernel="/boot/vmlinuz-0-rescue-ffffffffffffffffffffffffffffffff"
args="ro ro"
root="UUID=ab0c20c2-c002-42ed-903a-d92aeb83d7b4"
initrd="/boot/initramfs-0-rescue-ffffffffffffffffffffffffffffffff.img"
title="Fedora Linux (0-rescue-ffffffffffffffffffffffffffffffff) 42 (Adams)"
id="ffffffffffffffffffffffffffffffff-0-rescue"
index=1
kernel="/boot/vmlinuz-6.14.8-300.fc42.aarch64"
args="ro ro"
root="UUID=ab0c20c2-c002-42ed-903a-d92aeb83d7b4"
initrd="/boot/initramfs-6.14.8-300.fc42.aarch64.img"
title="Fedora Linux (6.14.8-300.fc42.aarch64) 42 (Adams)"
id="52e1582928754799aa40aab822f5eb19-6.14.8-300.fc42.aarch64"
index=2
kernel="/boot/vmlinuz-0-rescue-52e1582928754799aa40aab822f5eb19"
args="ro ro"
root="UUID=ab0c20c2-c002-42ed-903a-d92aeb83d7b4"
initrd="/boot/initramfs-0-rescue-52e1582928754799aa40aab822f5eb19.img"
title="Fedora Linux (0-rescue-52e1582928754799aa40aab822f5eb19) 42 (Adams)"
id="52e1582928754799aa40aab822f5eb19-0-rescue"

System administration from the command line

I don’t always use a graphical desktop to do my work. It’s good to know how to access the power of the command line to get things done, especially system administration tasks.

Use man grubby to see a full list of what else you can do with grubby to manage your system. For example, you can also use --set-default to set the path to a new default kernel, or --default-kernel to display the current default kernel.

Leave a Reply