Thursday 8 October 2015

Cannot remove unused kernal from Ubuntu because the boot partition full

For some odd reason unbuntu seems to make it’s boot partitions stupidly small. For logical reasons, it also doesn’t remove unused kernel images. Eventually you will fill your stupidly small /boot/ partition. Ultimately you will not be able to remove old kernels via apt because there is no free space left to install the one that failed to install because the device was full. Rock and hard place.

Fear not, there is a basic solution that I present to you here - but beware. You will be root and you will be moving kernels around. If you screw something up it is very easy to render your box very unwilling to boot. But you know what you’re doing right?

sudo su
cd /root/
mkdir /unused-kernals
cd /boot/

Here’s where you pay attention. The list of kernels you have may be very different from the list of kernals I had when I was writing this post. First find out what the current kernel is (because you want to leave that in place) and then mv the kernals in ascending order (I thought this would be safer than rm’ing) to the /root/unused-kernals directory until you have about 70% available space left.

uname -r
mv *3.13.0-51* /root/unused-kernals/
mv *3.13.0-52* /root/unused-kernals/
df -h  # do you have enough space yet…
mv *3.13.0-55* /root/unused-kernals/
df -h  # do you have enough space yet?
# etc etc

Once you’ve cleared about 30% space off /boot/ then you can remove things properly. Do a dry run first and make extra extra sure everything looks right…

dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | grep -E "(image|headers)" | xargs sudo apt-get --dry-run remove

Ok, go for it…

dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | grep -E "(image|headers)" | xargs sudo apt-get remove --yes
# do this to complete the installs of things that failed
sudo apt-get -f
# do this to remove all the old kernals
sudo apt-get autoremove

And that’s how I got myself out of a full /boot/. You may have a better way of doing it.