Build a simple RPM that packages a single file

Build a simple RPM that packages a single file

# Tested on RHEL 6 & 7

# The 'rpmbuild' command is used to build a binary RPM from source code, as configured
# with a .spec file.

# Install the necessary packages: rpm-build and rpmdevtools:

yum install rpm-build rpmdevtools


# If possible,  avoid creating packages as 'root' as this is quite dangerous because we
# could delete or overwrite OS existing files.

# Set up the folder structure. Placed on the HOME folder of a user without admin privileges,
# run following command:

user@myhost:/home/user#> rpmdev-setuptree

# This will create rpmbuild structure where we will work on building packages:

   rpmbuild/SRPMS
   rpmbuild/SPECS
   rpmbuild/SOURCES
   rpmbuild/RPMS
   rpmbuild/BUILD



# This can be alternatively done by running these commands:

user@myhost:/home/user#> mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

user@myhost:/home/user#> echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros



# Under rpmbuild/SOURCES create a folder with source file(s). Do not forget to provide
# a revision number. For instance, our source file will be a script named 'HelloWorld.sh'

user@myhost:/home/user/rpmbuild/SOURCES#> cd ~/rpmbuild/SOURCES

user@myhost:/home/user/rpmbuild/SOURCES#> mkdir HelloWorld-1.0

user@myhost:/home/user/rpmbuild/SOURCES#> vi HelloWorld-1.0/HelloWorld.sh
   echo "Hello world!"

user@myhost:/home/user/rpmbuild/SOURCES#> chmod 755 HelloWorld-1.0/HelloWorld.sh

user@myhost:/home/user/rpmbuild/SOURCES#> touch HelloWorld-1.0/configure

user@myhost:/home/user/rpmbuild/SOURCES#> chmod 755 HelloWorld-1.0/configure

user@myhost:/home/user/rpmbuild/SOURCES#> tar czvf HelloWorld-1.0.tar.gz HelloWorld-1.0



# Create the spec file: This is the hard part - configuring the RPM on what to build,
# install, and configure. A sample spec file can be created by running:

user@myhost:/home/user/rpmbuild/SOURCES#> cd

user@myhost:/home/user#> rpmdev-newspec rpmbuild/SPECS/HelloWorld-1.0.spec
Skeleton specfile (minimal) has been created to "rpmbuild/SPECS/HelloWorld-1.0.spec".

# But there is still a lot to add and remove to make this work. This is a spec file
# created using the sample file.

# Edit spec file and add/modify necessary parameters (my setup in bold letters)

user@myhost:/home/user#> vi ~/rpmbuild/SPECS/HelloWorld-1.0.spec

Name: HelloWorld

Version: 1.0                       <--- Version must be the same as the name of the
                                         folder containing the package file(s)

Release: 1%{?dist}                 <--- Release number, added to package's name

Summary: Hello World Script

Group: Miscellaneous               <--- For general purposes use 'Miscellaneous'

License: License text

# URL:

Source0: HelloWorld-1.0.tar.gz     <--- Pay attention to the name of tar file

BuildArch: noarch                  <--- If nothing specified, current architecture
                                           is used [ i386 | x86_64 | noarch ]

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

# BuildRequires:                   <--- Required packages to build this one

# Requires:                        <--- Required packages for installing this one

%description
This is a text describing what the Package is meant for

%prep

%setup -q

%build
# %configure                       <--- We have nothing to configure or compile
# make %{?_smp_mflags}                   so we comment these two lines out

%install

rm -rf $RPM_BUILD_ROOT

# make install DESTDIR=$RPM_BUILD_ROOT    <--- We have nothing to compile

install -d -m 0755 $RPM_BUILD_ROOT/opt/HelloWorld
install -m 0755 HelloWorld.sh $RPM_BUILD_ROOT/opt/HelloWorld/HelloWorld.sh

^ These two directives indicate where folders and files will be
    created/installed and their effective rights

%clean

rm -rf $RPM_BUILD_ROOT

%files

%defattr(-,root,root,-)

# %doc

/opt/HelloWorld/HelloWorld.sh       <--- We confirm the file(s) to install

%changelog




# Once the spec file is complete, build the package with:

user@myhost:/home/user#> rpmbuild -ba rpmbuild/SPECS/HelloWorld-1.0.spec

# Assuming no errors occurred, new package is under the RPMS folder ready to be installed.


user@myhost:/home/user#> cd ~/rpmbuild/RPMS/noarch

user@myhost:/home/user/rpmbuild/RPMS/noarch#> ll
total 4
-rw-rw-r-- 1 user user 2104 Sep 26 16:53 HelloWorld-1.0-1.el6.noarch.rpm

user@myhost:/home/user/rpmbuild/RPMS/noarch#> rpm -qpl HelloWorld-1.0-1.el6.noarch.rpm
/opt/HelloWorld/HelloWorld.sh


# Let's install our new package (as 'root')

root@myhost:/root#> rpm -ihv /home/user/rpmbuild/RPMS/noarch/HelloWorld-1.0-1.el6.noarch.rpm
Preparing...                ########################################### [100%]
   1:HelloWorld             ########################################### [100%]


root@myhost:/root#> rpm -qa HelloWorld
HelloWorld-1.0-1.el6.noarch


root@myhost:/root#> /opt/HelloWorld/HelloWorld.sh
Hello world!
0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
RHEL7 slow ssh login
Viewed 4461 times since Tue, Aug 6, 2019
PROCESSOR AND MEMORY INFORMATION
Viewed 5658 times since Sat, Jun 2, 2018
FIO (Flexible I/O) – a benchmark tool for any operating system
Viewed 19542 times since Wed, Jul 25, 2018
How to sort IP addresses in Linux
Viewed 3681 times since Sun, May 20, 2018
debian How to Upgrade Debian 8 Jessie to Debian 9 Stretch
Viewed 2401 times since Sun, Sep 23, 2018
10 nmap Commands Every Sysadmin Should Know
Viewed 10042 times since Wed, May 22, 2019
30 Handy Bash Shell Aliases For Linux / Unix / MacOS
Viewed 4907 times since Thu, Feb 11, 2021
CONFIGURE OCFS2
Viewed 8163 times since Sat, Jun 2, 2018
Secure Secure Shell
Viewed 10964 times since Fri, Aug 21, 2020
Linux Linux Network Statistics Tools / Commands
Viewed 9259 times since Mon, Sep 21, 2020