My /var/lib/dpkg/status file got deleted and according to apt I had no packages installed, as it determines them from the status file. I used the following method to regenerate the /var/lib/dpkg/status file. The method worked very fine for me in Kali GNU/Linux, which is Debian-based.
NOTE:
This method assumes that all installed packages have created entries in the /usr/share/doc directory.
This method starts from an empty /var/lib/dpkg/status file.
This method may not work if you have more than 1 repository providing the same package. If you do have more than 1 repository providing the same package, temporarily disable the subsidiary repo, then delete the package cache lists for the repo in /var/lib/apt/lists/.
Delete the current status and status-old files. Make a backup of the files before deletion incase you get errors.
mkdir ~/dpkg-status-backup
cp /var/lib/dpkg/{status,status-old} ~/dpkg-status-backup
rm /var/lib/dpkg/{status,status-old}
Get list of all packages installed, one package per line.
Note: it's 1 (one) not l (lower-case L).
ls -1 /usr/share/doc > installed-packages.list
Rewrite the packages list into one line.
tr '\n' ' ' < installed-packages.list > installed-packages-one-line.list
Create an empty status file to enable apt-cache to run.
touch /var/lib/dpkg/status
Generate package records using apt-cache.
cat installed-packages-one-line.list | xargs apt-cache show > raw-status-file
The raw-status-file may contain Status: fields for some packages. Delete them to avoid duplication.
sed -i '/^Status:/d' raw-status-file
Add the Status: field for every package to indicate to apt & dpkg that they're installed.
sed -i 's/^Package:.*/&\nStatus: install ok installed/' raw-status-file
Remove fields unwanted by dpkg in status file: SHA1:, SHA256:, MD5sum:, Description-md5:, Size: & Filename: .
sed -i '/^MD5sum:/d' raw-status-file
sed -i '/^SHA1:/d' raw-status-file
sed -i '/^SHA256:/d' raw-status-file
sed -i '/^Size:/d' raw-status-file
sed -i '/^Filename:/d' raw-status-file
sed -i '/^Description-md5:/d' raw-status-file
The resultant raw-status-file after running the above commands is now the actual dpkg status file.
mv raw-status-file status
Copy the status file to /var/lib/dpkg/ as status and status-old.
cp status /var/lib/dpkg/status
cp status /var/lib/dpkg/status-old
Now run dpkg to validate your new status file. If you get errors, take hints from dpkg's error report, & solve accordingly. If you get no errors, then the status file has the correct syntax.
dpkg --get-selections
Re-enable the disabled repos then run apt to update package cache lists to prevent the error packages have unmet dependencies.
apt update
If you run into packages have unmet dependencies error in apt, then make sure you've enabled the subsidiary repos that provide the dependencies, then run aptitude. I prefer aptitude because it's better at solving package installation errors.
aptitude full-upgrade
If you get no errors/your errors have been resolved from apt or dpkg, and get "PACKAGE is already the newest version..." when you try to install a program you're sure is already installed, such as coreutils, then you've successfully regenerated the dpkg status file.
Clean-up the temp files created and the backup.
rm installed-packages.list installed-packages-one-line.list status
rm -r ~/dpkg-status-backup