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!


Article Number: 134
Posted: Sat, Jun 2, 2018 9:00 AM
Last Updated: Sat, Jun 2, 2018 9:00 AM

Online URL: http://kb.ictbanking.net/article.php?id=134