Querying the Package Management Database






Querying the Package Management Database

The RPM package management database is an essential source of information about your system. The database is created when the system is installed and is updated whenever packages are added or removed.

As RPM packages are installed on your system, the metadata for those packages is stored in a database that can be queried. If you have a mystery file on your system and want to know where it came from, or want to know which version of a package is installed, or what a package does, an RPM query can answer your question in a few seconds.

How Do I Do That?

The rpm program provides access to the RPM database. The -q option enables query mode.

The default query takes a package name and tells you whether it is installed and, if so, which version is installed:

$ rpm -q selmyscan
package selmyscan is not installed
$ rpm -q httpd
httpd-2.0.54-10.2

More advanced queries use two different sets of arguments: one to control which packages are reported in the output, and one to control what is reported about the selected packages.

Figure describes the most commonly used options for selecting packages.

RPM query options for package selection
OptionDescription
-a

Selects all packages.
-f file
                           

Selects the package that installed file.
-g pkggroup
                           

Selects the packages that belong to pkggroup (such as Applications/Productivity).
-p pkgfile
                           

Selects the uninstalled RPM package file pkgfile, which can be a local filename or an HTTP or FTP URI. Information is retrieved from the package file instead of from the RPM database.
--triggeredby package
                           

Selects packages that have scripts that are triggered by the installation or removal of package. For example, a mail-client package may have a script that changes its configuration if the local mail server is changed from sendmail to postfix.
--whatprovides capability
                           

Selects packages that provide a certain capability, such as the ability to run perl scripts.
--whatrequires capability
                           

Selects packages that require a capability.
                              packagename
                           

Selects a package with the given name.


For example, to find out which package installed the file /usr/lib/libcdda_interface.so:

$ rpm -qf /usr/lib/libcdda_interface.so
cdparanoia-libs-alpha9.8-25

Or to find out which packages provide smtpdaemon (inbound mail server) capability:

$ rpm -q --whatprovides smtpdaemon
sendmail-8.13.4-2
postfix-2.2.2-2

Sometimes, though, you need more information than the name and version number of the packages selected. Figure lists the most common query output options.

Query output options
OptionDescription
--changelog

Shows the package changelog, a list of changes to the various versions of the package (not necessarily to various versions of the software).
-c

Shows the configuration files included in the package.
-d

Shows the documentation files included in the package.
-l

Lists files included in the package.
--filesbypkg

Same as -l, except that the package name is printed in front of each file; useful when multiple packages are selected.
-i

Provides detailed information about the package (package description, license, group, origin, and so forth).
--provides

Lists the capabilities provided by the package.
--requires

Lists the capabilities required to successfully use the package.
--scripts

Displays pre- and post-installation scripts, and pre- and post-uninstallation (removal) scripts.
--triggers

Displays the trigger scripts in the package. Trigger scripts are invoked when another, related package is installed or removed.


When output options and selection options are combined, rpm becomes a very powerful tool. For example, to see the description of the package that installed /etc/mail/access:

$ rpm -qif /etc/mail/access
Name        : sendmail                     Relocations: (not relocatable)
Version     : 8.13.4                            Vendor: Red Hat, Inc.
Release     : 2                             Build Date: Fri 06 May 2005 08:35:13 AM EDT
Install Date: Mon 29 Aug 2005 12:46:19 AM EDT      Build Host: decompose.build.redhat.com
Group       : System Environment/Daemons    Source RPM: sendmail-8.13.4-2.src.rpm
Size        : 1332268                          License: Sendmail
Signature   : DSA/SHA1, Fri 20 May 2005 01:44:43 PM EDT, Key ID b44269d04f2a6fd2
Packager    : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Summary     : A widely used Mail Transport Agent (MTA).
Description :
The Sendmail program is a very widely used Mail Transport Agent (MTA).
MTAs send mail from one machine to another. Sendmail is not a client
program, which you use to read your email. Sendmail is a
behind-the-scenes program which actually moves your email over
networks or the Internet to where you want it to go.

If you ever need to reconfigure Sendmail, you will also need to have
the sendmail.cf package installed. If you need documentation on
Sendmail, you can install the sendmail-doc package.

To see all of the files installed by the package that installed /usr/lib/libcdda_interface.so:

