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
20 Practical Examples of RPM Commands in Linux rpm
Viewed 7567 times since Mon, Feb 18, 2019
How to remove CTRL-M (^M) characters from a file in Linux
Viewed 2079 times since Thu, Feb 7, 2019
logrotate Understanding logrotate utility
Viewed 1577 times since Sun, Jan 12, 2020
Understanding System auditing with auditd
Viewed 8990 times since Fri, Apr 5, 2019
linux-training.be gives you books for free to study Linux
Viewed 4594 times since Sat, Jun 2, 2018
LVM: Reduce an existing Volume Group by removing one of its disks
Viewed 2276 times since Sat, Jun 2, 2018
logrotate - rotates, compresses, and mails system logs.
Viewed 1503 times since Fri, Nov 30, 2018
RHEL: Crash kernel dumps configuration and analysis on RHEL 6
Viewed 4315 times since Sat, Jun 2, 2018
How to stop and disable auditd on RHEL 7
Viewed 38380 times since Tue, Aug 6, 2019
Linux - How to shutdown or reboot
Viewed 1869 times since Fri, Jun 8, 2018