The problem
Have you ever logged into your linux box and noticed this warning :
Well then it is time to remove all your old linux kernels because they are filling up your /boot directory. What is the content of this mysterious directory ?
/dev/vda1 on /boot type ext4 (rw)
$ ls /boot
abi-3.2.0-64-generic config-3.2.0-64-generic grub initrd.img-3.2.0-68-generic System.map-3.2.0-64-generic vmlinuz-3.2.0-64-generic
abi-3.2.0-65-generic config-3.2.0-65-generic initrd.img-3.2.0-64-generic lost+found System.map-3.2.0-65-generic vmlinuz-3.2.0-65-generic
abi-3.2.0-67-generic config-3.2.0-67-generic initrd.img-3.2.0-65-generic memtest86+.bin System.map-3.2.0-67-generic vmlinuz-3.2.0-67-generic
abi-3.2.0-68-generic config-3.2.0-68-generic initrd.img-3.2.0-67-generic memtest86+_multiboot.bin System.map-3.2.0-68-generic vmlinuz-3.2.0-68-generic
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 110M 98M 6.0M 95% /boot
.
.
As you can see there is a lot of stuff in this directory: namely the linux kernels that you can choose from in the grub bootscreen. While simply deleting the old and unused kernelfiles will free up memory on the boot partition the old kernels will be still shown in the boot menu of grub. The correct way to ged rid of them is to uninstall them via the packet manager. So lets have a look which kernel we are currently running:
3.2.0-68-generic
Ok, we are running the 3.2.0-68-generic kernel. This means that we can delete all the other ones - but which kernel components are installed ? ... Lets ask dpkg. If they where installed throug the package manager then dpkg should know it:
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Description
+++-====================================-====================================-========================================================================================
un linux-doc-3.2.0 <none> (no description available)
ii linux-firmware 1.79.16 Firmware for Linux kernel drivers
un linux-headers <none> (no description available)
un linux-headers-3 <none> (no description available)
un linux-headers-3.0 <none> (no description available)
un linux-headers-3.2.0-60 <none> (no description available)
un linux-headers-3.2.0-60-generic <none> (no description available)
un linux-headers-3.2.0-61 <none> (no description available)
un linux-headers-3.2.0-61-generic <none> (no description available)
un linux-headers-3.2.0-63 <none> (no description available)
un linux-headers-3.2.0-63-generic <none> (no description available)
un linux-headers-3.2.0-64 <none> (no description available)
un linux-headers-3.2.0-64-generic <none> (no description available)
un linux-headers-server <none> (no description available)
un linux-image <none> (no description available)
un linux-image-3.0 <none> (no description available)
un linux-image-3.2.0-60-generic <none> (no description available)
un linux-image-3.2.0-61-generic <none> (no description available)
rc linux-image-3.2.0-63-generic 3.2.0-63.95 Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii linux-image-3.2.0-64-generic 3.2.0-64.97 Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii linux-image-3.2.0-65-generic 3.2.0-65.99 Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii linux-image-3.2.0-67-generic 3.2.0-67.101 Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii linux-image-3.2.0-68-generic 3.2.0-68.102 Linux kernel image for version 3.2.0 on 64 bit x86 SMP
ii linux-image-server 3.2.0.68.81 Linux kernel image on Server Equipment.
un linux-initramfs-tool <none> (no description available)
un linux-kernel-headers <none> (no description available)
un linux-kernel-log-daemon <none> (no description available)
ii linux-libc-dev 3.2.0-68.102 Linux Kernel Headers for development
un linux-restricted-common <none> (no description available)
un linux-server <none> (no description available)
un linux-source-3.2.0 <none> (no description available)
un linux-tools <none> (no description available)
Solution A: Remove a specific kernel
All the lines starting with ii are indicating installed kernels. To remove a specific one via the package manager we can run the following command:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'linux-image-3.2.0-65-generic-pae' for regex 'linux-image-3.2.0.65-generic'
Note, selecting 'linux-image-3.2.0-65-generic' for regex 'linux-image-3.2.0.65-generic'
The following packages will be REMOVED:
linux-image-3.2.0-65-generic
0 upgraded, 0 newly installed, 1 to remove and 4 not upgraded.
After this operation, 150 MB disk space will be freed.
Do you want to continue [Y/n]? Y
.
.
This removes the kernel from the operatingsystem. It also updates your grub configuration accordingly.
Solution B: Removing all kernels (but the current)
Nice, but what if i want to remove ALL old kernels except the current one ? -> This can be solved via a small awk script:
This script works in five stages that are piped together:
- Find all known packages that contain the term 'linux' -> This may be headers, kernels, images and anything in between)
- Reduce the result to all lines that look like an linux-image with 3 numbers afterwards (the version numbers)
- Reduce this result an remove every line that contains the version number of our currently installed kernel
- Take only the second column of this result
- Uninstall all packages that are matching without asking for confirmation
This script also uninstalls the kernels by utilizing the package manager - so it also updates the grup bootloader accordingly. No worries about missing space on /boot any more. This is the final result:
abi-3.2.0-68-generic grub lost+found memtest86+_multiboot.bin vmlinuz-3.2.0-68-generic
config-3.2.0-68-generic initrd.img-3.2.0-68-generic memtest86+.bin System.map-3.2.0-68-generic
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 110M 32M 72M 31% /boot
.
.