create rpm from script

How to Create RPM for Script

RPM (Redhat Package Manager) is a package manager for CentOS/RHEL/Fedora/SUSE Linux systems. Most packages for these Linux systems are available as .rpm files. But sometimes you want create an RPM file from script. In this article, we will learn how to create RPM for script.

How to Create RPM for Script

Here are the steps to create RPM for Script.

1. Install Pre-requisite packages

You need certain packages on your system to be able to create RPM files. Open terminal and run the following command to install them.

$ yum install rpm-build rpmdevtools

2. Create Directory Structure

We also need to create a directory structure to store RPM files. First, go to your home directory

$ cd /home/user

Run the following command to create folder structure there.

$ rpmdev-setuptree

This will create a folder called rpmbuild in your present working directory.

You can verify the files, with the following command.

$ ls -l rpmbuild

drwxr-xr-x 2 root root 6 Oct 25 03:09 BUILD
drwxr-xr-x 2 root root 6 Oct 25 03:09 RPMS
drwxr-xr-x 2 root root 6 Oct 25 03:09 SOURCES
drwxr-xr-x 2 root root 6 Oct 25 03:09 SPECS
drwxr-xr-x 2 root root 6 Oct 25 03:09 SRPMS

If the above command does not create the folders as shown above, you can manually create them using mkdir command, as shown below.

$ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS,tmp}

3. Create rpmmacro file

Create an rpmmacro file with the configuration of your shell script.

$ sudo vi ~/.rpmmacros

Add the following lines to it. Replace YOUR_NAME with the desired name of your package.

%packager YOUR_NAME

%_topdir %(echo $HOME)/rpmbuild

%_smp_mflags %( \
    [ -z "$RPM_BUILD_NCPUS" ] \\\
        && RPM_BUILD_NCPUS="`/usr/bin/nproc 2>/dev/null || \\\
                             /usr/bin/getconf _NPROCESSORS_ONLN`"; \\\
    if [ "$RPM_BUILD_NCPUS" -gt 16 ]; then \\\
        echo "-j16"; \\\
    elif [ "$RPM_BUILD_NCPUS" -gt 3 ]; then \\\
        echo "-j$RPM_BUILD_NCPUS"; \\\
    else \\\
        echo "-j3"; \\\
    fi )

%__arch_install_post \
    [ "%{buildarch}" = "noarch" ] || QA_CHECK_RPATHS=1 ; \
    case "${QA_CHECK_RPATHS:-}" in [1yY]*) /usr/lib/rpm/check-rpaths ;; esac \

4. Copy Files to SOURCES Folder

Copy all your files and scripts to ~/rpmbuild/SOURCES folder.

$ cp -r /data/test_script /home/user/rpmbuild/SOURCES
$ cd ~/rpmbuild/SOURCES
$ ls -l test_script

-rw-r--r-- 1 root root 4693 Oct 25 03:54

Create tarball of your folder.

$ tar czf test_script.tar.gz test_script

5. Create SPEC File

Next, you need to create SPEC file ~/rpmbuild/SPECS/test_script.spec

$ vi ~/rpmbuild/SPECS/test_script.spec

Add the following lines to it. As you can see, the left side consists of parameters like Name, Version, Release, etc. You can customize them as per your requirement.

Name:           test_script
Version:        1
Release:        0
Summary:        Test script to RPM conversion

Group:          Admin
BuildArch:      noarch
License:        GPL

Write some description about your package here

%setup -q
install -m 0755 -d $RPM_BUILD_ROOT/etc/test_script

install -m 0755 $RPM_BUILD_ROOT/etc/test_script/



6. Build RPM

Run the following commands to build RPM packages.

$ cd ~/rpmbuild
$ rpmbuild -ba SPECS/test_script.spec

If things go properly, you will see a file ~/rpmbuild/RPMS/noarch/test_script-1-0.noarch.rpm created.

You can install the RPM package with the following command.

$ rpm -ivh test_script-1-0.noarch.rpm

On a related note, if you are looking to create RPM file from .deb file, then follow our steps here.

In this article, we have learnt how to create RPM file from script.

Also read:

Enable Email Alerts in Keepalived
How to Use NMAP in Kali Linux
How to Install VirtualBox in Ubuntu
How to Pass Parameter in MySQL Query
How to Combine Django Querysets

Leave a Reply

Your email address will not be published.