Pages

Wednesday, October 8, 2014

Build a RPM

Previously Linux programs used to come in source code with all files, configuration stuff so that the user has to build them to work. Now days they are coming in the form of packages which ship ready for installation. We just need to download the RPM and install that into the system using the package management tools like RPM and YUM. For more details see here.

In this article we will see how we can build a Sample RPM in Linux.

1. For building RPM we need the following tools rpmdevtools, rpm-build and make. In order to install them use
yum install rpmdevtools rpm-build make

2. Lets create our sample project. We will create a Sample Shell file which will be installed as a part of the RPM. For this

mkdir hello
touch hello/hello.sh

Create a tar for the directory
tar czvf SOURCES/hello.tar.gz hello/
hello/
hello/hello.sh

3. Create the RPM directory Structure using
rpmdev-setuptree

4. Once the command is executed we can see a rpmdbuild directory created in the home location. It looks as

[oracle@localhost ~]# ls -l rpmbuild/
total 20
drwxr-xr-x. 2 root root 4096 Oct  8 19:58 BUILD
drwxr-xr-x. 2 root root 4096 Oct  8 19:58 RPMS
drwxr-xr-x. 2 root root 4096 Oct  8 19:58 SOURCES
drwxr-xr-x. 2 root root 4096 Oct  8 19:58 SPECS
drwxr-xr-x. 2 root root 4096 Oct  8 19:58 SRPMS

5. Copy the hello.tar.gz to the ~/rpmbuild/SOURCES location
6. Now in order to create a RPM file we need a Spec file which describes the process to build, install the sources. For creating a Spec file use,
rpmdev-newspec  ~/rpmbuild/SPECS/hello.spec

7. Now we can see the hello.spec file in the SPECS directory.

The stages of the Spec File include
Stages:
● Preamble
● Setup
● Build
● Install
● Clean
● Files
● Changelog

Preamble: This is the initial section which defines package characteristics, name, version, group, license, Build, Install, Summery, description and release information
Setup: In the setup stage, the source tree are generated, unpacked and patches are applied. All Pre-build actions are performed here.
Build: the Build stage makes sure all the necessary binaries are created.
Install: In the install stage,  the buildroot is created , the file system is set  ,Puts built files in buildroot and also Cleans up unnecessary installed files
Clean: the buildroot set in the previous stage is removed at this point.
Changelog: the change log allows for auditing and also used to track package changes.

8. After making some necessary changes to the hello.spec file, the final version looks as

Name:           hello
Version:        1
Release:        1%{?dist}
Summary:        Hello Program

Group:      Utilities
License:    GPL   
Source:  %{name}.tar.gz     
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}

%description
Test Program For Build

%prep
%setup -n hello

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

%clean
rm -rf $RPM_BUILD_ROOT

%files
/opt/hello

9. Once the file changes are done, run the rpmbuild command as

[oracle@localhost SPECS]$ rpmbuild -bb -v hello.spec
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.7hgZ7L
+ umask 022
+ cd /home/oracle/rpmbuild/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ cd /home/oracle/rpmbuild/BUILD
+ rm -rf hello
+ /usr/bin/gzip -dc /home/oracle/rpmbuild/SOURCES/hello.tar.gz
+ /bin/tar -xvvf -
drwxr-xr-x oracle/oinstall   0 2014-10-08 21:24 hello/
-rw-r--r-- oracle/oinstall  39 2014-10-08 21:24 hello/hello.sh
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd hello
+ /bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.AL04Os
+ umask 022
+ cd /home/oracle/rpmbuild/BUILD
+ '[' /home/oracle/rpmbuild/BUILDROOT/hello-1-1.el6.x86_64 '!=' / ']'
+ rm -rf /home/oracle/rpmbuild/BUILDROOT/hello-1-1.el6.x86_64
++ dirname /home/oracle/rpmbuild/BUILDROOT/hello-1-1.el6.x86_64
+ mkdir -p /home/oracle/rpmbuild/BUILDROOT
+ mkdir /home/oracle/rpmbuild/BUILDROOT/hello-1-1.el6.x86_64
+ cd hello
+ LANG=C
+ export LANG
+ unset DISPLAY
+ mkdir -p /home/oracle/rpmbuild/BUILDROOT/hello-1-1.el6.x86_64/opt/hello
+ cp -R hello.sh /home/oracle/rpmbuild/BUILDROOT/hello-1-1.el6.x86_64/opt/hello
+ /usr/lib/rpm/check-rpaths /usr/lib/rpm/check-buildroot
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump
+ /usr/lib/rpm/brp-python-bytecompile
+ /usr/lib/rpm/redhat/brp-python-hardlink
+ /usr/lib/rpm/redhat/brp-java-repack-jars
Processing files: hello-1-1.el6.x86_64
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Checking for unpackaged file(s): /usr/lib/rpm/check-files /home/oracle/rpmbuild/BUILDROOT/hello-1-1.el6.x86_64
Wrote: /home/oracle/rpmbuild/RPMS/x86_64/hello-1-1.el6.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.5cV57Q
+ umask 022
+ cd /home/oracle/rpmbuild/BUILD
+ cd hello
+ rm -rf /home/oracle/rpmbuild/BUILDROOT/hello-1-1.el6.x86_64
+ exit 0

10. Now the build is executed with no issues. If we check the files in the RPMS location we can see

[oracle@localhost rpmbuild]$ cd RPMS/
[oracle@localhost RPMS]$ ll
drwxr-xr-x. 2 oracle oinstall 4096 Oct  8 21:37 x86_64
[oracle@localhost RPMS]$ cd x86_64/
[oracle@localhost x86_64]$ ll
-rw-r--r--. 1 oracle oinstall 2196 Oct  8 21:37 hello-1-1.el6.x86_64.rpm

11. Now in order to install the RPM created we can use
[root@localhost x86_64]# rpm -ivh hello-1-1.el6.x86_64.rpm
Preparing... ########################################### [100%]
   1:hello    ########################################### [100%]

12. In order to make sure the rpm is installed use,
[root@localhost x86_64]# rpm -q hello
hello-1-1.el6.x86_64


Happy learning, more to come

No comments :

Post a Comment