1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2012 Inktank, Inc.
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License kkjversion 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
13 #include <boost/program_options/variables_map.hpp>
14 #include <boost/program_options/parsers.hpp>
19 #include "common/errno.h"
20 #include "global/global_init.h"
22 #include "os/DBObjectMap.h"
23 #include "kv/KeyValueDB.h"
26 namespace po
= boost::program_options
;
28 int main(int argc
, char **argv
) {
29 po::options_description
desc("Allowed options");
30 string store_path
, cmd
, oid
, backend
;
33 ("help", "produce help message")
34 ("omap-path", po::value
<string
>(&store_path
),
35 "path to omap directory, mandatory (current/omap usually)")
36 ("paranoid", "use paranoid checking")
37 ("debug", "Additional debug output from DBObjectMap")
38 ("oid", po::value
<string
>(&oid
), "Restrict to this object id when dumping objects")
39 ("command", po::value
<string
>(&cmd
),
40 "command arg is one of [dump-raw-keys, dump-raw-key-vals, dump-objects, dump-objects-with-keys, check, dump-headers, repair, compact], mandatory")
41 ("backend", po::value
<string
>(&backend
),
42 "DB backend (default rocksdb)")
44 po::positional_options_description p
;
47 vector
<string
> ceph_option_strings
;
50 po::parsed_options parsed
=
51 po::command_line_parser(argc
, argv
).options(desc
).positional(p
).allow_unregistered().run();
57 ceph_option_strings
= po::collect_unrecognized(parsed
.options
,
58 po::include_positional
);
59 } catch(po::error
&e
) {
60 std::cerr
<< e
.what() << std::endl
;
64 vector
<const char *> ceph_options
;
65 ceph_options
.reserve(ceph_option_strings
.size());
66 for (vector
<string
>::iterator i
= ceph_option_strings
.begin();
67 i
!= ceph_option_strings
.end();
69 ceph_options
.push_back(i
->c_str());
72 if (vm
.count("debug")) debug
= true;
74 if (vm
.count("help")) {
75 std::cerr
<< desc
<< std::endl
;
79 auto cct
= global_init(
80 NULL
, ceph_options
, CEPH_ENTITY_TYPE_OSD
,
81 CODE_ENVIRONMENT_UTILITY_NODOUT
, 0);
82 common_init_finish(g_ceph_context
);
83 cct
->_conf
.apply_changes(nullptr);
85 g_conf().set_val_or_die("log_to_stderr", "true");
86 g_conf().set_val_or_die("err_to_stderr", "true");
88 g_conf().apply_changes(nullptr);
90 if (vm
.count("omap-path") == 0) {
91 std::cerr
<< "Required argument --omap-path" << std::endl
;
95 if (vm
.count("command") == 0) {
96 std::cerr
<< "Required argument --command" << std::endl
;
100 if (vm
.count("backend") == 0) {
104 KeyValueDB
* store(KeyValueDB::create(g_ceph_context
, backend
, store_path
));
106 std::cerr
<< "Invalid backend '" << backend
<< "' specified" << std::endl
;
109 /*if (vm.count("paranoid")) {
110 std::cerr << "Enabling paranoid checks" << std::endl;
111 store->options.paranoid_checks = true;
113 DBObjectMap
omap(cct
.get(), store
);
115 int r
= store
->open(out
);
117 std::cerr
<< "Store open got: " << cpp_strerror(r
) << std::endl
;
118 std::cerr
<< "Output: " << out
.str() << std::endl
;
121 // We don't call omap.init() here because it will repair
122 // the DBObjectMap which we might want to examine for diagnostic
123 // reasons. Instead use --command repair.
126 std::cout
<< "Version: " << (int)omap
.state
.v
<< std::endl
;
127 std::cout
<< "Seq: " << omap
.state
.seq
<< std::endl
;
128 std::cout
<< "legacy: " << (omap
.state
.legacy
? "true" : "false") << std::endl
;
130 if (cmd
== "dump-raw-keys") {
131 KeyValueDB::WholeSpaceIterator i
= store
->get_wholespace_iterator();
132 for (i
->seek_to_first(); i
->valid(); i
->next()) {
133 std::cout
<< i
->raw_key() << std::endl
;
136 } else if (cmd
== "dump-raw-key-vals") {
137 KeyValueDB::WholeSpaceIterator i
= store
->get_wholespace_iterator();
138 for (i
->seek_to_first(); i
->valid(); i
->next()) {
139 std::cout
<< i
->raw_key() << std::endl
;
140 i
->value().hexdump(std::cout
);
143 } else if (cmd
== "dump-objects") {
144 vector
<ghobject_t
> objects
;
145 r
= omap
.list_objects(&objects
);
147 std::cerr
<< "list_objects got: " << cpp_strerror(r
) << std::endl
;
150 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
153 if (vm
.count("oid") != 0 && i
->hobj
.oid
.name
!= oid
)
155 std::cout
<< *i
<< std::endl
;
158 } else if (cmd
== "dump-objects-with-keys") {
159 vector
<ghobject_t
> objects
;
160 r
= omap
.list_objects(&objects
);
162 std::cerr
<< "list_objects got: " << cpp_strerror(r
) << std::endl
;
165 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
168 if (vm
.count("oid") != 0 && i
->hobj
.oid
.name
!= oid
)
170 std::cout
<< "Object: " << *i
<< std::endl
;
171 ObjectMap::ObjectMapIterator j
= omap
.get_iterator(ghobject_t(i
->hobj
));
172 for (j
->seek_to_first(); j
->valid(); j
->next()) {
173 std::cout
<< j
->key() << std::endl
;
174 j
->value().hexdump(std::cout
);
178 } else if (cmd
== "check" || cmd
== "repair") {
180 bool repair
= (cmd
== "repair");
181 r
= omap
.check(ss
, repair
, true);
183 std::cerr
<< ss
.str() << std::endl
;
185 std::cerr
<< "check got " << r
<< " error(s)" << std::endl
;
189 std::cout
<< (repair
? "repair" : "check") << " succeeded" << std::endl
;
191 } else if (cmd
== "dump-headers") {
192 vector
<DBObjectMap::_Header
> headers
;
193 r
= omap
.list_object_headers(&headers
);
195 std::cerr
<< "list_object_headers got: " << cpp_strerror(r
) << std::endl
;
198 for (auto i
: headers
)
199 std::cout
<< i
<< std::endl
;
201 } else if (cmd
== "resetv2") {
203 omap
.state
.legacy
= false;
205 } else if (cmd
== "compact") {
209 std::cerr
<< "Did not recognize command " << cmd
<< std::endl
;