Compiling an individual kernel module (Debian/Ubuntu)

12,529

Try this:

How do I build a single in-tree kernel module?

Alternatively, the way I normally do this is something like the following. This is from memory and may or may not work for you. It also builds all the modules.

Install current kernel source:

apt-get source linux-image-$(uname -r)
cd /usr/src/linux-$(uname -r)
cp /boot/config-$(uname -r) .
make menuconfig
... enable the device

then...
make modules
make modules_install
reboot

Some devices need to be added the module name to /etc/modules if it doesn't get automatically loaded.

Share:
12,529

Related videos on Youtube

Sebi
Author by

Sebi

Updated on September 18, 2022

Comments

  • Sebi
    Sebi over 1 year

    I need to modify the ELF loader's kernel implementation of an Ubuntu 14.04 distribution. Having downloaded the sources using:

    sudo apt-get source linux-image-$(uname -r)
    

    I ran the configuration script:

    make config
    

    in the root source tree. After a seemingly endless sequence of input requests, the script created the .config file needed to build the kernel(or a set of modules). The kernel version I am using is linux-3.13.0 and has the following source tree layout:

    $ ls 
    arch   COPYING  crypto         Documentation  dropped.txt  FileSystemMakefile  fs       init  Kbuild   kernel  MAINTAINERS  mm   README          samples  security   sound  ubuntu  virt
    block  CREDITS  debian.master  drivers        elf.dat      firmware            include  ipc   Kconfig  lib     Makefile     net  REPORTING-BUGS  scripts  shortcuts  tools  usr
    

    The ELF loader is located in /path/to/source/fs/binfmt_elf.c. Following this question,in order to compile an individual module it is sufficient to run

    make /path/to/module/directory 
    

    In this case that would be:

    make ./path/to/source/fs
    

    The compilation is quite lengthy; it takes about twenty minutes(on a virtual machine) and the output is written(by default) in the same directory in which the module is located. I've found the object files by running:

    find . -name "*.o"
    

    in /path/to/source/fs. Filtering by name the ELF loader can be located by running:

    find . -name "*elf*.o"
    

    In the current sources it is written(by default) in:

    /path/to/source/fs/binfmt_elf.o
    

    Having gone through this tutorial, I've noticed that kernel modules have the naming convention [module_name].ko in order to distinguish them from user space object files.

    My question is how can I insert the new(modified) ELF loader into the kernel given that the current ELF loader is present(as unloading it may prevent binaries from being executed)?

    Edit #1:

    Running lsmod gives:

    $ lsmod
    Module                  Size  Used by
    nls_utf8               12557  1 
    isofs                  39835  1 
    vboxsf                 39690  0 
    snd_intel8x0           38153  2 
    snd_ac97_codec        130285  1 snd_intel8x0
    ac97_bus               12730  1 snd_ac97_codec
    snd_pcm               102099  2 snd_ac97_codec,snd_intel8x0
    snd_page_alloc         18710  2 snd_intel8x0,snd_pcm
    snd_seq_midi           13324  0 
    snd_seq_midi_event     14899  1 snd_seq_midi
    rfcomm                 69160  0 
    snd_rawmidi            30144  1 snd_seq_midi
    bnep                   19624  2 
    bluetooth             391196  10 bnep,rfcomm
    snd_seq                61560  2 snd_seq_midi_event,snd_seq_midi
    snd_seq_device         14497  3 snd_seq,snd_rawmidi,snd_seq_midi
    snd_timer              29482  2 snd_pcm,snd_seq
    joydev                 17381  0 
    snd                    69238  12 snd_ac97_codec,snd_intel8x0,snd_timer,snd_pcm,snd_seq,snd_rawmidi,snd_seq_device,snd_seq_midi
    serio_raw              13462  0 
    vboxguest             248441  7 vboxsf
    i2c_piix4              22155  0 
    soundcore              12680  1 snd
    mac_hid                13205  0 
    parport_pc             32701  0 
    ppdev                  17671  0 
    vboxvideo              12658  0 
    drm                   303102  1 vboxvideo
    lp                     17759  0 
    parport                42348  3 lp,ppdev,parport_pc
    hid_generic            12548  0 
    usbhid                 52570  0 
    hid                   106148  2 hid_generic,usbhid
    psmouse               106678  0 
    ahci                   25819  2 
    libahci                32560  1 ahci
    e1000                 145174  0 
    

    Which module needs to be compiled as a LKM in order to include the ELF loader. By default the loader is built into the base kernel.

    • hookenz
      hookenz about 9 years
      If you did a make modules_install or similar you should be able to use modprobe. To just insert the running module file try insmod filename.
    • Sebi
      Sebi about 9 years
      At this point I don't know where the module is actually located in /fs. I have a set of object files that don't resemble a module entry point(each object file has the same name as the source file from which it was generated). I wouldn't go about insmod()-ing each file individually.
    • Some Linux Nerd
      Some Linux Nerd about 9 years
      Are you SURE your problem requires futzing with the elf loader? You should try posting the original problem here as well.
    • hookenz
      hookenz about 9 years
    • Sebi
      Sebi about 9 years
      I need to modify the ELF loader in such a way that it should be able to load different .text sections based on a number of bytes located in the ELF header. This also means that the structure of the ELF files is modified.
  • Sebi
    Sebi about 9 years
    Where are the modules being written by default? With the above build command everything was stored in the same directory(/fs). I only want to build a single module fs(that includes the ELF loader). A whole kernel build takes half a day.
  • hookenz
    hookenz about 9 years
    Try the link above that links to askubuntu
  • Sebi
    Sebi about 9 years
    I am making the modules now. In the above link, the actual module was easily identifiable(ft1000). The problem in my case is that I am not sure what modules not to include into the base kernel build and simply compile them as LKMs(they must include the ELF loader, but at the same time an ELF loader is needed all the time).
  • TafT
    TafT over 7 years
    Half a day might happen if you forget to replace make with make --jobs=$(grep -c ^processor /proc/cpuinfo) --max-load=$(grep -c ^processor /proc/cpuinfo) or something better. I find *1.3 for jobs and *1.5 for load is about optimum but it varies a lot by system. I have a nice generate-makeopts script on my path calculates it and prints out something like "--jobs=10 --max-load=12" with an alias for make=make generate-makeopts. First time I compile as other users I always wonder why its taking so long.