Ubuntu Kernel Clean-Up

Ubuntu is great at “set it and forget it” maintenance. Patches come down, and generally life is good.

However, the frequency of kernel updates can fill-up a modest /boot partition.

Recently faced with an inability to install an updated kernel due to a limited /boot partition, I had to do some housekeeping. I put together two quick scripts to help with this.
 

showInactiveKernels
Of the kernels installed on the system, which are not the one currently running?

#!/usr/bin/env bash

echo "Current Kernel: $(uname -r)"
old=$(dpkg -l "linux-image*" | grep '^[i]' | awk '{print $2}' | egrep -v "linux-image-$(uname -r)|linux-image-generic")
if [ "" == "$old" ] ; then
  echo "No inactive kernels found"
  exit
fi

for o in $old; do
  echo "  inactive kernel: $o"
done

 

removeInactiveKernels
Elaborating on the last script, prompting to remove them as you go

#!/usr/bin/env bash

old=$(dpkg -l "linux-image*" | grep '^[i]' | awk '{print $2}' | egrep -v "linux-image-$(uname -r)|linux-image-generic")
if [ "" == "$old" ] ; then
  echo "No inactive kernels found"
  exit
fi

for o in $old; do
  response='no'
  echo ""
  echo "Current Kernel: $(uname -r)"
  read -p "Do you wish to remove $o? ([no]|yes) " response
  if [ "$response" == "yes" ] ; then
    echo "  ... removing $o"
    apt-get -y remove "$o"
  fi
done

 

Using these provided a quick way to identify which kernels I could consider for removal/uninstallation, allowing me to clean-up /boot in no time.