RPM Howto

It takes a bit of time how to learn to build an RPM, but it is straightforward.  The easiest way to get started is by going through an example.  You should read the RPM HOWTO and rpm.org for a more in-depth view of RPM.  In our example, we go through how to add a PHP page to ClarkConnect's web-based administration tool.

Setup the RPM Environment

We suggest you create a regular user account -- avoid using "root" whenever possible.  First, you need to setup your own RPM playground.  RPM requires the following directory structure:

- BUILD
- RPMS
    - athlon
    - i386
    - i486
    - i586
    - i686
    - noarch
- SOURCES
- SPECS
- SRPMS

You also need to define the path for the RPM playground in your .rpmmacros file in your home directory.  You can create this directory structure and a basic .rpmmacros file with the following commands.

Creating the RPM directory structure

mkdir -p --verbose ~/rpms/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
mkdir -p --verbose ~/rpms/RPMS/{athlon,i386,i486,i586,i686,noarch}
echo "%_topdir ~/rpms" > ~/.rpmmacros

Create a "spec" File

A spec file is used to describe the RPM.  In fact, for most software packages, you can take the existing source (in tar.gz format), create a spec file, and voila... an RPM.  Our example spec file contains the bandwidth manager module for the ClarkConnect.  You can download a copy of the spec file here.

You can read about the layout of the spec file in RPM HOWTO - Spec File..  Here are some things to note about ClarkConnect spec files:

  • Make sure Requires is correct -- otherwise the apt-get system won't find the dependencies.
  • Make sure you create any required directories in the %install section... a common gotcha!
  • The %file manifest section must be correct: no unpackaged files are allowed.
  • For install scripts, keep this in mind: when the first version of a package is installed, its %pre and %post scripts will be passed an argument equal to 1.
  • For uninstall scripts, keep this in mind: when the last version of a package is erased, its %preun and %postun scripts will be passed an argument equal to 0.
Example spec file - cc-bandwidth.spec

#------------------------------------------------------------------------------
# P A C K A G E  I N F O
#------------------------------------------------------------------------------

Name: cc-bandwidth
Version: 2.2
Release: 40
Group: System Environment/Base
Packager: Point Clark Networks
Summary: Bandwidth manager and limiter module
Vendor: Point Clark Networks
Source: %{name}-%{version}.tar.gz
License: GPL
Requires: iproute, coreutils, cc-webconfig >= 2.2, cc-setup >= 2.2
Requires(post,preun): chkconfig
Conflicts: shapecfg
BuildRoot: /tmp/%{name}-build

%description
Bandwidth manager and limiter module


#------------------------------------------------------------------------------
# B U I L D
#------------------------------------------------------------------------------

%prep
%setup
%build


#------------------------------------------------------------------------------
# I N S T A L L  F I L E S
#------------------------------------------------------------------------------

%install
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT

mkdir -p -m 755 $RPM_BUILD_ROOT/var/webconfig
mkdir -p -m 755 $RPM_BUILD_ROOT/etc/sysconfig/cbq
mkdir -p -m 755 $RPM_BUILD_ROOT/etc/rc.d/init.d
mkdir -p -m 755 $RPM_BUILD_ROOT/sbin

install -m 755 cbq $RPM_BUILD_ROOT/sbin/
install -m 755 bandwidth $RPM_BUILD_ROOT/etc/rc.d/init.d/
cp -r webconfig/* $RPM_BUILD_ROOT/var/webconfig/


#------------------------------------------------------------------------------
# I N S T A L L  S C R I P T
#------------------------------------------------------------------------------

%post

if [ "$1" = "1" ]; then
	chkconfig --add bandwidth
fi


#------------------------------------------------------------------------------
# U N I N S T A L L  S C R I P T
#------------------------------------------------------------------------------

%preun

if [ "$1" = "0" ]; then
	service bandwidth stop >/dev/null 2>&1
	/sbin/chkconfig --del bandwidth
fi


#------------------------------------------------------------------------------
# C L E A N  U P
#------------------------------------------------------------------------------

%clean
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT


#------------------------------------------------------------------------------
# F I L E S
#------------------------------------------------------------------------------

%files
%defattr(-,root,root)
%attr(-,webconfig,webconfig) /var/webconfig/
/sbin/cbq
/etc/sysconfig/cbq
/etc/rc.d/init.d/bandwidth