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
LVM basic
Viewed 746 times since Sat, Jun 2, 2018
RHEL7 slow ssh login
Viewed 774 times since Tue, Aug 6, 2019
ZFS: Verify/change properties of a zfs filesystem
Viewed 634 times since Sun, Jun 3, 2018
Linux Cluster Tutorial
Viewed 454 times since Sat, Sep 29, 2018
Creating SWAP partition using FDISK & FALLOCATE commands
Viewed 270 times since Thu, Jan 16, 2020
stunnel Howto A Guide to create SSL access to a HTTP-only webserver with stunnel
Viewed 457 times since Fri, Sep 28, 2018
Linux - How to perform I/O performance test with dd command
Viewed 528 times since Fri, Jun 8, 2018
Inxi: Find System And Hardware Information On Linux
Viewed 823 times since Sat, Jun 2, 2018
linux-training.be gives you books for free to study Linux
Viewed 718 times since Sat, Jun 2, 2018
LVM: Reduce an existing Volume Group by removing one of its disks
Viewed 645 times since Sat, Jun 2, 2018