Skip to content

Latest commit

 

History

History
84 lines (59 loc) · 2.42 KB

README.md

File metadata and controls

84 lines (59 loc) · 2.42 KB

Rust Loadable Kernel Module Template

Setup

Note that you need to have a rust enabled kernel to use this template. Follow the instructions on the link or see one of the references.

The default paths for scripts in this template are:

  • KERNEL: ../linux
  • BUSYBOX: ../busybox

So, you should have a KERNEL dir with a rust enabled kernel already built and a BUSYBOX dir with a ALREADY built and configured busybox.

For automating the setup of the environment, run the setup script:

./scripts/setup

Refer to the script for more info.

Building

I wrote a simple wrapper script for the make command for compiling this LKM. It is located in the scripts directory. You can use it like this:

./scripts/build
./scripts/build -t clean
./scripts/build -t rust-analyzer
./scripts/build -k /path/to/rust/enabled/kernel -b /path/to/busybox
# ... and so on

Please refer to the kernel docs for more info on basic Kbuild usage.

There's also rust specific targets. For example, you can build the rust-analyzer project configuration with:

./scripts/build -t rust-analyzer

Note that we have a bug in the current mainline kernel that prevents rust-analyzer from working properly with LKMs. You can find a patch for it here.

Running

First, build the LKM. Then, you can use the run script to run it in a qemu VM:

# Default locations: KERNEL=../linux, BUSYBOX=../busybox
./scripts/run
# Custom locations
./scripts/run -k /path/to/rust/enabled/kernel -b /path/to/busybox

This script will simply copy all the .ko files to the initramfs and run the VM.

Hot-reloading

You can use the hot-reload script to hot-reload the LKM in the running VM:

./scripts/hot-reload

It uses the cargo-watch crate to watch for changes in the source code and then running ./scripts/build to build the LKM and ./scripts/run to run it in the VM. Please refer to the script for more info.

References