2

I am working on a non-redhat based machine and I need to extract Version: x.y.z and Release: a.b.c information from some .rpms that pass across the machine.

Typically one would do:

rpm -qip ./Foo.x.y.z.rpm | grep Version | awk ...

But this isn't a redhat-based machine so I don't have rpm and I don't really want to install a 2nd package manager anyway.

I thought that if I did:

rpm2cpio ./Foo.x.y.z.rpm | cpio -idmv

Then when the cpio was extracted I would find some file named .preamble or something that would have the data in it. But no luck. Anyone know another way?

nolandda
  • 193
  • 1
  • 9

2 Answers2

4

rpm2cpio use case is to extract files without the metadata, the opposite of what you want. It like other tools requires linking against librpm. Even Midnight Commander exploring an rpm file uses the rpm binary.

On an system with yum, use createrepo to create a repository from these rpms. XML and sqlite will be generated with the metadata. While easier to parse than the rpm binary format, you will have to write the queries yourself if you are not using yum. Keep the repodata so you have a functional yum repo.

Going up yet another level of abstraction, systems lifecycle tools include content management. Namely, Katello or Red Hat Satellite. A bit heavyweight compared to a shell script, but you can get hosts and the packages they subscribe to with an API.

John Mahowald
  • 30,009
  • 1
  • 17
  • 32
  • Accepting this as the answer since the pointer to librpm was the key. It is ~80 lines of C to do what I want using librpm. https://github.com/nolandda/rpmquery – nolandda Apr 07 '19 at 17:32
2

Just go ahead and install rpm. It's the easiest thing. Your distro almost certainly has it available to you.

Now you can use RPM query formats to extract the information you want.

For example:

file=/path/to/filename.rpm

name=$(rpm --queryformat "%{name}" -qp $file)
version=$(rpm --queryformat "%{version}" -qp $file)
release=$(rpm --queryformat "%{release}" -qp $file)

echo "This RPM named $name has version $version release $release"
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940