Friday, July 23, 2010

Removing unwanted kernels in Ubuntu and updating Grub2 configuration

The installation of a new kernel adds two new entries in the Grub menu. I have set the default selected entry to something different from the first item, so each time a new kernel is installed, the default entry points to the wrong menu item. Using update-grub will automatically update all Grub2 configuration files, and that's when the kernels present in the file system are automatically detected (Grub2).

I want to remove the old kernels, which I won't need anymore. There are several ways to do it. I'll show how to do it on the command line. First, let's list the installed kernels


dpkg -l | grep linux-im

ii linux-image-2.6.32-23-generic 2.6.32-23.37 Linux kernel image for version 2.6.32 on x86
ii linux-image-2.6.32-24-generic 2.6.32-24.38 Linux kernel image for version 2.6.32 on x86
ii linux-image-generic 2.6.32.24.25 Generic Linux kernel image


Here, we see package names and package version numbers. The syntax to completely remove packages is "apt-get purge packagename". Let's remove the unwanted kernel image, which is 2.6.32-23.37. The string used after "purge" is a regular expression. I don't bother to escape it.

sudo apt-get purge .*.2.6.32-23

All packages matching this regular expression will be completely removed. This operation must be done with caution. Fortunatly, before packages are removed, a confirmation will be displayed (in Japanese in my local environment):

以下のパッケージは「削除」されます:
linux-headers-2.6.32-23* linux-headers-2.6.32-23-generic*
linux-image-2.6.32-23-generic*
アップグレード: 0 個、新規インストール: 0 個、削除: 3 個、保留: 0 個。
この操作後に 184MB のディスク容量が解放されます。
続行しますか [Y/n]? Y

This shows that three packages will be removed. That's what I was expecting. After accepting the changes, the Grub configuration will be automatically updated ! No need to update it yourself.

Running postrm hook script /usr/sbin/update-grub.
Generating grub.cfg ...
...
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.32-24-generic
Found initrd image: /boot/initrd.img-2.6.32-24-generic
Found memtest86+ image: /boot/memtest86+.bin
Found Microsoft Windows XP Home Edition on /dev/sda1
Found Windows NT/2000/XP on /dev/sda3
done

Voila ! The unwanted kernels should be gone from the file system and from the Grub menu.

No comments:

Post a Comment