2

I want to learn linux kernel programming and so I downloaded Linux kernel 3.7.6 source from www.kernel.org. However before editing anything, I tried to compile it using online tutorials.

The make takes a lot of time to compile and I haven't done it completely yet. Is there any quick way to change the source code and test it? Because I've read that kernel compilation takes around an hour.

Also, If it is possible to copy and test the modified kernel on a USB drive? Is it possible to install GRUB on a USB drive, copy the kernel on USB, configure the GRUB on USB and then run it? If so, please give me any resources which have steps of doing the same. I also have GRUB installed on my laptop. How to set up new GRUB on USB without disturbing the configuration on my laptop?

Thanks.. :)

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
upInCloud
  • 979
  • 2
  • 10
  • 29
  • 5
    Don't worry. It only takes long time once. If you modify one source file and recompile, only that file will be compiled. Such are the wonders of `make`. – n. m. could be an AI Feb 10 '13 at 16:59
  • 3
    I reccomend you use a virtual machine for testing, at least for the firsts iterations: Qemu can boot a kernel directly. It is faster and easier to boot, and you do not risk your own machine when you break things. Because you _will_ break something... – rodrigo Feb 10 '13 at 17:50

2 Answers2

6

You mention that you want to learn Linux kernel programming so I think I have a better suggestion for you, since you're new to all of this.

Why don't you for starters try to do little kernel programming like introducing VERY simple drivers or something to do with netfilter(you can almost control the whole TCP/IP using this) all without even having to compile a kernel?

Kernel modules are your friends :)
They're a simple piece of code that you compile separately then ask the running kernel (using the insmod Linux command) to attach this code to the running kernel and there you go! You have your own code running in kernel space.

I do realize this doesn't exactly answer your question, but I really think starting like this is easier for you and might be even more useful; once you're more familier with the many things you can find here, try moving on to actually modifiying the existing kernel code.

To let you know what you can do with kernel modules:
-Drivers
-Adding a new functionality to the kernel
-I've personally used it with netfilter(a set of functions and hooks in the kernel) to modify handling network traffic
-You can also patch your kernel(modify source code, compile, run the whole thing) then use kernel modules to edit somethings that typically need a whole re-compilation like editing system-calls; although this approach is not recommended but it can boost your testing time where each time you want to do a test you don't have to compile and restart.
-Much more..

Let me know if I was confusing at some point.

Fingolfin
  • 5,363
  • 6
  • 45
  • 66
2

You need to build the kernel it till the end if you want to boot it. However it should be faster second time after only a small change in the code, only required part of the kernel will be rebuild.

It is very easy to have multiple boot options (using different kernels) by adding new entries to grub.cfg (for grub2). I however cannot just repeat multiple pages of documentation here.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • I got grub2 set up (I havent't created grub.cfg yet. Just formatted USB drive with ext4 and installed grub2 on it using `grub-install`. – upInCloud Feb 10 '13 at 18:10