This repo is for my own self-study about linux kernel driver programming. In the following sections, I record what I learned and what I did, and save some useful material for future references.
- For host environment setup: Compile toolchain from scratch
- For host environment setup: Download precompiled toolchain
- For driver programming: LKMPG 5.4
- For makefile: Makefile in linux
- For sysfs: KObject demessify
Whenever you trying to develop kernel module, make sure you are in a virtual machine. As you are programming in kernel space, wrong code might cause kernel crashing and you need to reboot your computer frequently.
First, download the vagrant
, libvirt
, and qemue
utilities from offical repository in your host computer
$ sudo apt install -y \
qemu-kvm \
libvirt-dev \
bridge-utils \
libvirt-daemon-system \
libvirt-daemon \
virtinst \
bridge-utils \
libosinfo-bin \
libguestfs-tools \
virt-top
Download libvirt box and boot up the virtual machine with the downloaded box. (You can first modify the provided Vagrantfile
to sync what directory you want to pass to the virtual machine)
$ vagrant up --provider=libvirt
Then you are good to login into the virtual machine and download essential tools for kernel development.
$ vagrant ssh
# Inside the virtual environment
$ sudo apt-get update
$ sudo apt-get install -y build-essential kmod
$ sudo apt-get install linux-headers-`uname -r`
In the top-level directory, the following command will compile all the modules in the sub-directories.
$ cd [TOP_DIR]
$ make
You can also go to each subdirectory to compile the module one-by-one
$ cd [SUB_DIR]
$ make
Find files with the specified pattern recursively in current directory
$ grep -rnw '.' -e '[PATTERN]'
Find directories containing specific file recursively in current directory
$ find . -type f -name '[PATTERN]' | sed -r 's|/[^/]+$||' | sort | uniq
bcc
A collection of eBPF toolkits to used (e.g. trace kernel stack)ftrace
Native kernel tracing tool to trace kernel event or available kernel functions.