Building an RPM

In this example, I build a patched cfengine 2.1.21 on a RHEL 4 system.

One of the tenets of RPM building is to never build as root. Building as root with an incorrect .spec file could, in the worst case, render the build system inoperable.

I will be building RPMs in ~/rpmbuild as my non-root user. If needed, create the RPM build directory structure, and modify ~/.rpmmacros to use the new directory structure instead of the default (e.g. /usr/src/redhat).

$ mkdir -p ~/rpmbuild/{BUILD,RPMS,S{OURCE,PEC,RPM}S}
$ echo -e "%_topdir\t$HOME/rpmbuild" >> ~/.rpmmacros

Because I am patching the pristine source, I extract the cfengine source twice in order to create the patch file for the RPM.

$ cp cfengine-2.1.21.tar.gz /tmp
$ cp cfengine-2.1.21.tar.gz ~/rpmbuild/SOURCES

$ cd /tmp
$ tar zxvf cfengine-2.1.21.tar.gz

$ mv cfengine-2.1.21 cfengine-2.1.21.orig
$ tar zxvf cfengine-2.1.21.tar.gz
$ cd cfengine-2.1.21/src

I make a modification to do.c.

$ awk 'NR != 2469 { print }; NR == 2469 { sub("cfinform","cfverbose") ; print }' do.c >
$ mv do.c

Since I've modified the pristine source, I create a patch. Patches should be placed in %{topdir}_SOURCES.

$ cd /tmp
$ diff -Naur cfengine-2.1.21.orig/ cfengine-2.1.21/ > ~/rpmbuild/SOURCES/cfengine-2.1.21.patch

I then create the .spec file.


Summary: A configuration engine
Name: cfengine
Version: 2.1.21
Release: 1
Copyright: GPL
Group: Applications/System
Patch0: %{name}-%{version}.patch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root

Cfengine, or the configuration engine is an autonomous agent and a middle to hi
gh level policy language and agent for building expert systems to administrate
and configure large computer networks. Cfengine is designed to be a part of a c
omputer immune system. It is ideal for cluster management and has been adopted
for use all over the world in small and huge organizations alike.

%setup -q
%patch -p1

./configure --with-docs



I have not yet included a %files stanza in the .spec file. RPM will return a list of files that are installed but unpackaged (i.e. files to include in the %files stanza) when I attempt to build the package.

$ rpmbuild -bi cfengine.spec

A list of installed but unpackaged files will be displayed. To obtain this list of files:
$ rpmbuild -bl cfengine.spec 2>&1 | egrep '^[[:space:]]+/' | cut -c4- | sort | uniq

I populated the %files stanza based on the list above.

%doc /usr/share/cfengine/*
%doc /usr/share/info/*
%doc /usr/share/man/man8/*

I then build the source and binary RPMs.

$ rpmbuild -ba cfengine.spec

More information:
RPM Tutorial
[] - The fight
Packaging software with RPM, Part 1
Packaging software with RPM, Part 2
Packaging software with RPM, Part 3
Better Living Through RPM, Part 2

Back to

Last modified: 2007/07/26