[thin_dump] --dev-id

This commit is contained in:
Joe Thornber 2016-03-22 14:41:02 +00:00
parent 8b2ec7a3aa
commit a3f4940f1e
5 changed files with 82 additions and 27 deletions

View File

@ -39,6 +39,10 @@ the thin provisioning device-mapper target, else try the one at block#.
See the thin provisioning target documentation on how to create or release
a metadata snapshot and retrieve the block number from the kernel.
.IP "\fN\-\-dev\-id\fP <dev-id>".
Dump the specified device. This option may be specified multiple
times to select more than one thin device.
.IP "\fB\-h, \-\-help\fP".
Print help and exit.

View File

@ -88,8 +88,13 @@ namespace {
class details_extractor : public device_tree_detail::device_visitor {
public:
details_extractor(dump_options const &opts)
: opts_(opts) {
}
void visit(block_address dev_id, device_tree_detail::device_details const &dd) {
dd_.insert(make_pair(dev_id, dd));
if (opts_.selected_dev(dev_id))
dd_.insert(make_pair(dev_id, dd));
}
dd_map const &get_details() const {
@ -97,6 +102,7 @@ namespace {
}
private:
dump_options const &opts_;
dd_map dd_;
};
@ -161,21 +167,24 @@ namespace {
class mapping_tree_emitter : public mapping_tree_detail::device_visitor {
public:
mapping_tree_emitter(metadata::ptr md,
mapping_tree_emitter(dump_options const &opts,
metadata::ptr md,
emitter::ptr e,
dd_map const &dd,
bool repair,
mapping_tree_detail::damage_visitor::ptr damage_policy)
: md_(md),
: opts_(opts),
md_(md),
e_(e),
dd_(dd),
repair_(repair),
damage_policy_(damage_policy) {
}
void visit(btree_path const &path, block_address tree_root) {
block_address dev_id = path[0];
if (!opts_.selected_dev(dev_id))
return;
dd_map::const_iterator it = dd_.find(path[0]);
if (it != dd_.end()) {
device_tree_detail::device_details const &d = it->second;
@ -194,7 +203,7 @@ namespace {
}
e_->end_device();
} else if (!repair_) {
} else if (!opts_.repair_) {
ostringstream msg;
msg << "mappings present for device " << dev_id
<< ", but it isn't present in device tree";
@ -210,10 +219,10 @@ namespace {
walk_mapping_tree(tree, static_cast<mapping_tree_detail::mapping_visitor &>(me), *damage_policy_);
}
dump_options const &opts_;
metadata::ptr md_;
emitter::ptr e_;
dd_map const &dd_;
bool repair_;
mapping_tree_detail::damage_visitor::ptr damage_policy_;
};
@ -234,10 +243,10 @@ namespace {
//----------------------------------------------------------------
void
thin_provisioning::metadata_dump(metadata::ptr md, emitter::ptr e, bool repair)
thin_provisioning::metadata_dump(metadata::ptr md, emitter::ptr e, dump_options const &opts)
{
details_extractor de;
device_tree_detail::damage_visitor::ptr dd_policy(details_damage_policy(repair));
details_extractor de(opts);
device_tree_detail::damage_visitor::ptr dd_policy(details_damage_policy(opts.repair_));
walk_device_tree(*md->details_, de, *dd_policy);
e->begin_superblock("", md->sb_.time_,
@ -249,8 +258,8 @@ thin_provisioning::metadata_dump(metadata::ptr md, emitter::ptr e, bool repair)
boost::optional<block_address>());
{
mapping_tree_detail::damage_visitor::ptr md_policy(mapping_damage_policy(repair));
mapping_tree_emitter mte(md, e, de.get_details(), repair, mapping_damage_policy(repair));
mapping_tree_detail::damage_visitor::ptr md_policy(mapping_damage_policy(opts.repair_));
mapping_tree_emitter mte(opts, md, e, de.get_details(), mapping_damage_policy(opts.repair_));
walk_mapping_tree(*md->mappings_top_level_, mte, *md_policy);
}

View File

@ -22,13 +22,40 @@
#include "emitter.h"
#include "metadata.h"
#include <boost/optional.hpp>
#include <set>
//----------------------------------------------------------------
namespace thin_provisioning {
class dump_options {
public:
dump_options()
: repair_(false) {
}
bool selected_dev(uint64_t dev_id) const {
return !dev_filter_ || dev_filter_->count(dev_id);
}
void select_dev(uint64_t dev_id) {
if (!dev_filter_)
dev_filter_ = dev_set();
dev_filter_->insert(dev_id);
}
bool repair_;
using dev_set = std::set<uint64_t>;
using maybe_dev_set = boost::optional<dev_set>;
maybe_dev_set dev_filter_;
};
// Set the @repair flag if your metadata is corrupt, and you'd like
// the dumper to do it's best to recover info. If not set, any
// corruption encountered will cause an exception to be thrown.
void metadata_dump(metadata::ptr md, emitter::ptr e, bool repair);
void metadata_dump(metadata::ptr md, emitter::ptr e, dump_options const &opts);
void metadata_dump_subtree(metadata::ptr md, emitter::ptr e, bool repair, uint64_t subtree_root);
}

View File

@ -38,11 +38,10 @@ namespace {
// FIXME: put the path into the flags
struct flags {
flags()
: repair(false),
use_metadata_snap(false) {
: use_metadata_snap(false) {
}
bool repair;
dump_options opts;
bool use_metadata_snap;
optional<block_address> snap_location;
};
@ -70,7 +69,7 @@ namespace {
exit(1);
}
metadata_dump(md, e, flags.repair);
metadata_dump(md, e, flags.opts);
} catch (std::exception &e) {
cerr << e.what() << endl;
@ -99,13 +98,14 @@ thin_dump_cmd::thin_dump_cmd()
void
thin_dump_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}" << endl
<< "Options:" << endl
<< " {-h|--help}" << endl
<< " {-f|--format} {xml|human_readable}" << endl
<< " {-r|--repair}" << endl
<< " {-m|--metadata-snap} [block#]" << endl
<< " {-o <xml file>}" << endl
out << "Usage: " << get_name() << " [options] {device|file}\n"
<< "Options:\n"
<< " {-h|--help}\n"
<< " {-f|--format} {xml|human_readable}\n"
<< " {-r|--repair}\n"
<< " {-m|--metadata-snap} [block#]\n"
<< " {-o <xml file>}\n"
<< " {--dev-id} <dev-id>\n"
<< " {-V|--version}" << endl;
}
@ -118,6 +118,7 @@ thin_dump_cmd::run(int argc, char **argv)
char *end_ptr;
string format = "xml";
block_address metadata_snap = 0;
uint64_t dev_id;
struct flags flags;
const struct option longopts[] = {
@ -126,6 +127,7 @@ thin_dump_cmd::run(int argc, char **argv)
{ "output", required_argument, NULL, 'o'},
{ "format", required_argument, NULL, 'f' },
{ "repair", no_argument, NULL, 'r'},
{ "dev-id", required_argument, NULL, 1 },
{ "version", no_argument, NULL, 'V'},
{ NULL, no_argument, NULL, 0 }
};
@ -141,7 +143,7 @@ thin_dump_cmd::run(int argc, char **argv)
break;
case 'r':
flags.repair = true;
flags.opts.repair_ = true;
break;
case 'm':
@ -150,7 +152,7 @@ thin_dump_cmd::run(int argc, char **argv)
// FIXME: deprecate this option
metadata_snap = strtoull(optarg, &end_ptr, 10);
if (end_ptr == optarg) {
cerr << "couldn't parse <metadata_snap>" << endl;
cerr << "couldn't parse <metadata-snap>" << endl;
usage(cerr);
return 1;
}
@ -163,6 +165,16 @@ thin_dump_cmd::run(int argc, char **argv)
output = optarg;
break;
case 1:
dev_id = strtoull(optarg, &end_ptr, 10);
if (end_ptr == optarg) {
cerr << "couldn't parse <dev-id>\n";
usage(cerr);
return 1;
}
flags.opts.select_dev(dev_id);
break;
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;

View File

@ -25,7 +25,10 @@ namespace {
block_manager<>::ptr old_bm = open_bm(old_path, block_manager<>::READ_ONLY);
metadata::ptr old_md(new metadata(old_bm, false));
metadata_dump(old_md, e, true);
dump_options opts;
opts.repair_ = true;
metadata_dump(old_md, e, opts);
} catch (std::exception &e) {
cerr << e.what() << endl;