Go2Linux | Linux Operating System

A site dedicated to: Linux Operating System

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

Date: 2007-10-11 00:00:00 -0400

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 for haven a cleaner grub menu, but also to gain disk space, as all those kernel images uses a lot of space in your disk.

I will show you this under Ubuntu, but should 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 as good on that stuff.

If you liked this article please share it.

powered by TinyLetter

If you want to contact me in any other way, please use the contact page.