Pages

Wednesday 10 October 2012

Building Own RPM in RHEL 6 / CentOS 6


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. This can seem like a large undertaking at first, but after a few times the process is pretty simple. To build an RPM, you must do the following:

First Login as regular user like student or any other user

Step 1. Create a directory hierarchy.
You have two options here to create directory hierarchy.

1.  you can download a rpm package (redhat-release) from 192.168.0.254 machine or from Internet and then install this to create directory hierarchy.

NOTE-: in my environment, I have a system which has the IP address of 192.168.0.254 and (redhat-release package is available on my /pub directory. 

[student@desktop14 ~]$ wget ftp://192.168.0.254/pub/redhat-release-6-6.0.0.24.el6.src.rpm

[student@desktop14 ~]$ rpm -ivh redhat-release-6-6.0.0.24.el6.src.rpm

Now you can use the ls command to display the rpmbuild directory which is created or not.

[student@desktop14 ~]$ ls
Desktop    Music     redhat-release-6-6.0.0.24.el6.src.rpm  Videos
Documents  Pictures  rpmbuild 
Downloads  Public    Templates

Note-: you will see here, rpmbuild directory has been created

2. If you don't want to use redhat-release rpm package you can create directory hierarchy manually, use the following steps.

[student@desktop14 ~]$mkdir rpmbuild

[student@desktop14 ~]$mkdir rpmbuild/SOURCES

[student@desktop14 ~]$mkdir rpmbuild/SPECS

Step 2. Create a directory with some sample files or scripts that you like to build in the package:

[student@desktop14 ~]$mkdir hello-0.1

[student@desktop14 ~]$cd hello-0.1/

[student@desktop14 hello-0.1]$vim hello1
#!/bin/bash
echo "Hello Friends, this is a demo package to test the rpm-building practical"

:wq (save and exit)

[student@desktop14 hello-0.1]$cd

[student@desktop14 ~]$

Now create an archive file based on your sample source:

[student@desktop14 ~]$tar -czvf hello-0.1.tar.gz hello-0.1/

Now copy this source to /home/student/rpmbuild/SOURCES/

[student@desktop14 ~]$cp hello-0.1.tar.gz /home/student/rpmbuild/SOURCES/

Step 3: One final step before package creation involves the creation of a spec file. The spec file is the set of instructions used to create the actual package itself. If you couldn't guess, this file must be located in the SPECS directory.

[student@desktop14 ~]$cd /home/student/rpmbuild/SPECS/
[student@desktop14 SPECS]$vim hello.spec
Name:           hello
Version:         0.1
Release:        1%{?dist}
Summary:     GOOD
#Group:
License:        GNUv2
# URL:
# Source0:
Source:         hello-0.1.tar.gz
BuildRoot:%(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)

# BuildRequires:
# Requires:

%description
THIS IS AWESOME

%prep
%setup -q


%build
echo " OK "

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/root/bin
cp hello1 $RPM_BUILD_ROOT/root/bin

%clean
rm -rf $RPM_BUILD_ROOT


%files
%defattr(-,root,root,-)
%attr(0777,root,root)/root/bin/hello1

%changelog
* Thu Jan 17 2011 test<test@example.com>-0.1
TYPE ANY COMMENT WHAT YOU LIKE

Step 4. Now you have to build the rpm package using this spec file. (One spec file is for one per RPM)

NOTE-: Before building the rpm, you have to check the required package is installed or not, the name of the required package is rpm-build. if it's not installed, you can use the following command to install.

[student@desktop14 ~]$su -
password:
[root@desktop14 ~]$rpm -qa rpm-build<tabkey>
                           or
[root@desktop14 ~]$yum install rpm-build

[root@desktop14 ~]#logout

[student@desktop14 ~]$

Now use the following command to build the rpm

[student@desktop14 ~]$rpmbuild -ba /home/student/rpmbuild/SPECS/hello.spec

Finally your RPM has been build. if you have any error, you have to fix that and run the above command again, now you can display your RPM package from this path (/home/student/rpmbuild/RPMS/x86_64/) using ls command.

[student@desktop14 ~]$ ls /home/student/rpmbuild/RPMS/x86_64/

hello-1-1.el6.x86_64.rpm  hello-debuginfo-1-1.el6.x86_64.rpm

NOTE-: it can be different output on your system.

You can see following directory, inside the /home/student/rpmbuild/ directory:

BUILD         - Contains scratch space used to compile software
RPMS           - Contains the binary RPM that is built
SOURCES    - Holds the source code for the RPM
SPECS           - Contains the spec file(s) (one per RPM)
SRPMS          - Contains the source RPM built during the process


Now you can install this RPM in the system using "rpm" command or you can use "yum"
Now I am going to install this RPM using "rpm" command. so first you have to login as root user

[student@desktop14 ~]$su -
password:
[root@desktop14 ~]#rpm -ivh /home/student/rpmbuild/RPMS/x86_64/hello-1-1.el6.x86_64.rpm

After installing the rpm you can use the commands

[root@desktop14 ~]#hello1

Hello Friends, this is a demo package to test the rpm-building practical

[root@desktop14 ~]#

if you want to install this package using "yum", you have to create repository first.

NOTE-: I am using HTTP protocol to share this repository to clients 

[root@desktop14 ~]#yum install httpd -y
[root@desktop14 ~]#mkdir /var/www/html/package

Now copy your rpm package from /home/student/rpmbuild/RPMS/x86_64/hello-1-1.el6.x86_64.rpm  to /var/www/html/package/ directory.


[root@desktop14 ~]#cd /home/student/rpmbuild/RPMS/x86_64/


[root@desktop14  x86_64]#cp hello-1-1.el6.x86_64.rpm /var/www/html/package/

Now create a repository file for yum server, in /etc/yum.repos.d/ directory for /var/www/html/package/

[root@desktop14 ~]#vim /etc/yum.repos.d/www.repo

[www]
name=yum server
baseurl=file:///var/www/html/package
enabled=1
gpgcheck=0

:wq (save and exit)

Now build the database for yum server

[root@desktop14 ~]#createrepo -v /var/www/html/package/

Now start the HTTPD service to access this repository via HTTPD protocol

#service httpd restart
#chkconfig httpd on

Now you can use the yum server to install this rpm package

[root@desktop14 ~]#yum install hello* -y

if you want to install this rpm package on client machine, you have to configure repository file on client to use this repository from yum server to client.

use the following step to configure yum client.

[root@client14 ~]#cd /etc/yum.repos.d/
[root@client14 ~]#vim client.repo
[www]
name=yum server
baseurl=http://<IP_ADDRESS_OF_HTTPD_SERVER>/package
enabled=1
gpgcheck=0

:wq (save and exit)

[root@client14 ~]#yum install hello* -y




RPM Building has been finished, So Enjoy..................................!

No comments:

Post a Comment