Inspecting and extracting RPM package contents with & without installing the package | Packagecloud Blog (2024)

Overview

This post covers:

  • How to examine files in an RPM package you possess
  • How to examine files of an RPM package installed on a system
  • How to extract a cpio archive from an RPM package
  • How to extract files from a cpio archive
  • How to show preinstall and postinstall scripts of an RPM package
  • How to examine files in an RPM package in a remote repository

Extract files from an RPM (quick start)

For the impatient, this is the command to extract files from an RPM package you possess:

$ rpm2cpio ./packagecloud-test-1.1-1.x86_64.rpm | cpio -idmv

Continue reading this post for more information about listing and extracting RPMs.

Related Post

Inspecting and extracting Debian package contents

What is an RPM package?

RPM stands for RedHat Package Manager and it is used in RedHat Linux and all its derivative Linuxes. RPM is also used to refer to the package format used to package software used by RedHatLinux and its derivates. An RPM package is simply a header structure on top of aCPIOarchive. The package itself is comprised of four sections: a header with a leading identifier (magic number) that identifies the file as an RPM package, a signature to verify the integrity of the package, the header or ‘tagged’ data containing package information, version numbers, and copyright messaging, and the archive containing the actual program files.

How to list files in an RPM package file using therpmcommand

The RPM package managerrpmcomes with various utilities to interact with packages. The following command will list all the files inside an RPM package:

$ rpm -qlp ./path/to/test.rpm 

For example:

$ rpm -qlpv ./packagecloud-test-1.1-1.x86_64.rpm-rwxr-xr-x 1 root root 8286 Jul 16 2014 /usr/local/bin/packagecloud_hello

In this example, therpmcommand is used with the flag-qto specify it as a query command,-lto list the files in the package, and-pso it knows to query the uninstalled package file. The-vflag (verbose) just provides additional information (permissions, owner, etc.) for the sake of this example. As we can see, the package installs an executable binary calledpackagecloud_hellointo/usr/local/bin/.

How to list files of an installed RPM package

Use therpmcommand with-qand-lflags to list the files from an installed RPM package:

$ rpm -ql packagecloud-test

NOTEthe use of a package’s name in the previous command and not the path to a specific RPM package.

How to extractcpioarchive from RPM packages

To extract files from an RPM package you must first extract acpioarchive from the package itself. RedHat provides a utility calledrpm2cpiowhich does exactly that:

$ rpm2cpio ./packagecloud-test-1.1-1.x86_64.rpm

How to extract files from an RPM package’scpioarchive

Therpm2cpiocommand will output (to stdout) acpioarchive from the RPM package. To extract the package files we’ll use the output fromrpm2cpioand then use thecpiocommand to extract and create the files we need.

For example:

$ rpm2cpio ./packagecloud-test-1.1-1.x86_64.rpm | cpio -idmv./usr/local/bin/packagecloud_hello17 blocks

Thecpiocommand copies files to and from archives. In the example above, we usecpiowith the-iflag to extract the files from the archive,-dto create the leading directories where needed, and-mto preserve the file modification times when creating files. The-vflag (verbose) is to list the files processed for the sake of this example.

The result of our previous example is the creation of a./usr/folder in our working directory containing the files from the RPM packagepackagecloud-test-1.1-1.x86_64.rpm.

$ file usr/local/bin/packagecloud_hellousr/local/bin/packagecloud_hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x77fe4f2fa02ee973bf4d74867729e950fcde7107, not stripped

NOTEthat simply extracting package files to the root directory doesNOTproperly install a package. Use theyumorrpmtools to correctly install RPM packages.

How to show RPM package preinstall and postinstall scripts

To show the scripts that will run when a package is installed or uninstalled from a system, use the--scriptsflag when querying a package usingrpm. The following command will show the scripts for an uninstalled packagetest-1.1-1.el6.x86_64.rpm:

$ rpm -qp --scripts ./packagecloud-test-1.1-1.x86_64.rpm

This will output something like:

preinstall scriptlet (using /bin/sh):# Do somethingpostinstall scriptlet (using /bin/sh):if [ $1 -eq 1 ] ; then # Do another thingfipreuninstall scriptlet (using /bin/sh):if [ $1 -eq 0 ] ; then # Do something elsefipostuninstall scriptlet (using /bin/sh):# Do things here, too

To view the scriptlets of an already installed package, you can use the following syntax when usingrpm

$ rpm -q --scripts <packagename>

How to view contents of RPM packages on remote repositories usingrepoquery

repoqueryis provided by theyum-utilspackage, make sure it’s installed:

$ yum install yum-utils

Therepoquerycommand is used to query information fromYumrepositories installed on the system. By default, therepoquerycommand will download theYumrepo metadata and update the cache. To runrepoqueryentirely from theYumcache, use the-Cor--cacheflag. To list the contents of a package, pass the--listflag to therepoquerycommand:

$ repoquery --list <packagename>

For example:

$ repoquery --list packagecloud-test/usr/local/bin/packagecloud_hello

This can be useful when viewing the contents of packages that aren’t downloaded or installed on your the system.repoquerywill only provide information on packages avaliable in the configuredYumrepositories.

Conclusion

Understanding how packages interact with the systems they’re installed on can be helpful in day-to-day operations. By knowing that the RPM package is comprised of acpioarchive and header data, we can extract the information needed with already existing tools (rpm2cpioandcpio) and use the RPM toolchain to query, inspect, and view the contents of an RPM package.

Set up your own package repository.

Fast, reliable, and secure software starts here.

Try Packagecloud

Inspecting and extracting RPM package contents with & without installing the package | Packagecloud Blog (1)

Inspecting and extracting RPM package contents with & without installing the package | Packagecloud Blog (2024)
Top Articles
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 5943

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.