# Linux Kernel Modules

## Lsmod

> Show the status of modules in the Linux Kernel

Let's look at two modules

```bash
user@workstation:~$ lsmod | grep button
button                 16384  1 i915
user@workstation:~$ lsmod | grep pcspkr
pcspkr                 16384  0
user@workstation:~$
```

**Challenge**

Find out where button and pcspkr are under:

* /lib/modules/
* Kernel Configuration
* Mainline

## Hello World Module Source Code

Make a "helloworld" directory

```bash
user@workstation:~$ mkdir helloworld
```

Create helloworld.c file under our helloworld directory and add the C code below, this is a simple Hello World Kernel Module

```bash
user@workstation:~/helloworld$ helloworld.c
```

```bash
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

static int module_init_function(void)

{
    printk(KERN_INFO "Module? Hello!\n");
    return 0;
}

static void module_exit_function(void)
{
    printk(KERN_INFO "Module? Bye!\n");
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("xe1gyq");
MODULE_DESCRIPTION("My First Linux Kernel Module");

module_init(module_init_function);
module_exit(module_exit_function);
```

## Hello World Module Makefile

Create the Makefile under helloworld directory and add the code below

```bash
user@workstation:~/helloworld$ nano Makefile
obj-m += helloworld.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
```

## Hello World Module Compilation

```
user@workstation:~/helloworld$ ls
helloworld.c  Makefile
user@workstation:~/helloworld$
```

Now compile your Hello World Module

```bash
user@workstation:~/helloworld$ make
make -C /lib/modules/4.12.0-kali2-amd64/build M=/home/user/helloworld modules
make[1]: Entering directory '/usr/src/linux-headers-4.12.0-kali2-amd64'
  CC [M]  /home/user/helloworld/helloworld.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/user/helloworld/helloworld.mod.o
  LD [M]  /home/user/helloworld/helloworld.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.12.0-kali2-amd64'
user@workstation:~/helloworld$
```

## Hello World Module Files

```bash
user@workstation:~/helloworld$ ls
helloworld.c  helloworld.ko  helloworld.mod.c  helloworld.mod.o  helloworld.o  Makefile  modules.order    Module.symvers
user@workstation:~/helloworld$
```

```bash
user@workstation:~/helloworld$ modinfo helloworld.ko
```

```bash
user@workstation:~/helloworld$ insmod helloworld.ko
```

```bash
user@workstation:~/helloworld$ dmesg
```

```bash
user@workstation:~/helloworld$ lsmod
```

```bash
user@workstation:~/helloworld$ cat /proc/modules
```

```bash
user@workstation:~/helloworld$ rmmod helloworld
```

```bash
user@workstation:~/helloworld$ dmesg
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://theiotlearninginitiative.gitbook.io/coba/linux-kernel-modules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
