diff --git a/.github/workflows/fedora.yml b/.github/workflows/fedora.yml index f0f78b7281e..7b13c282358 100644 --- a/.github/workflows/fedora.yml +++ b/.github/workflows/fedora.yml @@ -51,7 +51,7 @@ jobs: - task: PROFILEDIR=debug make -f Makefile build-no-ipc toolchain: 1.75.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN components: cargo - - task: PROFILEDIR=debug make -f Makefile stratis-dumpmetadata + - task: PROFILEDIR=debug make -f Makefile stratisd-tools toolchain: 1.75.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN components: cargo - task: make -f Makefile docs-ci diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 918191ba38c..7f72a5551be 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -51,7 +51,7 @@ jobs: - task: PROFILEDIR=debug make -f Makefile build-no-ipc toolchain: 1.75.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN components: cargo - - task: PROFILEDIR=debug make -f Makefile stratis-dumpmetadata + - task: PROFILEDIR=debug make -f Makefile stratisd-tools toolchain: 1.75.0 # CURRENT DEVELOPMENT RUST TOOLCHAIN components: cargo - task: make -f Makefile docs-ci diff --git a/Cargo.toml b/Cargo.toml index 205b075d4d2..942cd19646c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ name = "stratisd" required-features = ["engine"] [[bin]] -name = "stratis-dumpmetadata" +name = "stratisd-tools" required-features = ["engine", "extras", "min"] [[bin]] diff --git a/Makefile b/Makefile index d0bb1d78063..42073b61cfe 100644 --- a/Makefile +++ b/Makefile @@ -165,7 +165,7 @@ audit-all-rust: build-all-rust ./target/${PROFILEDIR}/stratis-utils \ ./target/${PROFILEDIR}/stratis-str-cmp \ ./target/${PROFILEDIR}/stratis-base32-decode \ - ./target/${PROFILEDIR}/stratis-dumpmetadata + ./target/${PROFILEDIR}/stratisd-tools ## Check for spelling errors check-typos: @@ -253,12 +253,12 @@ build-stratis-base32-decode: # so we use two distinct targets to build the two binaries build-udev-utils: build-stratis-str-cmp build-stratis-base32-decode -## Build the stratis-dumpmetadata program -stratis-dumpmetadata: +## Build the stratisd-tools program +stratisd-tools: PKG_CONFIG_ALLOW_CROSS=1 \ RUSTFLAGS="${DENY}" \ cargo ${BUILD} ${RELEASE_FLAG} \ - --bin=stratis-dumpmetadata ${EXTRAS_FEATURES} ${TARGET_ARGS} + --bin=stratisd-tools ${EXTRAS_FEATURES} ${TARGET_ARGS} ## Build stratis-min for early userspace stratis-min: @@ -314,8 +314,11 @@ install-binaries: mkdir -p $(DESTDIR)$(BINDIR) mkdir -p $(DESTDIR)$(UNITGENDIR) $(INSTALL) -Dpm0755 -t $(DESTDIR)$(BINDIR) target/$(PROFILEDIR)/stratis-min + + $(INSTALL) -Dpm0755 -t $(DESTDIR)$(BINDIR) target/$(PROFILEDIR)/stratisd-tools + ln --force --verbose $(DESTDIR)$(BINDIR)/stratisd-tools $(DESTDIR)$(BINDIR)/stratis-dumpmetadata + $(INSTALL) -Dpm0755 -t $(DESTDIR)$(BINDIR) target/$(PROFILEDIR)/stratis-utils - $(INSTALL) -Dpm0755 -t $(DESTDIR)$(BINDIR) target/$(PROFILEDIR)/stratis-dumpmetadata mv --force --verbose $(DESTDIR)$(BINDIR)/stratis-utils $(DESTDIR)$(BINDIR)/stratis-predict-usage ln --force --verbose $(DESTDIR)$(BINDIR)/stratis-predict-usage $(DESTDIR)$(UNITGENDIR)/stratis-clevis-setup-generator ln --force --verbose $(DESTDIR)$(BINDIR)/stratis-predict-usage $(DESTDIR)$(UNITGENDIR)/stratis-setup-generator @@ -341,7 +344,7 @@ install-daemons: install: install-udev-cfg install-man-cfg install-dbus-cfg install-dracut-cfg install-systemd-cfg install-binaries install-udev-binaries install-fstab-script install-daemons ## Build all Rust artifacts -build-all-rust: build build-min build-udev-utils stratis-dumpmetadata +build-all-rust: build build-min build-udev-utils stratisd-tools ## Build all man pages build-all-man: docs/stratisd.8 docs/stratis-dumpmetadata.8 @@ -366,6 +369,7 @@ clean-ancillary: rm -fv $(DESTDIR)$(UDEVDIR)/stratis-str-cmp rm -fv $(DESTDIR)$(UDEVDIR)/stratis-base32-decode rm -fv $(DESTDIR)$(BINDIR)/stratis-predict-usage + rm -fv $(DESTDIR)$(BINDIR)/stratisd-tools rm -fv $(DESTDIR)$(BINDIR)/stratis-dumpmetadata rm -fv $(DESTDIR)$(UNITGENDIR)/stratis-setup-generator rm -fv $(DESTDIR)$(UNITGENDIR)/stratis-clevis-setup-generator @@ -506,7 +510,7 @@ clippy: clippy-macros clippy-min clippy-udev-utils clippy-no-ipc install-binaries install-daemons install-dbus-cfg - install-dracut-cfg + sinstall-dracut-cfg install-fstab-script install-man-cfg install-systemd-cfg diff --git a/src/bin/stratis-dumpmetadata.rs b/src/bin/stratis-dumpmetadata.rs deleted file mode 100644 index cd855fb8aeb..00000000000 --- a/src/bin/stratis-dumpmetadata.rs +++ /dev/null @@ -1,88 +0,0 @@ -// 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/. - -mod tools; - -use std::{env, process}; - -use env_logger::Builder; - -use clap::{Arg, ArgAction, Command}; - -use tools::dump_metadata; - -/// Configure and initialize the logger. -/// Read log configuration parameters from the environment if RUST_LOG -/// is set. Otherwise, just accept the default configuration, which is -/// to log at the severity of error only. -fn initialize_log() { - let mut builder = Builder::new(); - - if let Ok(s) = env::var("RUST_LOG") { - builder.parse_filters(&s); - } - - builder.init() -} - -fn parse_args() -> Command { - Command::new("stratis-dumpmetadata") - .next_line_help(true) - .arg( - Arg::new("dev") - .required(true) - .help("Print metadata of given device"), - ) - .arg( - Arg::new("print_bytes") - .long("print-bytes") - .action(ArgAction::SetTrue) - .num_args(0) - .short('b') - .help("Print byte buffer of signature block"), - ) - .arg( - Arg::new("only") - .long("only") - .action(ArgAction::Set) - .value_name("PORTION") - .value_parser(["pool"]) - .help("Only print specified portion of the metadata"), - ) -} - -fn main() { - let matches = parse_args().get_matches(); - let devpath = matches - .get_one::("dev") - .map(|s| s.as_str()) - .expect("'dev' is a mandatory argument"); - - initialize_log(); - - match dump_metadata::run( - devpath, - matches.get_flag("print_bytes"), - matches - .get_one::("only") - .map(|v| v == "pool") - .unwrap_or(false), - ) { - Ok(()) => {} - Err(e) => { - eprintln!("Error encountered: {}", e); - process::exit(1); - } - } -} - -#[cfg(test)] -mod tests { - use super::parse_args; - - #[test] - fn test_dumpmetadata_parse_args() { - parse_args().debug_assert(); - } -} diff --git a/src/bin/stratisd-tools.rs b/src/bin/stratisd-tools.rs new file mode 100644 index 00000000000..449a309cfff --- /dev/null +++ b/src/bin/stratisd-tools.rs @@ -0,0 +1,66 @@ +// 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/. + +mod tools; + +use std::{env, path::Path, process}; + +use env_logger::Builder; + +use crate::tools::cmds; + +fn basename(path: &str) -> Option<&Path> { + Path::new(path).file_name().map(Path::new) +} + +/// Configure and initialize the logger. +/// Read log configuration parameters from the environment if RUST_LOG +/// is set. Otherwise, just accept the default configuration, which is +/// to log at the severity of error only. +fn initialize_log() { + let mut builder = Builder::new(); + + if let Ok(s) = env::var("RUST_LOG") { + builder.parse_filters(&s); + } + + builder.init() +} + +fn main() { + initialize_log(); + + let mut command_line_args = env::args().peekable(); + command_line_args.next_if(|p| { + basename(p) + .map(|n| n == Path::new("stratisd-tools")) + .unwrap_or(false) + }); + + let args = command_line_args.collect::>(); + if args.is_empty() { + process::exit(2); + } + + let argv1 = args[0].as_str(); + + let command_name = match basename(argv1).and_then(|n| n.to_str()) { + Some(name) => name, + None => { + process::exit(1); + } + }; + + if let Some(c) = cmds().iter().find(|x| command_name == x.name()) { + match c.run(args) { + Ok(()) => {} + Err(e) => { + eprintln!("Error encountered: {}", e); + process::exit(1); + } + } + } else { + process::exit(2); + } +} diff --git a/src/bin/tools/cmds.rs b/src/bin/tools/cmds.rs new file mode 100644 index 00000000000..8f6384a1a7c --- /dev/null +++ b/src/bin/tools/cmds.rs @@ -0,0 +1,80 @@ +// 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 clap::{Arg, ArgAction, Command}; + +use crate::tools::dump_metadata; + +pub trait ToolCommand<'a> { + fn name(&self) -> &'a str; + fn run(&self, command_line_args: Vec) -> Result<(), String>; +} + +struct StratisDumpMetadata; + +impl StratisDumpMetadata { + fn cmd() -> Command { + Command::new("stratis-dumpmetadata") + .next_line_help(true) + .arg( + Arg::new("dev") + .required(true) + .help("Print metadata of given device"), + ) + .arg( + Arg::new("print_bytes") + .long("print-bytes") + .action(ArgAction::SetTrue) + .num_args(0) + .short('b') + .help("Print byte buffer of signature block"), + ) + .arg( + Arg::new("only") + .long("only") + .action(ArgAction::Set) + .value_name("PORTION") + .value_parser(["pool"]) + .help("Only print specified portion of the metadata"), + ) + } +} + +impl<'a> ToolCommand<'a> for StratisDumpMetadata { + fn name(&self) -> &'a str { + "stratis-dumpmetadata" + } + + fn run(&self, command_line_args: Vec) -> Result<(), String> { + let matches = StratisDumpMetadata::cmd().get_matches_from(command_line_args); + let devpath = matches + .get_one::("dev") + .map(|s| s.as_str()) + .expect("'dev' is a mandatory argument"); + + dump_metadata::run( + devpath, + matches.get_flag("print_bytes"), + matches + .get_one::("only") + .map(|v| v == "pool") + .unwrap_or(false), + ) + } +} + +pub fn cmds<'a>() -> Vec>> { + vec![Box::new(StratisDumpMetadata)] +} + +#[cfg(test)] +mod tests { + + use super::StratisDumpMetadata; + + #[test] + fn test_dumpmetadata_parse_args() { + StratisDumpMetadata::cmd().debug_assert(); + } +} diff --git a/src/bin/tools/mod.rs b/src/bin/tools/mod.rs index a2c434ec00f..b7d2ac52d29 100644 --- a/src/bin/tools/mod.rs +++ b/src/bin/tools/mod.rs @@ -2,4 +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 cmds; pub mod dump_metadata; + +pub use cmds::cmds;