Welcome to bytebang » The blog about all and nothing » Removing old Linux kernels from ubuntu

Removing old Linux kernels from ubuntu

Sep 14 2014

The problem

Have you ever logged into your linux box and noticed this warning :

/boot is using 89.9% of 109MB

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 ?

$ mount | grep boot
/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:

$ uname -r
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:

$ dpkg -l 'linux-*'
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:

$ sudo apt-get remove linux-image-3.2.0.65-generic
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:

$ dpkg -l 'linux-*' | awk '/ii.*linux-image-[0-9]*.[0-9]*.[0-9]*/ {print $0}' | grep -v "$(uname -r)" | awk '{print $2}' | xargs sudo apt-get -y purge

This script works in five stages that are piped together:

  1. Find all known packages that contain the term 'linux' -> This may be headers, kernels, images and anything in between)
  2. Reduce the result to all lines that look like an linux-image with 3 numbers afterwards (the version numbers)
  3. Reduce this result an remove every line that contains the version number of our currently installed kernel
  4. Take only the second column of this result
  5. Uninstall all packages that are matching without asking for confirmation
If you want to see how this script works then try to omit the last part (including the last pipe symbol), then the next, and so on.

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:

$ ls /boot
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
.
.

Get Social


(c) 2024, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen
-