2012-02-27 14:07:16 +00:00
|
|
|
// 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 11:43:13 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
2012-02-27 14:07:16 +00:00
|
|
|
#include "version.h"
|
|
|
|
|
2014-08-27 14:01:31 +01:00
|
|
|
#include "base/application.h"
|
2013-08-16 14:07:04 +01:00
|
|
|
#include "base/error_state.h"
|
2017-09-15 15:22:04 +01:00
|
|
|
#include "base/file_utils.h"
|
2013-09-11 11:40:46 +01:00
|
|
|
#include "persistent-data/file_utils.h"
|
2014-08-27 14:01:31 +01:00
|
|
|
#include "thin-provisioning/commands.h"
|
2020-02-16 16:43:31 +08:00
|
|
|
#include "thin-provisioning/metadata_checker.h"
|
|
|
|
#include "thin-provisioning/superblock.h"
|
2013-05-21 14:38:33 +01:00
|
|
|
|
2013-08-16 14:07:04 +01:00
|
|
|
using namespace base;
|
2012-02-27 14:07:16 +00:00
|
|
|
using namespace std;
|
2020-02-16 16:43:31 +08:00
|
|
|
using namespace persistent_data;
|
2012-02-27 14:07:16 +00:00
|
|
|
using namespace thin_provisioning;
|
|
|
|
|
2013-05-21 14:38:33 +01:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2012-02-27 14:07:16 +00:00
|
|
|
namespace {
|
2013-05-23 11:25:54 +01:00
|
|
|
struct flags {
|
2014-03-27 12:00:17 +00:00
|
|
|
flags()
|
2020-07-21 13:03:53 +08:00
|
|
|
: quiet(false) {
|
2014-03-27 12:00:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 16:43:31 +08:00
|
|
|
check_options check_opts;
|
2013-06-19 13:38:14 +02:00
|
|
|
bool quiet;
|
2013-05-23 11:25:54 +01:00
|
|
|
};
|
|
|
|
|
2014-04-02 13:43:16 +01:00
|
|
|
// Returns 0 on success, 1 on failure (this gets returned directly
|
|
|
|
// by main).
|
2013-05-23 11:25:54 +01:00
|
|
|
int check(string const &path, flags fs) {
|
2014-03-27 12:00:17 +00:00
|
|
|
bool success = false;
|
2013-05-21 14:38:33 +01:00
|
|
|
|
2012-03-02 10:00:31 +00:00
|
|
|
try {
|
2020-02-16 16:43:31 +08:00
|
|
|
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;
|
2020-07-21 13:03:53 +08:00
|
|
|
success = check_metadata(path, fs.check_opts, output_opts);
|
2014-03-27 12:00:17 +00:00
|
|
|
|
2012-03-02 10:00:31 +00:00
|
|
|
} catch (std::exception &e) {
|
2013-06-19 13:38:14 +02:00
|
|
|
if (!fs.quiet)
|
|
|
|
cerr << e.what() << endl;
|
|
|
|
|
2012-02-27 14:07:16 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-22 11:13:19 +01:00
|
|
|
return !success;
|
2012-02-27 14:07:16 +00:00
|
|
|
}
|
2016-01-08 12:51:52 +00:00
|
|
|
}
|
2012-02-27 14:07:16 +00:00
|
|
|
|
2016-01-08 12:51:52 +00:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
thin_check_cmd::thin_check_cmd()
|
|
|
|
: command("thin_check")
|
|
|
|
{
|
2012-02-27 14:07:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 12:51:52 +00:00
|
|
|
void
|
|
|
|
thin_check_cmd::usage(std::ostream &out) const
|
|
|
|
{
|
2020-06-09 14:19:59 +01:00
|
|
|
out << "Usage: " << get_name() << " [options] {device|file}\n"
|
|
|
|
<< "Options:\n"
|
|
|
|
<< " {-q|--quiet}\n"
|
|
|
|
<< " {-h|--help}\n"
|
|
|
|
<< " {-V|--version}\n"
|
|
|
|
<< " {-m|--metadata-snap}\n"
|
2020-08-12 10:45:20 +01:00
|
|
|
<< " {--auto-repair}\n"
|
2020-06-09 14:19:59 +01:00
|
|
|
<< " {--override-mapping-root}\n"
|
|
|
|
<< " {--clear-needs-check-flag}\n"
|
|
|
|
<< " {--ignore-non-fatal-errors}\n"
|
|
|
|
<< " {--skip-mappings}\n"
|
2016-01-08 12:51:52 +00:00
|
|
|
<< " {--super-block-only}" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thin_check_cmd::run(int argc, char **argv)
|
2012-02-27 14:07:16 +00:00
|
|
|
{
|
|
|
|
int c;
|
2013-08-15 10:35:07 +01:00
|
|
|
flags fs;
|
2013-07-29 12:29:05 +02:00
|
|
|
|
2020-06-12 13:41:47 +01:00
|
|
|
char const shortopts[] = "qhVm";
|
2013-05-23 13:57:15 +01:00
|
|
|
option const longopts[] = {
|
2012-03-02 12:59:59 +00:00
|
|
|
{ "quiet", no_argument, NULL, 'q'},
|
2012-02-27 14:07:16 +00:00
|
|
|
{ "help", no_argument, NULL, 'h'},
|
|
|
|
{ "version", no_argument, NULL, 'V'},
|
2020-06-09 14:19:59 +01:00
|
|
|
{ "metadata-snap", no_argument, NULL, 'm'},
|
2013-04-23 15:21:44 +01:00
|
|
|
{ "super-block-only", no_argument, NULL, 1},
|
2013-05-23 11:25:54 +01:00
|
|
|
{ "skip-mappings", no_argument, NULL, 2},
|
2013-05-23 11:57:02 +01:00
|
|
|
{ "ignore-non-fatal-errors", no_argument, NULL, 3},
|
2014-03-27 12:00:17 +00:00
|
|
|
{ "clear-needs-check-flag", no_argument, NULL, 4 },
|
2017-10-04 11:43:13 +01:00
|
|
|
{ "override-mapping-root", required_argument, NULL, 5},
|
2020-08-12 10:45:20 +01:00
|
|
|
{ "auto-repair", no_argument, NULL, 6},
|
2012-02-27 14:07:16 +00:00
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
2016-01-08 12:51:52 +00:00
|
|
|
usage(cout);
|
2012-02-27 14:07:16 +00:00
|
|
|
return 0;
|
2012-03-02 12:59:59 +00:00
|
|
|
|
|
|
|
case 'q':
|
2013-06-19 13:38:14 +02:00
|
|
|
fs.quiet = true;
|
2012-03-02 12:59:59 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-27 14:07:16 +00:00
|
|
|
case 'V':
|
2012-03-13 12:32:30 +00:00
|
|
|
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
2012-02-27 14:07:16 +00:00
|
|
|
return 0;
|
2012-03-13 12:32:30 +00:00
|
|
|
|
2020-06-09 14:19:59 +01:00
|
|
|
case 'm':
|
|
|
|
fs.check_opts.set_metadata_snap();
|
|
|
|
break;
|
|
|
|
|
2013-04-23 15:21:44 +01:00
|
|
|
case 1:
|
2013-05-23 11:25:54 +01:00
|
|
|
// super-block-only
|
2020-02-16 16:43:31 +08:00
|
|
|
fs.check_opts.set_superblock_only();
|
2013-05-23 11:25:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// skip-mappings
|
2020-02-16 16:43:31 +08:00
|
|
|
fs.check_opts.set_skip_mappings();
|
2013-04-23 15:21:44 +01:00
|
|
|
break;
|
|
|
|
|
2013-05-23 11:57:02 +01:00
|
|
|
case 3:
|
|
|
|
// ignore-non-fatal-errors
|
2020-07-02 16:03:23 +01:00
|
|
|
fs.check_opts.set_ignore_non_fatal();
|
2013-05-23 11:57:02 +01:00
|
|
|
break;
|
|
|
|
|
2014-03-27 12:00:17 +00:00
|
|
|
case 4:
|
|
|
|
// clear needs-check flag
|
2020-07-21 13:03:53 +08:00
|
|
|
fs.check_opts.set_clear_needs_check();
|
2014-03-27 12:00:17 +00:00
|
|
|
break;
|
|
|
|
|
2017-10-04 11:43:13 +01:00
|
|
|
case 5:
|
|
|
|
// override-mapping-root
|
2020-02-16 16:43:31 +08:00
|
|
|
fs.check_opts.set_override_mapping_root(boost::lexical_cast<uint64_t>(optarg));
|
2017-10-04 11:43:13 +01:00
|
|
|
break;
|
|
|
|
|
2020-07-14 17:57:29 +08:00
|
|
|
case 6:
|
2020-08-12 10:45:20 +01:00
|
|
|
// auto-repair
|
2020-07-14 17:57:29 +08:00
|
|
|
fs.check_opts.set_fix_metadata_leaks();
|
|
|
|
break;
|
|
|
|
|
2012-03-05 19:34:05 +01:00
|
|
|
default:
|
2016-01-08 12:51:52 +00:00
|
|
|
usage(cerr);
|
2012-03-05 19:34:05 +01:00
|
|
|
return 1;
|
2012-02-27 14:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 17:57:29 +08:00
|
|
|
if (!fs.check_opts.check_conformance()) {
|
2020-06-09 14:19:59 +01:00
|
|
|
usage(cerr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-03-05 19:34:05 +01:00
|
|
|
if (argc == optind) {
|
2013-06-19 13:38:14 +02:00
|
|
|
if (!fs.quiet) {
|
|
|
|
cerr << "No input file provided." << endl;
|
2016-01-08 12:51:52 +00:00
|
|
|
usage(cerr);
|
2013-06-19 13:38:14 +02:00
|
|
|
}
|
|
|
|
|
2012-02-27 14:07:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-05-23 11:25:54 +01:00
|
|
|
return check(argv[optind], fs);
|
2012-02-27 14:07:16 +00:00
|
|
|
}
|
2014-08-27 14:01:31 +01:00
|
|
|
|
|
|
|
//----------------------------------------------------------------
|