On one of my Mandrake Linux systems, I had once this problem:
[pascal@hermes pascal]$ su
Password:
[root@hermes pascal]# rpm -q -a
failed to open //var/lib/rpm/packages.rpm
rpmQuery: rpmdbOpen() failed
[root@hermes pascal]# rpm --rebuilddb
failed to open //var/lib/rpm/packages.rpm
[root@hermes pascal]# rpm --initdb
[root@hermes pascal]# rpm -q -a
failed to open //var/lib/rpm/packages.rpm
rpmQuery: rpmdbOpen() failed
[root@hermes pascal]# rpm --version
RPM version 3.0.3
Well, clearly the RPM database was completely out of usage. I had to rebuild it. You may think that it's simply a matter of using :
rpm --rebuilddb
However, I learned that --rebuilddb is REALLY AND COMPLETELY
USELESS. May be the authors and contributors to rpm could take some
idea from this NFAQ to actually implement a true
--rebuilddb option.
The main idea of the process is to use:
rm -f /var/lib/rpm/* ; rpm --initdb
and:
rpm -i -v --nodeps --noscripts --notriggers --excludepath / packages...
to fill a package database without really installing anything (not overriding installed files, not running any script, ...). This is done in the seventh step; everything before I made to recover a list of installed packages.
This is not perfect, since some packages I have upgraded or modified the sources and upgraded. I should have checked for /usr/src/RPMS too; I'll update the database later by hand to synchronize it with the handfull of packages I touched after the initial installation.
+------------------------------------------------------------------+
| COOKBOOK PROCEDURE TO REBUILD A RPM DATABASE DAMAGED BEYOND HOPE |
+------------------------------------------------------------------+
####
#### BEWARE, THIS IS NOT AN AUTOMATIC SCRIPT!
####
#### Use this only as a guideline to rebuild your rpm database.
####
#####
# 1 # Find the files currently installed.
# # ON THE HOST WHO LOST ITS RPM DATABASE.
#####
find / -print | sort > /tmp/dsk-contents
#####
# 2 # Find all the files installed by the installation RPMs,
# # with their corresponding RPM.
#####
losetup /dev/loop0 /mnt/usr3/mandrake/mandrake61-1.iso
mount -o ro -t iso9660 /dev/loop0 /mnt/cdrom
cd /mnt/cdrom/Mandrake/RPMS
rm -f /tmp/rpm-contents
for f in *.rpm ; do
rpm -q -l -p $f \
| awk -v pn=$f '{printf "%s:%s\n",pn,$0;}' >> /tmp/rpm-contents
done
sort -t: +1 -o /tmp/rpm-contents /tmp/rpm-contents
#####
# 3 # Obtain the actually installed RPMs.
#####
gzip -d < lgetvalue-src.tar.gz | tar xf -
cd lgetvalue-src
make all test
./lgetvalue --field-separator=: \
--key-position=2 \
--value-position=1 \
/tmp/rpm-contents /tmp/dsk-contents \
| sort -u > /tmp/rpm-installed
#####
# 4 # Reinitialize a new rpm database.
#####
mv /var/lib/rpm /var/lib/rpm-damaged-beyond-hope
mkdir /var/lib/rpm
rpm --initdb
#####
# 5 # Let rpm say what problems there may be
# # (trying to do a pseudo-installation.
#####
cd /mnt/cdrom/Mandrake/RPMS
rpm -i -v --noscripts --notriggers --excludepath / `cat /tmp/rpm-installed`
#####
# 6 # Correct the list of installed RPMs.
#####
emacs /tmp/rpm-installed
#####
# 7 # Rebuild the rpm data base, without installing any file
# # nor running any script.
#####
rpm -i -v --nodeps --noscripts --notriggers --excludepath / \
`cat /tmp/rpm-installed `
#####
# 8 # Check you have a valid rpm database.
#####
rpm -q -a