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
;
28 int main(int argc
, char **argv
) {
29 po::options_description
desc("Allowed options");
30 string store_path
, cmd
, out_path
, oid
;
33 ("help", "produce help message")
34 ("omap-path", po::value
<string
>(&store_path
),
35 "path to mon 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], mandatory")
42 po::positional_options_description p
;
45 vector
<string
> ceph_option_strings
;
48 po::parsed_options parsed
=
49 po::command_line_parser(argc
, argv
).options(desc
).positional(p
).allow_unregistered().run();
55 ceph_option_strings
= po::collect_unrecognized(parsed
.options
,
56 po::include_positional
);
57 } catch(po::error
&e
) {
58 std::cerr
<< e
.what() << std::endl
;
62 vector
<const char *> ceph_options
, def_args
;
63 ceph_options
.reserve(ceph_option_strings
.size());
64 for (vector
<string
>::iterator i
= ceph_option_strings
.begin();
65 i
!= ceph_option_strings
.end();
67 ceph_options
.push_back(i
->c_str());
70 if (vm
.count("debug")) debug
= true;
72 auto cct
= global_init(
73 &def_args
, ceph_options
, CEPH_ENTITY_TYPE_OSD
,
74 CODE_ENVIRONMENT_UTILITY_NODOUT
, 0);
75 common_init_finish(g_ceph_context
);
76 g_ceph_context
->_conf
->apply_changes(NULL
);
77 g_conf
= g_ceph_context
->_conf
;
79 g_conf
->set_val_or_die("log_to_stderr", "true");
80 g_conf
->set_val_or_die("err_to_stderr", "true");
82 g_conf
->apply_changes(NULL
);
84 if (vm
.count("help")) {
85 std::cerr
<< desc
<< std::endl
;
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 KeyValueDB
* store(KeyValueDB::create(g_ceph_context
, "leveldb", store_path
));
100 /*if (vm.count("paranoid")) {
101 std::cerr << "Enabling paranoid checks" << std::endl;
102 store->options.paranoid_checks = true;
104 DBObjectMap
omap(cct
.get(), store
);
106 int r
= store
->open(out
);
108 std::cerr
<< "Store open got: " << cpp_strerror(r
) << std::endl
;
109 std::cerr
<< "Output: " << out
.str() << std::endl
;
112 // We don't call omap.init() here because it will repair
113 // the DBObjectMap which we might want to examine for diagnostic
114 // reasons. Instead use --command repair.
118 if (cmd
== "dump-raw-keys") {
119 KeyValueDB::WholeSpaceIterator i
= store
->get_iterator();
120 for (i
->seek_to_first(); i
->valid(); i
->next()) {
121 std::cout
<< i
->raw_key() << std::endl
;
123 } else if (cmd
== "dump-raw-key-vals") {
124 KeyValueDB::WholeSpaceIterator i
= store
->get_iterator();
125 for (i
->seek_to_first(); i
->valid(); i
->next()) {
126 std::cout
<< i
->raw_key() << std::endl
;
127 i
->value().hexdump(std::cout
);
129 } else if (cmd
== "dump-objects") {
130 vector
<ghobject_t
> objects
;
131 r
= omap
.list_objects(&objects
);
133 std::cerr
<< "list_objects got: " << cpp_strerror(r
) << std::endl
;
136 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
139 if (vm
.count("oid") != 0 && i
->hobj
.oid
.name
!= oid
)
141 std::cout
<< *i
<< std::endl
;
144 } else if (cmd
== "dump-objects-with-keys") {
145 vector
<ghobject_t
> objects
;
146 r
= omap
.list_objects(&objects
);
148 std::cerr
<< "list_objects got: " << cpp_strerror(r
) << std::endl
;
151 for (vector
<ghobject_t
>::iterator i
= objects
.begin();
154 if (vm
.count("oid") != 0 && i
->hobj
.oid
.name
!= oid
)
156 std::cout
<< "Object: " << *i
<< std::endl
;
157 ObjectMap::ObjectMapIterator j
= omap
.get_iterator(ghobject_t(i
->hobj
));
158 for (j
->seek_to_first(); j
->valid(); j
->next()) {
159 std::cout
<< j
->key() << std::endl
;
160 j
->value().hexdump(std::cout
);
163 } else if (cmd
== "check" || cmd
== "repair") {
165 bool repair
= (cmd
== "repair");
166 r
= omap
.check(ss
, repair
);
168 std::cerr
<< ss
.str() << std::endl
;
170 std::cerr
<< "check got " << r
<< " error(s)" << std::endl
;
175 std::cout
<< (repair
? "repair" : "check") << " succeeded" << std::endl
;
176 } else if (cmd
== "dump-headers") {
177 vector
<DBObjectMap::_Header
> headers
;
178 r
= omap
.list_object_headers(&headers
);
180 std::cerr
<< "list_object_headers got: " << cpp_strerror(r
) << std::endl
;
184 for (auto i
: headers
)
185 std::cout
<< i
<< std::endl
;
187 std::cerr
<< "Did not recognize command " << cmd
<< std::endl
;