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.

Friday, April 8, 2016

Exploring Linux Kernel with Virtualbox - Part 1 - The environment setup

Finally few experiments is about to fire with Linux Kernel. I am sharing those learning curve with a blog-post to invite readers for feedback. I have setup virtualbox to debug linux kernel and it has been fun so far. I want to make it more interesting by debugging/exploring major functionalities of linux kernel. Here is my initial start to this festival and I hope it will last long!

For all the demotivators, stop right here and read:

There are humongous online references and in form of printed text. So why is this juggling required ?

You know! experiences ingrain to brain more than reading theory. When you dont have such related work in your office, you buy those experiences by motivating yourself!

It's just beginning with exploration towards networking aspect of kernel. Hope I will come up with various other parts of linux not limited to networking.

The initial few sessions concentrate on bringing up setup and a tiny debug session.

My setup

Host: Ubuntu x86_64
Guest 1: Ubuntu i686
Guest 2: Ubuntu i686 (As of now not much in use. Primarily used to simulate network forwarding)

What you need in Host OS?

a) VirtualBox
b) VirtualboxGuestAdditions
c) Ubuntu i686 ISO (from Ubuntu site)

Install above utils with

sudo apt-get install virtualbox virtualbox-guest-additions-iso

Once installed, it is time to create virtual machine. I am not elaborating the process involved since lots of tutorials point at this procedure. Here are the steps leading to installation of guest OS.

Creating a virtual network

Create a Host only adapter Network interface under File-->Preferences-->Network.

Here I have added vboxnet1 as well but it is not required as of now.

Edit the settings (as shown in figure below) of added interface and disable the DHCP server and assign a manual address of your choice.

Choosing RAM size

I have healthy 16GB of RAM and hence chose 2GB for my VM. You can choose appropriate size that fits to your actual physical memory or choose size recommended by virtualbox.

Choosing disk space

Make sure to give at-least 24GB of disk space to accommodate guest OS. I prefer fixed size since we will not do much installation other than linux kernel. VDI image was chosen for storing VM disk. I have not experimented with other formats. If you wish to inter operate with other VMs, you can chose other formats.


Networking between host & guest

After VM is created, click on created VM and click on Machine Settings in Toolbar.

Note: There are two types of settings

(i) Global Settings which is applicable to all VMs
(ii) Per VM settings which refers to VM specific settings.

When I refer as "Machine Settings" it means VM specific settings.

Enable Adapter1 as NAT for connecting towards Internet.

Note: If Internet requires proxy, the proxy has to be set inside guest OS.

Enable another Host-only adapter interface and choose already created interface in Creating Virtuan Network section

Enable clipboard sharing

This is very important if you need to work with clipboard data between host & guest (copy & paste)

Goto Machine Settings-->General-->Advanced, choose "Bidirectional" for both parameters as shown below.

Enable file sharing

This enables you to share files between host & guest. To make use of this functionality, Goto Machine Settings-->Shared Folders and add a folder of your choice.

Installing OS

Boot the machine and it will guide you do browse for virtual boot disk. At that time, point to ISO file downloaded for installation. Make sure to disable downloading updates from internet.

The installation is like any other linux OS. The only recommendation from me is to have custom partitions as below.

(i) Dedicated /home partition for kernel compilation (about 14GB)
(ii) Dedicated / partition for installing new kernel (about 8GB)
(iii) Rest 2GB can be for swap

Note: The below picture is just an illustration and does not conform to the above mentioned parameters

Installing Guest Additions

Once installation is completed, first step is to Install guest additions on guest OS. But I prefer to defer this step for the moment. Why? Soon the secret will be unfolded ;).

Once the guest OS is booted, first and foremost, download the kernel source.

sudo apt-get install linux-source

This will be place /usr/src directory as compressed bz2 file.

Create a directory in /home/<user>/ say name "kernel" and copy the bz2 file to that directory. Once done extract the kernel source. Below file name is just for illustration. It could be something else in your native system.

tar -xf linux-source-4.2.0.tar.bz2

Now the system is ready for kernel game and we are ready to cheer! The sequel will be explained in subsequent blog post.