If you have used Ubuntu for a while, you will notice that your grub startup screen is filled with old unused kernels. In my system there were around 12 entries for the old kernels. It is quite surprising that the kernel installation’s post process scripts do not remove the old kernels. I agree that keeping the last few kernels is a good idea – If the latest version screws up (which happens very very rarely), you can keep working using the older kernel till the issue is fixed. I have been using Ubuntu for a long time and my startup screen is incredibly cluttered. The presence of recovery kernel entries exacerbate the problem. Couple of months ago, I got really annoyed and decided to do some thing.
To find all the kernel packages installed in your system you can give the following command :
dpkg –get-selections | grep -E "linux-(header|image).*2.6" | grep -iw install | sort
This will possibly show a long list of packages. If you are not a command line person, you can always use Synaptic (System -> Administration -> Synaptic Package Manager) to view the kernel packages. In the search box, enter 2.6.32 or whatever minor version uname -a gave. (eg from 2.6.32-24 you get 2.6.32). Sort the filtered packages by their installation status(the S field) to find the kernel packages currently installed. Alternatively, you can also get the installed versions from the grub startup screen .
In Ubuntu, there are three packages that are related to the kernel - linux-headers-2.6.x-y, linux-headers-2.6.x-y-generic and linux-image-2.6.x-y-generic where x and y are minor version numbers that keep increasing. If you want to find the current version of your system , type the following command in the terminal :
It is a good idea to have atleast 2 different versions of kernel (latest and immediate previous) in your system as backup. Your mileage might vary but I think 2 is a good number. So other than the 2 latest kernel packages, select others and uninstall them. If you are using Synaptic, mark them for complete removal. If you prefer using command line then enter the following command in terminal.
sudo apt-get remove –purge 2.6.x-y-*
Of course, you need to replace x and y with the minor version of the kernel you want to uninstall. Repeat the process for each kernel to be uninstalled. In my case, the clean up freed up a whopping 1.5 GB of free space.
Ideally, you want to do this process once a month or so. I wrote a python script to do that – The basic idea is this : get all installed kernel packages individually (ie all linux-headers-*, linux-headers-*-generic and linux-image-*-generic) , sort them and remove the entries other than the last two. Ideally, you may want to split this up into two tasks – In my case, the first cron job runs on 1st of every month and sends me an email saying it is going to remove kernel versions blah1, blah2 etc. On 4th it does the actual removal. This should give you some time to react if you do not want it to run.
Hope this post helped you to save some hard disk space and also screen space in the boot screen !