]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/ceph_osdomap_tool.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / tools / ceph_osdomap_tool.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2012 Inktank, Inc.
7 *
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.
12 */
13 #include <boost/program_options/variables_map.hpp>
14 #include <boost/program_options/parsers.hpp>
15
16 #include <stdlib.h>
17 #include <string>
18
19 #include "common/errno.h"
20 #include "global/global_init.h"
21
22 #include "os/filestore/DBObjectMap.h"
23 #include "kv/KeyValueDB.h"
24
25 namespace po = boost::program_options;
26
27 int main(int argc, char **argv) {
28 po::options_description desc("Allowed options");
29 string store_path, cmd, oid, backend;
30 bool debug = false;
31 desc.add_options()
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)")
42 ;
43 po::positional_options_description p;
44 p.add("command", 1);
45
46 vector<string> ceph_option_strings;
47 po::variables_map vm;
48 try {
49 po::parsed_options parsed =
50 po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run();
51 po::store(
52 parsed,
53 vm);
54 po::notify(vm);
55
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;
60 return 1;
61 }
62
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();
67 ++i) {
68 ceph_options.push_back(i->c_str());
69 }
70
71 if (vm.count("debug")) debug = true;
72
73 if (vm.count("help")) {
74 std::cerr << desc << std::endl;
75 return 1;
76 }
77
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);
83 if (debug) {
84 g_conf().set_val_or_die("log_to_stderr", "true");
85 g_conf().set_val_or_die("err_to_stderr", "true");
86 }
87 g_conf().apply_changes(nullptr);
88
89 if (vm.count("omap-path") == 0) {
90 std::cerr << "Required argument --omap-path" << std::endl;
91 return 1;
92 }
93
94 if (vm.count("command") == 0) {
95 std::cerr << "Required argument --command" << std::endl;
96 return 1;
97 }
98
99 if (vm.count("backend") == 0) {
100 backend = "rocksdb";
101 }
102
103 KeyValueDB* store(KeyValueDB::create(g_ceph_context, backend, store_path));
104 if (store == NULL) {
105 std::cerr << "Invalid backend '" << backend << "' specified" << std::endl;
106 return 1;
107 }
108 /*if (vm.count("paranoid")) {
109 std::cerr << "Enabling paranoid checks" << std::endl;
110 store->options.paranoid_checks = true;
111 }*/
112 DBObjectMap omap(cct.get(), store);
113 stringstream out;
114 int r = store->open(out);
115 if (r < 0) {
116 std::cerr << "Store open got: " << cpp_strerror(r) << std::endl;
117 std::cerr << "Output: " << out.str() << std::endl;
118 return r;
119 }
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.
123
124 omap.get_state();
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;
128
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;
133 }
134 return 0;
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);
140 }
141 return 0;
142 } else if (cmd == "dump-objects") {
143 vector<ghobject_t> objects;
144 r = omap.list_objects(&objects);
145 if (r < 0) {
146 std::cerr << "list_objects got: " << cpp_strerror(r) << std::endl;
147 return r;
148 }
149 for (vector<ghobject_t>::iterator i = objects.begin();
150 i != objects.end();
151 ++i) {
152 if (vm.count("oid") != 0 && i->hobj.oid.name != oid)
153 continue;
154 std::cout << *i << std::endl;
155 }
156 return 0;
157 } else if (cmd == "dump-objects-with-keys") {
158 vector<ghobject_t> objects;
159 r = omap.list_objects(&objects);
160 if (r < 0) {
161 std::cerr << "list_objects got: " << cpp_strerror(r) << std::endl;
162 return r;
163 }
164 for (vector<ghobject_t>::iterator i = objects.begin();
165 i != objects.end();
166 ++i) {
167 if (vm.count("oid") != 0 && i->hobj.oid.name != oid)
168 continue;
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);
174 }
175 }
176 return 0;
177 } else if (cmd == "check" || cmd == "repair") {
178 ostringstream ss;
179 bool repair = (cmd == "repair");
180 r = omap.check(ss, repair, true);
181 if (r) {
182 std::cerr << ss.str() << std::endl;
183 if (r > 0) {
184 std::cerr << "check got " << r << " error(s)" << std::endl;
185 return 1;
186 }
187 }
188 std::cout << (repair ? "repair" : "check") << " succeeded" << std::endl;
189 return 0;
190 } else if (cmd == "dump-headers") {
191 vector<DBObjectMap::_Header> headers;
192 r = omap.list_object_headers(&headers);
193 if (r < 0) {
194 std::cerr << "list_object_headers got: " << cpp_strerror(r) << std::endl;
195 return 1;
196 }
197 for (auto i : headers)
198 std::cout << i << std::endl;
199 return 0;
200 } else if (cmd == "resetv2") {
201 omap.state.v = 2;
202 omap.state.legacy = false;
203 omap.set_state();
204 } else if (cmd == "compact") {
205 omap.compact();
206 return 0;
207 } else {
208 std::cerr << "Did not recognize command " << cmd << std::endl;
209 return 1;
210 }
211 }