Hello World Kernel Module
Writing your first kernel module is a fundamental step in understanding how device drivers interact with the Linux kernel. A typical starting point is the classic Hello World kernel module, which demonstrates how to insert code into the kernel safely, perform simple logging, and remove the module cleanly. This minimal example will help you become familiar with module structure, kernel logging, and the basic workflow of compiling and loading a module.
hello_world.c
123456789101112131415161718192021#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_INFO "Hello, kernel world!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye, kernel world!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple Hello World kernel module");
The key to communicating from within kernel modules is the printk function, which is similar to printf in user space but designed for kernel logging. The kernel module above defines two special functions: one marked with __init, which runs when the module is loaded, and one with __exit, which runs when the module is removed. The module_init and module_exit macros register these functions with the kernel. The printk function is used here with the KERN_INFO log level to print informational messages to the kernel log, which you can view using dmesg after loading or unloading the module. The module metadata macros at the end provide information about the module to the kernel.
hello_warn.c
12345678910111213141516171819#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_WARNING "This is a warning from the kernel module!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_WARNING "Kernel module exit with a warning!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL");
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about the `printk` function and its log levels?
How do the `__init` and `__exit` function attributes work in kernel modules?
What is the purpose of the module metadata macros?
Awesome!
Completion rate improved to 3.85
Hello World Kernel Module
Swipe to show menu
Writing your first kernel module is a fundamental step in understanding how device drivers interact with the Linux kernel. A typical starting point is the classic Hello World kernel module, which demonstrates how to insert code into the kernel safely, perform simple logging, and remove the module cleanly. This minimal example will help you become familiar with module structure, kernel logging, and the basic workflow of compiling and loading a module.
hello_world.c
123456789101112131415161718192021#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_INFO "Hello, kernel world!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye, kernel world!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple Hello World kernel module");
The key to communicating from within kernel modules is the printk function, which is similar to printf in user space but designed for kernel logging. The kernel module above defines two special functions: one marked with __init, which runs when the module is loaded, and one with __exit, which runs when the module is removed. The module_init and module_exit macros register these functions with the kernel. The printk function is used here with the KERN_INFO log level to print informational messages to the kernel log, which you can view using dmesg after loading or unloading the module. The module metadata macros at the end provide information about the module to the kernel.
hello_warn.c
12345678910111213141516171819#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int __init hello_init(void) { printk(KERN_WARNING "This is a warning from the kernel module!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_WARNING "Kernel module exit with a warning!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL");
Thanks for your feedback!