Package Bash/Perl script in Rpm Format

Package Bash/Perl script in Rpm Format:

Recently  I was working for one of the Script development. The automation package supposed to delivered on rhel6.
In process of roll out, I thought of using yum, It would be easy to install.
In this blog we will be creating a sample script and package it RPM format.

#!/bin/bash
date
ls

Note: Save the script named as test.sh
Below are the steps I followed.

  • Install rpmbuild package, so we can get rpm build command.
     yum install rpmbuild
  • Create a directory structure, We need a directory structure for rpm buildmkdir /root/rpmbuild
    mkdir -p /root/rpmbuild/{RPMS,SRPMS,SPEC,SOURCE,BUILD,tmp}
  • Create a folder in SOURCEcd /root/rpmbuild/SOURCE
    mkdir test ; cp /root/test.sh /root/rpmbuild/SOURCE/test/
  • Create a tar folder :tar -czvf  test.tar.gz test
  • create a SPEC file in SPEC folder
    test.spec [Content of spec file]
    Name:test-1
    Version:1
    Release:1
    Summary: This is rpm development
    BuildArch:noarch
    Group:Dev
    License:GPL
    URL:http://localhost
    Source0:test.tar.gz
    Buildroot:%{_tmppath}/%{Name}-%{Version}-%{Release}-root-%{%{__id_u} -n}
    %description
    This is rpm development
    %prep
    %setup -q
    %install
    mkdir -p “$RPM_BUILD_ROOT/opt/test”
    cp -R * “$RPM_BUILD_ROOT/opt/test”
    %files
    /opt/test
    %clean
    rm -rf $RPM_BUILD_ROOt
    %post
    chmod 775 /opt/test
    *Save the spec file in /root/rpmbuild/SPEC folder
  • Now we will build the rpm
    rpmbuild -v -bb /root/rpmbuild/SPEC/test.spec
    /* rpm will build and saved in /root/rpmbuild/RPMS/noarch folder
  • Now we can install rpm using command
    rpm -ivh test.rpm
    %files in spec file specified installation location.After that we can check the test.sh file  /opt/test folder