Fedora SRC RPM

So, you want to rebuild a Fedora package from source using a src.rpm file? Maybe make some tweaks along the way? This is how.

Quick Prerequisites Check

First, make sure you have yum-utils and rpmdevtools installed

sudo yum -y install yum-utils rpmdevtools 

Next, configure your user’s .rpmmacros file. Use your username, not root! Edit ~/.rpmmacros and add these lines:

%_topdir      %(echo $HOME)/rpmbuild
%__arch_install_post   /usr/lib/rpm/check-rpaths   /usr/lib/rpm/check-buildroot

The first line, indicates the rpm build top directory. Then, we add some some post install commands. Systems with SMP enabled should also add the following to enable parallel compiles during builds. Adjust the job number to be the number of CPU cores available. In my example, two.

%_smp_mflags  -j2

I also like to format my rpm query string so that architecture is part of the name:

%_query_all_fmt %%{name}-%%{version}-%%{release}.%%{arch}

To review, here is my .rpmmacros file:

%_topdir      %(echo $HOME)/rpmbuild
%_smp_mflags  -j2
%__arch_install_post   /usr/lib/rpm/check-rpaths   /usr/lib/rpm/check-buildroot
%_query_all_fmt %%{name}-%%{version}-%%{release}.%%{arch}

Get the Source

Build the rpmbuild folder and it’s subfolders using rpmdev-setuptree from the rpmdevtools package. Note that you can also wipe ~/rpmbuild clean with /usr/bin/rpmdev-wipetree.

/usr/bin/rpmdev-setuptree

Then, cd into ~/rpmbuild/SRPMS and use yumdownloader (part of yum-utils) to download the source code. In the following example, I am downloading httpd.

cd ~/rpmbuild/SRPMS/
yumdownloader --source httpd

Build the Source

Use yum-builddep as root to install all packages needed to build the source RPM. The yum-builddep tool is part of the yum-utils package.

sudo yum-builddep httpd-2.2.8-3.src.rpm

You have two options when compiling the RPM from source. One, you just rebuild the src.rpm as-is. This will drop a binary RPM in ~/rpmbuild/RPMS/$(arch):

rpmbuild --rebuild httpd-2.2.8-3.src.rpm

Otherwise, if you want to make changes to the source RPM’s spec file, first install the source RPM:

rpm -ivh httpd-2.2.8-3.src.rpm

Now you can edit the spec file in ~/rpmbuild/SPECS and build a binary package using the edited spec file:

cd ~/rpmbuild
vim SPECS/httpd.spec
rpmbuild -bb SPECS/httpd.spec

To also build a new source RPM from your edited SPEC, use rpmbuild -bs

rpmbuild -bs SPECS/httpd.spec

Again, binary RPMs are produced in ~/rpmbuild/RPMS/$(arch).

Sources

FedoraProject.org: Docs/CustomKernel
HowToForge.com: How To Compile A Kernel – The Fedora Way

RPM -e Error Specifies Multiple Packages

Running into this?

bash$ sudo rpm -e zlib-devel
error: "zlib-devel" specifies multiple packages

This is because zlib-devel.i386 and zlib-devel.x86_64 are both installed. It is possible to remove them individually:

bash$ sudo rpm -e zlib-devel.i386
bash$ sudo rpm -e zlib-devel.x86_64

By default, Fedora, CentOS and RedHat shells do not specify the architecture of an RPM in the query format. This can lead to duplicate entries from queries:

bash$ rpm -q zlib-devel
zlib-devel-1.2.3-14.fc8
zlib-devel-1.2.3-14.fc8

You can use the –queryformat switch when running rpm -q, or configure the query format setting in ~/.rpmmacros.

bash$ rpm -q --queryformat "%{name}.%{arch}\n" zlib-devel
zlib-devel.i386
zlib-devel.x86_64
 
bash$ cat ~/.rpmmacros 
%_query_all_fmt %%{name}-%%{version}-%%{release}.%%{arch}
 
bash$ rpm -q zlib-devel
zlib-devel-1.2.3-14.fc8.i386
zlib-devel-1.2.3-14.fc8.x86_64

YUM Force Reinstall

Since Yum does not have a force flag, rpm commands must be used along with Yum to do some heavy lifting. Here are a few ways to force the reinstall of a broken package on a Yum Managed system.

Yum Remove and then Install

The easiest solution is to yum remove the package and then yum install the same package. If there are too many dependencies at stake with the package in question, try another method.

yum remove PACKAGE
yum install PACKAGE

Force Erase and then Yum Install

RPM dependencies sometimes make a simple yum remove impossible and Yum will want to erase your entire OS before moving on. In this case, use rpm to force erase, then yum to install.

rpm -e --nodeps PACKAGE
yum install PACKAGE

Prune RPM Database and then Yum Install

If your package install is so corrupted that an rpm -e is dangerous or impossible, even with –nodeps, remove the package from the local RPM database to trick yum into reinstalling the package. No files are deleted when using rpm -e with –justdb.

rpm -e --justdb --nodeps PACKAGE
yum install PACKAGE