SQLite RPM database in Red Hat Enterprise Linux 9 and later
SQLite RPM database (rpmdb) in Red Hat Enterprise Linux (RHEL) 9 and later
The database backend has seen significant improvements with Red Hat Enterprise Linux (RHEL) 9. Now based on the sqlite3 database engine, it is anticipated that a large number of corruption events will simply no longer be possible within the newer code base. Should a database corruption event occur, it is recommended to reach out to Red Hat Support to gain assistance in investigating the source.
Details:
- In RHEL 9, the
rpmdbis located at/var/lib/rpm, while in RHEL 10, it has been moved to/usr/lib/sysimage/rpm/. rpmdb.sqliteis the main database file.- Other two files (-shm and -wal) may exist as below.
RHEL 9: # ls -l /var/lib/rpm -rw-r--r--. 1 root root 43454464 Mar 18 02:56 rpmdb.sqlite -rw-r--r--. 1 root root 32768 Apr 5 03:50 rpmdb.sqlite-shm -rw-r--r--. 1 root root 0 Mar 18 02:56 rpmdb.sqlite-wal RHEL 10: # ls -l /var/lib/rpm lrwxrwxrwx. 1 root root 26 May 28 18:15 /var/lib/rpm -> ../../usr/lib/sysimage/rpm # ls -l /usr/lib/sysimage/rpm/ -rw-r--r--. 1 root root 38940672 May 28 18:16 rpmdb.sqlite -rw-r--r--. 1 root root 32768 Jun 9 19:52 rpmdb.sqlite-shm -rw-r--r--. 1 root root 0 May 28 18:16 rpmdb.sqlite-wal
What is the WAL file?
- WAL: Write-Ahead Logging
- One of the “atomic commit and rollback” implementations in SQLite.
- Steps per event
- MODIFY
A change was written to the WAL file (rpmdb.sqlite-wal). The original database (rpmdb.sqlite) has not changed yet. - COMMIT
A special record indicating a commit is added to the WAL file. - CHECKPOINT
WAL file transaction moves into the original database.
- MODIFY
How to check the database integrity?
# rpm --verifydb
# echo $?
0
If the command returns 0, it indicates that the database is healthy and no errors were detected. Any non-zero value suggests potential corruption or structural issues.
How to recover data from a corrupted database?
In advance of a database issue, create a current backup.
RHEL 9:
# cd /var/lib
# tar zcvf /var/preserve/rpmdb-`date +"%d%m%Y"`.tar.gz rpm
RHEL 10:
# cd /usr/lib/sysimage
# tar zcvf /var/preserve/rpmdb-`date +"%d%m%Y"`.tar.gz rpm
Most problems can then be solved by running:
# rpmdb --rebuilddb
or
# rpm --rebuilddb
which creates the database structure from the RPM headers that are also stored in the database. This should succeed as long as the primary Packages table is not corrupted.
Alternatively, try dumping the RPM database to a file with the following command:
# rpmdb --exportdb > /tmp/rpmdb-exportdb.out
If the verify fails and the dump succeeds, the rpmdb-exportdb.out can be used to create a new RPM database:
RHEL 9:
# rm /var/lib/rpm/*
# rpmdb --importdb < /tmp/rpmdb-exportdb.out
RHEL 10:
# rm /usr/lib/sysimage/rpm/*
# rpmdb --importdb < /tmp/rpmdb-exportdb.out
Direct database access
The following steps are provided for clarity and completeness. However, Database access and manipulation is only supported via the rpm and various package managers such as DNF. Direct access as shown above should be strictly avoided without the guidance of Red Hat Support Engineers.
How to access the underlying database?
- Use the
sqlite3command.# yum install sqlite RHEL 9: # sqlite3 /var/lib/rpm/rpmdb.sqlite sqlite> .databases main: /var/lib/rpm2/rpmdb.sqlite r/w sqlite> .tables Basenames Name Sigmd5 Conflictname Obsoletename Suggestname Dirnames Packages Supplementname Enhancename Providename Transfiletriggername Filetriggername Recommendname Triggername Group Requirename Installtid Sha1header RHEL 10: # sqlite3 /usr/lib/sysimage/rpm/rpmdb.sqlite sqlite> .databases main: /usr/lib/sysimage/rpm/rpmdb.sqlite r/w sqlite> .tables Basenames Name Sigmd5 Conflictname Obsoletename Suggestname Dirnames Packages Supplementname Enhancename Providename Transfiletriggername Filetriggername Recommendname Triggername Group Requirename Installtid Sha1header
How to dump all data from the database?
# sqlite3 rpmdb.sqlite '.dump' > /tmp/rpmdb.dump
How to restore from a dump file?
# cat /tmp/rpmdb.dump | sqlite3 rpmdb-new.sqlite
Reference
- Content from rpm.org is not included.Content from rpm.org is not included.https://rpm.org/user_doc/db_recovery.html
- Content from sqlite.org is not included.Content from sqlite.org is not included.https://sqlite.org/wal.html
- Content from sqlite.org is not included.Content from sqlite.org is not included.https://sqlite.org/pragma.html
- https://access.redhat.com/solutions/6903