Add a `test_str` string parameter alongside the existing integer parameter (renamed from `test_parameter` to `test_int` for clarity) in the rust_minimal sample module.
The init function now prints both parameters to the kernel log, showing how string parameters are declared, defaulted, and read back via StringParam::as_cstr(). Also add module-level documentation showing usage via insmod and kernel command-line with dotted notation. Signed-off-by: Matthew Wood <[email protected]> --- samples/rust/rust_minimal.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs index 8eb9583571d7..59955e95e31a 100644 --- a/samples/rust/rust_minimal.rs +++ b/samples/rust/rust_minimal.rs @@ -1,6 +1,23 @@ // SPDX-License-Identifier: GPL-2.0 //! Rust minimal sample. +//! +//! This is a sample module written in Rust. It is intended to be a minimal +//! example of how to write a module in Rust. It does not do anything useful, +//! except print a message when it is loaded and unloaded. +//! +//! It provides examples of how to receive module parameters, which can be provided +//! by the user when the module is loaded: +//! +//! ``` +//! insmod /lib/modules/$(uname -r)/kernel/samples/rust/rust_minimal.ko test_int=2 test_str=world +//! ``` +//! +//! or via kernel cmdline with module dotted notation (when built-in and not built as a module): +//! +//! ``` +//! ... rust_minimal.test_int=2 rust_minimal.test_str=world ... +//! ``` use kernel::prelude::*; @@ -11,10 +28,14 @@ description: "Rust minimal sample", license: "GPL", params: { - test_parameter: i64 { + test_int: i64 { default: 1, description: "This parameter has a default of 1", }, + test_str: string { + default: "hello", + description: "This parameter has a default of hello", + } }, } @@ -26,9 +47,13 @@ impl kernel::Module for RustMinimal { fn init(_module: &'static ThisModule) -> Result<Self> { pr_info!("Rust minimal sample (init)\n"); pr_info!("Am I built-in? {}\n", !cfg!(MODULE)); + pr_info!("test_int: {}\n", *module_parameters::test_int.value()); pr_info!( - "test_parameter: {}\n", - *module_parameters::test_parameter.value() + "test_str: {}\n", + module_parameters::test_str + .value() + .as_cstr() + .expect("test_str has a default value") ); let mut numbers = KVec::new(); -- 2.52.0

