Skip to content

OzMaatuk/LinuxKernelWorks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Linux Kernel Development Guide

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.

Prerequisites

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 grub2

Configure 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-password

Clone the Linux Kernel

Clone 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_mainline

Copy the current kernel configuration:

cp /boot/config-$(uname -r) .config

Update the configuration:

make oldconfig

Build and Install the Kernel

Build the kernel:

make -j$(nproc) all

Install the kernel:

sudo make modules_install install

Check the Kernel Messages

You 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_info

Configure GRUB

Uncomment the GRUB_TIMEOUT line in /etc/default/grub and set it to 10:

GRUB_TIMEOUT=10

Comment out the GRUB_TIMEOUT_STYLE line:

#GRUB_TIMEOUT_STYLE=hidden

Add the earlyprintk=vga option to the GRUB_CMDLINE_LINUX line:

GRUB_CMDLINE_LINUX="earlyprintk=vga"

Update GRUB:

sudo update-grub

Add a New Module

Create a new directory for your module:

mkdir linux_mainline/custom_module
cd linux_mainline/custom_module

In 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 clean

Create a Kconfig file:

menuconfig CUSTOM_CONFIG
        tristate "Linux custom module"
		default y

Now, in the module directory, you can build and install the module:

make all
make install

In 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/directory

Load the module:

sudo modprobe -f custom_module

Check the loaded modules and kernel messages:

lsmod | head
dmesg | tail

Add 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:

Adding a New Module to Kernel Configuration

Follow these steps to add a new module to the kernel configuration:

  1. Edit the .config File: In the kernel top directory, open the .config file and add the following line:
CONFIG_CUSTOM_MODULE=y

This line tells the kernel to include your custom module as a built-in module.

  1. Edit the Kconfig File: In the same directory, open the Kconfig file 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.

  1. Edit the Makefile: Open the Makefile in 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.

  1. 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_install

The 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.

  1. 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-grub

Edit an Existing Module

Choose a module from the list of loaded modules:

lsmod

In 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/directory

Compile and install the whole kernel with the edited module:

make
sudo make modules_install install

Additional Notes

Here's a revised version of your notes with some additional explanations and Markdown formatting for clarity:

Module Directory Structure

  • 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 Kconfig and Makefile in all parent directories of the final level module directory.

Module Reloading

  • If the module did not reload as expected, you can manually stop it with the rmmod command:
sudo rmmod <module_name>
  • After stopping the module, you can start it again with either the modprobe or insmod command:
sudo modprobe <module_name>

or

sudo insmod <path_to_module_object_file>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages