Friday 16 December 2011

Simple RPM creation

To create a rpm the following directory structure is needed,

SRPMS : This directory contains the source code of the application in archive format like "test.tar.gz "

BUILD : The source code is extracted and built here

RPMS   : The final rpm's are placed here

SPECS  : The rpm spec files are placed here, it is the source code to build a rpm I've created a simple rpm which just places a file inside a directory, so i don't use "SRPMS " directory as my file is placed directly inside the "BUILD" directory. Below is the spec file,

[madhu@localhost rpmbuild]$ cat SPECS/madhu-test.spec
%define _topdir /home/madhu/rpmbuild     #names starting with % signs are macros,they are like variables define it  and use it anywhere inside the spec file.

%define _tmppath /home/madhu/rpmbuild/tmp  #define the tmp path this is temporary location where the files will be installed during rpmbuild

%define packager madhusudhanan     #define the packager name with email to contact in-case of support

Name: madhu-test #name of the rpm

Version: 1.0 #version number

Release: 1 #release number,basically we need to find a way to increment it automatically for every build
Summary: test file installation #summary about the package
Source: Hello-world #name of the source file
BuildArch: noarch #mention the architecture for which it is built, noarch is architecture independent, if this variable is not defined the rpm will be created for the arch on which it is built
Group: Applications #this rpm belongs to applications group
License: GPL #define the license of the software
BuildRoot: %{_tmppath}/%{name}-buildroot #BuildRoot is the temporary directory used to compile and install the files
%description
Install the test file to a custom location
%install
rm -rf $RPM_BUILD_ROOT #RPM_BUILD_ROOT is a kind of alias to BuildRoot and i am cleaning the temporary directory
install -d $RPM_BUILD_ROOT/usr/local/madhu-test #this command creates the directory if it doesn't exist
install Hello-world $RPM_BUILD_ROOT/usr/local/madhu-test #copy the file "Hello-world" to the location "/home/madhu/rpmbuild/tmp/madhu-test"
%files #mention the files which needs to be included in the rpm in this section
/usr/local/madhu-test/Hello-world #the file along with the path is picked from the temporary directory

[madhu@localhost rpmbuild]$ rpmbuild -bb SPECS/madhu-test.spec #-bb option is to build binary package

[madhu@localhost rpmbuild SPECS]$ ls ../RPMS/noarch/

madhu-test-1.0-1.noarch.rpm

One more thing i was looking for is to use environment variable in the spec file, and below does satisfy the need.

In the spec file, mention the below line

%define workspace  #workspace is the macro for which i will pass value in the command line

run the below command for building the rpm

export WORKSPACE =  /home/madhu/rpmbuild

rpmbuild --define="workspace $WORKSPACE" -bb madhu-test.spec

$WORKSPACE is the environment variable.

This way I've generic spec file and the values can be passed in the command line rather having multiple spec file for the same rpm. One use case is to have multiple Jenkins jobs for building trunk, branches.

For detailed reference see RPM-GUIDE

No comments:

Post a Comment