Ubuntu – Boot partition full, failing to delete old kernels. Broken packages


Sorry for asking about what is probably really simple, but I've been Googling about for a week now and I've tried a lot of stuff without success.

To start off, I get a pop-up every time I boot saying my /boot partition is full, and the Synaptic Manager says linux-image-generic is broken, but it fails to reinstall either of them (linux-image-extra-3.13.0-54-generic and linux-image-generic).

My terminal tells me to try using install -f to fix things so I've tried that, and this is what it returned. Sadly, I'm still learning and I haven't been able to crack the code, I would like some help

aksel@aksels-PC:~$ sudo apt-get install -f
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
The following extra packages will be installed:
Suggested packages:
 fdutils linux-doc-3.13.0 linux-source-3.13.0 linux-tools
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 344 not upgraded.
5 not fully installed or removed.
Need to get 0 B/14,7 MB of archives.
After this operation, 32,8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
(Reading database ... 431129 files and directories currently installed.)
Preparing to unpack .../linux-image-3.13.0-54-generic_3.13.0-54.91_i386.deb ...
Unpacking linux-image-3.13.0-54-generic (3.13.0-54.91) ...
dpkg: error processing archive /var/cache/apt/archives/linux-image-3.13.0-54-generic_3.13.0-54.91_i386.deb (--unpack):
 cannot copy extracted data for './boot/vmlinuz-3.13.0-54-generic' to '/boot/vmlinuz-3.13.0-54-generic.dpkg-new': failed to write (No space left on device)
No apport report written because the error message indicates a disk full error

dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 3.13.0-54-generic /boot/vmlinuz-3.13.0-54-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 3.13.0-54-generic /boot/vmlinuz-3.13.0-54-generic
Errors were encountered while processing:
E: Sub-process /usr/bin/dpkg returned an error code (1)

Best Answer

By using linux-purge

I have created a helper script for this purpose. It is called linux-purge and its project page is here in Launchpad.net. In the announcements section there are installation instructions here.

In this specific case you would run

sudo linux-purge --fix

Manual method

As an alternative, there are instructions on how to do the task manually at Communiy Wiki Help which I have contributed to (as jarnos).

Anyway, here are my manual instructions as an archive:

First remove any leftover temporary files from previous kernel updates.

sudo rm -rv ${TMPDIR:-/var/tmp}/mkinitramfs-*

(There may be some due to bug in Ubuntu 16.04 and older.)

List the successfully installed kernels excluding booted kernel by command:

dpkg-query -W -f='${Status} ${Package}\n' | awk '/^[^ ]+ ok installed linux-image-[0-9]+/{print $4}' | grep -Fv $(uname -r)

Delete the initrd.img file of an unneeded kernel (due to bug); here the chosen kernel release is 4.2.0-23-generic:

sudo update-initramfs -d -k 4.2.0-23-generic

Purge the kernel by

sudo dpkg --purge linux-image-extra-4.2.0-23-generic linux-image-4.2.0-23-generic

to free some space in /boot. If the command fails, some installed package depends on the kernel. The output of dpkg tells the name of the package. Purge it first.

You may also purge respective header package

sudo dpkg --purge linux-headers-4.2.0-23-generic

and even the common header package

sudo dpkg --purge linux-headers-4.2.0-23

if no other package depends on it; otherwise the command will fail, but it is safe to run.

Then run

sudo apt-get install -f

to fix the broken dependency. If that fails due to insufficient disk space, you have to remove another kernel the way told above.

Note that there still may be too many kernels installed in your system, but now you should be able to purge the extra ones by e.g. sudo apt-get purge followed by kernel package name(s).

Related Question