{"id":1756,"date":"2017-06-18T15:05:36","date_gmt":"2017-06-18T14:05:36","guid":{"rendered":"http:\/\/cdblog.cdstealer.com\/?p=1756"},"modified":"2017-09-02T16:13:07","modified_gmt":"2017-09-02T15:13:07","slug":"creating-and-applying-a-patch","status":"publish","type":"post","link":"https:\/\/cdblog.cdstealer.com\/?p=1756","title":{"rendered":"Creating and applying a patch."},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2011\/06\/gnometerminal.png\" alt=\"\" width=\"256\" height=\"256\" class=\"alignnone size-full wp-image-425\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2011\/06\/gnometerminal.png 256w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2011\/06\/gnometerminal-150x150.png 150w\" sizes=\"auto, (max-width: 256px) 100vw, 256px\" \/><\/p>\n<p>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.<\/p>\n<p>Let's take the config from <a href=\"http:\/\/cdblog.cdstealer.com\/?p=1736\" target=\"_blank\">this<\/a> post as an example.<\/p>\n<p>Copy the file <strong>\/etc\/grub.d\/10_linux<\/strong> to <strong>\/etc\/grub.d\/10_linux_patched<\/strong>.<\/p>\n<p>Open <strong>\/etc\/grub.d\/10_linux_patched<\/strong> in your favourite editor and make the required changes (line 204), then save.<\/p>\n<p>Original:<\/p>\n<pre>initrd=\r\n  for i in \"initrd.img-${version}\" \"initrd-${version}.img\" \"initrd-${version}.gz\" \\\r\n           \"initrd-${version}\" \"initramfs-${version}.img\" \\\r\n           \"initrd.img-${alt_version}\" \"initrd-${alt_version}.img\" \\\r\n           \"initrd-${alt_version}\" \"initramfs-${alt_version}.img\" \\\r\n           \"initramfs-genkernel-${version}\" \\\r\n           \"initramfs-genkernel-${alt_version}\" \\\r\n           \"initramfs-genkernel-${GENKERNEL_ARCH}-${version}\" \\\r\n           \"initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}\"; do\r\n if test -e \"${dirname}\/${i}\" ; then\r\n           initrd=\"$i\"\r\n           break\r\n fi\r\n done<\/pre>\n<p>Patched:<\/p>\n<pre> initrd=\r\n for i in \"initrd.img-${version}\" \"initrd-${version}.img\" \"initrd-${version}.gz\" \\\r\n            \"initrd-${version}\" \"initramfs-${version}.img\" \\\r\n            \"initrd.img-${alt_version}\" \"initrd-${alt_version}.img\" \\\r\n            \"initrd-${alt_version}\" \"initramfs-${alt_version}.img\" \\\r\n            \"initramfs-genkernel-${version}\" \\\r\n            \"initramfs-genkernel-${alt_version}\" \\\r\n            \"initramfs-genkernel-${GENKERNEL_ARCH}-${version}\" \\\r\n            \"initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}\"; do\r\n if test -e \"${dirname}\/${i}\" ; then\r\n            initrd=\"early_ucode.cpio ${rel_dirname}\/$i\"\r\n            break\r\n else\r\n            initrd=\"early_ucode.cpio\"\r\n fi\r\n done<\/pre>\n<p>If you just run a diff on the files, you see the changes.<\/p>\n<pre># diff 10_linux_unpatched 10_linux_patched\r\n204c204\r\n<            initrd=\"$i\"\r\n---\r\n>            initrd=\"early_ucode.cpio ${rel_dirname}\/${i}\"\r\n205a206,207\r\n>     else\r\n>            initrd=\"early_ucode.cpio\"<\/pre>\n<p>But this output we can't use to patch with.  Now we rerun the diff command, but with a 'u' switch.<\/p>\n<pre># diff -u 10_linux_unpatched 10_linux_patched\r\n--- 10_linux_unpatched\t2017-06-18 14:38:05.204929981 +0100\r\n+++ 10_linux_patched\t2017-06-18 14:38:26.540589618 +0100\r\n@@ -201,8 +201,10 @@\r\n \t   \"initramfs-genkernel-${GENKERNEL_ARCH}-${version}\" \\\r\n \t   \"initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}\"; do\r\n     if test -e \"${dirname}\/${i}\" ; then\r\n-           initrd=\"$i\"\r\n+           initrd=\"early_ucode.cpio ${rel_dirname}\/${i}\"\r\n            break\r\n+    else\r\n+           initrd=\"early_ucode.cpio\"\r\n     fi\r\n   done<\/pre>\n<p>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.<\/p>\n<p><code>diff -u 10_linux_unpatched 10_linux_patched > 10_linux.patch<\/code><\/p>\n<p>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.  <\/p>\n<p>To apply the patch, just execute:<br \/>\n<code>patch < 10_linux.patch<\/code><\/p>\n<p>If you now diff the 2 files, they should match ;)<\/p>\n<p>So if not applying in the root of the files, we need to inform patch to omit the preceding segments.<\/p>\n<p>example:<\/p>\n<p>If the first 2 lines of the patch have a relative path.<\/p>\n<p><code>--- a\/10_linux_unpatched\t2017-06-18 14:38:05.204929981 +0100<br \/>\n+++ b\/10_linux_patched\t2017-06-18 14:38:26.540589618 +0100<\/code><\/p>\n<p>We would use the 'p' switch.<\/p>\n<p><code>patch -p1 < 10_linux.patch<\/code><\/p>\n<p>This will ignore the 'a' & 'b' path segments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/cdblog.cdstealer.com\/?p=1756\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Creating and applying a patch.<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[86,18,299],"class_list":["post-1756","post","type-post","status-publish","format-standard","hentry","category-gentoo","tag-bash","tag-linux","tag-patch"],"_links":{"self":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1756"}],"version-history":[{"count":4,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1756\/revisions"}],"predecessor-version":[{"id":1799,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1756\/revisions\/1799"}],"wp:attachment":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}