Skip to content

Latest commit

 

History

History
91 lines (81 loc) · 2.65 KB

README.md

File metadata and controls

91 lines (81 loc) · 2.65 KB

1: Our First Module

Let's create our first module and see how to use it.

New Files

Be sure to check the following files in this directory:

Makefile

Everything should be clear here. We are actually calling makefile from our linux headers directory.

module_one.c

We've got a couple of things going on here. (check .c code for ordering)

  • imports - most of the headers have macro definitions and enum constants in them
  • Some module information we'll check later with modinfo command
  • name and somenumber will be our params (argument variables)
  • Two functions are defined which we'll register with module_init and module_exit

What does all of this do ???

Compile the module using

 $ sudo make

list everything in directory with ls and you should see new files created. ' .ko ' file is what we are interested in.

 $ modinfo ./module_one.ko

You should see the information we've used in MODULE macros in our .c file.

Let's do a brief walkthrough of what we did. Use lsmod to list all currently loaded modules:

 $ lsmod

Let's insert our module using insmod:

 $ sudo insmod module_one.ko

If we use lsmod now we should see module_one on the list.

 $ lsmod | grep module_one

Open another terminal and cd to '/sys/module' directory

 $ cd /sys/module
 $ ls

You should see module_one directory. Let's enter it and list the files and it's contents:

 $ cd module_one
 $ ls
 $ cat version

You can also find some information in '/proc/modules' file.

 $ cat /proc/modules

Return to our main terminal and remove module.

 $ sudo rmmod module_one

Try repeating all we did but using module params this time:

 $ sudo insmod module_one.ko name="SOME NAME" somenumber=1230
 $ sudo rmmod module_one

Once you've done that check '/var/log/kern.log' for log information.

 $ nano /var/log/kern.log

What about rebooting? Does our module stay loaded? NO IT DOES NOT.

mkdir /lib/modules/$(uname -r)/custom_modules
sudo cp module_one.ko /lib/modules/$(uname -r)/custom_modules
echo "module_one" | sudo tee -a /etc/modules
sudo depmod -a
sudo modprobe module_one

What about now? Try finding out what we did line by line.

Be sure to notice ...

We are using static keywords on variables and functions. This is needed to restrict the scope to within the module.

All those macros are there so you can use them.

Previous Next
0. Preparation 2. Devices