1

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.

thedreamer
  • 319
  • 1
  • 5
  • 13
  • 1
    Installing software is usually performed by `make install` command. Just create "install" target in your makefile and place all needed actions in its recipes. – Tsyvarev Nov 02 '15 at 07:17
  • Yes, and call `make … modules_install` from it. – 0andriy Nov 02 '15 at 12:26
  • Any command that you can run in the command shell, you can add that action as rules for a target in makefile. – cm161 Nov 03 '15 at 12:59
  • @Tsyvarev : I tried but seems I am doing something wrong - I have updated the post with relevant information – thedreamer Nov 04 '15 at 21:55
  • @AndyShevchenko - I tried can u see what I am doing wrong – thedreamer Nov 04 '15 at 21:55
  • @cm161 : Thanks, but there is associated grammer with Makefile which is causing problem in getting it to work – thedreamer Nov 04 '15 at 21:56
  • `I dont see any modules in the build path` - you cannot find your modules under `/home/xyz/Desktop/Drivers/symbols`? According to your output of `make install` they should exist. As for installed path, it is `/lib/modules/2.6.32-5-amd64/extra/`. BTW, you post makefile with very strange definition of `INSTALL_MOD_PATH` variable (`$` sign in definition string) and receipt of `install` target (`=/build`). – Tsyvarev Nov 05 '15 at 07:16
  • @Tsyvarev - sorry for using wrong terminology with line I dont see any modules - I guess a newbie sign .. what I meant was after doing make install I expected them to be present after I do lsmod - I did see .ko files in my folder - however with steps pointed by cm161 - i was able to cross the hurdle - thanks for all the pointers - appreciate it !!! – thedreamer Nov 05 '15 at 08:36

2 Answers2

1
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
cm161
  • 492
  • 3
  • 6
  • Thanks for the answer - there was small change that I needed to make instead of modules_install - i had to change it to modules ..but with your steps pointed out I understand things better .. thanks a lot for the answer – thedreamer Nov 05 '15 at 08:34
1

Enhancing the answer posted by @cm161 for future users to highlight exact steps which worked for me

With below Makefile use following steps

Step 1: make ( type just make command) - for creation of modules i.e. .ko files and associated files

Step 2: make install

Step 3 : now do lsmod you should be able to see new modules

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
    cp wakeup_counter.ko /lib/modules/2.6.32-5-amd64/
    cp thread_module.ko /lib/modules/2.6.32-5-amd64/

install:
    modprobe wakeup_counter
    modprobe thread_module

clean:
    make -C $(KDIR) M=$(PWD) clean
thedreamer
  • 319
  • 1
  • 5
  • 13
  • You can automate copying of .ko files through the makefile by adding 'cp' rules for 'all' target as follows: all:make ....cp wakeup_counter.ko $(DEST_DIR)/.cp thread_module.ko $(DEST_DIR)/. where DEST_DIR = /lib/modules/2.6.32-5-amd64 – cm161 Nov 05 '15 at 08:54