Unbutu: Removing unused kernel images and headers

As part of normal long-term operations, the number of kernel images on your system will accumulate and take up disk space.  This issue with space will be even more pronounced if /boot is mounted to its own smaller partition.

With Ubuntu 16.04, ‘apt autoremove –purge’ and configuration of the unattended upgrades can ensure that old kernel images are cleaned, but if you are using Ubuntu 14.04 or need to manually purge, then the instructions below can lead you through the process.

Before removing this unnecessary baggage, the first step is to check what kernel version is currently being used and the installation state.

> uname -r
4.4.0-57-generic

We must make sure this version of the kernel is not removed in our upcoming operations, but this is a basic answer and we need to get a more complete picture:

> dpkg -l | grep linux-image

Which will return a list of the kernels and in the first column, the desired and current package state (for example: ii, if).  These states are described in ‘man dpkg-query’.

ii  linux-image-4.2.0-25-generic 4.2.0-25.30~14.04.1 amd64 Linux kernel image for version 4.2.0 on 64 bit x86 SMP
ii  linux-image-4.2.0-27-generic 4.2.0-27.32~14.04.1 amd64 Linux kernel image for version 4.2.0 on 64 bit x86 SMP
ii linux-image-extra-4.2.0-42-generic 4.2.0-42.49~14.04.1 amd64 Linux kernel extra modules for version 4.2.0 on 64 bit x86 SMP
ii linux-image-extra-4.4.0-57-generic 4.4.0-57.78~14.04.1 amd64 Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii linux-image-generic-lts-wily 4.2.0.42.34 amd64 Generic Linux kernel image
ii linux-image-generic-lts-xenial 4.4.0.57.44 amd64 Generic Linux kernel image

In the above list, all the kernel images are ‘ii’ meaning both the current and desired state are installed.  But, as an example, if the linux-image-generic-lts-xenial state was ‘if’, it would mean the kernel is only half configured and the system is still in the process of making it the active kernel.

For good measure, we want to keep the current kernel (4.4.0.57) and its previous version (4.2.0.42), but the older 4.2.0-25 and 4.2.0-27 can be removed.  For a scripted way of getting the list of older installed kernels and kernel headers installed on the system:

> kernelver=$(uname -r | sed -r 's/-[a-z]+//'); dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve $kernelver

Older kernel headers are generally safe to remove, unless they are used to compile specific applications or patched kernels.

Now armed with this list of kernels and headers that are candidates for removal, you can use ‘apt-get purge’ to remove the packages that are no longer used:

# apt-get purge linux-image-4.2.0-{25,27}-generic -y

And similarly when removing headers:

# apt-get purge linux-headers-4.2.0-{25,27}-generic -y

 

REFERENCES

http://askubuntu.com/questions/808888/freeing-up-boot-space-for-upgrade

http://askubuntu.com/questions/298487/not-enough-free-disk-space-when-upgrading

http://askubuntu.com/questions/253048/safe-to-remove-usr-src-linux-headers-after-purging-older-linux-images

http://askubuntu.com/questions/2793/how-do-i-remove-old-kernel-versions-to-clean-up-the-boot-menu

http://askubuntu.com/questions/800189/unable-to-free-space-on-boot

http://askubuntu.com/questions/18804/what-do-the-various-dpkg-flags-like-ii-rc-mean

http://ubuntuhandbook.org/index.php/2016/05/remove-old-kernels-ubuntu-16-04/

apt-get -f install (fix broken)