Skip to main content
CentOS

Building RPM Files on CentOS 6.5

By May 15, 2014October 17th, 2016No Comments

Building RPM FilesBuilding RPM Files

For this demonstration of building RPM files, we are going to use the simple scenario of wanting to distribute a YUM repository file easily to many machines using an RPM file. In this way, the RPM is installing just a single text file to /etc/yum.repos.d/ but the example would hold true for any plain text file distribution. The demonstration makes use of a CentOS 6.5 64 bit system. We use RPMs all the time on Red Hat, SUSE and CentOS systems without having to give it a lot of thought. Running through the RPM creation procedure allows you to consider using them in solving your own problems.

Install Required RPMs

yum install -y rpm-build rpmdevtools

Create RPM Build User

We do not want to build RPMs as the root account and maybe not even our personal account. It is common to have some form of rpm build account, maybe even, innovatively called rpmbuild. Logging as that user to build RPM files.

useradd -m build
passwd build

Build the RPM

We now need to login as the build account, in our case.

From the home directory of the build user run the following commands to create the directory structure required.

rpmdev-setuptree

This will create the directory rpmbuild with several sub-directories.

Populate the SOURCES Directory

We now change to the SOURCES directory to create a compressed archive with the rpm content

cd ~/rpmbuild/SOURCES
mkdir -p tuprepo-1/etc/yum.repos.d
cp /tmp/CentOS-Tup.repo !$

Within the SOURCES directory, we create a directory representing the RPM and the required directory structure starting with the rpm name and version and then the target filesystem, in our case /etc/yum.repos.d. I then copy my desired repo file into the rpms structure. I can use !$ to represent the last argument, the directory just created in this case

We then need to gzip this all up:

cd ~/rpmbuild/SOURCES
tar -zcvf tuprepo-1.tar.gz tuprepo-1/

The SPEC File

This is the instructions for the build process and is created in the rpmbuild/SPECS directory. You can use rpmdev-newspec tuprepo.spec to create a sample file in your current directory. that should be SPECS.

Then edit as required.

Name: tuprepo
Version: 1 
Release: 0
Summary: TUP Local Network CentOS Repo 
Group: System Environment/Base
License: GPL
URL: https://theurbanpenguin.com 
Source0: tuprepo-1.tar.gz
BuildArch: noarch
BuildRoot: %{_tmppath}/%{name}-buildroot 
%description
Creates YUM repository pointing local network CentOS repository

%prep
%setup -q

%install
mkdir -p "$RPM_BUILD_ROOT"
cp -R * "$RPM_BUILD_ROOT"

%clean
rm -rf "$RPM_BUILD_ROOT"

%post
echo ..
echo "Adding repo"
%files
%defattr(-,root,root,-)
/etc/yum.repos.d/CentOS-Tup.repo

 

RPMBUILD

We can now use the rpmbuild process to create the rpm -v for verbose and -bb for binary rpm without source rpm.

cd 
rpmbuild -v -bb rpmbuild/SPECS/tuprepo.spec

The RPM will be built in the rpmbuild/RPMS directory The final file structure should look like the following screenshot created with the tree command.final-tree