Skip to content

Commit

Permalink
Have strat engine verify existance of required binaries
Browse files Browse the repository at this point in the history
Assume that all binaries will be required by stratisd at some time,
and that it shouldn't run if they are not available.

This seems better to me than having a panic if we try to use them and they
are not needed.

It looks like using lazy_static and using the techniques that were used
for implementing get_dm are not really all that different.

Hoist cmd.rs up a level, since it seems like the engine really has to
interact with it.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jun 6, 2018
1 parent 3db74bd commit 8ea4f92
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 94 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ libc = "0.2.36"
mnt = "0.3.1"
error-chain = "0.11.0"
libudev = "0.2.0"
lazy_static = "1.0.0"

[dependencies.uuid]
version = "0.6"
Expand Down
184 changes: 184 additions & 0 deletions src/engine/strat_engine/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// 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/.

// Handles invoking external binaries.
// This module assumes that, for a given machine, there is only one place
// where the desired executable might be installed. It expects the engine
// to identify that place at its initialization by invoking verify_binaries(),
// and to exit immediately if verify_binaries() return an error. If this
// protocol is followed then when any command is executed the unique absolute
// path of the binary for this machine will already have been identified.
// However stratisd may run for a while and it is possible for the binary
// to be caused to be uninstalled while stratisd is being run. Therefore,
// the existence of the file is checked before the command is invoked, and
// an explicit error is returned if the executable can not be found.

use std::path::{Path, PathBuf};
use std::process::Command;

use uuid::Uuid;

use stratis::{ErrorEnum, StratisError, StratisResult};

/// Find the binary with the given name by looking in likely locations.
/// Return None if no binary was found.
fn find_binary(name: &str) -> Option<PathBuf> {
["/usr/sbin", "/sbin", "/usr/bin", "/bin"]
.iter()
.map(|pre| [pre, name].iter().collect::<PathBuf>())
.find(|path| path.exists())
}

lazy_static! {
static ref MKFS_BIN: Option<PathBuf> = find_binary("mkfs.xfs");
static ref XFS_GROWFS_BIN: Option<PathBuf> = find_binary("xfs_growfs");
static ref XFS_ADMIN_BIN: Option<PathBuf> = find_binary("xfs_admin");
static ref THIN_CHECK_BIN: Option<PathBuf> = find_binary("thin_check");
static ref THIN_REPAIR_BIN: Option<PathBuf> = find_binary("thin_repair");
}

/// Verify that all binaries that the engine might invoke are available at some
/// path. Return an error if any are missing. Required to be called on engine
/// initialization.
pub fn verify_binaries() -> StratisResult<()> {
if MKFS_BIN.is_none() {
Err(StratisError::Engine(ErrorEnum::NotFound, "mkfs.xfs".into()))
} else if XFS_GROWFS_BIN.is_none() {
Err(StratisError::Engine(
ErrorEnum::NotFound,
"xfs_growfs".into(),
))
} else if XFS_ADMIN_BIN.is_none() {
Err(StratisError::Engine(
ErrorEnum::NotFound,
"xfs_admin".into(),
))
} else if THIN_CHECK_BIN.is_none() {
Err(StratisError::Engine(
ErrorEnum::NotFound,
"thin_check".into(),
))
} else if THIN_REPAIR_BIN.is_none() {
Err(StratisError::Engine(
ErrorEnum::NotFound,
"thin_repair".into(),
))
} else {
Ok(())
}
}

/// Common function to call a command line utility, returning an Result with an error message which
/// also includes stdout & stderr if it fails.
fn execute_cmd(cmd: &mut Command, error_msg: &str) -> StratisResult<()> {
let result = cmd.output()?;
if result.status.success() {
Ok(())
} else {
let std_out_txt = String::from_utf8_lossy(&result.stdout);
let std_err_txt = String::from_utf8_lossy(&result.stderr);
let err_msg = format!(
"{} stdout: {} stderr: {}",
error_msg, std_out_txt, std_err_txt
);
Err(StratisError::Engine(ErrorEnum::Error, err_msg))
}
}

/// Create a filesystem on devnode.
pub fn create_fs(devnode: &Path, uuid: Uuid) -> StratisResult<()> {
let executable = MKFS_BIN
.as_ref()
.expect("verify_binaries() returned no error");
if !executable.exists() {
return Err(StratisError::Engine(ErrorEnum::NotFound, "mkfs.xfs".into()));
}
execute_cmd(
Command::new(executable.as_os_str())
.arg("-f")
.arg("-q")
.arg(&devnode)
.arg("-m")
.arg(format!("uuid={}", uuid)),
&format!("Failed to create new filesystem at {:?}", devnode),
)
}

/// Use the xfs_growfs command to expand a filesystem mounted at the given
/// mount point.
pub fn xfs_growfs(mount_point: &Path) -> StratisResult<()> {
let executable = XFS_GROWFS_BIN
.as_ref()
.expect("verify_binaries() returned no error");
if !executable.exists() {
return Err(StratisError::Engine(
ErrorEnum::NotFound,
"xfs_growfs".into(),
));
}
execute_cmd(
Command::new(executable.as_os_str())
.arg(mount_point)
.arg("-d"),
&format!("Failed to expand filesystem {:?}", mount_point),
)
}

