Ubuntu 16.04 broken kernel packages won't let me install or remove anything with apt-get
Note: As ChaiT.Rex commented below, only steps 4 & 5 are required to fix the problem.
I leave steps 1,2&3 documented for anyone who may need to manually remove some packages. But try first this https://askubuntu.com/a/490677/26246 which looks easier.
-
Remove them manually, be careful if you are retyping the below commands
rm -rf
is a dangerous command when it's run as root.sudo rm -rf /lib/modules/4.4.0-38-generic /lib/modules/4.4.0-42-generic sudo rm -rf /usr/share/doc/linux-image-extra-4.4.0-38-generic /usr/share/doc/linux-image-extra-4.4.0-42-generic sudo rm /var/lib/dpkg/info/linux-image-extra-4.4.0-38-generic* /var/lib/dpkg/info/linux-image-extra-4.4.0-42-generic*
-
Change their status in dpkg status file.
Make backup just in case
sudo cp /var/lib/dpkg/status /var/lib/dpkg/status.backup0
Find the related entries (command with expected output, line number will be different):
~$ grep -n -e "linux-image-extra-4.4.0-38-generic" -e "linux-image-extra-4.4.0-42-generic" /var/lib/dpkg/status 18488:Package: linux-image-extra-4.4.0-42-generic 77123:Package: linux-image-extra-4.4.0-38-generic
Open status file for edit and change the package status:
sudo nano +18488 /var/lib/dpkg/status
from:
Package: linux-image-extra-4.4.0-38-generic Status: install ok installed
to:
Package: linux-image-extra-4.4.0-38-generic Status: deinstall ok config-files
Ctrl+o to save. Ctrl+x to exit.
Make same changes to linux-image-extra-4.4.0-42-generic .
-
Manually remove linux-image-4.4.0-38-generic & linux-image-4.4.0-42-generic too
sudo rm -f *4.4.0-38-generic *4.4.0-42-generic sudo rm -rf /usr/share/doc/linux-image-4.4.0-38-generic /usr/share/doc/linux-image-4.4.0-42-generic
Same as before edit their status to
deinstall ok config-files
$ grep -n -e "Package: linux-image-4.4.0-38-generic" -e "Package: linux-image-4.4.0-42-generic" /var/lib/dpkg/status 61217:Package: linux-image-4.4.0-38-generic 98051:Package: linux-image-4.4.0-42-generic
-
Remove that troublesome virtualbox file
sudo rm /etc/kernel/postinst.d/vboxadd
-
Update dpkg status & grub menu
sudo dpkg --audit sudo apt-get install -f sudo update-grub