I want to try my hand at writing a linux driver. I am trying to set up my environment. My current kernel:

$ uname -r

I then download the source code:

$ apt-get source linux-image-$(uname -r)
Reading package lists... Done 
Picking 'linux' as source package instead of 'linux-image-4.10.0-37-generic'

I go to compile and modprobe my driver, it fails. Looking in dmesg, it shows:

version magic '4.10.17 SMP mod_unload ' should be '4.10.0-37-generic SMP mod_unload '

At this point, I'm confused. I go back to the source tree I downloaded, and when I run

$ make kernelversion

Ok, try two. Download kernel 4.10.17 and install it.

$ uname -r

Still error:

version magic '4.10.17 SMP mod_unload ' should be '4.10.17-041017-generic SMP mod_unload

So maybe someone can help: what is the best and correct way for me to get a working kernel and matching source on ubuntu (well, xubuntu, but I don't think it should matter)? Do I need to get the code from kernel.org and build it from scratch? I kinda want to match the shipping Ubuntu kernel.

  • The linux-source-VERSION where VERSION is 4.10.17 in your case is probably the package you want to install.
    – ivanivan
    Oct 11 '17 at 19:48
  • If you want to compile the packages, you need use debuild.
    – sebasth
    Oct 11 '17 at 19:49
  • @ivan seems like linux-source defaults to linux-source-4.10.17 but it doesn't match 4.10.17-041017-generic
    – Will I Am
    Oct 11 '17 at 19:51
  • I pulled the code from kernel.org and I'm going to try to match it that way. @sebasth I didn't see any mention of debuild on any of the guides at ubuntu.com. I'd need a little more help, though for now I'm compiling the kernel.org code.
    – Will I Am
    Oct 11 '17 at 19:57
  • 1
    @GAD3R, no, don’t do that. Oct 12 '17 at 8:03

There are a number of approaches...

  1. If you’re trying to build an external module (including one you’re developing), you only need the kernel headers:

    apt install linux-header-$(uname -r)

    This will provide the necessary files so that the /lib/modules/$(uname -r)/{build,source} symlinks point to something meaningful. Then you can build a module in another directory by running

    make -C /lib/modules/$(uname -r)/build SUBDIRS="/path/to/your/module" modules

    This will ensure that the module is built for the kernel you’re running.

  2. If you want to base your development on the Ubuntu kernel, use the appropriate linux-source package; for your release of Ubuntu, that’s currently linux-source-4.10.0:

    apt install linux-source-4.10.0
    cd /usr/src
    tar xf linux-source-4.10.0.tar.bz2

    This will include the Ubuntu kernel patches, allowing you to build a kernel with the same features as your current kernel. Note however the caveat from the package description:

    This package is mainly meant for other packages to use, in order to build custom flavours.

    If you wish to use this package to create a custom Linux kernel, then it is suggested that you investigate the package kernel-package, which has been designed to ease the task of creating kernel image packages.

    If you are simply trying to build third-party modules for your kernel, you do not want this package. Install the appropriate linux-headers package instead.

  3. If you want to base your development on the upstream kernel (which is what I’d recommend), you should clone Linus’ tree and work there. To test your module, you’ll need to either build a full upstream kernel, or build your module using the approach given in point 1 above.

In any case it’s not a good idea to use the linux source package itself (as obtained using apt-get source), since that’s really designed for building all the kernels used in Ubuntu. If you blindly debuild using that source package, you’ll wait for many hours before the build finishes... (There are circumstances where this is appropriate, and the Ubuntu kernel documentation will explain what to do; but this is very likely not one of them.)

  • I installed 'linux-source-4.10.0' and get the same error - 'version magic '4.10.17 SMP mod_unload ' should be '4.10.0-37-generic SMP mod_unload'. If you check Makefile in 4.10.0-37-generic - its configured that version is '4.10.17'
    – progopis
    Oct 13 '17 at 16:02

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.