“Init” is the first program that gets executed when linux kernel boots up. I created a compressed root file system in cpio format that hold only init file inside to it.

Prerequisites…

To get latest stable kernel click here

For qemu emulator installation click here

Short story

  • compile minimal kernel
  • create init from scratch
  • test using qemu emulator

Big story

Compiling minimal Kernel

$ tar -xvf linux-3.8.5.tar.xz

$ cd linux-3.8.5/

$ make distclean

$ make defconfig

$ make

As a result of above steps you will get kernel image

linux-3.8.5/arch/x86/boot/bzImage

Creating init from scratch

Create a fresh directory

$ mkdir test

$ cd test/

Create dummy.c file

$ vim dummy.c

FYI, content of dummy.c

#include <stdio.h>
int main(int argc,char *argv[])
{
char name[100];
printf(“Hello World…!\n”);
puts(“enter your name :”);
gets(name);
printf(“your name is : %s\n”,name);
while(1);
}

You can judge it as simple program!

Yes my init will prompt me to enter my name and it will print back to me, nothing more than this.

I am compiling statically linked version of above program, and naming the executable as init

$ gcc -static dummy.c -o init

To create cpio.gz format of executable

$ echo init | cpio -o -H newc | gzip > initramfs.cpio.gz

Copy kernel image, inside test directory

$ cp ../linux-3.8.5/arch/x86/boot/bzImage .

Testing using Qemu emulator

Be aware of kernel image and initramfs.cpio.gz are in same directory, then type the below command

$ qemu-system-i386 -kernel ./bzImage -initrd ./initramfs.cpio.gz

you will get,

qemu-system-i386: pci_add_option_rom: failed to find romfile "efi-e1000.rom"

VNC server running on `::1:5900'

Open a new terminal and type the below command

xvnc4viewer : 5900

Kernel will boot inside qemu emulator, and prompt for name, after insertion of name, it will again print back, as we expected.

Something like,

Hello World…!

enter your name : 

Mohammed Audhil

your name is : Mohammed Audhil

Congrats..! You made kernel to work for you…!

Enjoy…!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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