$ rpm -qlf /usr/lib/libcdda_interface.so
/usr/lib/libcdda_interface.so
/usr/lib/libcdda_interface.so.0
/usr/lib/libcdda_interface.so.0.9.8
/usr/lib/libcdda_paranoia.so
/usr/lib/libcdda_paranoia.so.0
/usr/lib/libcdda_paranoia.so.0.9.8

Or to see those files along with the package name:

$ rpm -qf --filesbypkg /usr/lib/libcdda_interface.so
cdparanoia-libs           /usr/lib/libcdda_interface.so
cdparanoia-libs           /usr/lib/libcdda_interface.so.0
cdparanoia-libs           /usr/lib/libcdda_interface.so.0.9.8
cdparanoia-libs           /usr/lib/libcdda_paranoia.so
cdparanoia-libs           /usr/lib/libcdda_paranoia.so.0
cdparanoia-libs           /usr/lib/libcdda_paranoia.so.0.9.8

To see all of the other capabilities provided by the package that provides the capability perl:

$ rpm -q --whatprovides perl --provides
APItest.so
B.so
Base64.so
Byte.so
ByteLoader.so
...(Lines snipped)...
perl(warnings::register) = 1.00
re.so
scalar.so
shared.so
threads.so
via.so
perl = 3:5.8.6-15

To see the scripts that will be triggered by removing sendmail:

$ rpm -q --triggeredby sendmail --triggers
triggerpostun scriptlet (using /bin/sh) -- sendmail < 8.10.0
/sbin/chkconfig --add sendmail
triggerpostun scriptlet (using /bin/sh) -- sendmail < 8.11.6-11
/usr/sbin/alternatives --auto mta

To list the files in the uninstalled RPM package file a52dec-0.7.4-4.fr.i386.rpm:

$ rpm -qlp a52dec-0.7.4-4.fr.i386.rpm
/usr/bin/a52dec
/usr/bin/extract_a52
/usr/share/doc/a52dec-0.7.4
/usr/share/doc/a52dec-0.7.4/AUTHORS
/usr/share/doc/a52dec-0.7.4/COPYING
/usr/share/doc/a52dec-0.7.4/ChangeLog
/usr/share/doc/a52dec-0.7.4/NEWS
/usr/share/doc/a52dec-0.7.4/README
/usr/share/doc/a52dec-0.7.4/TODO
/usr/share/doc/a52dec-0.7.4/liba52.txt
/usr/share/man/man1/a52dec.1.gz
/usr/share/man/man1/extract_a52.1.gz

If that RPM were on a remote web server or FTP server, you could substitute the URI for the filename:

$ rpm -qlp \ftp://ftp.ntua.gr/pub/video/videolan/testing/vlc-0.7.0-test1/rpm/rh9-fc1/rh9-fc1/vlc/a52dec-0.7.4-4.fr.i386.rpm
/usr/bin/a52dec
/usr/bin/extract_a52
...(Lines snipped)...

How Does It Work?

RPM packages are compressed archives of files with metadata. The archive is in cpio format, with gzip compression; the metadata is stored in a flexible, easily extensible format for forward- and (limited) backward-compatibility.

When a package is installed, the metadata is copied to the RPM database. If this were not done, it would be necessary to keep all of the original package files in storage to find out about installed packages, and queries would run very slowly because hundreds of files would have to be individually opened and searched.

The RPM database is stored in several files in /var/lib/rpm. These databases are in the indexed DBM/GDBM format, which is also used for other configuration databases such as /etc/aliases.db; this indexed format permits high-speed searching.

What About...

...converting an RPM to a plain archive?

The rpm2cpio command will convert an RPM package to a cpio archive:

$ rpm2cpio gnome-applet-gvid-0.3-1.i386.rpm > gnome-applet.cpio

You can then use cpio to examine or install the archive. Note that rpm2cpio removes the gzip compression on the archive contents, so the resulting file is larger than the original RPM file. If you want to extract a specific file from the archive, you can use the cpio command. However, this is not a good way to install the file, since none of the scripts and other install-time actions will be performed:

$ rpm2cpio gnome-applet-gvid-0.3-1.i386.rpm | cpio -idv
               

...a damaged RPM database?

Use rpm with the -- rebuilddb option to recover from most forms of database corruption (this can take a while to run). You will need to run it as root:

# rpm --rebuilddb

Where Can I Learn More?



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows