Learn To Build Your Own RPM On CentOS/RHEL 6/7
Linux Linux Fun Shell Scripting Tutorials

Learn To Build Your Own RPM On CentOS/RHEL 6/7

Linux is a fascinating OS technology because everything in Linux is a file. You must have heard of RPM (RedHat Package Manager), it’s a package management system for RedHat based operating system like CentOS. In RHEL/CentOS, it is possible to create your installation rpm file. Let’s say we have a code which creates a database dump of a specific database.

Scenario:

Host OS: CentOS/RHEL 6/7
Host IP: 192.168.1.188
RAM: 4GB memory
Home Dir: /root/


Prerequisite:

You should have your own code/scripts pre-ready for which we are creating your own rpm.

Let’s start

Step 1: Get Essential Tools

To build your own RPM you require some of the tools fo the building process. So run the following command for setting build environment for RPM

Step 2: Directory Structure Environment

For building process, there is a directory structure. You may create this directory structure anywhere you like to do. For this example, I am creating this structure in my root’s directory.

Note: If above command ‘rpmdev-setuptree’ failed to execute or did not create your structure. Run the following command to get it done.

Now, When you will list your root’s directory you will see a directory with name rpm. Running tree command with rpm directory you can see whole directory structure.

Step 3: Write rpmMacro File

The RPM system defines a lot of handy macros so that your specification files can work regardless of where system directories are located. You simply have to use the macro, such as %_bindir, in place of hard-coded paths. The %_bindir macro, for example, identifies the default directory for binary executables, /usr/bin. You may use these macros wherever possible to avoid hard-coded paths and settings.
So for this example, you need a rpmmacro file in home directory file with the following contents. Read more about rpmmacro

Note: Change the values black bold text with according to your setup.

Sometimes there could be some pre-written text inside, so clear the file and copy below-written lines and fill these details

Now save the file using :wq!

Step 4: Put Files in SOURCES Directory

Now I have a script for MySQL dump with name “mysql.sh” inside dump-1 directory. we need to copy all our files and script directory inside /root/rpm/SOURCES directory, (All these things will get add in RPM) and run the following command

Now check the structure of your file

Now enter the SOURCE Directory and create a tarball or the dump-1 directory.



Step 5: Creating the Spec File

SPEC file direct the RPM in the build process. A spec file contains eight different sections, most of which are required. The SPEC file contents are known as the Preamble. The Preamble contains a wealth of information about the package being built, and the people that built it. You may read more about the preamble. So here’s dump’s preamble:

Clear all the content and Put the following content inside the file.

After writing (pasting :P) all the lines and editing save the changes with :wq!

Note: Change package name, script path, archive name, description etc., (Black Bold Area) According to you need

Step 6: Start Building RPM

We have completed all the requirement and we have completed most of the configuration. So we may proceed for the building our rpm by executing following command.

Step 7: Verify Your Work

After the successful build, in the output, you will find a line like Wrote: /root/rpm/RPMS/noarch/dump-1-0.noarch.rpm in means you have successfully build your own RPM.

Now you can check your work by installing your rpm using the command “rpm  -ivh dump-1-0.noarch.rpm “and check for the installed files as per your SPEC file.

Hope this tutorial was interesting and you find it helpful. Feel free to comment  and share

 

Video Coming Soon:



Kapendra
Love to write technical stuff with personal experience as I am working as a Sr. Linux Admin. and every day is a learning day and Trust me being tech geek is really cool.
http://kapendra.com

Leave a Reply