Minimal Linux setup for kernel development

Introduction

This example can be used to setup a minimal Linux installation for any task. In this tutorial however I am going to use kernel development as an example. Since the process I have used in the past have been from sporadic sources, I wanted to consolidate the information for my own need. This tutorial is the result of that effort. So that next time if I feel like doing something kernel related, I don’t have to start over again.

install

We are going to setup a minimal ubuntu installation (no GUI) for kernel development. Install ubuntu minimal from this link. For this tutorial I will use Ubuntu 12.04 LTS version in virtualbox with 17 GB space. Make sure the architecture (32-bit, 64-bit) matches that supported by virtualbox. In virtualbox select at least 2 cpu and half of system RAM (keep everything under the green line). Since we are going to install necessary package later on, you don’t have to install any special packages during the installation. Skip the package installation step and complete the installation.

Packages

Since we are creating a kernel development environment, we don’t need fancy GUI packages. As for the development itself we are going to use vim (you can use other non-gui editors of your choice or install a Desktop Environment or Window Manager if you prefer that instead of pure command line interface). Install following packages.

sudo apt-get build-dep linux-image-$(uname -r)
sudo apt-get install libncurses5-dev bc vim

Build and Install Linux Kernel

Since using mainline kernel is the preferred way to start kernel development. You can install mainline kernel from kernel.org . In this tutorial I will use stable 3.13.2 kernel.

mkdir kernel
cd kernel
wget -c https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.13.2.tar.xz
tar xf linux-3.13.2.tar.xz
cd linux-3.13.2
cp /boot/config-$(uname -r) .config
make menuconfig    # Do these activities in sequence (load -> exit -> save) for the .config file 
                   # present in current directory using the ncurses buttons.
make -j5           # It will create arch/<yourarch>/boot/bzImage
sudo make modules_install
sudo make install
sudo reboot

If grub does not recognize your new kernel, you might have to run following command (most of the time it is executed by the make install command)

sudo update-grub

Sometimes you might even have to run this command if grub get corrupted (rarely)

sudo grub-install /dev/sda

During boot in virtualbox if kernel panics you might have to repeat the process from “sudo make install” once again. If during boot, virtualbox acts wierd, you might have to press “Right Ctrl + r” to reset virtualbox and hold UP/DOWN arrow key to select the boot entry from grub menu.

Hello world module

As it is customary to write a hello world example while learning programming, I am not going to break that rule. Type following code into a file called hello.c inside ~/test/hello_first directory. You can use vim or nano to type the following code.

#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
 printk(KERN_INFO "init_module() called\n");
 return 0;
}

void cleanup_module(void)
{
 printk(KERN_INFO "cleanup_module() called\n");
}

kernel_code

Create another file called Makefile inside the directory with following content.

obj-m += hello.o
all:
 make -C ~/kernel/linux-3.13.2/ M=$(PWD) modules
clean:
 make -C ~/kernel/linux-3.13.2/ M=$(PWD) clean

To compile type “make” and to remove the compiler generated files type “make clean“. To load, print output and unload use following commands.

sudo insmod hello.ko
lsmod | grep hello
dmesg | tail
sudo rmmod hello
lsmod | grep hello
dmesg | tail

testing

In dmesg output you will be able to see the output of init_module and cleanup_module after you insert and remove the module.

Conclusion

Purpose of this tutorial is to help you setup a minimal installation for kernel development. If you follow only the Introduction section you can expand the installation to target other usage as well. The reason I selected ubuntu as the guest system in virtualbox was because of its easiness in setup even for a non-GUI system and familiarity of Debian commands among many Linux users. After the installation you can install openssh-server in the guest environment, setup bridge network with your host (e.g if you use wifi, bridge with its interface rather than eth0) and ssh into the development environment (ssh user@ip.address). This way you can use normal terminal of your operating system to program and you don’t have to be limited by the small screen size (because of lack of video drivers in the guest OS). If you want to learn about device driver programming (one part of kernel programming) you can download the freely available PDFs of Linux Device Drivers at lwn.net or buy the book. To learn about internals of kernel, Linux Kernel Development is a good source. If you learn using more practical and complete examples you can follow the device drivers tutorial at linuxforu. I suggest you first read the first three chapters of Linux Device Drivers thoroughly before exploring other options. For file system understanding I found this chapter from Operating Systems: Three Easy Pieces to be quite good.

References

  1. http://www.cyberciti.biz/tips/build-linux-kernel-module-against-installed-kernel-source-tree.html
  2. Linux Kernel Development by Robert Love
  3. https://help.ubuntu.com/community/Installation/MinimalCD
About these ads

2 Comments

Filed under Uncategorized

2 responses to “Minimal Linux setup for kernel development

  1. Many Thanks for the writeup. I was exactly looking for this information for sometime as I spent(wasted) lot of time doing the right distribution/version of Linux to experiment on Kernel Programming and I was unsuccessful.
    I am confident that this article will give me a good start.
    Thanks again

  2. This is my ideal setup for virtualbox, if I have to experiment something. If you need to setup proper resoluton (for some lightweight desktop environment), you may also want to checkout setting up VBoxAdditions.sh from command line.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s