Tag Archives: patch

Creating and applying a patch.

Just because! So sometimes you may need to patch a kernel source file, or a script, or maybe even your life who knows. But whatever the reason, creating a patch file and applying it are extremely easy.

Let's take the config from this post as an example.

Copy the file /etc/grub.d/10_linux to /etc/grub.d/10_linux_patched.

Open /etc/grub.d/10_linux_patched in your favourite editor and make the required changes (line 204), then save.

Original:

initrd=
  for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
           "initrd-${version}" "initramfs-${version}.img" \
           "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
           "initrd-${alt_version}" "initramfs-${alt_version}.img" \
           "initramfs-genkernel-${version}" \
           "initramfs-genkernel-${alt_version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
 if test -e "${dirname}/${i}" ; then
           initrd="$i"
           break
 fi
 done

Patched:

 initrd=
 for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
            "initrd-${version}" "initramfs-${version}.img" \
            "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
            "initrd-${alt_version}" "initramfs-${alt_version}.img" \
            "initramfs-genkernel-${version}" \
            "initramfs-genkernel-${alt_version}" \
            "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
            "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
 if test -e "${dirname}/${i}" ; then
            initrd="early_ucode.cpio ${rel_dirname}/$i"
            break
 else
            initrd="early_ucode.cpio"
 fi
 done

If you just run a diff on the files, you see the changes.

# diff 10_linux_unpatched 10_linux_patched
204c204
<            initrd="$i"
---
>            initrd="early_ucode.cpio ${rel_dirname}/${i}"
205a206,207
>     else
>            initrd="early_ucode.cpio"

But this output we can't use to patch with. Now we rerun the diff command, but with a 'u' switch.

# diff -u 10_linux_unpatched 10_linux_patched
--- 10_linux_unpatched	2017-06-18 14:38:05.204929981 +0100
+++ 10_linux_patched	2017-06-18 14:38:26.540589618 +0100
@@ -201,8 +201,10 @@
 	   "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
 	   "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
     if test -e "${dirname}/${i}" ; then
-           initrd="$i"
+           initrd="early_ucode.cpio ${rel_dirname}/${i}"
            break
+    else
+           initrd="early_ucode.cpio"
     fi
   done

Hopefully the above output will make sense. Basically, '-' is old and '+' is new. So all we need to do is direct this output to a file.

diff -u 10_linux_unpatched 10_linux_patched > 10_linux.patch

The filenames that precede the '---' & '+++' are the files to be read '+++' and the file to be changed '---'. These 2 lines may also have a full or partial path to the files.

To apply the patch, just execute:
patch < 10_linux.patch

If you now diff the 2 files, they should match ;)

So if not applying in the root of the files, we need to inform patch to omit the preceding segments.

example:

If the first 2 lines of the patch have a relative path.

--- a/10_linux_unpatched 2017-06-18 14:38:05.204929981 +0100
+++ b/10_linux_patched 2017-06-18 14:38:26.540589618 +0100

We would use the 'p' switch.

patch -p1 < 10_linux.patch

This will ignore the 'a' & 'b' path segments.