Help! /boot is full!

Help! /boot is full!

That won’t be the first time. When /boot is full, and you can’t purge any kernel because of those damn dependencies. I’ve bookmarked a script found at github for a while ago, which explains a safe way of cleaning old kernels. However, it won’t help all the way, as some dependencies might break the cleanup. So I combined this solution with a forced removal with the help of dpkg since some of the dependencies are’nt necessary if we want to REMOVE the kernels anyway. And one huge problem is that people only decide to show one part of the solution. Not the fact that some dependencies can and almost certainly will break the removal in some step.

So this is how it went!

#!/bin/bash

ipv4="-o Acquire::ForceIPv4=true"

if [ "$1" = "4" ] ; then
	withip=$ipv4
	echo "Going IPv4 ($withip)"
fi

echo "Autoremove+Purge."
apt-get $withip -y -f autoremove --purge >/dev/null 2>&1

if [ "$?" != "0" ] ; then
	echo "Auto Removal Failed!"
fi

echo "Old dependency fix."
apt-get $withip -f -y install >/dev/null 2>&1

if [ "$?" != "0" ] ; then
	echo "That failed. So we'll try to make up to it during this process."
fi

echo "Now, going old kernel cleanup!"
kern=$(dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r`)
hadErrors=0

for k in $kern
do
	echo apt-get -y purge $k
	apt-get $withip -y purge $k >/dev/null 2>&1

	if [ "$?" != "0" ] ; then
		echo "Failed apt-purge... Using plan B (--force-all -P)..."
		dpkg --force-all -P $k >/dev/null 2>&1
		echo "Rerunning stuff (apt-get -f -y install) for dependencies..."
		apt-get $withip -f -y install >/dev/null 2>&1
		if [ "$?" != "0" ] ; then
			echo "Still failing..."
			hadErrors=1
		fi
	fi
done

if [ "$hadErrors" = "1" ] ; then
	echo "I had errors. I should rerun this process, to see if there are more kernels that were left out after cleanup..."
	/usr/local/tornevall/cleankernel
fi

apt-get $withip autoremove
apt-get $withip update
apt-get $withip upgrade
apt-get $withip dist-upgrade

grb=$(which update-grub)
if [ "" != "$grb" ] ; then
	update-grub
else
	echo "Can't upgrade grub since update-grub is missing..."
fi