Skip to content

Commit

Permalink
Modify kernel-dev tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
FrantisekKrenzelok committed Mar 12, 2024
1 parent b164379 commit 5ac577a
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions _posts/2024-03-12-linux-kernel-module-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ tags:
---
Looking to dip your toes into Linux kernel module development? Well, you're in for a treat! We've got a no-frills, step-by-step guide that'll take you from zero to hero in no time. In this blog, we'll walk you through setting up your local development environment, using qemu for safe testing, and the optional (but highly recommended) integration of gdb for debugging. Perfect for beginners, this guide will simplify the seemingly complex world of kernel development into an approachable and manageable process. Ready to get started?

# Abstract
This guide provides a comprehensive setup for Linux kernel module development. It enables development on a local machine and testing on a virtual one, protecting the development system from potential kernel panics. Using a Virtual Machine (VM) also facilitates system-wide debugging with gdb.

# Outcome
- Setup local development
- A **qemu** VM for safe testing
Expand All @@ -31,7 +28,8 @@ _This is by no means a definitive guide or a best solution, yet it is one that h
File names used in this document are relative. Following the naming conventions provided will ease the referencing process but isn't mandatory.

# Set-up
--
---

## fedora dependencies

``` shell
Expand All @@ -49,8 +47,8 @@ menuconfig for modifying the kernel configuration using gui menu
sudo dnf install ncurses-devel
```
## Qemu VM
_I have followed this fedora guide: https://developer.fedoraproject.org/tools/virtualization/installing-qemu-on-fedora-linux.html_
_Non-fedora users should be able to follow it._
>The following section is in based on the following fedora guide: (link)[https://developer.fedoraproject.org/tools/virtualization/installing-qemu-on-fedora-linux.html]
_Non-fedora users should also be able to follow it._
### Steps
1. Download a ISO, server flavor(no gui) is probably all you need depending on the module's purpose
2. Create the following folder structure and move the ISO file accordingly
Expand All @@ -59,13 +57,15 @@ _Non-fedora users should be able to follow it._
│   └── distro.iso
└── datadrct
~~~

3. Create a raw image for the VM, this is a "virtual" disk
``` shell
qemu-img create -f raw datadrct/image.raw 16G
```

Note: if you use non-minimal system flavor or will be using a lot of disk space, increase the size accordingly.
5. **Start** the VM and complete the installation. I have chosen the username to be `devel` which will be used in rest of this tutorial.
>_Note: if you use non-minimal system flavor or will be using a lot of disk space, increase the size accordingly._
4. **Start** the VM and complete the installation. I have chosen the username to be `devel` which will be used in rest of this tutorial.

``` shell
qemu-system-x86_64 \
Expand All @@ -92,33 +92,35 @@ Create a backup of the Image after a completing the system backup including the
With this you will run in graphics mode unless you add `-nographics`, You can use it for the VM maintenance.

``` shell
qemu-system-x86_64 -boot menu=on
-m 2048
-cpu max
-smp 4
-s
-drive file=datadrct/image.raw,format=raw
-accel kvm
-net nic
-net user,hostfwd=tcp::2323-:22
qemu-system-x86_64 \
-boot menu=on \
-m 2048 \
-cpu max \
-smp 4 \
-s \
-drive file=datadrct/image.raw,format=raw \
-accel kvm \
-net nic \
-net user,hostfwd=tcp::2323-:22
```
_You will need to run it at leas once to determine the root mount point e.g `/dev/sda3`. You can do so after running the VM by executing the `df` command, look for the `/` symbol in the `Mounted on` column and write it down._
### Running the VM (Development)
VM can access internet
We can SSH to the VM using localhost and a arbitrary port (2323)

``` shell
qemu-system-x86_64 -m 2048
-cpu max
-smp 4
-s
-kernel path/to/kernel/arch/x86_64/boot/bzImage
-drive file=datadrct/image.raw,format=raw
-accel kvm
-nographic
-append "root=/dev/sda3 nokaslr"
-net nic
-net user,hostfwd=tcp::2323-:22
qemu-system-x86_64 \
-m 2048 \
-cpu max \
-smp 4 \
-s \
-kernel path/to/kernel/arch/x86_64/boot/bzImage \
-drive file=datadrct/image.raw,format=raw \
-accel kvm \
-nographic \
-append "root=/dev/sda3 nokaslr" \
-net nic \
-net user,hostfwd=tcp::2323-:22
```
* `root=/dev/sda3` - specify the root partition based. see previous running option
* Feel free to exclude the `-nographic` option if you wish to have a separate gui window for the VM.
Expand Down Expand Up @@ -190,14 +192,15 @@ add `add-auto-load-safe-path /path/to/linux-build` to `~/.gdbinit`

# Development
---
>_This guide doesn't covert the actual module developement as there are enough reources on this toppic https://www.thegeekstuff.com/2013/07/write-linux-kernel-module/
## Rebuilding the kernel
If you modify the module files only then perform the following:

``` shell
make modules M=/path/to/module.ko
```
If you modify something outside of the module itself (e.g. some files in include, something in the kernel core..) then re[build](#build) the kernel again
>_If you modify something outside of the module itself (e.g. some files in include, something in the kernel core..) then re[build](#build) the kernel again_
## Provide the build to the VM system
The following will copy the module to the user's home directory.
Expand Down Expand Up @@ -229,7 +232,7 @@ _The system freezes upon connecting. Use `continue` to resume._

_Feel free to interrupt at any moment to enter __gdb__ commands by pressing `CTRL+C`_

[Load](##load-the-kernel-module-inside-the-vm) __the modules before the next step__
>_[Load](#load-the-kernel-module-inside-the-vm) the modules before the next step_
load kernel and module symbols

Expand All @@ -244,7 +247,7 @@ __continue__ as normal...
- Incrementally test your setup.
- Use version control like Git for your development.

# Sriptlets
# Scriptlets
_scripts you might find useful_

Mount the image, kill the running VM if needed
Expand All @@ -271,18 +274,20 @@ _replace `module` with the name of your module_

# Other development notes
## Working on VM
I have used Tmux ([viz.](https://github.com/tmux/tmux/wiki/Getting-Started))on my local machine as well as the VM for development.
If you are unfamiliar with it I encourage you to try for the VM.
You can use Tmux ([viz.](https://github.com/tmux/tmux/wiki/Getting-Started)) on a local machine as well as the VM for testing.
You are encouraged to try it even if you aren't familiar with it.

### Attaching remote (VM) Tmux session
First you have to create a Tmux session on the VM you would do so by running it at the system startup (adding `tmux` to startup script).

Then you attach from the local machine to the VM using:
```
ssh -p 2323 devel@localhost -t tmux attach-session
```


---
# Resources
https://docs.kernel.org/dev-tools/gdb-kernel-debugging.html
https://developer.fedoraproject.org/tools/virtualization/installing-qemu-on-fedora-linux.html
(https://docs.kernel.org/dev-tools/gdb-kernel-debugging.html)
(https://developer.fedoraproject.org/tools/virtualization/installing-qemu-on-fedora-linux.html)
(http://dalleau.re/post/qemu\_debugging/)
(https://nickdesaulniers.github.io/blog/2018/10/24/booting-a-custom-linux-kernel-in-qemu-and-debugging-it-with-gdb/)

0 comments on commit 5ac577a

Please sign in to comment.