Tuesday, April 19, 2016

Exploring Linux Kernel with Virtualbox - Part 2 - Compiling kernel and install guest additions

Continued from Part 1

I am back!

This post primarily concentrates on following aspects.

(i) Kernel compilation
(ii) Installation of guest additions
(iii) Sharing folders for kernel installation
(iv) Tips

Before starting with mainline topics, I have one more Machine Settings for boot order. Not sure, how much improvisation apparently it yields, but worth the try.

Goto Machine Settings-->System-->Motherboard-->(Uncheck Floppy option)

The irrelevance of Floppy drives in contemporary technology, inspired me to un-check such an option. However, it might be useful if you want to pamper with older systems like DOS.

Also move up Hard disk boot order above all and Optical drive as next priority.

Kernel Compilation

Once the source tar ball is extracted. You need to install libcurses to open up menuconfig.

sudo apt-get install libcurses-ocaml-dev

Once done with installation, enter the below command in extracted source directory to configure kernel.

make menuconfig

Following configurations are required for debugging kernel.

Note: * represents the option is enabled. Press Spacebar to toggle option.

Enter the Kernel Hacking sub-option and enable following. There are umpteen options for debugging but as of now these two enough for basic exploration.

(i) Enable Kernel Debugging: By default this option is enabled, if not make sure to enable

(ii) Enable Stack backrace support: This is required to dump stack trace at required code points

Few tips to fasten up kernel compilation. I will update, if I spot anymore.
  1. Disable container and namespace support: I prefer to enable it for future purpose.
  2. Disable unnecessary file system support: This can save lot of time. You do only need basic filesystem support and others required only if you wish to know the filesystem structure. Since the linux follows VFS, the flow is identical from User space except for lower layer implementation.
  3. Disable excess driver support: This requires careful attention. Many drivers such as apple device support etc.. can be easily omitted. Make sure you do not disable basic drivers such as Mouse, Keypad, USB etc..
Once done, keep pressing Exit till it returns to main menu. Then press Save to save configuration. Let the default name be retained.

The below picture confirms the saved configuration.

Now we are ready to compile! Execute below command to start compilation

make && make modules

It will require 3-4 hrs depending on hardware configuration and kernel configuration to complete the process. Sit back and listen to few uplifting music, sip a coffee, eat churmuri or watch a movie even [Kung Fu Panda 3 preferred :)].

Once completed you will get success message.

Now we have the kernel image and ready to explore.

Install the new kernel image with following commands.

sudo make modules_install (To install modules)

sudo make install (To install the new kernel bzimage)

Note: Make sure to install the modules first and then kernel image

Reboot the machine!

After reboot check if the kernel version is what you compiled.

vmguest1@heramba:~$ uname -a
Linux heramba 4.2.8-ckt3 #4 SMP Sat Apr 9 11:02:58 IST 2016 i686 athlon i686 GNU/Linux
vmguest1@heramba:~$ date
Sat Apr  9 11:24:45 IST 2016

Hurray!! It is done.

Installing Guest Additions:

In my previous post, I preferred to defer installation of guest additions. The reason is, installation of guest additions involves compilation of kernel modules. Hence if we had done installation with vanilla build, it would have required recompilation of guest additions (since we installed our own compiled kernel). In order to mitigate the time involved, it is better to install guest additions after kernel is compiled.

Once ubuntu is booted, click on Devices --> Insert Guest Additions CD Image

You will get prompt as below and click on Run

The process involves compilation of guest modules with installed kernel and later installation.

Once done, reboot the guest OS and you will recognize the change in display rendering of guest OS. Now the guest OS display fits into your native display.

Please note that in my case guest additions were pre-installed and hence one does not see the default display rendering of VM

The guest additions are required for following purposes. This is non-exhaustive list of guest additions' usage.
  1. Native display support
  2. File sharing between guest and host OSes
  3. Clipboard data sharing
Next part will explain few tips and short debug session.