Why rpm database gets corrupted
There can be multiple reasons which can lead to rpm database corruption. One can not exactly pinpoint a single RCA for that because of the limitations, there is no mechanism to monitor rpm database corruption.
– rpm database gets queried every time Administrator/some process run any rpm command.
– Even during yum transactions, rpm database is altered heavily.
– A small pointer can also cause such corruption.
– If LD_LIBRARY_PATH is set to some value, even rpm database corrupts.
– If any third party package is installed which causes un-necessary changes in rpm database.
– Incomplete transactions too can lead to such corruptions.
– In case of /var partition located on SAN volumes.
So in total, lots of factors can be considered for such rpm database corruption. If rpm database goes corrupt frequently, it’s recommended to take backup of RPM database frequently via cron.
Repair the RPM database
The RPM tool uses its own custom database implementation to contain its information. Sometimes that database can get corrupted; symptoms include claims that an installed RPM package is missing; or attempts to update an RPM simply hang. Attempts to query (rpm -q) some packages may fail with below error.
rpmdb: /var/lib/rpm/Packages: unexpected file type or format error: cannot open Packages index using db3 - Invalid argument (22)
If your RPM database is hung or corrupted you can try to repair it with these commands. Please note: This process is not guaranteed to work. You must be logged in with superuser privilege (aka “root”) for these steps. The commands may be copied and pasted directly into a root terminal window.
1. Make sure there are no RPM processes left running on your system. Use the ps command to identify them. Use the kill command to terminate any “rpm” processes you find; a “kill -9” may be necessary.
# ps -ef | grep-i rpm
2. Delete the lock files that RPM uses:
# rm -f /var/lib/rpm/__db*
# /usr/lib/rpm/rpmdb_verify Packages
3. If you were experiencing a hanging “rpm” command, try it again. If it works, you are finished. If not, perform Step #1 and #2 again. Then proceed to the next step.
4. Make a backup of your RPM database:
# cd /var/lib # mkdir rpm-backup # rsync -av ./rpm/. ./rpm-backup/.
5. Rebuild your RPM database:
# rpm -vv --rebuilddb > /tmp/rpmrebuilddb.log 2>&1
6. Try verifying the rpm database again.
# cd /var/lib/rpm # /usr/lib/rpm/rpmdb_verify Packages
6. Retry your failed RPM command.