RHEL: How to rebuild and/or patch a RPM package

RHEL: How to rebuild and/or patch a RPM package

# Tested on RHEL 6 & 7

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

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

# As a non-root user, create a build tree based on a ~/rpmbuild/ directory:

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

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



# Given a simple package like the one we've built in
# https://sites.google.com/site/syscookbook/rhel/rhel-rpm-build


# Rebuilding the RPM from SRPM
# ------------------------------------------------------------------------------------------

# If you only want to recompile a source rpm, without doing any changes, do the following:

user@myhost:/home/user#> rpmbuild --rebuild </path_to_rpm>/HelloWorld-1.0-1.el6.src.rpm

# Your new package will be under /home/user/rpmbuild/RPMS/<arch>/




# Patching the SRPM
# ------------------------------------------------------------------------------------------

# First of all, install the source RPM

user@myhost:/home/user#> rpm -ivh </path_to_rpm>/HelloWorld-1.0-1.el6.src.rpm
   1:HelloWorld             ########################################### [100%]

# This will restore a specfile under /home/user/rpmbuild/SPECS, and a source tarball, as
# well as any other other patches or additional files, under /home/user/rpmbuild/SOURCES

user@myhost:/home/user#> ll rpmbuild/SPECS/
total 4
-rw-rw-r-- 1 user group 776 Sep 29 11:48 HelloWorld-1.0.spec

user@myhost:/home/user#> ll rpmbuild/SOURCES/
total 4
-rw-rw-r-- 1 user group 210 Sep 29 11:45 HelloWorld-1.0.tar.gz


# Extract the sources, make a copy of the source and make your changes

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

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

user@myhost:/home/user/rpmbuild/SOURCES#> cp -pr HelloWorld-1.0 HelloWorld-1.0.p

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

# After the .new source tree is modified, generate the patch. To generate the patch,
# run diff against the entire new and original source trees:


user@myhost:/home/user/rpmbuild/SOURCES#> diff -uNrp HelloWorld-1.0 HelloWorld-1.0.p > ../SOURCES/mynewpatch.patch



# Now, we have to add the patch to the specfile. There may be other patches already,
# and they are applied in order of their number in the specfile, so we'll have to
# number ours appropriately.

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

# Add a line after "Source 0" line like following one (respect appropriate patch number)

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

   Patch0:
mynewpatch.patch

# After that, add a patch command that corresponds with the patch line above, usually
# after "%setup" line:

[...]
   %prep
   %setup -q
   %patch0 -p1
[...]



# Rebuilding the package

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



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

user@myhost:/home/user/rpmbuild/RPMS/noarch#> ll
total 4
-rw-rw-r-- 1 user group 2092 Sep 29 12:52 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%]
        package HelloWorld-1.0-1.el6.noarch is already installed
        file /opt/HelloWorld/HelloWorld.sh from install of HelloWorld-1.0-1.el6.noarch conflicts with file from package HelloWorld-1.0-1.el6.noarch

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


root@myhost:/root#> /opt/HelloWorld/HelloWorld.sh
Hello NEW 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
RHEL: Multipathing basics
Viewed 8716 times since Sat, Jun 2, 2018
Lsyncd: live file syncronization across multiple Linux servers
Viewed 6793 times since Wed, Oct 31, 2018
debian Debian/Ubuntu Linux: Find If Installed APT Package Includes a Fix/Patch Via CVE Number
Viewed 9357 times since Sun, Sep 23, 2018
How to Migrate from RHEL 8 to CentOS 8
Viewed 2857 times since Fri, May 15, 2020
stunnel Securing telnet connections with stunnel
Viewed 1403 times since Sun, Dec 6, 2020
RHEL: Back-up/Replicate a partition table
Viewed 3288 times since Sun, May 27, 2018
Build a simple RPM that packages a single file
Viewed 8343 times since Sat, Jun 2, 2018
Do you Know These 5 Use of V$session View ?
Viewed 105044 times since Thu, Jun 21, 2018
How to convert RAW image to VDI and otherwise
Viewed 14843 times since Wed, Oct 3, 2018
3 Ways to Check Linux Kernel Version in Command Line
Viewed 11472 times since Fri, Apr 19, 2019