In this exercise you will inspect a "Hello world" kernel module written in Rust. It writes a message to the system log when it is loaded. You will extend it to also print a message when the module is unloaded.
- Connect to your VM using ssh.
Start tmux so that connection issues do not prevent you from being able to get back to your shell:
tmux -u attach -d || tmux -u
Build the module with make
:
make
Inspect Makefile
, the only difference from the C version is that we have LLVM=1
to use the LLVM toolchain instead of gcc
.
Load the module using insmod
:
sudo insmod hello.ko
Check for messages from the module using dmesg
:
sudo dmesg
You should see a messages as follows:
[ 2736.667232] hello: Hello world
Remove the module using rmmod
:
sudo rmmod hello
Add the following code to hello.rs
:
impl Drop for Hello {
fn drop(&mut self) {
pr_info!("Goodbye world!\n");
}
}
Repeat steps 3 to 6 above. After step 6, check the log again:
sudo dmesg
You should see a message as follows:
[ 2742.305703] hello: Goodbye world!
In this exercise, you got familiar with building kernel modules in Rust, loading and unloading them, and inspecting the system log for messages.