These instructions will guide you through compiling, installing, and managing the ai_kernel_driver kernel module. You will also learn how to load and unload the driver, as well as how to clean up build artifacts.
- Prerequisites
- Step 1: Compile the Kernel Module
- Step 2: Install (Load) the Kernel Module
- Step 3: Verify the Module Installation
- Step 4: Interact with the Device Node
- Step 5: Uninstall (Unload) the Kernel Module
- Step 6: Clean Build Artifacts
- Testing
-
Linux Environment: A Linux distribution with kernel headers installed.
-
Root or Sudo Access: Required for installing and removing kernel modules.
-
Kernel Headers: Ensure kernel headers matching your current kernel version are installed.
-
Basic Knowledge: Familiarity with compiling kernel modules and using
make. -
Installation of
gcc-12: Make suregcc-12is installed. You can install it by running:-
For Ubuntu/Debian:
sudo apt update sudo apt install gcc-12 g++-12
-
For Fedora/RHEL:
sudo dnf install gcc-12 g++-12
Verify the installation:
gcc --version
-
-
Installation of
make: Ensuremakeis installed, which is required for building kernel modules. Install it using:-
For Ubuntu/Debian:
sudo apt install build-essential
-
For Fedora/RHEL:
sudo dnf groupinstall "Development Tools"
Verify the installation:
make --version
-
-
Code modification for newer kernels: If you are using a newer version of the Linux kernel, you need to modify the
class_createfunction call in your code:-
Original code (causing the error):
ai_dev.class = class_create(THIS_MODULE, CLASS_NAME);
-
Updated code (for newer kernel versions):
ai_dev.class = class_create(CLASS_NAME);
You can check your kernel version by running:
uname -r
-
-
Open a terminal in the directory containing
ai_kernel_driver.c,Makefile, andmanage_ai_kernel_driver.sh. -
Ensure that the
Makefileis correctly configured. -
Compile the kernel module using the provided
makecommand:make
- This command will invoke the kernel build system to compile the module.
-
If the compilation is successful, an object file
ai_kernel_driver.kowill be created.
-
Ensure the
manage_ai_kernel_driver.shscript is executable:chmod +x manage_ai_kernel_driver.sh
-
Use the script to install the module:
sudo ./manage_ai_kernel_driver.sh install
-
This script will:
- Build the module (if not already built).
- Insert the module into the kernel using
insmod. - Create the device node
/dev/ai_driver.
-
-
If prompted, enter your password for
sudoprivileges.
-
Check if the Module is Loaded:
lsmod | grep ai_kernel_driver- This should display a line indicating that
ai_kernel_driveris loaded.
- This should display a line indicating that
-
Check for Device Node:
ls -l /dev/ai_driver
- Ensure that the device node
/dev/ai_driverexists with appropriate permissions.
- Ensure that the device node
-
View Kernel Messages:
dmesg | tail -n 20- Check the kernel messages for any errors or confirmation messages related to the driver.
You can now interact with the device node /dev/ai_driver using standard file operations or through a test application.
-
Read from the Device:
cat /dev/ai_driver
-
Write to the Device:
echo "Test Data" | sudo tee /dev/ai_driver
-
Use the Test Application:
- If you have a user-space application (e.g.,
ai_driver_test) that interacts with the driver, you can run it now.
sudo ./ai_driver_test
- If you have a user-space application (e.g.,
-
Use the
manage_ai_kernel_driver.shscript to remove the module:sudo ./manage_ai_kernel_driver.sh uninstall
-
This script will:
- Remove the device node
/dev/ai_driver. - Unload the kernel module using
rmmod.
- Remove the device node
-
-
Verify the Module is Unloaded:
lsmod | grep ai_kernel_driver- The module should no longer be listed.
-
To clean up the build artifacts, run:
make clean
-
This will remove files generated during the build process.
-
System Stability: Loading kernel modules can affect system stability. Ensure you understand the code before installing.
-
Kernel Version Compatibility: The module should be compiled against the headers of your currently running kernel.
-
Permissions: Running commands with
sudoprivileges can have significant effects on your system. Use with caution. -
Error Handling: Check
dmesgfor any error messages if something doesn't work as expected. -
Modify
NUM_THREADS: If applicable, adjust the number of threads in your test application based on your CPU cores. -
Dependencies: Ensure all dependencies and kernel headers are installed before compiling the module.