Recovering from a broken apt-get upgrade

When doing the usual apt-get upgrade, I had a harddrive failure which resulted in an aborted upgrade.

After a reboot, I got the usual unmet dependencies, and the friendly suggestion to run apt-get -f install to continue. Doing so gave me another friendly unable to execute installed pre-removal script all the time, even though I tried forcing dpkg to cooperate:

root@nimbus:~# dpkg --purge --force all linux-image-2.6.31-21-generic
(Reading database ... 193743 files and directories currently installed.)
Removing linux-image-2.6.31-21-generic ...
dpkg (subprocess): unable to execute installed pre-removal script: Exec format error
dpkg: error processing linux-image-2.6.31-21-generic (--purge):
 subprocess installed pre-removal script returned error exit status 2
dpkg (subprocess): unable to execute installed post-installation script: Exec format error
dpkg: error while cleaning up:
 subprocess installed post-installation script returned error exit status 2
Errors were encountered while processing:

After mucking about with the various dpkg-* tools, I hit upon the idea of trying to install the new package “manually” using dpkg directly. Bingo!

root@nimbus:~# dpkg -i /var/cache/apt/archives/linux-image-2.6.31-21-generic_2.6.31 21.59_amd64.deb
Selecting previously deselected package linux-image-2.6.31-21-generic.
(Reading database ... 193747 files and directories currently installed.)
Preparing to replace linux-image-2.6.31-21-generic 2.6.31-21.59 (using 
 .../linux-image-2.6.31-21-generic_2.6.31-21.59_amd64.deb) ...
dpkg (subprocess): unable to execute old pre-removal script: Exec format error
dpkg: warning: old pre-removal script returned error exit status 2
dpkg - trying script from the new package instead ...
dpkg: ... it looks like that went OK.
Done.
Unpacking replacement linux-image-2.6.31-21-generic ...
dpkg (subprocess): unable to execute old post-removal script: Exec format error
dpkg: warning: old post-removal script returned error exit status 2
dpkg - trying script from the new package instead ...
Running postrm hook script /usr/sbin/update-grub.
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-2.6.31-21-generic
....

And now everything is back to normal.

Update: The harddrive failure was due to the SATA controller on the motherboard dying slowly.