Recently, the root file system on a test vm got corrupted and /var/lib/dpkg was wiped out along with various other files. Here is some info on rebuilding that systems dpkg database to a useful state in absence of a backup. YMMV depending on what files are available.

Recover dpkg status file:

  cp /var/backups/dpkg.status.0 /var/lib/dpkg/status

Some dpkg and debconf perl modules were missing. However /var/cache/apt/archives/ had a full set of deb files for the system.

Reinstall dpkg and debconf (package versions for hardy here):

  cd /var/cache/apt/archives/
  dpkg --force-depends -i dpkg_1.14.16.6ubuntu4_i386.deb
  dpkg --force-depends -i debconf_1.5.20_all.deb

Now all of the /var/lib/dpkg/info/ files were missing, so dependency checking is completely fubar, and dpkg-reconfigure wont work to fix it. However, apt can rebuild all of that stuff.

Reinstall apt:

  cd /var/cache/apt/archives/
  dpkg --force-depends -i apt_0.7.9ubuntu17.2_i386.deb
  dpkg --force-depends -i apt-utils_0.7.9ubuntu17.2_i386.deb
  apt-get update

How to reinstall all of the missing /var/lib/dpkg/info files?

Get a list of packages that are supposedly installed:

  dpkg -l | grep ii

Create a script and use apt to reinstall them:

  dpkg -l | grep ii | awk '{print "apt-get --reinstall -y install", $2}' > /tmp/reinstall
  sh /tmp/reinstall

Some packages will still not install correctly as more stuff is missing from /var/lib/dpkg. In particular the package "ucf" is important to enable the kernel and possibly some others to install correctly.

Without /var/lib/dpkg/info/ucf.templates the kernel install produces this error:

  failed to install/upgrade: User postinst hook script [/sbin/update-grub] exited with value 10

Install ucf:

  apt-get --reinstall install ucf

After a while of re-downloading and reinstalling every package on the system, all is "well".