Oops… forgot to compile a driver module for your Linux kernel?

A co-worker wondered aloud the other day, after compiling a Linux kernel, how he could add drivers without re-compiling the entire kernel. I wasn’t sure how to do it at the time.

Now I am!

A co-worker wondered aloud the other day, after compiling a Linux kernel, how he could add drivers without re-compiling the entire kernel. I wasn’t sure how to do it at the time.

Now I am!

So, here’s how you compile an individual driver when you’ve FUBAR’d and forgotten to do it. This only works with ones you’ll need as modules (because in order to get one “into” the kernel, such as Reiserfs, you’d need to recompile the kernel anyway). Just kind of got stumbling around with gcc tonight, and found a combination that works…

Be aware, though, that with certain drivers that strongly touch others, I understand that you may need to re-do “make clean”, “make menuconfig” (or oldconfig, xconfig, config, whatever you use to configure your kernel), and “make dep” to get these to build. Also, if they are drivers that should be in the kernel when you boot, you’ll either need to rebuild your kernel or rebuild your initrd — which is a topic for another day 🙂

Of course, change the path to your linux kernel sources. This is a gaming kernel I’m hacking drivers on tonight while I’m home.

 gcc -I/usr/src/linux-2.4.20-gaming-r3/include -include \ /usr/src/linux-2.4.20-gaming-r3/include/linux/modversions.h \ -DMODULE -D__KERNEL__ -O6 -c sg.c

This was to compile the “sg.c” driver, when I was in the /usr/src/linux-2.4.20-gaming-r3/drivers/scsi/ directory. The -I (note the lack of any space in the path after) option is basically “Include this stuff”, with an additional include to modversions.h so you don’t end up with a version mismatch.

Worked a charm for me; I copied sg.o to /usr/lib/modules/2.4.20-gaming-r3/kernel/drivers/scsi/ and the did an “insmod sg.o”, and (since this is Gentoo), added “sg” and “ide-scsi” (in that order) to my /etc/modules.autoload.d, and everything was beautiful.

In large part, the reason I committed this to my blog is so that I don’t forget how to do it. I was really, really not looking forward to recompiling my entire kernel tonight because I forgot to include the “sg.o” module (generic scsi support, necessary for CD burning in Linux). What a relief; this took like 10 minutes of research, and about 30 seconds of compiling to make work!

One thought on “Oops… forgot to compile a driver module for your Linux kernel?”

  1. Saves the bacon

    This page has saved my bacon three times in the past five years, and today it might be saving a co-worker’s bacon. I need to write more stuff like this.


    Matthew P. Barnson

Comments are closed.