Friday, 27 January 2017

Creating and applying patch files in CentOS 7

Patch files are often are often included when compiling applications from source and often provide fixes for security and stability in cases where maintained versions in the repositories are not kept up to date / patched quickly.

For this tutorial we will start of by creating a simple patch file that will make the relevant changes for version of a simple python program:

vi /tmp/main.py

list=[1,2,3,4,5,6,7,8,9]

for i in list:
    print('The number is: ' + str(i))

vi /tmp/main_new.py

list=['this','is','a','test']

for i in list:
    print('The word is: ' + i)

Now create the diff file:

diff -u main.py main_new.py > main.patch

and inspect the contents with:

cat test.patch

--- main.py 2017-01-27 14:34:49.077788860 +0000
+++ main_new.py 2017-01-27 14:34:36.044762764 +0000
@@ -1,4 +1,5 @@
-list=[1,2,3,4,5,6,7,8,9]
+list=['this', 'is', 'a', 'test']
 
 for i in list:
-    print('The number is: ' + str(i))
+    print('The word is: ' + i)
+

Now we can patch the original file with the 'patch' command:

patch < main.patch

Although in the real world you are more likely to encounter much larger patches than single files - in some cases the patch might span several files over an entire source tree.

To demonstrate we will download two different versions of freeradius:

cd /usr/source

wget ftp://ftp.freeradius.org/pub/freeradius/freeradius-server-3.0.12.tar.bz2
wget https://ftp.yz.yamagata-u.ac.jp/pub/network/freeradius/old/freeradius-server-3.0.8.tar.bz2

tar xvf freeradius-server-3.0.12.tar.bz2 && tar xvf freeradius-server-3.0.8.tar.bz2

Create the patch file:

diff -Naur /tmp/freeradius-server-3.0.8 /tmp/freeradius-server-3.0.12 > freerad.patch

and then apply the patch with:

patch -p3 freerad.patch


0 comments:

Post a comment