Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have strat engine verify existence of required binaries #986

Merged
merged 1 commit into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
160 changes: 160 additions & 0 deletions src/engine/strat_engine/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// 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::collections::HashMap;
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.
/// Search an explicit list of directories rather than the user's PATH
/// environment variable. stratisd may be running when there is no PATH
/// variable set.
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())
}

// These are the external binaries that stratisd relies on.
// Any change in this list requires a corresponding change to BINARIES,
// and vice-versa.
const MKFS_XFS: &str = "mkfs.xfs";
const THIN_CHECK: &str = "thin_check";
const THIN_REPAIR: &str = "thin_repair";
const XFS_ADMIN: &str = "xfs_admin";
const XFS_GROWFS: &str = "xfs_growfs";

lazy_static! {
static ref BINARIES: HashMap<String, Option<PathBuf>> = [
(MKFS_XFS.to_string(), find_binary(MKFS_XFS)),
(THIN_CHECK.to_string(), find_binary(THIN_CHECK)),
(THIN_REPAIR.to_string(), find_binary(THIN_REPAIR)),
(XFS_ADMIN.to_string(), find_binary(XFS_ADMIN)),
(XFS_GROWFS.to_string(), find_binary(XFS_GROWFS))
].iter()
.cloned()
.collect();
}

/// 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<()> {
match BINARIES.iter().find(|(_, ref path)| path.is_none()) {
None => Ok(()),
Some((ref name, _)) => Err(StratisError::Error(format!(
"Unable to find absolute path for \"{}\"",
name
))),
}
}

/// 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))
}
}

/// Get an absolute path for the executable with the given name.
/// Precondition: verify_binaries() has already been invoked.
fn get_executable(name: &str) -> StratisResult<PathBuf> {
let executable = BINARIES
.get(name)
.expect("name arguments are all constants defined with BINARIES, lookup can not fail")
.as_ref()
.expect("verify_binaries() was previously called and returned no error");
if !executable.exists() {
return Err(StratisError::Error(format!(
"Executable previously located at \"{}\" seems to have been removed since stratisd was started",
executable.to_str().expect("All parts of path are constructed from strictly ASCII components")
)));
}
Ok(executable.to_path_buf())
}

/// Create a filesystem on devnode.
pub fn create_fs(devnode: &Path, uuid: Uuid) -> StratisResult<()> {
execute_cmd(
Command::new(get_executable(MKFS_XFS)?.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<()> {
execute_cmd(
Command::new(get_executable(XFS_GROWFS)?.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<()> {
execute_cmd(
Command::new(get_executable(XFS_ADMIN)?.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<()> {
execute_cmd(
Command::new(get_executable(THIN_CHECK)?.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<()> {
execute_cmd(
Command::new(get_executable(THIN_REPAIR)?.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