Removing Old Kernel Versions From GRUB After Ubuntu Update

On my desktop machine I use Ubuntu most of the time, but I also have Windows installed that I boot using GRUB loader. I really like Ubuntu’s update mechanism but after few kernel updates I find my GRUB menu somewhat flooded with previous kernel versions.  So, I’ve decided to make a cleanup in my boot menu and make a note about it in case someone else is on the same task.

DISCLAIMER: Just in case, I don’t take any responsibility if you follow my instructions and it doesn’t work for you or you mess up your system.

First, list the GRUB configuration with:

$ sudo cat /boot/grub/menu.lst

Among the lines of menu.lst you should be able to find the list of available kernels:

## End Default Options

title        Ubuntu 8.04.1, kernel 2.6.24-22-generic root        (hd0,0) kernel        /boot/vmlinuz-2.6.24-22-generic root=UUID=db08e3b8-4fc1-4ba3-b702-5166cf864ea6 ro quiet splash xforcevesa initrd        /boot/initrd.img-2.6.24-22-generic quiet

title        Ubuntu 8.04.1, kernel 2.6.24-22-generic (recovery mode) root        (hd0,0) kernel        /boot/vmlinuz-2.6.24-22-generic root=UUID=db08e3b8-4fc1-4ba3-b702-5166cf864ea6 ro single initrd        /boot/initrd.img-2.6.24-22-generic

title        Ubuntu 8.04.1, kernel 2.6.24-21-generic root        (hd0,0) kernel        /boot/vmlinuz-2.6.24-21-generic root=UUID=db08e3b8-4fc1-4ba3-b702-5166cf864ea6 ro quiet splash xforcevesa initrd        /boot/initrd.img-2.6.24-21-generic quiet

title        Ubuntu 8.04.1, kernel 2.6.24-21-generic (recovery mode) root        (hd0,0) kernel        /boot/vmlinuz-2.6.24-21-generic root=UUID=db08e3b8-4fc1-4ba3-b702-5166cf864ea6 ro single initrd        /boot/initrd.img-2.6.24-21-generic

title        Ubuntu 8.04.1, kernel 2.6.24-19-generic root        (hd0,0) kernel        /boot/vmlinuz-2.6.24-19-generic root=UUID=db08e3b8-4fc1-4ba3-b702-5166cf864ea6 ro quiet splash xforcevesa initrd        /boot/initrd.img-2.6.24-19-generic quiet

title        Ubuntu 8.04.1, kernel 2.6.24-19-generic (recovery mode) root        (hd0,0) kernel        /boot/vmlinuz-2.6.24-19-generic root=UUID=db08e3b8-4fc1-4ba3-b702-5166cf864ea6 ro single initrd        /boot/initrd.img-2.6.24-19-generic

title        Ubuntu 8.04.1, memtest86+ root        (hd0,0) kernel        /boot/memtest86+.bin quiet

END DEBIAN AUTOMAGIC KERNELS LIST

You can see that I have 6 menu options for 3 different kernel versions, plus memory test, plus Windows boot option (not quoted above). That’s a bit too much for my taste so I’ve decided to remove the oldest kernel version leaving the previous one (just as a backup option if I notice anything wrong with the latest one).

Note that I decided to remove kernel files only, not just GRUB menu item. That would be too easy. :-)

So, it appears that the kernel that I need to remove from the system is 2.6.24-19. Let’s see what happens if we try to remove kernel packages (option -s is for dry run without real removal):

$ sudo apt-get -s remove linux-image-2.6.24-19-generic linux-image-2.6.24-19-server Reading package lists… Done Building dependency tree Reading state information… Done Package linux-image-2.6.24-19-server is not installed, so not removed The following packages were automatically installed and are no longer required: linux-headers-2.6.24-19 linux-headers-2.6.24-19-generic Use ‘apt-get autoremove’ to remove them. The following packages will be REMOVED: linux-image-2.6.24-19-generic linux-restricted-modules-2.6.24-19-generic linux-ubuntu-modules-2.6.24-19-generic 0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded. Remv linux-restricted-modules-2.6.24-19-generic [2.6.24.13-19.45] Remv linux-ubuntu-modules-2.6.24-19-generic [2.6.24-19.28] Remv linux-image-2.6.24-19-generic [2.6.24-19.41]

This looks safe enough to run it for real:

$ sudo apt-get remove linux-image-2.6.24-19-generic linux-image-2.6.24-19-server Reading package lists… Done Building dependency tree Reading state information… Done Package linux-image-2.6.24-19-server is not installed, so not removed The following packages were automatically installed and are no longer required: linux-headers-2.6.24-19 linux-headers-2.6.24-19-generic Use ‘apt-get autoremove’ to remove them. The following packages will be REMOVED: linux-image-2.6.24-19-generic linux-restricted-modules-2.6.24-19-generic linux-ubuntu-modules-2.6.24-19-generic 0 upgraded, 0 newly installed, 3 to remove and 0 not upgraded. After this operation, 147MB disk space will be freed. Do you want to continue [Y/n]? Y (Reading database … 145768 files and directories currently installed.) Removing linux-restricted-modules-2.6.24-19-generic … Removing linux-ubuntu-modules-2.6.24-19-generic … update-initramfs: Generating /boot/initrd.img-2.6.24-19-generic Removing linux-image-2.6.24-19-generic … Running postrm hook script /sbin/update-grub. Searching for GRUB installation directory … found: /boot/grub Searching for default file … found: /boot/grub/default Testing for an existing GRUB menu.lst file … found: /boot/grub/menu.lst Searching for splash image … none found, skipping … Found kernel: /boot/vmlinuz-2.6.24-22-generic Found kernel: /boot/vmlinuz-2.6.24-21-generic Found kernel: /boot/memtest86+.bin Replacing config file /var/run/grub/menu.lst with new version Updating /boot/grub/menu.lst … done

Notice that removal of linux-image package also updated GRUB menu. After reboot kernel 2.6.24-19 does not appear any more in the GRUB menu.

Note: If you do “apt-cache search” looking for other packages where “2.6.24-19” appears, you can see that actually there are still some kernel packages left. It’s getting too late here and I feel somewhat lazy to investigate this further at this point. I might give it a second look in the round (with the next kernel for removal).

Comments