Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm writing a module for the Linux kernel. When this module is ready, I'd like it to run in any device running the kernel.

How can I test it in an Android phone? Do I need to recompile the whole tree for that phone with the module in to test it? If so, how can I do that? Is there some kind of insmod for Android?

share|improve this question
add comment

2 Answers

Yes, of course there is insmod for Android because Android is based on a normal Linux kernel. The answers to this question will be the same as for any other Linux kernel.

And also just like any other cross-compile situation you will need to compile your module against the kernel source and options of the kernel on your devices as well as for the right system architecture. This varies widely by device, manufacture, ROM, etc.

Your "how do I do that" question is unanswerable because you don't provide any details about the device, kernel version, ROM or your development environment.

I would seriously question whether you are on the right track at all here. You cannot simply write a module and "run it on any device". Modules are kernel version and architecture dependent. Most devices are locked so that you don't have access to the root file system, much less the kernel. Distributing something like this would typically mean bundling it into your own ROM for a specific device.

Typically the only reason you would need to write a kernel module would be to support some specific hardware function, in which case this question seems like you're starting at all the wrong end of things. The other possibility is that you want to be evil and do something Linux wouldn't normally let you do. Don't do that.

share|improve this answer
    
Each Android release comes with a specific kernel version, and it should be easy to get a binary module that works on any device with that kernel version. It's even possible to make a module binary that works against any Linux version (within a reasonable range), but that requires more care to use only stable ABIs. –  Gilles Jul 5 '11 at 10:08
    
I do not intend to do anything evil. And I'm sure the module is perfectly valid. And it should work on any device because I'll release the code. So anyone can compile it to their device, or release their version with it in. Just like the kernel itself. –  jpmelos Jul 7 '11 at 5:55
add comment

There's no fundamental difference between Android and other Linux platforms. To build a module, you need kernel headers and a few files that are generated during the kernel compilation. These files depend on the kernel configuration. Typical Linux distributions ship with a package containing these kernel headers and other files, to make it easier to build custom modules. I don't think the Android NDK comes with kernel headers (compared with the number of people who write native applications, very few people build kernel modules, and many are hardware manufacturers who are building their own system image anyway).

So get the Android source, specifically the kernel tree (kernel/common). Get the kernel configuration for the device you're targetting; the default image provides its configuration in /proc/config.gz, you can get it with adb shell 'gzip -d </proc/config.gz'.

Once you have the source tree and the configuration file, follow the usual drill: copy your configuration file to .config in the kernel tree, run make oldconfig and make modules_prepare, then make M=/path/to/your/module/tree to build your module. The Linux Kernel Module Programming Guide and Linux Device Drivers are good references; the kernel build system is documented in Documentation/kbuild/modules.txt.

If the device you're targetting doesn't have modules enabled (CONFIG_MODULES in the configuration file), you'll need to rebuild a kernel with modules and copy it into your system image.

The Android image in the emulator comes with very basic module utilities: insmod, lsmod and rmmod are present, but insmod isn't capable of specifying arguments. If that's a problem, get Busybox (it's useful for other things as well). You can get it from the Market or compile it from source.

There are many tutorials on kernel and module compilation for Android. Here are a few; I've only skimmed them so I don't guarantee quality.

share|improve this answer
    
Thanks for the detailed answer! :) –  jpmelos Jul 7 '11 at 5:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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