Fall2009/Kernel Hacking Exercise

From OpenSourceSoftwarePractice

Jump to: navigation, search

Kernel Hacking Exercise

Contents

Download the source code

Expand

  cd 
  mkdir KernelHacking
  cd KernelHacking
  wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.30.9.tar.bz2
  tar xjvf linux-2.6.30.9.tar.bz2

Configure

  cd linux-2.6.30.9
  wget http://files.robescriva.com/ossp/config
  mv {,.}config
  make menuconfig

Build

To build the kernel (both the image and modules), type:

 make -j4

Where "4" is meant as double the number of processors of your system. For example, if you have a quad-core processor, then you should use -j8.

Your newly compiled kernel image can be found in

 arch/<platform>/boot/bzImage

Most likely, your <platform> will be x86 or i386

To install the new kernel image, copy it to the /boot directory

 sudo cp arch/x86/boot/bzImage /boot/OpenSourceClassKernel

To install the modules, type:

 sudo make modules_install

They should now appear under:

 /lib/modules/2.6.30.9OpenSourceClass

Reconfigure GRUB

  cd /boot/grub
  edit menu.lst

insert the lines

  title   Ubuntu-OpenSourceClass
  root    (hd0,0)
  kernel  /boot/OpenSourceClassKernel
  

Reboot

Before rebooting the first time, you will then need to edit the grub configuration file

http://www.linuxchix.org/content/courses/kernel_hacking/lesson4

 Rule number one is never ever delete your current working kernel
 or bootloader configuration files. 
 Never copy your new kernel over your original kernel. 
 You don't need to keep every kernel you ever compile, 
 but do pick one or more "safe" kernels and keep them 
 and their configuration files intact.

After modifying your grub configuration, reboot.

 sudo shutdown -r now

Modify

Go to the directory "init"

  cd init

and edit the file

  main.c

go to lines 931 where you will see lines

  /*
   * Ok, we have completed the initial bootup, and
   * we're essentially up and running. Get rid of the
   * initmem segments and start the user-mode stuff..
   */
  init_post();
  return 0;
  }

and before "init_post()" insert the following line:

  printk("OpenSourceClass: My First Kernel Message");

go back to the parent directory

  cd ..

Build / Reboot

Now that you are back in the linux-2.6.30.9 directory, build your modified kernel, copy it to /boot, and restart.

 make -j4
 sudo cp arch/x86/boot/bzImage /boot/OpenSourceClassKernel
 sudo shutdown -r now

You do not need to edit your grub config again.

Modify

Go to the directory "fs"

  cd fs

and edit the file

  read_write.c


go to lines 799 and insert the following line of code

  printk(KERN_DEBUG "OpenSource Class Exercise: %s:%i\n", __FILE__, __LINE__);


go back to the parent directory

  cd ..

Build / Reboot

Just as before, rebuild the kernel, copy it to /boot and restart.

Personal tools