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
Open SSL Creating Certificate Signing Request — CSR Generation
Viewed 2021 times since Mon, Feb 18, 2019
How to find the largest files and directories in Linux?
Viewed 3549 times since Sun, May 20, 2018
Odpalenie polecenia tylko na jedną godzinę
Viewed 2962 times since Thu, May 24, 2018
rabbitmq Troubleshooting TLS-enabled Connections
Viewed 2635 times since Sun, Dec 6, 2020
RHEL: ACLs basics
Viewed 6401 times since Sun, May 27, 2018
SPRAWDZONA KONFIGURACJA RSYSLOG I LOGROTATE, JAKO ZEWNĘTRZNEGO SERWERA SYSLOG
Viewed 3965 times since Fri, Nov 30, 2018
Telnet – Send GET/HEAD HTTP Request
Viewed 3515 times since Mon, Feb 18, 2019
How to sort IP addresses in Linux
Viewed 3828 times since Sun, May 20, 2018
LVM: Extend SWAP size by growing existing Logical Volume
Viewed 2600 times since Sat, Jun 2, 2018
“Too many authentication failures” with SSH
Viewed 6200 times since Mon, May 21, 2018