2008-09-29

Compiling a kernel using the modules you actually have loaded.

So today on reddit, someone named sping was wondering if anything existed to take the modules you have loaded on a linux system, and automatically compiling a kernel with those compiled in.

Now, this is probably not that great of an idea if you plan on updating your kernel any time soon, but it may come in useful for people who know have a computer whose kernel and/or hardware aren't going to need updating for a few years.

I wasn't (and am not) aware of a tool for doing this, so I decided to write one - as it sounded like a fun (not to mention educational) exercise. I figured there would be a pretty easy-to-find correlation between module names and kernel configuration options (since they are marked with "m" or "y" and such). I'm far from being a kernel-configuration guru, or a master of the kernel internals, but I know a little bit (and better yet I know how to find stuff out, hooray.).

It was a lot harder than I expected to figure out how to relate the currently loaded modules to their configuration options. The reliable method I found is slow.

Basically, once you have the kernel sources, you can find what kernel option you need with this nastly oneliner, from the sources directory, where FOO is the name of the module you're looking for:

find . -name Makefile | xargs grep .*CONFIG.*FOOo$ | awk -F "$" '{print $2}' | grep -o \(.*\) | sed -e "s/(\|)//g"
This looks pretty nasty, but it's not that bad. Its just doing this:

  1. Find any file named Makefile under this directory.
  2. For each match of step 1, look for a string in it containing "CONFIG", "FOO", and ending with an o. These lines look like this: "./fs/hppfs/Makefile:obj-$(CONFIG_HPPFS) += hppfs.o
  3. For each match of step 2, split the line on the "$" and print out the part of the second half surrounded by parentheses with the parentheses removed.
Anyhow, once you have the links between module names and kernel configuration options, all you have to do is modify the kernel config file and compile (and perhaps still answer a few compile config questions if an invalid kernel config got generated).

This was a lot more work than I expected it to be, but really wasn't that hard when you take into consideration that I don't really know much about linux internals, or how the kernel works except at a high level, and was able to come to a solution primarily by searching through and modifying text files, and having a program call the commands that I normally would.

I wrote the actual "I would give this to people who know what they're doing to try" version of this in python, you can see it (or download it) here. It might for any debian-based distro, but I've only tested it on Ubuntu. There's a script called prelims.sh that will download and install what you need to compile a new kernel, and the kernel sources. The script is still rather hackish, there is some ugly code, may only work on Ubuntu, and is not appropriate for people who don't know how to step through a kernel config process already.

So, it's not polished - but it works as a proof of concept, and that was the goal here.

7 comments:

  1. Thanks, tisha! I'm glad to hear someone found it interesting.

    ReplyDelete
  2. Interesting... but, did it speed up your boot time?

    ReplyDelete
  3. Compared to a stock Ubuntu kernel, yes - but not a huge amount.

    I did this mainly to show it could be done - it's probably not something anyone wants to actually do though, especially considering that the modules you have loaded right now may not be all the modules you need all the time.

    ReplyDelete
  4. Some advertising networks require that publishers are
    at least 18 years of age to join them. By following them, you will shortly embark on
    a blogging career that you never imagined. Promote
    and distribute your posts on to other social media platforms such as
    Facebook, Twitter and Linked - In.

    Stop by my web-site: hostgator coupon code

    ReplyDelete
  5. Irrespective of the new trend, many females continued to be married in numerous
    diverse colors and types of cheap wedding dresses

    Here is my blog :: go here

    ReplyDelete
  6. When covering the area, it should be enough to use a regular
    bandage. As a word of caution you should also never try to manually remove the
    warts yourself, as that can lead to the virus spreading.
    Make certain that the information you get is genuine in
    order that you can effectively do away with
    your warts.

    Also visit my page: How to get rid of warts

    ReplyDelete