How do I rebuild a corrupt dpkg status file?

You should be able to work with a previous known good status file and update from there. Every time you do an install or a update, the status file is saved to a gzipped backup under /var/backups. Doing an ls -l dpkg * on the directory shows:

-rw-r--r-- 1 root root   2266732 2010-09-30 08:35 dpkg.status.0
-rw-r--r-- 1 root root    624182 2010-09-29 08:49 dpkg.status.1.gz
-rw-r--r-- 1 root root    623844 2010-09-28 08:55 dpkg.status.2.gz
-rw-r--r-- 1 root root    620358 2010-09-24 11:04 dpkg.status.3.gz
-rw-r--r-- 1 root root    619021 2010-09-23 15:34 dpkg.status.4.gz
-rw-r--r-- 1 root root    619013 2010-09-23 08:03 dpkg.status.5.gz
-rw-r--r-- 1 root root    618968 2010-09-21 08:33 dpkg.status.6.gz

There's also a backup of the file created in the /var/lib/dpkg/ directory named status-old. Doing an ls -l status* on the directory shows:

-rw-r--r-- 1 root root 2266732 2010-09-30 08:35 status
-rw-r--r-- 1 root root 2267191 2010-09-30 08:35 status-old

So, to recover from a corruption, you should be able to do the following:

1. Make a backup of the corrupt status file:

mv /var/lib/dpkg/status /var/lib/dpkg/status_bkup

2. Copy an recent dpkg status file into place from either of the sources above:

either

cp /var/lib/dpkg/status-old /var/lib/dpkg/status

or

cp /var/backups/dpkg.status.#.gz /var/lib/dpkg/
gunzip -d /var/lib/dpkg/dpkg.status.#.gz 
mv /var/lib/dpkg/dpkg.status.# /var/lib/dpkg/status

3. Then run apt-get update:

sudo apt-get update

That should do it.


I have finally fixed my system of this. Restoring a backup of the status file didn't work as I've had the issue for so long, it's in all of my backups.

The fix involves grepping for the actual formatting breaks and fixing them manually. It's not as hard as it sounds.

http://thepcspy.com/read/fixing-dpkg-status-corruption/


I was able to fix this problem by removing the packages which had corrupted entries in the status file.

sudo dpkg -r handbrake-cli

The accepted solution via pcregrep didn't work (pcregrep didn't find anything).


Try a "dpkg -P " for the offending package. That will purge it from the local repository, removing all traces. On my system, that was the fix for removed (but not yet purged) packages that produced that error.


In this case I would back up the corrupted /var/lib/dpkg/status file and then correct it manually (around the lines 1888 and 9550) using the information from

apt-cache show libssl0.9.8
apt-cache show udev