Clean up your grub menu from the kernels you do not use

Written by
Date: 2007-10-11 10:36:30 00:00


For Distros with lots of Kernel upgrades Like Ubuntu Gutsy now that it is still in Beta, it is annoying to start every day your PC and see that your grub list keep growing and growing.

Clean this unused kernel images is not only good because of a cleaner grub menu, but also to gain some disk space, as all those kernel images uses a lot of space in your disk.

I will show you do this under Ubuntu, but should also work for other distros just taking care of the names of their kernel images, and also its package managers.

If you remove the kernel you are using, you will broke your Linux, if you remove all your kernel images, you will surely broke your Linux, so use this with care!

1. Determine which Kernel you are using

Just run:

uname -r

and write down the result, in my case this was my output:

$ uname -r
2.6.22-14-generic

2. Look for all installed kernel images

Go to /boot/ and list its contents.

cd /boot ls vmlinuz*

Take note of all versions you have installed This was my output:

$ ls vmlinuz*
vmlinuz-2.6.20-16-generic
vmlinuz-2.6.22-11-generic
vmlinuz-2.6.22-12-generic
vmlinuz-2.6.22-13-generic
vmlinuz-2.6.22-14-generic

3. Remove the kernels you want

As I told you before, take care with this step.

Potential risk of breaking your Linux if you erase the wrong kernel

Run:

sudo apt-get remove linux-image-[version]-generic linux-image-[version]-generic

For all the versions you may want to erase, keep at least two or three kernel images, the one you are using and one or two more. This was my output:

$ sudo apt-get remove linux-image-2.6.22-11-generic linux-image-2.6.22-12-generic linux-image-2.6.20-16-generic
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-image-2.6.20-16-generic linux-image-2.6.22-11-generic linux-image-2.6.22-12-generic linux-restricted-modules-2.6.20-16-generic
  linux-restricted-modules-2.6.22-11-generic linux-restricted-modules-2.6.22-12-generic linux-ubuntu-modules-2.6.22-11-generic linux-ubuntu-modules-2.6.22-12-generic
0 upgraded, 0 newly installed, 8 to remove and 0 not upgraded.
Need to get 0B of archives.
After unpacking 346MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 110373 files and directories currently installed.)
Removing linux-restricted-modules-2.6.20-16-generic ...
Removing linux-image-2.6.20-16-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.22-14-generic
Found kernel: /boot/vmlinuz-2.6.22-13-generic
Found kernel: /boot/vmlinuz-2.6.22-12-generic
Found kernel: /boot/vmlinuz-2.6.22-11-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

Removing linux-restricted-modules-2.6.22-11-generic ...
Removing linux-ubuntu-modules-2.6.22-11-generic ...
update-initramfs: Generating /boot/initrd.img-2.6.22-11-generic
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
find: /lib/firmware/2.6.22-11-generic: No such file or directory
Removing linux-image-2.6.22-11-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.22-14-generic
Found kernel: /boot/vmlinuz-2.6.22-13-generic
Found kernel: /boot/vmlinuz-2.6.22-12-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

Removing linux-ubuntu-modules-2.6.22-12-generic ...
update-initramfs: Generating /boot/initrd.img-2.6.22-12-generic
Removing linux-restricted-modules-2.6.22-12-generic ...
Removing linux-image-2.6.22-12-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.22-14-generic
Found kernel: /boot/vmlinuz-2.6.22-13-generic
Found kernel: /boot/memtest86+.bin
Updating /boot/grub/menu.lst ... done

As you may see apt-get takes care of the menu.lst file also, anyway you may run:

sudo update-grub

In case your menu.lst file was not automatically cleaned.

I am sure there should be a better way to do it, I am thinking in some line that can automatically parse all the content of /boot/ and remove the result of uname -r command and then perform the clean automatically, maybe with awk, and regular expressions, but I am not good in that field