A simple Linux kernel module that implements a character device driver with a corresponding test program.
.
├── Makefile # Build configuration
├── README.md # This file
├── src/ # Source files
│ └── mychardev.c # Kernel module source code
├── test/ # Test files
│ └── test_mychardev.c # User-space test program
└── build/ # Build artifacts (generated)
├── mychardev.ko # Compiled kernel module
├── test_mychardev # Test executable
└── ... # Other build files
- Linux development environment
- Kernel headers for your running kernel
- GCC compiler
- Make utility
On Ubuntu/Debian:
sudo apt update
sudo apt install build-essential linux-headers-$(uname -r)On CentOS/RHEL/Fedora:
sudo yum groupinstall "Development Tools"
sudo yum install kernel-devel-$(uname -r)makeThis builds both the kernel module and the test program.
make modulemake testmake cleanmake install
# or manually:
sudo insmod build/mychardev.komake info
# or manually:
modinfo build/mychardev.kolsmod | grep mychardevsudo ./build/test_mychardevmake remove
# or manually:
sudo rmmod mychardev| Target | Description |
|---|---|
all |
Build both kernel module and test program (default) |
module |
Build only the kernel module |
test |
Build only the test program |
clean |
Remove all build artifacts |
install |
Load the kernel module |
remove |
Unload the kernel module |
info |
Display module information |
All build artifacts are placed in the build/ directory:
mychardev.ko- The compiled kernel moduletest_mychardev- The test executable*.o,*.mod*- Intermediate object filesModule.symvers,modules.order- Kernel build metadata
The source directories (src/ and test/) remain clean and contain only source code.
- Edit source files in
src/ortest/ - Build with
make - Test by loading the module and running the test program
- Debug using kernel logs:
dmesg | tail - Clean up with
make cleanwhen needed
- Ensure kernel headers are installed for your running kernel
- Check that you have sufficient permissions
- Verify GCC and make are installed
- Check
dmesgfor kernel messages - Ensure the module isn't already loaded:
lsmod | grep mychardev - Verify you're running as root/sudo for module operations
- Module loading/unloading requires root privileges
- Use
sudoformake install,make remove, and running test programs
# Build everything
make
# Load the module
make install
# Get hold of the major number that the module was registered with
sudo dmesg | tail -5
sudo mknod /dev/mychardev c <major_number> 0
# Check it's loaded
lsmod | grep mychardev
# Run the test
sudo ./build/test_mychardev
# Check kernel logs
dmesg | tail
# Unload the module
make remove
# Clean up build files
make clean- The kernel module creates a character device that can be accessed from user space
- The test program demonstrates how to interact with the device
- All build artifacts are isolated in the
build/directory - The project uses the Linux kernel build system (kbuild) for proper module compilation