fbpx

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

By Tornevall

- Stories from Reality - Musician | Bedroom DJ | Tug of War | Photographer | DevOps Thomas blends a passion for music, photography, and technology. With a background in 1990s dance music, his journey evolved from early experiments with FastTracker 2 to becoming a DJ and competitor in tug of war. His creative output includes documenting tug of war competitions across Sweden, while also working as a systems developer focusing on WordPress and e-commerce platforms.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.