Wednesday, July 13, 2011

How To Create Your Own RPM Package

A key skill for a system administrator is being able to deploy your own custom software. However, you first need to build an RPM package that contains your custom software. To build an RPM, you must do the following:

Step 1. Create a directory hierarchy

BUILD         Contains scratch space used to compile software
RPMS Hold the source code for the RPM
SOURCES Contains the spec file(s) 
SRPMS         Contains the source RPM built during the process

Step 2. Copy or create you source code into the directory hierarchy
Step 3. Create a specific file
Step 4. Build the RPM

Aside from the directories we have covered, you also need to install a few packages.

Now, Let’s start to create an RPM

Step 1. Verify the packages are installed correctly:

# yum install -y rpm-build

Step 2. Create the required directories:


This is a personal preference to build packages under the /usr/src directory. The tmp directory is used as temporary build directory as well. 

Step 3. Create a directory with some sample files that you’d like in the package:

# mkdir /usr/src/redhat/mysample
# cd /usr/src/redhat/mysample
# touch first_file second_file keys config_file

Step 4. Create an archive file based on your sample source:

# cd /usr/src/redhat
# tar cf mysample.tar.gz mysample
# mv mysample.tar.gz SOURCES/

Step 5. Create spec file for  instructions (sample.spec)

Summary: This package is a sample. 
Name: mysample 
Version: 1.0 
Release: 0 
License: GPL 
Packager: Han Thuy 
Group: Development/Tools 
Source: %{name}.tar.gz 
BuildRoot: /usr/src/redhat/tmp/%{name}-%{version} 

%description 
This package is just a sample. 

%prep 
%setup -n mysample 

%install 
mkdir -p "$RPM_BUILD_ROOT/opt/sample_pkge" 
cp -R * "$RPM_BUILD_ROOT/opt/sample_pkge" 

%files 
/opt/sample_pkge 

%clean 
rm -rf "$RPM_BUILD_ROOT" 

%post 
chown root:root -R /opt/sample_pkge 
chmod 775 -R /opt/sample_pkge

Step 6. Build the package with the rpmbuild command:

# rpmbuild -v -bb /usr/src/redhat/SPECS/sample.spec 

Step 7. View the new RPM package:


Step 8: Install and verify the new package to ensure that is works properly:


Have fun!