Linux Kernel – Saving Time While Compiling

compilingkernel-moduleslinux-kernel

I have to demonstrate a practical where I have to make my own module, add this module to a kernel's source and implement the system call. I'm using 3.16 kernel on Ubuntu but it takes around 2 hours to install the kernel from source.

Is it possible to remove some part of kernel(like unnecessary drivers,etc) from the source to save time as I'm not going to use this newly installed kernel for regular use? If yes, how?

Best Answer

As mentioned in comments, you should be building using something like make -j4. Use a number equal or slightly higher than the number of CPU cores you have.

make localmodconfig

The following instructions apply to building a kernel from upstream. Personally I find that simplest. I don't know how to obtain a tree with the ubuntu patches applied, ready to build like this.

(1) Theoretically the way you build kernels in more reasonable timespans for testing is supposed to be

cp /boot/config-`uname -r` .config

you don't need to enable anything newer, so - only problem is this breaks if they renamed stuff:

make oldnoconfig

now disable all the modules not currently loaded. (Make sure you have all your usb devices you need plugged in...):

make localmodconfig

It worked for me recently, so might be useful. Not so well the previous time I tried it.

I think I got it from about one hour down to ten minutes. Even after make localmodconfig it's still building crazy amounts of stuff I don't need. OTOH actually finding and disabling that stuff (e.g. in make xconfig) takes a while too (and even longer if you mistakenly disable something you do need).

I guess it's worth knowing it exists, it's just not guaranteed to make you happy.

(2) I don't think it should take two hours to build every modification to your "module". (It actually needs to be a builtin if you're implementing a new system call). make will just recompile your modified files and integrate it into a kernel binary. So in case getting the Kconfig right is too much trouble, then maybe an initial two-hour build is not too bad.

You might be having this problem if you are building with a distribution kernel source package. (You can switch to manual builds, or you might be able to trick the distro source package into using ccache). Or, your modifications might be modifying a header file which is unfortunately included by many many source files.

Even so, it might be useful to make custom Kconfigs, e.g. much smaller Kconfigs, if you want to port to different kernel versions, do git bisect, test different build options, etc.

Related Question