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
ubuntu How to Reset Forgotten Root Password in Ubuntu
Viewed 2980 times since Tue, Dec 8, 2020
How to deal with dmesg timestamps
Viewed 3577 times since Wed, Oct 3, 2018
debian How to Upgrade Debian 8 Jessie to Debian 9 Stretch
Viewed 2401 times since Sun, Sep 23, 2018
LVM: Extend an existing Volume Group by adding a new disk
Viewed 2140 times since Sat, Jun 2, 2018
How to use yum command on CentOS/RHEL
Viewed 11098 times since Wed, Oct 17, 2018
RHEL7: Configure automatic updates.
Viewed 1945 times since Wed, Oct 17, 2018
Manage Linux Password Expiration and Aging Using chage
Viewed 4723 times since Tue, Sep 11, 2018
How To Set Up an SSL Tunnel Using Stunnel on Ubuntu
Viewed 3383 times since Fri, Sep 28, 2018
20 IPtables Examples For New SysAdmins
Viewed 2271 times since Fri, May 15, 2020
Linux - Cannot login from remote console but can access via ssh
Viewed 5304 times since Fri, Jun 8, 2018