kernel_module_learn/README.md

91 lines
2 KiB
Markdown
Raw Normal View History

2025-02-08 17:01:11 +01:00
## Setup env
On NixOS:
- Ensure Linux Kernel is the same as you configuration.
2025-02-17 21:01:32 +01:00
Use this command to update nix inputs
```bash
nix flake update --override-input nixpkgs "github:NixOS/nixpkgs/$(nixos-version --revision)"
```
2025-02-08 17:01:11 +01:00
- Use direnv allow or nix develop to setup shell.
On other distros:
2025-02-10 13:44:44 +01:00
1. With direnv, in .envrc, change line by `use flake .#other`
2025-02-10 13:44:44 +01:00
2. With nix only, use `nix develop .#other`
3. Otherwise, you need to setup LINUX_MODULES_FOLDER to linux modules folder of your distro (ex: `/lib/modules/$(uname -r)`) in your shell (ex: .bashrc)
2025-02-08 17:01:11 +01:00
## make : targets list
2025-02-10 13:44:44 +01:00
- all : Compiling kernel (by default)
2025-02-08 17:01:11 +01:00
- clean : Cleaning build folder
All subfolder is configured to use LINUX_MODULES_FOLDER env variable set by flake develop
## How test module
Step 1: Load module
```bash
sudo insmod [module_name].ko
```
2025-02-10 13:44:44 +01:00
**Warning**: Can fail if secure boot is enabled (see notes about secure boot in below)
2025-02-08 17:01:11 +01:00
Step 2: Check logs
```bash
sudo dmesg | tail
```
Step 3: Unload module
```bash
sudo rmmod [module_name].ko
2025-02-08 17:33:19 +01:00
```
## Usefull links
- https://www.kernel.org/doc/html/latest/
- https://elixir.bootlin.com/linux/v6.13/source/
2025-02-10 13:47:33 +01:00
- https://static.lwn.net/images/pdf/LDD3/ch01.pdf
## Notes
2025-02-10 13:46:25 +01:00
### Find required headers files
```
nix shell nixpkgs#bear
cd 01_basic_module/
bear --append --output ../.vscode/compile_commands.json -- make -C $LINUX_MODULES_FOLDER/build M=$PWD modules
```
2025-02-10 13:26:47 +01:00
Search -I args and -D args
### Sign kernel module for testing with secure boot enabled
2025-02-10 13:44:44 +01:00
Prerequisites: Own secure boot keys configured.
2025-02-10 13:26:47 +01:00
Secure boot keys can be found in :
2025-02-10 13:44:44 +01:00
- Fedora : `/etc/pki/akmods`
2025-02-10 13:26:47 +01:00
- With sbctl : `/var/lib/sbctl`
```
sign-file sha256 $SECUREBOOT_KEYS_PATH/private_key.priv $SECUREBOOT_KEYS_PATH/public_key.der <module_file>.ko
```
2025-02-10 13:44:44 +01:00
sign-file executable is in `/usr/src/kernels/$(uname -r)/scripts`
2025-02-10 13:26:47 +01:00
Full example (run as root):
1. Fedora
```bash
/usr/src/kernels/$(uname -r)/scripts/sign-file sha256 /etc/pki/akmods/private/private_key.priv /etc/pki/akmods/certs/public_key.der <module_file>.ko
```