45

Is there any way to extract the spec file from rpm package ( I have only RPM file )

not by

   rpm --scripts -qp my-great-app-1.1.2.rpm 

( this syntax not get the spec file only the scripts from the rpm)

ILMostro_7
  • 1,422
  • 20
  • 28
jon
  • 903
  • 5
  • 14
  • 21
  • are you sure the spec file is stored in the RPM? In a .src.rpm, certainly, but for a normal RPM i'm not certain about that at all. – Mat Apr 10 '11 at 18:57
  • so if I have only rpm file where I can get the spec file , please help – jon Apr 10 '11 at 18:59
  • look for the source RPM, or that package's homepage. – Mat Apr 10 '11 at 19:00
  • I have only rpm file not source – jon Apr 10 '11 at 19:01
  • The spec file is useless without the source - it's only reason to exist is to be able to create the rpm from the source. What are you trying to achieve? What package is this? – Mat Apr 10 '11 at 19:03
  • I need to fix some of the scripts in the rpm then I need to pack the rpm again , in the spec I need to update the version .etc , so what to do , its urgunt and I need to finish this task until 24:00 – jon Apr 10 '11 at 19:06
  • find the spec, rebuild it using your knowledge of the package, look at the rpm with an editor to find all the scriptlets/dependencies you can extract, etc... – Mat Apr 10 '11 at 19:08
  • 1
    give me example how to do that , how with vi ? I see in the rpm many characters and Chinese word , I cant find the spec in this way -:( – jon Apr 10 '11 at 19:14
  • Something here not acceptable , I need to update the rpm pkg file and its obvious that I need also to update the spec file and the rebuild again the rpm , but I not have the spec file I have only rpm – jon Apr 10 '11 at 19:18
  • @jon i stuck with the same situation,did you succeeded to get spec file of any rpm ? – Juned Nov 01 '12 at 11:41

7 Answers7

30

Install rpmrebuild and "extract" (actually re-create) the spec file of your rpm file or your already installed package.

Examples:

rpmrebuild --package --notest-install -e oracle-instantclient-basic-10.2.0.4-1.x86_64.rpm
rpmrebuild -s hercules.spec hercules
reichhart
  • 813
  • 7
  • 13
  • 15
    Looking at rpmrebuild's code and then actually testing it, it doesn't "extract" the spec file; it regenerates most of a spec file (header, log, pre/post scripts). But, critically to rebuilding, it doesn't actually get the %prep or %build sections; it just leaves those at their default values (which are "make clean" and "make install," respectively). I suspect this is because /that/ information is not stored in RPMs. So, while this should work in the most general cases, it doesn't look like it will universally work (esp. for packages with complex compilation steps). :/ – dannysauer Jul 22 '15 at 20:16
  • dannysauer: sure, actually it is no extraction because it is a rebuild of the spec file ("extract" was the question). The spec is not part of the RPM package and therefore the rebuild is the only way to get a "spec template". But this could be a good starting point in many cases. In the past it helped me sometimes to fix binary rpm packages. – reichhart Jun 24 '17 at 01:54
16

The spec file is not stored in binary rpms unless the packager specifically included it for some reason (and there's really no reason to do that). The only information you can get from a binary rpm is the information that rpm -qi <package> returns, and the files that rpm -ql <package> lists. If you need more than that, you have to find the source package. If Google / the vendor's web site fails to provide that for you, there should be contact information provided in the Packager field for anything packaged by anyone competent. For example, here's a package that ships with RHEL and a package from a third party vendor:

$ rpm --qf '%{Packager}\n' -q redhat-release
Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
$ rpm --qf '%{Packager}\n' -q cfengine-community
CFEngine AS (packager@cfengine.com)

There you have a website and an email address, respectively, where you could ask about a spec file or srpm file.

dannysauer
  • 3,793
  • 1
  • 23
  • 30
11

spec files are usually not in rpm. They are in source rpm.

Rumple Stiltskin
  • 9,597
  • 1
  • 20
  • 25
10

you could use

yumdownloader --source < something.rpm

then:

rpm2cpio packagename | cpio -ivd
Kevin
  • 53,822
  • 15
  • 101
  • 132
kamal
  • 9,637
  • 30
  • 101
  • 168
10

Per @RumpleStiltskin's answer, the original spec files are found in source rpms which can be extracted. To get the source rpms, run the following:

yum install yum-utils # Only required if yumdownloader is not installed
yumdownloader --source <package name, like 'emacs-nox'>

This will install the package to the current directory. To extract it run:

rpm2cpio <package name>.src.rpm | cpio -civ '*.spec'

The .spec file will be in your current directory.

If you can't install yum-utils for some reason, look at the files in /etc/yum.repos.d/ and look for sections referring to source rpm repositories. You can type in the values for baseurl into your browser and manually search for the source package. Extracting the .spec still requires rpm2cpio.

Rick Smith
  • 9,031
  • 15
  • 81
  • 85
9

rpmrebuild is your friend. Use

rpmrebuild -e -p <rpm_file>

As it opens up the spec file in an editor, you can also make changes to rpm spec.

esmeagol
  • 107
  • 1
  • 2
-3

I could get all my .spec, source, Patches by this simple command

$ rpmbuild --recompile --noclean ./SRPMS/somerpm.src.rpm

Now one can change spec, src and rebuild RPM or SRPM.

Valy Dia
  • 2,781
  • 2
  • 12
  • 32