/// Set a new UUID for filesystem on the devnode.
pub fn set_uuid(devnode: &Path, uuid: Uuid) -> StratisResult<()> {
let executable = XFS_ADMIN_BIN
.as_ref()
.expect("verify_binaries() returned no error");
if !executable.exists() {
return Err(StratisError::Engine(
ErrorEnum::NotFound,
"xfs_admin".into(),
));
}
execute_cmd(
Command::new(executable.as_os_str())
.arg("-U")
.arg(format!("{}", uuid))
.arg(&devnode),
&format!("Failed to set UUID for filesystem {:?}", devnode),
)
}

/// Call thin_check on a thinpool
pub fn thin_check(devnode: &Path) -> StratisResult<()> {
let executable = THIN_CHECK_BIN
.as_ref()
.expect("verify_binaries() returned no error");
if !executable.exists() {
return Err(StratisError::Engine(
ErrorEnum::NotFound,
"thin_check".into(),
));
}
execute_cmd(
Command::new(executable.as_os_str()).arg("-q").arg(devnode),
&format!("thin_check for thin pool meta device {:?} failed", devnode),
)
}

/// Call thin_repair on a thinpool
pub fn thin_repair(meta_dev: &Path, new_meta_dev: &Path) -> StratisResult<()> {
let executable = THIN_REPAIR_BIN
.as_ref()
.expect("verify_binaries() returned no error");
if !executable.exists() {
return Err(StratisError::Engine(
ErrorEnum::NotFound,
"thin_repair".into(),
));
}
execute_cmd(
Command::new(executable.as_os_str())
.arg("-i")
.arg(meta_dev)
.arg("-o")
.arg(new_meta_dev),
&format!("thin_repair of thin pool meta device {:?} failed", meta_dev),
)
}
3 changes: 3 additions & 0 deletions src/engine/strat_engine/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::super::types::{DevUuid, Name, PoolUuid, Redundancy, RenameAction};
use super::backstore::{find_all, is_stratis_device, setup_pool};
#[cfg(test)]
use super::cleanup::teardown_pools;
use super::cmd::verify_binaries;
use super::devlinks;
use super::dm::{get_dm, get_dm_init};
use super::pool::StratPool;
Expand Down Expand Up @@ -52,8 +53,10 @@ impl StratEngine {
///
/// Returns an error if the kernel doesn't support required DM features.
/// Returns an error if there was an error reading device nodes.
/// Returns an error if the binaries on which it depends can not be found.
pub fn initialize() -> StratisResult<StratEngine> {
let dm = get_dm_init()?;
verify_binaries()?;
let minor_dm_version = dm.version()?.1;
if minor_dm_version < REQUIRED_DM_MINOR_VERSION {
let err_msg = format!(
Expand Down
1 change: 1 addition & 0 deletions src/engine/strat_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
mod backstore;
#[cfg(test)]
mod cleanup;
mod cmd;
mod device;
mod devlinks;
mod dm;
Expand Down
88 changes: 0 additions & 88 deletions src/engine/strat_engine/thinpool/cmd.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/engine/strat_engine/thinpool/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ use stratis::{ErrorEnum, StratisError, StratisResult};
use super::super::super::engine::Filesystem;
use super::super::super::types::{FilesystemUuid, Name};

use super::super::cmd::{create_fs, set_uuid, xfs_growfs};
use super::super::dm::get_dm;
use super::super::serde_structs::FilesystemSave;

use super::cmd::{create_fs, set_uuid, xfs_growfs};

/// TODO: confirm that 256 MiB leaves enough time for stratisd to respond and extend before
/// the filesystem is out of space.
pub const FILESYSTEM_LOWATER: Sectors = Sectors(256 * IEC::Mi / (SECTOR_SIZE as u64)); // = 256 MiB
Expand Down
3 changes: 1 addition & 2 deletions src/engine/strat_engine/thinpool/mdv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ use stratis::StratisResult;

use super::super::super::types::{FilesystemUuid, Name, PoolUuid};

use super::super::cmd::create_fs;
use super::super::dm::get_dm;
use super::super::engine::DEV_PATH;
use super::super::serde_structs::FilesystemSave;

use super::cmd::create_fs;

use super::filesystem::StratFilesystem;

// TODO: Monitor fs size and extend linear and fs if needed
Expand Down
1 change: 0 additions & 1 deletion src/engine/strat_engine/thinpool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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 cmd;
mod filesystem;
mod mdv;
mod thinids;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/strat_engine/thinpool/thinpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use super::super::super::structures::Table;
use super::super::super::types::{FilesystemUuid, Name, PoolUuid, RenameAction};

use super::super::backstore::Backstore;
use super::super::cmd::{thin_check, thin_repair};
use super::super::device::wipe_sectors;
use super::super::devlinks;
use super::super::dm::get_dm;
use super::super::dmnames::{format_flex_ids, format_thin_ids, format_thinpool_ids, FlexRole,
ThinPoolRole, ThinRole};
use super::super::serde_structs::{FlexDevsSave, Recordable, ThinPoolDevSave};

use super::cmd::{thin_check, thin_repair};
use super::filesystem::{FilesystemStatus, StratFilesystem};
use super::mdv::MetadataVol;
use super::thinids::ThinDevIdPool;
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extern crate error_chain;
#[cfg(test)]
extern crate quickcheck;

#[macro_use]
extern crate lazy_static;

pub mod engine;

#[cfg(feature = "dbus_enabled")]
Expand Down

0 comments on commit 8ea4f92

Please sign in to comment.