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
Sample logrotate configuration and troubleshooting part 2
Viewed 9421 times since Fri, Nov 30, 2018
ZFS: Create a new zfs filesystem
Viewed 2526 times since Sun, Jun 3, 2018
LUKS List available methods of encryption for LUKS
Viewed 2884 times since Fri, Jul 13, 2018
Increase A VMware Disk Size (VMDK) Formatted As Linux LVM without rebooting
Viewed 15314 times since Wed, May 30, 2018
LVM: Extend SWAP size by growing existing Logical Volume
Viewed 2418 times since Sat, Jun 2, 2018
Split and Reassemble files
Viewed 3435 times since Mon, May 28, 2018
How do I add ethtool settings to a network device permanently?
Viewed 6522 times since Mon, May 21, 2018
Linux get the list of FC HBA’s and WWPN
Viewed 3188 times since Tue, May 22, 2018
A tcpdump Tutorial and Primer with Examples
Viewed 5065 times since Sun, Jun 17, 2018
Linux - How to perform I/O performance test with dd command
Viewed 5974 times since Fri, Jun 8, 2018