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
How to run command or code in parallel in bash shell under Linux or Unix
Viewed 3054 times since Tue, Aug 6, 2019
RHEL: Reserved space on a ext2/ext3/ext4 filesystem
Viewed 4230 times since Sun, May 27, 2018
ZFS: Create a new zfs filesystem
Viewed 2451 times since Sun, Jun 3, 2018
Check a Website Availability from the Linux Command Line
Viewed 6346 times since Mon, Feb 18, 2019
Stunnel Setup
Viewed 17584 times since Fri, Sep 28, 2018
SSH ProxyCommand example: Going through one host to reach another server
Viewed 13406 times since Tue, Aug 6, 2019
How to automate SSH login with password? ssh autologin
Viewed 2566 times since Fri, Jun 8, 2018
Extending Linux LVM partitions script
Viewed 6263 times since Wed, Feb 6, 2019
SSH Essentials: Working with SSH Servers, Clients, and Keys
Viewed 4257 times since Wed, Jun 27, 2018
RHEL: How to change a USER/GROUP UID/GID and all owned files
Viewed 21050 times since Sat, Jun 2, 2018