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?

  • 4
    Rebuilding a module doesn't require rebuilding any of the kernel. Once it's compiled with the right parent and bus drivers once, the module code can be rebuilt as many times as needed, while still using the exact same kernel binary. So get yourself a kernel build tree, but don't do the build live.
    – Ben Voigt
    Apr 11, 2016 at 15:18
  • @BenVoigt : You're right indeed.But building the kernel from scratch is a part of this practical. I wish I could avoid it but unfortunately I do not have any other way around.
    – 7_R3X
    Apr 11, 2016 at 15:21
  • 2
    Kernel builds respond pretty well to throwing more cores at the problem. Increase the -j value of your make until you run out of processors (or RAM). Apr 11, 2016 at 15:26
  • @7_R3X: Perhaps build in advance and delete half a dozen .ko files and the arch outputs. Then the next build will have to recreate only those.
    – Ben Voigt
    Apr 11, 2016 at 16:45

2 Answers 2


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.

  • oooo, that make localmodconfig sounds very nice
    – Ben Voigt
    Apr 11, 2016 at 16:42

If you're repeatedly building the kernel on the same machine, ccache can help a lot, especially if you're using a VM. In my experience, successive clean builds of the same project on a VM will build in about half the time as a build that didn't use ccache. You will need some extra disk space, to store the object files saved from the first build. Also the very first build with ccache enabled will take slightly longer (maybe 5-10% longer) while it's writing all the object files to the ~/.ccache folder.

I have something like this in a setup script for our build machines:

sudo apt-get install ccache
for binary in gcc g++ cc c++ i686-linux-gnu-g++ i686-linux-gnu-gcc x86_64-linux-gnu-gcc x86_64-linux-gnu-g++
    if [[ -e /usr/bin/$binary ]]
        sudo ln --symbolic /usr/bin/ccache /usr/local/bin/$binary
        test $? -eq 0 || echo error: symlinking $binary to ccache
echo "If you have the space, you may want to increase your ccache size with \"ccache -s 5G\" (or however much space you can afford)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.