Wednesday, March 9, 2011

Creating a simple RPM binary package

Let take a simple example, you have a binary and a configuration file which you want to install on a Linux machine using a RPM. The binary file “ExampleBin” should be copy on /usr/bin and the configuration file “ExampleBin.conf” should be copy on /etc.
First of all, to be able to create a RPM you need to have installed the rpm-build tool.
Any RPM is created based on a “spec file”. This file basically explains to rpm-build tool how to create and configure the RPM pack.
So let create the “spec file” name “ExampleBin.spec”:

Summary: ExampleBin
Name: ExampleBin
Version: 0.1
Release: 1
Group: ExampleBin
License: ExampleBin
Source: %{expand:%%(pwd)}
BuildRoot: %{_topdir}/BUILD/%{name}-%{version}-%{release}


mkdir -p $RPM_BUILD_ROOT/usr/bin
mkdir -p $RPM_BUILD_ROOT/etc
cp %{SOURCEURL0}/iButtonServer ./usr/bin/
cp %{SOURCEURL0}/iButtonServer.cfg ./etc/

rm -r -f "$RPM_BUILD_ROOT"

%config(noreplace) %{_sysconfdir}/ ExampleBin.cfg
%{_bindir}/ ExampleBin

ExampleBin start

ExampleBin stop

On this example, the binary and configuration file are placed on the current directory (pwd).

In the %prep section, the build process is prepared. In our example the required files are copied to $RPM_BUILD_ROOT (build directory).

The %post section executes after the package has been installed. 
The %preun section executes before the user uninstall the package.

This script need _topdir environment variable. It should be set inside .rpmmacros file, placed on your home directory.
The next bash script set the _topdir enviroment variable and create the RPM calling using rpm-build tool:

echo '%_topdir '$(pwd) > ~/.rpmmacros
rpmbuild -bb ExampleBin.spec

Running this script placed on the same level like “spec file” the RPM should be generated.

No comments:

Post a Comment