2020-07-31 15:34:12 +05:30
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
use common::common_args::*;
|
2021-07-20 15:07:55 +05:30
|
|
|
use common::fixture::*;
|
2021-07-01 19:57:37 +05:30
|
|
|
use common::input_arg::*;
|
2021-07-20 15:07:55 +05:30
|
|
|
use common::process::*;
|
|
|
|
use common::program::*;
|
|
|
|
use common::target::*;
|
2020-08-07 19:00:00 +05:30
|
|
|
use common::test_dir::*;
|
2021-07-20 15:07:55 +05:30
|
|
|
use common::thin::*;
|
2020-07-31 15:34:12 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-10-11 16:37:26 +05:30
|
|
|
const USAGE: &str = "thin_check 0.9.0
|
|
|
|
Validates thin provisioning metadata on a device or file.
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
thin_check [FLAGS] [OPTIONS] <INPUT>
|
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
--auto-repair Auto repair trivial issues.
|
|
|
|
--clear-needs-check-flag Clears the 'needs_check' flag in the superblock
|
|
|
|
--ignore-non-fatal-errors Only return a non-zero exit code if a fatal error is found.
|
|
|
|
-m, --metadata-snapshot Check the metadata snapshot on a live pool
|
|
|
|
-q, --quiet Suppress output messages, return only exit code.
|
|
|
|
--super-block-only Only check the superblock.
|
|
|
|
--skip-mappings Don't check the mapping tree
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
--override-mapping-root <OVERRIDE_MAPPING_ROOT> Specify a mapping root to use
|
|
|
|
|
|
|
|
ARGS:
|
|
|
|
<INPUT> Specify the input device to check";
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
//-----------------------------------------
|
|
|
|
|
|
|
|
struct ThinCheck;
|
|
|
|
|
|
|
|
impl<'a> Program<'a> for ThinCheck {
|
|
|
|
fn name() -> &'a str {
|
|
|
|
"thin_check"
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:37:26 +05:30
|
|
|
fn cmd<I>(args: I) -> duct::Expression
|
|
|
|
where
|
|
|
|
I: IntoIterator,
|
|
|
|
I::Item: Into<std::ffi::OsString>,
|
|
|
|
{
|
|
|
|
thin_check_cmd(args)
|
2021-07-06 07:21:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fn usage() -> &'a str {
|
|
|
|
USAGE
|
|
|
|
}
|
|
|
|
|
|
|
|
fn arg_type() -> ArgType {
|
|
|
|
ArgType::InputArg
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bad_option_hint(option: &str) -> String {
|
|
|
|
msg::bad_option_hint(option)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> InputProgram<'a> for ThinCheck {
|
|
|
|
fn mk_valid_input(td: &mut TestDir) -> Result<std::path::PathBuf> {
|
|
|
|
mk_valid_md(td)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn file_not_found() -> &'a str {
|
|
|
|
msg::FILE_NOT_FOUND
|
|
|
|
}
|
|
|
|
|
|
|
|
fn missing_input_arg() -> &'a str {
|
|
|
|
msg::MISSING_INPUT_ARG
|
|
|
|
}
|
|
|
|
|
|
|
|
fn corrupted_input() -> &'a str {
|
|
|
|
msg::BAD_SUPERBLOCK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 21:49:44 +05:30
|
|
|
impl<'a> MetadataReader<'_> for ThinCheck {}
|
2021-07-06 07:21:27 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
test_accepts_help!(ThinCheck);
|
|
|
|
test_accepts_version!(ThinCheck);
|
|
|
|
test_rejects_bad_option!(ThinCheck);
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
test_missing_input_arg!(ThinCheck);
|
|
|
|
test_input_file_not_found!(ThinCheck);
|
|
|
|
test_input_cannot_be_a_directory!(ThinCheck);
|
|
|
|
test_unreadable_input_file!(ThinCheck);
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
test_help_message_for_tiny_input_file!(ThinCheck);
|
|
|
|
test_spot_xml_data!(ThinCheck);
|
|
|
|
test_corrupted_input_data!(ThinCheck);
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test exclusive flags
|
|
|
|
|
|
|
|
fn accepts_flag(flag: &str) -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_ok(thin_check_cmd(args![flag, &md]))?;
|
2020-07-31 15:34:12 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn accepts_superblock_only() -> Result<()> {
|
|
|
|
accepts_flag("--super-block-only")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn accepts_skip_mappings() -> Result<()> {
|
|
|
|
accepts_flag("--skip-mappings")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn accepts_ignore_non_fatal_errors() -> Result<()> {
|
|
|
|
accepts_flag("--ignore-non-fatal-errors")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn accepts_clear_needs_check_flag() -> Result<()> {
|
|
|
|
accepts_flag("--clear-needs-check-flag")
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
#[test]
|
|
|
|
fn accepts_auto_repair() -> Result<()> {
|
|
|
|
accepts_flag("--auto-repair")
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// test the --quiet flag
|
|
|
|
|
2020-07-31 15:34:12 +05:30
|
|
|
#[test]
|
|
|
|
fn accepts_quiet() -> Result<()> {
|
2020-07-31 18:56:22 +05:30
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-10-11 16:37:26 +05:30
|
|
|
let output = run_ok_raw(thin_check_cmd(args!["--quiet", &md]))?;
|
|
|
|
if output.stdout.len() > 0 {
|
|
|
|
eprintln!("stdout: {:?}", &std::str::from_utf8(&output.stdout));
|
|
|
|
}
|
|
|
|
|
|
|
|
if output.stderr.len() > 0 {
|
|
|
|
eprintln!("stderr: {:?}", &std::str::from_utf8(&output.stderr));
|
|
|
|
}
|
2020-07-31 15:34:12 +05:30
|
|
|
assert_eq!(output.stdout.len(), 0);
|
|
|
|
assert_eq!(output.stderr.len(), 0);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test superblock-block-only
|
2020-08-12 17:33:01 +05:30
|
|
|
|
2020-07-31 15:34:12 +05:30
|
|
|
#[test]
|
|
|
|
fn detects_corrupt_superblock_with_superblock_only() -> Result<()> {
|
2020-07-31 18:56:22 +05:30
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_zeroed_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let _stderr = run_fail(thin_check_cmd(args!["--super-block-only", &md]))?;
|
2020-07-31 15:34:12 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test info outputs
|
2020-07-31 15:34:12 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prints_info_fields() -> Result<()> {
|
2020-07-31 18:56:22 +05:30
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let stdout = run_ok(thin_check_cmd(args![&md]))?;
|
|
|
|
eprintln!("info: {:?}", stdout);
|
2020-07-31 15:34:12 +05:30
|
|
|
assert!(stdout.contains("TRANSACTION_ID="));
|
|
|
|
assert!(stdout.contains("METADATA_FREE_BLOCKS="));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test compatibility between options
|
|
|
|
|
2020-08-12 17:33:01 +05:30
|
|
|
#[test]
|
|
|
|
fn auto_repair_incompatible_opts() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_fail(thin_check_cmd(args!["--auto-repair", "-m", &md]))?;
|
|
|
|
run_fail(thin_check_cmd(args![
|
|
|
|
"--auto-repair",
|
|
|
|
"--override-mapping-root",
|
|
|
|
"123",
|
|
|
|
&md
|
|
|
|
]))?;
|
|
|
|
run_fail(thin_check_cmd(args![
|
|
|
|
"--auto-repair",
|
|
|
|
"--super-block-only",
|
|
|
|
&md
|
|
|
|
]))?;
|
|
|
|
run_fail(thin_check_cmd(args![
|
|
|
|
"--auto-repair",
|
|
|
|
"--skip-mappings",
|
|
|
|
&md
|
|
|
|
]))?;
|
|
|
|
run_fail(thin_check_cmd(args![
|
|
|
|
"--auto-repair",
|
|
|
|
"--ignore-non-fatal-errors",
|
|
|
|
&md
|
|
|
|
]))?;
|
2020-08-12 17:33:01 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clear_needs_check_incompatible_opts() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_fail(thin_check_cmd(args!["--clear-needs-check-flag", "-m", &md]))?;
|
|
|
|
run_fail(thin_check_cmd(args![
|
|
|
|
"--clear-needs-check-flag",
|
|
|
|
"--override-mapping-root",
|
|
|
|
"123",
|
|
|
|
&md
|
|
|
|
]))?;
|
|
|
|
run_fail(thin_check_cmd(args![
|
|
|
|
"--clear-needs-check-flag",
|
|
|
|
"--super-block-only",
|
|
|
|
&md
|
|
|
|
]))?;
|
|
|
|
run_fail(thin_check_cmd(args![
|
|
|
|
"--clear-needs-check-flag",
|
|
|
|
"--ignore-non-fatal-errors",
|
|
|
|
&md
|
|
|
|
]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
2021-07-01 19:57:37 +05:30
|
|
|
// test clear-needs-check
|
2020-08-13 18:19:11 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clear_needs_check() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
set_needs_check(&md)?;
|
|
|
|
|
|
|
|
assert!(get_needs_check(&md)?);
|
2021-10-11 16:37:26 +05:30
|
|
|
run_ok(thin_check_cmd(args!["--clear-needs-check-flag", &md]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
assert!(!get_needs_check(&md)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_clear_needs_check_if_error() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
set_needs_check(&md)?;
|
|
|
|
generate_metadata_leaks(&md, 1, 0, 1)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_fail(thin_check_cmd(args!["--clear-needs-check-flag", &md]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
assert!(get_needs_check(&md)?);
|
2020-08-12 17:33:01 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-13 18:19:11 +05:30
|
|
|
|
2020-08-21 15:56:48 +05:30
|
|
|
#[test]
|
|
|
|
fn clear_needs_check_if_skip_mappings() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2020-08-21 15:56:48 +05:30
|
|
|
set_needs_check(&md)?;
|
|
|
|
generate_metadata_leaks(&md, 1, 0, 1)?;
|
|
|
|
|
|
|
|
assert!(get_needs_check(&md)?);
|
2021-10-11 16:37:26 +05:30
|
|
|
run_ok(thin_check_cmd(args![
|
|
|
|
"--clear-needs-check-flag",
|
|
|
|
"--skip-mappings",
|
|
|
|
&md
|
|
|
|
]))?;
|
2020-08-21 15:56:48 +05:30
|
|
|
assert!(!get_needs_check(&md)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test ignore-non-fatal-errors
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
#[test]
|
|
|
|
fn metadata_leaks_are_non_fatal() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
|
|
|
generate_metadata_leaks(&md, 1, 0, 1)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_fail(thin_check_cmd(args![&md]))?;
|
|
|
|
run_ok(thin_check_cmd(args!["--ignore-non-fatal-errors", &md]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fatal_errors_cant_be_ignored() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
generate_metadata_leaks(&md, 1, 1, 0)?;
|
|
|
|
ensure_untouched(&md, || {
|
2021-10-11 16:37:26 +05:30
|
|
|
run_fail(thin_check_cmd(args!["--ignore-non-fatal-errors", &md]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test auto-repair
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
#[test]
|
|
|
|
fn auto_repair() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
|
|
|
|
|
|
|
// auto-repair should have no effect on good metadata.
|
|
|
|
ensure_untouched(&md, || {
|
2021-10-11 16:37:26 +05:30
|
|
|
run_ok(thin_check_cmd(args!["--auto-repair", &md]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
generate_metadata_leaks(&md, 16, 0, 1)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_fail(thin_check_cmd(args![&md]))?;
|
|
|
|
run_ok(thin_check_cmd(args!["--auto-repair", &md]))?;
|
|
|
|
run_ok(thin_check_cmd(args![&md]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn auto_repair_has_limits() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
generate_metadata_leaks(&md, 16, 1, 0)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
ensure_untouched(&md, || {
|
2021-10-11 16:37:26 +05:30
|
|
|
run_fail(thin_check_cmd(args!["--auto-repair", &md]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn auto_repair_clears_needs_check() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
set_needs_check(&md)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_ok(thin_check_cmd(args!["--auto-repair", &md]))?;
|
2020-08-13 18:19:11 +05:30
|
|
|
assert!(!get_needs_check(&md)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-31 15:34:12 +05:30
|
|
|
//------------------------------------------
|