2020-07-31 21:01:10 +05:30
|
|
|
use anyhow::Result;
|
|
|
|
use std::fs::OpenOptions;
|
2021-05-04 13:40:20 +05:30
|
|
|
use std::io::Write;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
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 21:01:10 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-10-19 19:54:16 +05:30
|
|
|
const USAGE: &str =
|
|
|
|
"thin_dump 0.9.0
|
|
|
|
Dump thin-provisioning metadata to stdout in XML format
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
thin_dump [FLAGS] [OPTIONS] <INPUT>
|
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-q, --quiet Suppress output messages, return only exit code.
|
|
|
|
-r, --repair Repair the metadata whilst dumping it
|
|
|
|
--skip-mappings Do not dump the mappings
|
|
|
|
-h, --help Prints help information
|
|
|
|
-V, --version Prints version information
|
|
|
|
|
|
|
|
OPTIONS:
|
|
|
|
--data-block-size <SECTORS> Provide the data block size for repairing
|
|
|
|
-m, --metadata-snapshot <METADATA_SNAPSHOT> Access the metadata snapshot on a live pool
|
|
|
|
--nr-data-blocks <NUM> Override the number of data blocks if needed
|
|
|
|
-o, --output <FILE> Specify the output file rather than stdout
|
|
|
|
--transaction-id <NUM> Override the transaction id if needed
|
|
|
|
|
|
|
|
ARGS:
|
|
|
|
<INPUT> Specify the input device to dump";
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
//-----------------------------------------
|
|
|
|
|
|
|
|
struct ThinDump;
|
|
|
|
|
|
|
|
impl<'a> Program<'a> for ThinDump {
|
|
|
|
fn name() -> &'a str {
|
|
|
|
"thin_dump"
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:29:17 +05:30
|
|
|
fn cmd<I>(args: I) -> Command
|
2021-10-11 16:37:26 +05:30
|
|
|
where
|
|
|
|
I: IntoIterator,
|
|
|
|
I::Item: Into<std::ffi::OsString>,
|
|
|
|
{
|
|
|
|
thin_dump_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 ThinDump {
|
|
|
|
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-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
test_accepts_help!(ThinDump);
|
|
|
|
test_accepts_version!(ThinDump);
|
|
|
|
test_rejects_bad_option!(ThinDump);
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
test_missing_input_arg!(ThinDump);
|
|
|
|
test_input_file_not_found!(ThinDump);
|
|
|
|
test_input_cannot_be_a_directory!(ThinDump);
|
|
|
|
test_unreadable_input_file!(ThinDump);
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// test dump & restore cycle
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dump_restore_cycle() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let output = run_ok_raw(thin_dump_cmd(args![&md]))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
let xml = td.mk_path("meta.xml");
|
2021-05-04 13:40:20 +05:30
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.read(false)
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.open(&xml)?;
|
2020-07-31 21:01:10 +05:30
|
|
|
file.write_all(&output.stdout[0..])?;
|
|
|
|
drop(file);
|
|
|
|
|
|
|
|
let md2 = mk_zeroed_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
run_ok(thin_restore_cmd(args!["-i", &xml, "-o", &md2]))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
2021-10-11 16:37:26 +05:30
|
|
|
let output2 = run_ok_raw(thin_dump_cmd(args![&md2]))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert_eq!(output.stdout, output2.stdout);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test no stderr with a normal dump
|
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
#[test]
|
2021-08-30 13:49:54 +05:30
|
|
|
#[cfg(not(feature = "rust_tests"))]
|
2020-07-31 21:01:10 +05:30
|
|
|
fn no_stderr() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let output = run_ok_raw(thin_dump_cmd(args![&md]))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
assert_eq!(output.stderr.len(), 0);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test superblock overriding & repair
|
|
|
|
// TODO: share with thin_repair
|
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
fn override_something(flag: &str, value: &str, pattern: &str) -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let output = run_ok_raw(thin_dump_cmd(args![&md, flag, value]))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
2021-10-20 17:36:15 +05:30
|
|
|
assert_eq!(output.stderr.len(), 0);
|
2021-09-10 08:03:53 +05:30
|
|
|
assert!(std::str::from_utf8(&output.stdout[0..])?.contains(pattern));
|
2020-07-31 21:01:10 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_transaction_id() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
override_something("--transaction-id", "2345", "transaction=\"2345\"")
|
2020-07-31 21:01:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_data_block_size() -> Result<()> {
|
|
|
|
override_something("--data-block-size", "8192", "data_block_size=\"8192\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_nr_data_blocks() -> Result<()> {
|
|
|
|
override_something("--nr-data-blocks", "234500", "nr_data_blocks=\"234500\"")
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// FIXME: duplicate with superblock_succeeds in thin_repair.rs
|
2020-07-31 21:01:10 +05:30
|
|
|
#[test]
|
|
|
|
fn repair_superblock() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-10-11 16:37:26 +05:30
|
|
|
let before = run_ok_raw(thin_dump_cmd(args![&md]))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
damage_superblock(&md)?;
|
|
|
|
|
2021-10-11 16:37:26 +05:30
|
|
|
let after = run_ok_raw(thin_dump_cmd(
|
2021-07-20 23:41:46 +05:30
|
|
|
args![
|
2021-07-01 19:57:37 +05:30
|
|
|
"--repair",
|
2021-09-10 08:03:53 +05:30
|
|
|
"--transaction-id=1",
|
2021-07-01 19:57:37 +05:30
|
|
|
"--data-block-size=128",
|
2021-09-10 08:03:53 +05:30
|
|
|
"--nr-data-blocks=20480",
|
2021-07-20 23:41:46 +05:30
|
|
|
&md
|
2021-07-01 19:57:37 +05:30
|
|
|
],
|
2021-10-11 16:37:26 +05:30
|
|
|
))?;
|
2021-08-30 13:49:54 +05:30
|
|
|
if !cfg!(feature = "rust_tests") {
|
|
|
|
assert_eq!(after.stderr.len(), 0);
|
|
|
|
}
|
2020-07-31 21:01:10 +05:30
|
|
|
assert_eq!(before.stdout, after.stdout);
|
2021-05-04 13:40:20 +05:30
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test compatibility between options
|
|
|
|
// TODO: share with thin_repair
|
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
#[test]
|
|
|
|
fn missing_transaction_id() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
|
|
|
damage_superblock(&md)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let stderr = run_fail(
|
2021-10-11 16:37:26 +05:30
|
|
|
thin_dump_cmd(
|
2021-07-20 23:41:46 +05:30
|
|
|
args![
|
2021-07-01 19:57:37 +05:30
|
|
|
"--repair",
|
|
|
|
"--data-block-size=128",
|
2021-09-10 08:03:53 +05:30
|
|
|
"--nr-data-blocks=20480",
|
2021-07-20 23:41:46 +05:30
|
|
|
&md
|
2021-07-01 19:57:37 +05:30
|
|
|
],
|
2021-10-11 16:37:26 +05:30
|
|
|
))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert!(stderr.contains("transaction id"));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_data_block_size() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
|
|
|
damage_superblock(&md)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let stderr = run_fail(
|
2021-10-11 16:37:26 +05:30
|
|
|
thin_dump_cmd(
|
2021-07-20 23:41:46 +05:30
|
|
|
args![
|
2021-07-01 19:57:37 +05:30
|
|
|
"--repair",
|
2021-09-10 08:03:53 +05:30
|
|
|
"--transaction-id=1",
|
|
|
|
"--nr-data-blocks=20480",
|
2021-07-20 23:41:46 +05:30
|
|
|
&md
|
2021-07-01 19:57:37 +05:30
|
|
|
],
|
2021-10-11 16:37:26 +05:30
|
|
|
))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert!(stderr.contains("data block size"));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_nr_data_blocks() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
|
|
|
damage_superblock(&md)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let stderr = run_fail(
|
2021-10-11 16:37:26 +05:30
|
|
|
thin_dump_cmd(
|
2021-07-20 23:41:46 +05:30
|
|
|
args![
|
2021-07-01 19:57:37 +05:30
|
|
|
"--repair",
|
2021-09-10 08:03:53 +05:30
|
|
|
"--transaction-id=1",
|
2021-07-01 19:57:37 +05:30
|
|
|
"--data-block-size=128",
|
2021-07-20 23:41:46 +05:30
|
|
|
&md
|
2021-07-01 19:57:37 +05:30
|
|
|
],
|
2021-10-11 16:37:26 +05:30
|
|
|
))?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert!(stderr.contains("nr data blocks"));
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|