APT Package Management – How to Recreate /var/lib/dpkg/status

aptdirectory-structuredpkgpackage-management

I have deleted some files around /var/lib/dpkg/, namely:

/var/lib/dpkg/status
/var/lib/dpkg/available
/var/lib/dpkg/info/*

I understand Debian uses these files to keep some information about installed packages. Now when I do apt-get update, I get following error:

Reading package lists... Error!
E: Could not open file /var/lib/dpkg/status - open 
(2: No such file or directory)
E: The package lists or status file could not be parsed or opened.

As I understand the FHS, files located in /var are not supposed to be system-critical. Rater these should be temporary files, logs, caches, and similar.

Is there therefore a way to recreate the deleted files ?

Best Answer

If you look at the purpose of /var as given in the Filesystem Hierarchy Standard, it says:

/var contains variable data files. This includes spool directories and files, administrative and logging data, and transient and temporary files.

Note that "transient and temporary" files are just one of the things it contains. It also contains "spool directories and files" and "administrative and logging data". You deleted critical "administrative data".

It goes on to explain why /var exists:

/var is specified here in order to make it possible to mount /usr read-only. Everything that once went into /usr that is written to during system operation (as opposed to installation and software maintenance) must be in /var.

That's the key thing about /var: the data in it changes, unlike /usr (which only changes when you add/remove/update software).

Further sections explain the various subdirectories of /var; for example, /var/lib (where the files you deleted used to live) holds "state information pertaining to an application or the system", defined as "data that programs modify while they run, and that pertains to one specific host."

You really shouldn't delete files without knowing what the specific file is for. With the files you deleted, unless you have a backup of these files, I think the only thing left to do is take a backup of /home, /etc etc. and reinstall. Until you do so, you'll be unable to use dpkg (and APT, etc.). Other than that, the system should continue to function.

Related Question