From 4555c9871bc45e4ed681d822d9b25a300913d1a4 Mon Sep 17 00:00:00 2001 From: Matthew Yeazel Date: Tue, 3 Sep 2024 02:54:21 +0000 Subject: [PATCH] driverdog: add ability copy instead of link There are cases where modules could be included that are already linked but could be loaded by driverdog. This adds the ability to specify the command line argument copy-modules and improves the configuration format to allow configurations to either link or copy modules. Signed-off-by: Matthew Yeazel --- sources/driverdog/README.md | 5 + sources/driverdog/src/main.rs | 199 ++++++++++++++++++++--- sources/driverdog/src/tests/copying.conf | 11 ++ sources/driverdog/src/tests/linking.conf | 18 ++ 4 files changed, 214 insertions(+), 19 deletions(-) create mode 100644 sources/driverdog/src/tests/copying.conf create mode 100644 sources/driverdog/src/tests/linking.conf diff --git a/sources/driverdog/README.md b/sources/driverdog/README.md index d3031542a..181ee636e 100644 --- a/sources/driverdog/README.md +++ b/sources/driverdog/README.md @@ -9,6 +9,11 @@ driverdog is a tool to link kernel modules at runtime. It uses a toml configurat `object-files`: hash with the object files to be linked, each object in the map should include the files used to link it `kernel-modules`: hash with the kernel modules to be linked, each kernel module in the map should include the files used to link it +There are two modes for driverdog: link then load and copy then load. Link then load takes unlinked files found in `objects-source` +and matched in `object-files` and `kernel-modules` to link together these files then copy them to `lib-modules-path`. Copy then load +finds the modules specified in `kernel-modules` and copies them to `lib-modules-path` from the source specified in `copy-source`. Both +modes iterate over the `kernel-modules` and load them from that path with `modprobe`. + ## Colophon This text was generated from `README.tpl` using [cargo-readme](https://crates.io/crates/cargo-readme), and includes the rustdoc from `src/main.rs`. diff --git a/sources/driverdog/src/main.rs b/sources/driverdog/src/main.rs index dc1eaa26d..592c6e819 100644 --- a/sources/driverdog/src/main.rs +++ b/sources/driverdog/src/main.rs @@ -5,6 +5,11 @@ driverdog is a tool to link kernel modules at runtime. It uses a toml configurat `objects-source`: path where the objects used to link the kernel module are `object-files`: hash with the object files to be linked, each object in the map should include the files used to link it `kernel-modules`: hash with the kernel modules to be linked, each kernel module in the map should include the files used to link it + +There are two modes for driverdog: link then load and copy then load. Link then load takes unlinked files found in `objects-source` +and matched in `object-files` and `kernel-modules` to link together these files then copy them to `lib-modules-path`. Copy then load +finds the modules specified in `kernel-modules` and copies them to `lib-modules-path` from the source specified in `copy-source`. Both +modes iterate over the `kernel-modules` and load them from that path with `modprobe`. */ #[macro_use] @@ -17,7 +22,7 @@ use snafu::{ensure, OptionExt, ResultExt}; use std::collections::HashMap; use std::ffi::OsStr; use std::fs; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::{self, Command}; /// Path to the drivers configuration to use @@ -62,8 +67,8 @@ struct Args { #[derive(FromArgs, Debug, PartialEq)] #[argh(subcommand)] enum Subcommand { - LinkModules(LinkModulesArgs), - LoadModules(LoadModulesArgs), + Link(LinkModulesArgs), + Load(LoadModulesArgs), } /// Links the kernel modules @@ -76,9 +81,17 @@ struct LinkModulesArgs {} #[argh(subcommand, name = "load-modules")] struct LoadModulesArgs {} +#[derive(Deserialize, Debug)] +#[serde(untagged)] +/// Enum to hold the two types of configurations supported +enum DriverType { + Linking(LinkingDriverConfig), + Copying(CopyingDriverConfig), +} + /// Holds the configuration used to link kernel modules #[derive(Deserialize, Debug)] -struct DriverConfig { +struct LinkingDriverConfig { #[serde(rename(deserialize = "lib-modules-path"))] lib_modules_path: String, #[serde(rename(deserialize = "objects-source"))] @@ -89,6 +102,15 @@ struct DriverConfig { object_files: HashMap, } +/// Holds the configuration used to copy kernel modules +#[derive(Deserialize, Debug)] +struct CopyingDriverConfig { + #[serde(rename(deserialize = "lib-modules-path"))] + lib_modules_path: String, + #[serde(rename(deserialize = "kernel-modules"))] + kernel_modules: HashMap, +} + /// Holds the objects to be linked for the object/kernel module #[derive(Deserialize, Debug)] struct Linkable { @@ -96,9 +118,16 @@ struct Linkable { link_objects: Vec, } +/// Holds the modules to be copied and loaded +#[derive(Deserialize, Debug)] +struct NonLinkable { + #[serde(rename(deserialize = "copy-source"))] + copy_source: PathBuf, +} + // Links the modules in the modules sets fn link_modules_sets( - modules_sets: &HashMap, + modules_sets: &HashMap, target: Option, ) -> Result<()> { // Get current kernel version @@ -109,11 +138,17 @@ fn link_modules_sets( let driver_config = modules_sets .get(&target) .context(error::MissingModuleSetSnafu { target })?; - link_modules(driver_config, &kernel_version)?; + match driver_config { + DriverType::Copying(config) => copy_modules(config, &kernel_version)?, + DriverType::Linking(config) => link_modules(config, &kernel_version)?, + } } else { // Link all the modules sets if no target module was given for driver_config in modules_sets.values() { - link_modules(driver_config, &kernel_version)?; + match driver_config { + DriverType::Copying(config) => copy_modules(config, &kernel_version)?, + DriverType::Linking(config) => link_modules(config, &kernel_version)?, + } } } @@ -121,7 +156,7 @@ fn link_modules_sets( } // Links the kernel modules for the given configuration, and for the given kernel version -fn link_modules(driver_config: &DriverConfig, kernel_version: S) -> Result<()> +fn link_modules(driver_config: &LinkingDriverConfig, kernel_version: S) -> Result<()> where S: AsRef, { @@ -276,9 +311,49 @@ where Ok(()) } +// Loads the kernel modules for the given configuration, and for the given kernel version +fn copy_modules(driver_config: &CopyingDriverConfig, kernel_version: S) -> Result<()> +where + S: AsRef, +{ + let kernel_version = kernel_version.as_ref(); + + // Destination for the kernel modules + let modules_path = Path::new(LIB_MODULES_PATH) + .join(kernel_version) + .join(&driver_config.lib_modules_path); + + // Next, copy the kernel modules + for (name, module) in driver_config.kernel_modules.iter() { + copy_kernel_module(name, &modules_path, &module.copy_source)?; + } + + Ok(()) +} + +// Copy the module to the modules path provided +fn copy_kernel_module(name: S, modules_path: P, driver_source_path: P) -> Result<()> +where + S: AsRef, + P: AsRef, +{ + let name = name.as_ref(); + let driver_path = driver_source_path.as_ref(); + let modules_path = modules_path.as_ref(); + + let source_path = driver_path.join(name); + let destination_path = modules_path.join(name); + fs::copy(&source_path, &destination_path).context(error::CopySnafu { + from: &source_path, + to: &destination_path, + })?; + info!("Copied {}", name); + Ok(()) +} + // Loads the modules in the modules sets fn load_modules_sets( - modules_sets: &HashMap, + modules_sets: &HashMap, target: Option, ) -> Result<()> { // Update the modules.dep before we attempt to load kernel modules @@ -291,6 +366,7 @@ fn load_modules_sets( let driver_config = modules_sets .get(&target) .context(error::MissingModuleSetSnafu { target })?; + load_modules(driver_config)? } else { // Load all the modules sets if no target module was given @@ -302,12 +378,24 @@ fn load_modules_sets( Ok(()) } -fn load_modules(driver_config: &DriverConfig) -> Result<()> { - let mut kernel_modules: Vec = driver_config - .kernel_modules - .keys() - .map(|k| k.split('.').collect::>()[0].to_string()) - .collect(); +fn load_modules(driver_config: &DriverType) -> Result<()> { + let mut kernel_modules: Vec; + match driver_config { + DriverType::Copying(config) => { + kernel_modules = config + .kernel_modules + .keys() + .map(|k| k.split('.').collect::>()[0].to_string()) + .collect(); + } + DriverType::Linking(config) => { + kernel_modules = config + .kernel_modules + .keys() + .map(|k| k.split('.').collect::>()[0].to_string()) + .collect(); + } + } // Load kernel modules let mut args = vec!["-a".to_string()]; @@ -356,7 +444,7 @@ fn run() -> Result<()> { let args: Args = argh::from_env(); setup_logger(&args)?; let driver_config_path = Path::new(&args.driver_config_path); - let mut all_modules_sets: HashMap = HashMap::new(); + let mut all_modules_sets: HashMap = HashMap::new(); for entry in (driver_config_path .read_dir() @@ -366,7 +454,7 @@ fn run() -> Result<()> { .flatten() { let path = entry.path(); - let modules_sets: HashMap = toml::from_str( + let modules_sets: HashMap = toml::from_str( &fs::read_to_string(&path).context(error::ReadPathSnafu { path: &path })?, ) .context(error::DeserializeSnafu { path: &path })?; @@ -375,8 +463,8 @@ fn run() -> Result<()> { } match args.subcommand { - Subcommand::LinkModules(_) => link_modules_sets(&all_modules_sets, args.modules_set), - Subcommand::LoadModules(_) => load_modules_sets(&all_modules_sets, args.modules_set), + Subcommand::Link(_) => link_modules_sets(&all_modules_sets, args.modules_set), + Subcommand::Load(_) => load_modules_sets(&all_modules_sets, args.modules_set), } } @@ -440,3 +528,76 @@ mod error { } type Result = std::result::Result; + +#[cfg(test)] +mod test { + use super::*; + use std::path::PathBuf; + + fn test_data() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/tests") + } + + #[test] + fn parse_linking_config() { + let driver_config_path = test_data(); + + let linking_path = driver_config_path.join("linking.conf"); + let modules_sets: HashMap = toml::from_str( + &fs::read_to_string(&linking_path) + .context(error::ReadPathSnafu { + path: &linking_path, + }) + .unwrap(), + ) + .context(error::DeserializeSnafu { + path: &linking_path, + }) + .unwrap(); + for (name, driver) in modules_sets { + assert_eq!(name, "linking-driver"); + assert!(matches!(driver, DriverType::Linking { .. })); + match driver { + DriverType::Copying(_) => panic!("Wrong type of driver configuration found"), + DriverType::Linking(config) => { + assert_eq!(config.object_files.len(), 2); + assert_eq!( + config.objects_source, + "/usr/share/linking/module-objects.d/" + ); + } + } + } + } + + #[test] + fn parse_copying_config() { + let driver_config_path = test_data(); + + let copying_path = driver_config_path.join("copying.conf"); + let modules_sets: HashMap = toml::from_str( + &fs::read_to_string(©ing_path) + .context(error::ReadPathSnafu { + path: ©ing_path, + }) + .unwrap(), + ) + .context(error::DeserializeSnafu { + path: ©ing_path, + }) + .unwrap(); + let intended_copy_source = Path::new("/usr/share/factory/copying-driver"); + for (name, driver) in modules_sets { + assert_eq!(name, "copying-driver"); + assert!(matches!(driver, DriverType::Copying { .. })); + match driver { + DriverType::Linking(_) => panic!("Wrong type of driver configuration found"), + DriverType::Copying(config) => { + for (_name, module) in config.kernel_modules.iter() { + assert_eq!(module.copy_source, intended_copy_source); + } + } + } + } + } +} diff --git a/sources/driverdog/src/tests/copying.conf b/sources/driverdog/src/tests/copying.conf new file mode 100644 index 000000000..e2615877c --- /dev/null +++ b/sources/driverdog/src/tests/copying.conf @@ -0,0 +1,11 @@ +[copying-driver] +lib-modules-path = "kernel/drivers/extra/copying" + +[copying-driver.kernel-modules."main.ko"] +copy-source = "/usr/share/factory/copying-driver" + +[copying-driver.kernel-modules."secondary.ko"] +copy-source = "/usr/share/factory/copying-driver" + +[copying-driver.kernel-modules."other.ko"] +copy-source = "/usr/share/factory/copying-driver" diff --git a/sources/driverdog/src/tests/linking.conf b/sources/driverdog/src/tests/linking.conf new file mode 100644 index 000000000..87d6b4446 --- /dev/null +++ b/sources/driverdog/src/tests/linking.conf @@ -0,0 +1,18 @@ +[linking-driver] +lib-modules-path = "kernel/drivers/extra/linking" +objects-source = "/usr/share/linking/module-objects.d/" + +[linking-driver.object-files."main.o"] +link-objects = ["first.o", "second.o"] + +[linking-driver.kernel-modules."main.ko"] +link-objects = ["main.o", "main.mod.o"] + +[linking-driver.object-files."secondary.o"] +link-objects = ["third.o", "fourth.o"] + +[linking-driver.kernel-modules."secondary.ko"] +link-objects = ["secondary.o", "secondary.mod.o"] + +[linking-driver.kernel-modules."other.ko"] +link-objects = ["other.o", "other.mod.o"]