Skip to content

Commit

Permalink
Merge pull request #3728 from mulkieran/metadata-check
Browse files Browse the repository at this point in the history
Add a command-line tool and module for pool inspection
  • Loading branch information
mulkieran authored Dec 11, 2024
2 parents 908402a + 8fef96a commit 59e00f5
Show file tree
Hide file tree
Showing 7 changed files with 840 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/bin/tools/check_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::{fs, path::Path};

use stratisd::engine::pool_inspection::inspectors;

pub fn run(infile: &Path, print: bool) -> Result<(), String> {
let metadata_str = fs::read_to_string(infile)
.map_err(|the_io_error| format!("Error opening file: {}", the_io_error))?;
let metadata = serde_json::from_str(&metadata_str)
.map_err(|the_json_error| format!("Error parsing json into structs: {}", the_json_error))?;

if print {
inspectors::print(&metadata).map_err(|the_error| format!("Error: {}", the_error))
} else {
inspectors::check(&metadata).map_err(|the_error| format!("Error: {}", the_error))
}
}
76 changes: 73 additions & 3 deletions src/bin/tools/cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::PathBuf;

use clap::{Arg, ArgAction, Command};

use crate::tools::dump_metadata;
use crate::tools::{check_metadata, dump_metadata};

use stratisd::stratis::VERSION;

Expand Down Expand Up @@ -70,17 +70,87 @@ impl<'a> ToolCommand<'a> for StratisDumpMetadata {
}
}

struct StratisCheckMetadata;

impl StratisCheckMetadata {
fn cmd() -> Command {
Command::new("stratis-checkmetadata")
.version(VERSION)
.about("Check validity of Stratis metadata")
.next_line_help(true)
.arg(
Arg::new("file")
.value_parser(clap::value_parser!(PathBuf))
.required(true)
.help("File containing pool-level metadata as JSON"),
)
}
}

impl<'a> ToolCommand<'a> for StratisCheckMetadata {
fn name(&self) -> &'a str {
"stratis-checkmetadata"
}

fn run(&self, command_line_args: Vec<String>) -> Result<(), String> {
let matches = StratisCheckMetadata::cmd().get_matches_from(command_line_args);
let infile = matches
.get_one::<PathBuf>("file")
.expect("'file' is a mandatory argument");

check_metadata::run(infile, false)
}
}

struct StratisPrintMetadata;

impl StratisPrintMetadata {
fn cmd() -> Command {
Command::new("stratis-printmetadata")
.version(VERSION)
.about("Print a human-suitable representation of Stratis metadata")
.next_line_help(true)
.arg(
Arg::new("file")
.value_parser(clap::value_parser!(PathBuf))
.required(true)
.help("File containing pool-level metadata as JSON"),
)
}
}

impl<'a> ToolCommand<'a> for StratisPrintMetadata {
fn name(&self) -> &'a str {
"stratis-printmetadata"
}

fn run(&self, command_line_args: Vec<String>) -> Result<(), String> {
let matches = StratisPrintMetadata::cmd().get_matches_from(command_line_args);
let infile = matches
.get_one::<PathBuf>("file")
.expect("'file' is a mandatory argument");

check_metadata::run(infile, true)
}
}

pub fn cmds<'a>() -> Vec<Box<dyn ToolCommand<'a>>> {
vec![Box::new(StratisDumpMetadata)]
vec![
Box::new(StratisCheckMetadata),
Box::new(StratisDumpMetadata),
Box::new(StratisPrintMetadata),
]
}

#[cfg(test)]
mod tests {

use super::StratisDumpMetadata;
use super::{StratisCheckMetadata, StratisDumpMetadata, StratisPrintMetadata};

#[test]
fn test_dumpmetadata_parse_args() {
StratisCheckMetadata::cmd().debug_assert();
StratisDumpMetadata::cmd().debug_assert();
StratisPrintMetadata::cmd().debug_assert();
}
}
1 change: 1 addition & 0 deletions src/bin/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

mod check_metadata;
mod cmds;
mod dump_metadata;

Expand Down
3 changes: 3 additions & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#[cfg(feature = "test_extras")]
pub use self::strat_engine::{ProcessedPathInfos, StratPool};

#[cfg(feature = "extras")]
pub use self::strat_engine::pool_inspection;

pub use self::{
engine::{BlockDev, Engine, Filesystem, KeyActions, Pool, Report},
shared::{total_allocated, total_used},
Expand Down
3 changes: 3 additions & 0 deletions src/engine/strat_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ mod writing;
#[cfg(feature = "test_extras")]
pub use self::{backstore::ProcessedPathInfos, pool::v1::StratPool};

#[cfg(feature = "extras")]
pub use self::pool::inspection as pool_inspection;

pub use self::{
backstore::integrity_meta_space,
crypt::{
Expand Down
Loading

0 comments on commit 59e00f5

Please sign in to comment.