makefilekernellinux-device-driverkernel-moduleinsmod

can we install kernel module with make file


I have working kernel module which I install manually with insmod/modprobe command as learnt by reading book. however I was wondering if there is way to do it automatically after compiling - So basically how to automate insmod/modprobe command ?

My modprobe has a dependent file thread_module.o as well

My make file so far

obj-m := wakeup_counter.o
obj-m += thread_module.o
$INSTALL_MOD_PATH = /lib/modules/2.6.32-5-amd64/

all:
    make -C /lib/modules/2.6.32-5-amd64/build M=$(PWD) modules

install:
    make $(INSTALL_MOD_PATH) =/build modules_install
clean:
    make -C /lib/modules/2.6.32-5-amd64/build M=$(PWD) modules

output after running : make install

root@xyz:/home/xyz/Desktop/Drivers/symbols# make install
make -C /lib/modules/2.6.32-5-amd64/build M=/home/xyz/Desktop/Drivers/symbols modules_install
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-5-amd64'
  INSTALL /home/xyz/Desktop/Drivers/symbols/thread_module.ko
  INSTALL /home/xyz/Desktop/Drivers/symbols/wakeup_counter.ko
  DEPMOD  2.6.32-5-amd64
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-5-amd64'

Edit: After going through comments and https://www.kernel.org/doc/Documentation/kbuild/modules.txt I tried to add install command but I dont see any modules in the build path - Also at high level I get what we write in cmd prompt we type in Makefile but if someone can give an example it would help me to understand with nice base case to refer.


Solution

  • obj-m := wakeup_counter.o
    obj-m += thread_module.o
    
    KDIR = /lib/modules/2.6.32-5-amd64/build
    
    all:
        make -C $(KDIR) M=$(PWD) modules_install
    
    clean:
        make -C $(KDIR) M=$(PWD) clean
    

    Example of command shell instruction being used as rule in Makefile:

    install:
        modprobe wakeup_counter
        modprobe thread_module