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
Red Hat Cluster Tutorial
Viewed 1973 times since Sun, Jun 3, 2018
RHEL: Bonding network interfaces
Viewed 3584 times since Sat, Jun 2, 2018
LVM: Move allocated PE between Physical Volumes
Viewed 3806 times since Sat, Jun 2, 2018
ZFS: Create a new zfs filesystem
Viewed 2503 times since Sun, Jun 3, 2018
YUM CRON RHEL7: Configure automatic updates.
Viewed 1900 times since Fri, Oct 26, 2018
Linux Kernel /etc/sysctl.conf Security Hardening
Viewed 23440 times since Fri, Aug 3, 2018
Do you Know These 5 Use of V$session View ?
Viewed 105044 times since Thu, Jun 21, 2018
LVM: Rename root VG/LV
Viewed 7487 times since Sat, Jun 2, 2018
RHEL: Services basic management - systemd
Viewed 18521 times since Sat, Jun 2, 2018
Super Grub2 Disk
Viewed 3337 times since Wed, May 22, 2019