Wednesday, September 10, 2014

Creating and Applying a Patch File


Recently, I went over the patch file creation & application process in linux. In this post, I will cover a few linux commands useful for creating a patch file & applying a patch file.


Creating a Patch File

Sometimes, we need to create a patch file & distribute it to customers, partners, etc.

To create a patch, we need a copy of original source code directory & the modified source code directory. Lets say we are starting to modify the sources at /home/babu/patch_tests//valgrind_tests. Before any modifications, its better to take a copy to /home/babu/patch_tests//valgrind_tests_new & then modify the sources in /home/babu/patch_tests//valgrind_tests_new.

Now, to create the patch file, execute this command:
diff –Naur    <old_dir>  <new_dir>      >   file.patch
Example: 

babu@babu-VirtualBox:~/patch_tests$ diff -Naur valgrind_tests valgrind_tests_new > vg.patch
babu@babu-VirtualBox:~/patch_tests$ cat vg.patch
diff -Naur valgrind_tests/vg_test.c valgrind_tests_new/vg_test.c
--- valgrind_tests/vg_test.c        2014-09-10 21:26:39.473561882 -0700
+++ valgrind_tests_new/vg_test.c            2014-09-10 21:29:41.966273636 -0700
@@ -4,6 +4,12 @@

 int global_array[100] = {-1};
 +/* Testing patch functionality */
+void patch_test()
+{
+  return 1;
+}
+
  void
 unintialized_use()
 {
babu@babu-VirtualBox:~/patch_tests$
Now, remove the first line in the patch - which displays the command. After the removal the file should be like this:
babu@babu-VirtualBox:~/patch_tests$ cat vg.patch
valgrind_tests/vg_test.c        2014-09-10 21:26:39.473561882 -0700
+++
valgrind_tests_new/vg_test.c            2014-09-10 21:29:41.966273636 -0700
@@ -4,6 +4,12 @@

 int global_array[100] = {-1};
 +/* Testing patch functionality */
+void patch_test()
+{
+  return 1;
+}
+

  void
 unintialized_use()
 {
babu@babu-VirtualBox:~/patch_tests$


Applying the Patch File

Lets say we are receiving patch files & need to apply them.

Now, to apply the patch file, execute this command:
patch -p1 <source file> < <patch specific to source file>
Example:
For the earlier example I discussed above, I duplicated the unchanged sources to  /home/babu/patch_tests//valgrind_tests_patched. Now, to patch vg.patch to this duplicated sources, here is what I did:
babu@babu-VirtualBox:~/patch_tests$ patch -p1 valgrind_tests_patched/vg_test.c < vg.patch
patching file valgrind_tests_patched/vg_test.c
babu@babu-VirtualBox:~/patch_tests$ diff -Naur valgrind_tests valgrind_tests_patched/
diff -Naur valgrind_tests/vg_test.c valgrind_tests_patched/vg_test.c
--- valgrind_tests/vg_test.c   2014-09-10 21:26:39.473561882 -0700
+++ valgrind_tests_patched/vg_test.c         2014-09-10 21:49:07.039469608 -0700
@@ -4,6 +4,12 @@
 
int global_array[100] = {-1};
 +/* Testing patch functionality */
+void patch_test()
+{
+  return 1;
+}
+

void
unintialized_use()
{
babu@babu-VirtualBox:~/patch_tests$
I will try to cover patching of multiple files in a directory some other time.


Hope this helps.


Tuesday, September 9, 2014

VirtualBox Error - Kernel driver not installed


I have been VirtualBox in my macbook pro. But, I stopped using it in last 3-4 months. And when I tried to use it today again, it threw up the following errors:


Error in Virtual Box window:
Failed to open a session for the virtual machine MyUbuntu.
Virtual machine 'MyUbuntu' has terminated unexpectedly during startup. 

NS_ERROR_FAILURE (0x80004005)
Machine
IMachine {<<<some big number here>>>}

Error in an additional popup window:
Kernel driver not installed (rc=-1908)
Make sure the kernel module has been loaded successfully.


Instead of debugging how to fix this issue, I just updated the virtualbox with an available newer version (Virtual Box on top left --> Check for updates). 

After update the error went away. None of my existing VMs or config was impacted due to the upgrade.


Hope this helps.

UA-48797665-1