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/filestore/DBObjectMap.h"
23 #include "kv/KeyValueDB.h"
25 namespace po
= boost::program_options
;
27 int main(int argc
, char **argv
) {
28 po::options_description
desc("Allowed options");
29 string store_path
, cmd
, oid
, backend
;
32 ("help", "produce help message")
33 ("omap-path", po::value
<string
>(&store_path
),
34 "path to omap directory, mandatory (current/omap usually)")
35 ("paranoid", "use paranoid checking")
36 ("debug", "Additional debug output from DBObjectMap")
37 ("oid", po::value
<string
>(&oid
), "Restrict to this object id when dumping objects")
38 ("command", po::value
<string
>(&cmd
),
39 "command arg is one of [dump-raw-keys, dump-raw-key-vals, dump-objects, dump-objects-with-keys, check, dump-headers, repair, compact], mandatory")
40 ("backend", po::value
<string
>(&backend
),
41 "DB backend (default rocksdb)")
43 po::positional_options_description p
;
46 vector
<string
> ceph_option_strings
;
49 po::parsed_options parsed
=
50 po::command_line_parser(argc
, argv
).options(desc
).positional(p
).allow_unregistered().run();
56 ceph_option_strings
= po::collect_unrecognized(parsed
.options
,
57 po::include_positional
);
58 } catch(po::error
&e
) {
59 std::cerr
<< e
.what() << std::endl
;
63 vector
<const char *> ceph_options
;
64 ceph_options
.reserve(ceph_option_strings
.size());
65 for (vector
<string
>::iterator i
= ceph_option_strings
.begin();
66 i
!= ceph_option_strings
.end();
68 ceph_options
.push_back(i
->c_str());
71 if (vm
.count("debug")) debug
= true;
73 if (vm
.count("help")) {
74 std::cerr
<< desc
<< std::endl
;
78 auto cct
= global_init(
79 NULL
, ceph_options
, CEPH_ENTITY_TYPE_OSD
,
80 CODE_ENVIRONMENT_UTILITY_NODOUT
, 0);
81 common_init_finish(g_ceph_context
);
82 cct
->_conf
.apply_changes(nullptr);
84 g_conf().set_val_or_die("log_to_stderr", "true");
85 g_conf().set_val_or_die("err_to_stderr", "true");
87 g_conf().apply_changes(nullptr);
89 if (vm
.count("omap-path") == 0) {
90 std::cerr
<< "Required argument --omap-path" << std::endl
;
94 if (vm
.count("command") == 0) {
95 std::cerr
<< "Required argument --command" << std::endl
;
99 if (vm
.count("backend") == 0) {
103 KeyValueDB
* store(KeyValueDB::create(g_ceph_context
, backend
, store_path
));
105 std::cerr
<< "Invalid backend '" << backend
<< "' specified" << std::endl
;
108 /*if (vm.count("paranoid")) {
109 std::cerr << "Enabling paranoid checks" << std::endl;
110 store->options.paranoid_checks = true;
112 DBObjectMap
omap(cct
.get(), store
);
114 int r
= store
->open(out
);
116 std::cerr
<< "Store open got: " << cpp_strerror(r
) << std::endl
;
117 std::cerr
<< "Output: " << out
.str() << std::endl
;
120 // We don't call omap.init() here because it will repair
121 // the DBObjectMap which we might want to examine for diagnostic
122 // reasons. Instead use --command repair.
125 std::cout
<< "Version: " << (int)omap
.state
.v
<< std::endl
;
126 std::cout
<< "Seq: " << omap
.state
.seq
<< std::endl
;
127 std::cout
<< "legacy: " << (omap
.state
.legacy
? "true" : "false") << std::endl
;
129 if (cmd
== "dump-raw-keys") {
130 KeyValueDB::WholeSpaceIterator i
= store
->get_wholespace_iterator();
131 for (i
->seek_to_first(); i
->valid(); i
->next()) {
132 std::cout
<< i
->raw_key() << std::endl
;
135 } else if (cmd
== "dump-raw-key-vals") {
136 KeyValueDB::WholeSpaceIterator i
= store
->get_wholespace_iterator();
137 for (i
->seek_to_first(); i
->valid(); i
->next()) {
138 std::cout
<< i
->raw_key() << std::endl
;
139 i
->value().hexdump(std::cout
);
142 } else if (cmd
== "dump-objects") {
143 vector
<ghobject_t
> objects
;
144 r
= omap
.list_objects(&objects
);
146 std::cerr
<< "list_objects got: " << cpp_strerror(r
) << std::endl
;
149 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
152 if (vm
.count("oid") != 0 && i
->hobj
.oid
.name
!= oid
)
154 std::cout
<< *i
<< std::endl
;
157 } else if (cmd
== "dump-objects-with-keys") {
158 vector
<ghobject_t
> objects
;
159 r
= omap
.list_objects(&objects
);
161 std::cerr
<< "list_objects got: " << cpp_strerror(r
) << std::endl
;
164 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
167 if (vm
.count("oid") != 0 && i
->hobj
.oid
.name
!= oid
)
169 std::cout
<< "Object: " << *i
<< std::endl
;
170 ObjectMap::ObjectMapIterator j
= omap
.get_iterator(ghobject_t(i
->hobj
));
171 for (j
->seek_to_first(); j
->valid(); j
->next()) {
172 std::cout
<< j
->key() << std::endl
;
173 j
->value().hexdump(std::cout
);
177 } else if (cmd
== "check" || cmd
== "repair") {
179 bool repair
= (cmd
== "repair");
180 r
= omap
.check(ss
, repair
, true);
182 std::cerr
<< ss
.str() << std::endl
;
184 std::cerr
<< "check got " << r
<< " error(s)" << std::endl
;
188 std::cout
<< (repair
? "repair" : "check") << " succeeded" << std::endl
;
190 } else if (cmd
== "dump-headers") {
191 vector
<DBObjectMap::_Header
> headers
;
192 r
= omap
.list_object_headers(&headers
);
194 std::cerr
<< "list_object_headers got: " << cpp_strerror(r
) << std::endl
;
197 for (auto i
: headers
)
198 std::cout
<< i
<< std::endl
;
200 } else if (cmd
== "resetv2") {
202 omap
.state
.legacy
= false;
204 } else if (cmd
== "compact") {
208 std::cerr
<< "Did not recognize command " << cmd
<< std::endl
;