This guide provides step-by-step instructions for Linux kernel development, including installing the necessary tools, cloning the Linux kernel, adding a custom module, and modifying an existing module.
Install the necessary tools with the following command:
sudo apt-get install build-essential vim git git-email cscope libncurses-dev libssl-dev bison flex git-email pahole util-linux kmod e2fsprogs jfsutils reiserfsprogs xfsprogs squashfs-tools btrfs-progs pcmciautils quota ppp mount procps udev iptables openssl libssl-dev bc cpio tar libelf-dev grub2Configure your Git settings:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
git config --global format.signoff true
git config --global core.editor vim
git config --global sendemail.smtpserver smtp.gmail.com
git config --global sendemail.smtpserverport 587
git config --global sendemail.smtpencryption tls
git config --global sendemail.smtpuser your-smtp-username
git config --global sendemail.smtppass your-smtp-passwordClone the Linux kernel from the mainline branch:
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git linux_mainline
cd linux_mainlineCopy the current kernel configuration:
cp /boot/config-$(uname -r) .configUpdate the configuration:
make oldconfigBuild the kernel:
make -j$(nproc) allInstall the kernel:
sudo make modules_install installYou can check the kernel messages using the dmesg command:
dmesg -t > dmesg_current
dmesg -t -k > dmesg_kernel
dmesg -t -l emerg > dmesg_current_emerg
dmesg -t -l alert > dmesg_current_alert
dmesg -t -l crit > dmesg_current_crit
dmesg -t -l err > dmesg_current_err
dmesg -t -l warn > dmesg_current_warn
dmesg -t -l info > dmesg_current_infoUncomment the GRUB_TIMEOUT line in /etc/default/grub and set it to 10:
GRUB_TIMEOUT=10Comment out the GRUB_TIMEOUT_STYLE line:
#GRUB_TIMEOUT_STYLE=hiddenAdd the earlyprintk=vga option to the GRUB_CMDLINE_LINUX line:
GRUB_CMDLINE_LINUX="earlyprintk=vga"Update GRUB:
sudo update-grubCreate a new directory for your module:
mkdir linux_mainline/custom_module
cd linux_mainline/custom_moduleIn the new module directory, create a code file named custom_module.c:
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_start(void)
{
printk(KERN_INFO "Loading custom module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);Create a Makefile:
obj-$(CONFIG_CUSTOM_MODULE) = custom_module.o
KVERSION = $(shell uname -r)
all:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
install: all
make -C /lib/modules/$(KVERSION)/build M=$(PWD) install_modules
clean:
make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
.PHONY: all install cleanCreate a Kconfig file:
menuconfig CUSTOM_CONFIG
tristate "Linux custom module"
default yNow, in the module directory, you can build and install the module:
make all
make installIn the kernel top directory, you can build and install the module:
make M=path/to/your/module/directory
sudo make modules_install M=path/to/your/module/directoryLoad the module:
sudo modprobe -f custom_moduleCheck the loaded modules and kernel messages:
lsmod | head
dmesg | tailAdd the module to the kernel configuration. In the kernel top directory, edit the .config file by adding the CONFIG_CUSTOM_MODULE=y line.
Edit the Kconfig file by adding the source "custom_module/Kconfig" line.
Sure, here's a revised version of your instructions with additional explanations and Markdown formatting:
Follow these steps to add a new module to the kernel configuration:
- Edit the .config File: In the kernel top directory, open the
.configfile and add the following line:
CONFIG_CUSTOM_MODULE=yThis line tells the kernel to include your custom module as a built-in module.
- Edit the Kconfig File: In the same directory, open the
Kconfigfile and add the following line:
source "custom_module/Kconfig"This line tells the kernel build system to include the Kconfig file from your custom module's directory.
- Edit the Makefile: Open the
Makefilein the same directory and add the following line:
obj-$(CONFIG_CUSTOM_MODULE) += custom_module/This line tells the kernel build system to descend into your custom module's directory when building the kernel.
- Rebuild the Kernel: After making these changes, you need to rebuild the kernel for the changes to take effect. You can do this with the following commands:
make oldconfig
make -j$(nproc) all
sudo make modules_installThe make oldconfig command updates the kernel configuration with your changes. The make -j$(nproc) all command rebuilds the kernel, and the sudo make modules_install command installs the new kernel modules.
- Update GRUB: Finally, you might need to update GRUB to include the new kernel at boot time. You can do this with the following command:
sudo update-grubChoose a module from the list of loaded modules:
lsmodIn the kernel top directory, find the module's directory by looking at the global Makefile:
git grep <module_name> '*Makefile'Edit the related .c file of the .o file mentioned in the Makefile.
In the kernel top directory, compile and install the module:
make M=path/to/module/directory
sudo make modules_install M=path/to/module/directoryCompile and install the whole kernel with the edited module:
make
sudo make modules_install installHere's a revised version of your notes with some additional explanations and Markdown formatting for clarity:
- The new module in this guide is located directly under the kernel directory, which is not common. Most modules are located in at least a second-level subdirectory within the kernel directory.
- This structure adds additional requirements for configuring the new module. These additional steps mostly involve editing the
KconfigandMakefilein all parent directories of the final level module directory.
- If the module did not reload as expected, you can manually stop it with the
rmmodcommand:
sudo rmmod <module_name>- After stopping the module, you can start it again with either the
modprobeorinsmodcommand:
sudo modprobe <module_name>or
sudo insmod <path_to_module_object_file>