|
| 1 | +# write a basic kernel module |
| 2 | + |
| 3 | +## hello world |
| 4 | + |
| 5 | +Writing a Linux kernel module involves creating code that can be loaded into and unloaded from the kernel dynamically, without rebooting the system. Here’s a simple step-by-step guide to help you write a basic kernel module: |
| 6 | + |
| 7 | +### 1. Set Up Your Environment |
| 8 | + |
| 9 | +Make sure you have the Linux kernel headers installed and a suitable development environment ready. For Ubuntu or Debian, install them with: |
| 10 | + |
| 11 | +```bash |
| 12 | +sudo apt-get install linux-headers-$(uname -r) build-essential |
| 13 | +``` |
| 14 | + |
| 15 | +### 2. Write the Kernel Module Code |
| 16 | + |
| 17 | +Here’s an example of a very basic Linux kernel module: |
| 18 | + |
| 19 | +```c |
| 20 | +// hello.c: A simple Linux kernel module |
| 21 | +#include <linux/init.h> // Macros for module initialization |
| 22 | +#include <linux/module.h> // Core header for loading modules |
| 23 | +#include <linux/kernel.h> // Kernel logging macros |
| 24 | + |
| 25 | +// Function executed when the module is loaded |
| 26 | +static int __init hello_init(void) |
| 27 | +{ |
| 28 | + printk(KERN_INFO "Hello, world!\n"); |
| 29 | + return 0; // Return 0 if successful |
| 30 | +} |
| 31 | + |
| 32 | +// Function executed when the module is removed |
| 33 | +static void __exit hello_exit(void) |
| 34 | +{ |
| 35 | + printk(KERN_INFO "Goodbye, world!\n"); |
| 36 | +} |
| 37 | + |
| 38 | +// Macros to define the module’s init and exit points |
| 39 | +module_init(hello_init); |
| 40 | +module_exit(hello_exit); |
| 41 | + |
| 42 | +MODULE_LICENSE("GPL"); // License type (GPL) |
| 43 | +MODULE_AUTHOR("Your Name"); // Module author |
| 44 | +MODULE_DESCRIPTION("A simple module"); // Module description |
| 45 | +MODULE_VERSION("1.0"); // Module version |
| 46 | +``` |
| 47 | +
|
| 48 | +### 3. Create a Makefile |
| 49 | +
|
| 50 | +To compile the kernel module, you’ll need a `Makefile`. Here's a simple one: |
| 51 | +
|
| 52 | +```makefile |
| 53 | +obj-m += hello.o # hello.o is the target |
| 54 | +
|
| 55 | +all: |
| 56 | + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules |
| 57 | +
|
| 58 | +clean: |
| 59 | + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean |
| 60 | +``` |
| 61 | + |
| 62 | +### 4. Compile the Module |
| 63 | + |
| 64 | +Run the following command in the directory where your `hello.c` and `Makefile` are located: |
| 65 | + |
| 66 | +```bash |
| 67 | +make |
| 68 | +``` |
| 69 | + |
| 70 | +This will generate a file called `hello.ko`, which is the compiled kernel module. |
| 71 | + |
| 72 | +### 5. Load the Module |
| 73 | + |
| 74 | +To insert the module into the kernel, use `insmod`: |
| 75 | + |
| 76 | +```bash |
| 77 | +sudo insmod hello.ko |
| 78 | +``` |
| 79 | + |
| 80 | +### 6. Check the Logs |
| 81 | + |
| 82 | +To see the output from the `printk` statements, use the `dmesg` command: |
| 83 | + |
| 84 | +```bash |
| 85 | +dmesg | tail |
| 86 | +``` |
| 87 | + |
| 88 | +You should see something like: |
| 89 | + |
| 90 | +```txt |
| 91 | +[ 1234.5678] Hello, world! |
| 92 | +``` |
| 93 | + |
| 94 | +### 7. Remove the Module |
| 95 | + |
| 96 | +To unload the module, use `rmmod`: |
| 97 | + |
| 98 | +```bash |
| 99 | +sudo rmmod hello |
| 100 | +``` |
| 101 | + |
| 102 | +Again, check the logs using `dmesg`: |
| 103 | + |
| 104 | +```bash |
| 105 | +sudo dmesg | tail |
| 106 | +``` |
| 107 | + |
| 108 | +You should see: |
| 109 | + |
| 110 | +```txt |
| 111 | +[ 1234.9876] Goodbye, world! |
| 112 | +``` |
| 113 | + |
| 114 | +### 8. Clean Up |
| 115 | + |
| 116 | +To clean up the build files, run: |
| 117 | + |
| 118 | +```bash |
| 119 | +make clean |
| 120 | +``` |
| 121 | + |
| 122 | +### Notes |
| 123 | + |
| 124 | +- **License**: The `MODULE_LICENSE("GPL")` ensures the module is GPL-compliant, which allows it to use symbols (functions) exported by the kernel. |
| 125 | +- **Debugging**: Use `printk` for logging within the module. It behaves similarly to `printf` but is designed for kernel space. |
| 126 | +- **Module Parameters**: You can add parameters to modules using `module_param()` to pass arguments when the module is loaded. |
| 127 | + |
| 128 | +### Next Steps |
| 129 | + |
| 130 | +Once you are familiar with this basic example, you can explore: |
| 131 | + |
| 132 | +- Writing more advanced modules that interact with hardware or the filesystem. |
| 133 | +- Using kernel-specific APIs like work queues, kthreads, or handling interrupts. |
| 134 | +- Diving into eBPF or loadable kernel module techniques for debugging and tracing kernel events. |
0 commit comments