thin-provisioning-tools/thin-provisioning/thin_check.cc

196 lines
4.5 KiB
C++
Raw Normal View History

2012-02-27 19:37:16 +05:30
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#include <iostream>
#include <getopt.h>
#include <libgen.h>
2017-10-04 16:13:13 +05:30
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
2012-02-27 19:37:16 +05:30
#include "version.h"
2014-08-27 18:31:31 +05:30
#include "base/application.h"
#include "base/error_state.h"
2017-09-15 19:52:04 +05:30
#include "base/file_utils.h"
2013-09-11 16:10:46 +05:30
#include "persistent-data/file_utils.h"
2014-08-27 18:31:31 +05:30
#include "thin-provisioning/commands.h"
#include "thin-provisioning/metadata_checker.h"
#include "thin-provisioning/superblock.h"
using namespace base;
2012-02-27 19:37:16 +05:30
using namespace std;
using namespace persistent_data;
2012-02-27 19:37:16 +05:30
using namespace thin_provisioning;
//----------------------------------------------------------------
2012-02-27 19:37:16 +05:30
namespace {
struct flags {
2014-03-27 17:30:17 +05:30
flags()
: quiet(false) {
2014-03-27 17:30:17 +05:30
}
check_options check_opts;
2013-06-19 17:08:14 +05:30
bool quiet;
};
// Returns 0 on success, 1 on failure (this gets returned directly
// by main).
int check(string const &path, flags fs) {
2014-03-27 17:30:17 +05:30
bool success = false;
2012-03-02 15:30:31 +05:30
try {
if (file_utils::get_file_length(path) < persistent_data::MD_BLOCK_SIZE) {
cerr << "Metadata device/file too small. Is this binary metadata?"
<< endl;
return 1;
}
output_options output_opts = !fs.quiet ? OUTPUT_NORMAL : OUTPUT_QUIET;
success = check_metadata(path, fs.check_opts, output_opts);
2014-03-27 17:30:17 +05:30
2012-03-02 15:30:31 +05:30
} catch (std::exception &e) {
2013-06-19 17:08:14 +05:30
if (!fs.quiet)
cerr << e.what() << endl;
2012-02-27 19:37:16 +05:30
return 1;
}
return !success;
2012-02-27 19:37:16 +05:30
}
}
2012-02-27 19:37:16 +05:30
//----------------------------------------------------------------
thin_check_cmd::thin_check_cmd()
: command("thin_check")
{
2012-02-27 19:37:16 +05:30
}
void
thin_check_cmd::usage(std::ostream &out) const
{
out << "Usage: " << get_name() << " [options] {device|file}\n"
<< "Options:\n"
<< " {-q|--quiet}\n"
<< " {-h|--help}\n"
<< " {-V|--version}\n"
<< " {-m|--metadata-snap}\n"
<< " {--fix-metadata-leaks}\n"
<< " {--override-mapping-root}\n"
<< " {--clear-needs-check-flag}\n"
<< " {--ignore-non-fatal-errors}\n"
<< " {--skip-mappings}\n"
<< " {--super-block-only}" << endl;
}
int
thin_check_cmd::run(int argc, char **argv)
2012-02-27 19:37:16 +05:30
{
int c;
flags fs;
2013-07-29 15:59:05 +05:30
2020-06-12 18:11:47 +05:30
char const shortopts[] = "qhVm";
2013-05-23 18:27:15 +05:30
option const longopts[] = {
2012-03-02 18:29:59 +05:30
{ "quiet", no_argument, NULL, 'q'},
2012-02-27 19:37:16 +05:30
{ "help", no_argument, NULL, 'h'},
{ "version", no_argument, NULL, 'V'},
{ "metadata-snap", no_argument, NULL, 'm'},
{ "super-block-only", no_argument, NULL, 1},
{ "skip-mappings", no_argument, NULL, 2},
2013-05-23 16:27:02 +05:30
{ "ignore-non-fatal-errors", no_argument, NULL, 3},
2014-03-27 17:30:17 +05:30
{ "clear-needs-check-flag", no_argument, NULL, 4 },
2017-10-04 16:13:13 +05:30
{ "override-mapping-root", required_argument, NULL, 5},
{ "fix-metadata-leaks", no_argument, NULL, 6},
2012-02-27 19:37:16 +05:30
{ NULL, no_argument, NULL, 0 }
};
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch(c) {
case 'h':
usage(cout);
2012-02-27 19:37:16 +05:30
return 0;
2012-03-02 18:29:59 +05:30
case 'q':
2013-06-19 17:08:14 +05:30
fs.quiet = true;
2012-03-02 18:29:59 +05:30
break;
2012-02-27 19:37:16 +05:30
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
2012-02-27 19:37:16 +05:30
return 0;
case 'm':
fs.check_opts.set_metadata_snap();
break;
case 1:
// super-block-only
fs.check_opts.set_superblock_only();
break;
case 2:
// skip-mappings
fs.check_opts.set_skip_mappings();
break;
2013-05-23 16:27:02 +05:30
case 3:
// ignore-non-fatal-errors
fs.check_opts.set_ignore_non_fatal();
2013-05-23 16:27:02 +05:30
break;
2014-03-27 17:30:17 +05:30
case 4:
// clear needs-check flag
fs.check_opts.set_clear_needs_check();
2014-03-27 17:30:17 +05:30
break;
2017-10-04 16:13:13 +05:30
case 5:
// override-mapping-root
fs.check_opts.set_override_mapping_root(boost::lexical_cast<uint64_t>(optarg));
2017-10-04 16:13:13 +05:30
break;
case 6:
// fix-metadata-leaks
fs.check_opts.set_fix_metadata_leaks();
break;
2012-03-06 00:04:05 +05:30
default:
usage(cerr);
2012-03-06 00:04:05 +05:30
return 1;
2012-02-27 19:37:16 +05:30
}
}
if (!fs.check_opts.check_conformance()) {
usage(cerr);
exit(1);
}
2012-03-06 00:04:05 +05:30
if (argc == optind) {
2013-06-19 17:08:14 +05:30
if (!fs.quiet) {
cerr << "No input file provided." << endl;
usage(cerr);
2013-06-19 17:08:14 +05:30
}
2012-02-27 19:37:16 +05:30
exit(1);
}
return check(argv[optind], fs);
2012-02-27 19:37:16 +05:30
}
2014-08-27 18:31:31 +05:30
//----------------------------------------------------------------