Oli Warner About Contact Oli on Twitter Subscribe

Fixing /var/lib/dpkg/status corruption

Sunday, 30 January 2011 dpkg

Apt and dpkg are wonderful tools when they work. However, like most tools, if you chuck rubbish at them, they’ll stop working. Here’s a quick guide to overcoming the most common break in /var/lib/dpkg/status.

If you’ve ever gone to apt-get remove something with tab-completion and it baulks out with:

grep-status: /var/lib/dpkg/status:26787: expected a colon

… you’re suffering from some corruption. Opening the file at the given line rarely shows the right error because formatting issues in the file causes a cascade error and it only detects a break much further on.

The required formatting in this file is truly bizarre.

Package: a-package-name
Status: install ok installed
Other-Fields: blah
Description: The description may have many lines
 but double-breaks need a full-stop.
 Like that.

Package: a-bad-package
Status: install ok installed
Other-Fields: blah
Description: Some packages aren't well formed.
 And break the rules. This is an example of a bad description

And there’s a blank newline between packages. So if a package gets inserted into the local database and it doesn’t follow these guidelines, dpkg breaks. In my case it has only ever broken the remove-tab-completion (as provided by grep-status) but I’ve always found it very hard to fix… Until now.

What we need is a tool that can search for multiple newlines where the first thing on the next line isn’t Package:. grep can’t do multi-line so we have to turn to pcregrep. It’s as simple as this:

sudo apt-get install pcregrep
pcregrep -nM '\n\s*\n[^P]+' /var/lib/dpkg/status

This will return a block of text and a line number like so:

$ pcregrep -nM '\n\s*\n[^P]+' /var/lib/dpkg/status
25643: The program uses wiper.sh to TRIM disks.

wiper.sh is part of the open source project hdparm
and can be obtained from the hdparm web page.

Homepage: http://disktrim.sourceforge.net

Then we just nano into the file (using the plus-argument to go straight to the right place):

sudo nano +25643 /var/lib/dpkg/status

And edit the description so any blank lines just start with a .. Save it and run a sudo apt-get update and you’ll find that apt-get remove tab-completion works!