]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/ceph_osdomap_tool.cc
update sources to v12.1.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 using namespace std;
27
28 int main(int argc, char **argv) {
29 po::options_description desc("Allowed options");
30 string store_path, cmd, out_path, oid;
31 bool debug = false;
32 desc.add_options()
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")
41 ;
42 po::positional_options_description p;
43 p.add("command", 1);
44
45 vector<string> ceph_option_strings;
46 po::variables_map vm;
47 try {
48 po::parsed_options parsed =
49 po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run();
50 po::store(
51 parsed,
52 vm);
53 po::notify(vm);
54
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;
59 return 1;
60 }
61
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();
66 ++i) {
67 ceph_options.push_back(i->c_str());
68 }
69
70 if (vm.count("debug")) debug = true;
71
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;
78 if (debug) {
79 g_conf->set_val_or_die("log_to_stderr", "true");
80 g_conf->set_val_or_die("err_to_stderr", "true");
81 }
82 g_conf->apply_changes(NULL);
83
84 if (vm.count("help")) {
85 std::cerr << desc << std::endl;
86 return 1;
87 }
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 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;
103 }*/
104 DBObjectMap omap(cct.get(), store);
105 stringstream out;
106 int r = store->open(out);
107 if (r < 0) {
108 std::cerr << "Store open got: " << cpp_strerror(r) << std::endl;
109 std::cerr << "Output: " << out.str() << std::endl;
110 return r;
111 }
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.
115
116 if (cmd == "dump-raw-keys") {
117 KeyValueDB::WholeSpaceIterator i = store->get_iterator();
118 for (i->seek_to_first(); i->valid(); i->next()) {
119 std::cout << i->raw_key() << std::endl;
120 }
121 return 0;
122 } else if (cmd == "dump-raw-key-vals") {
123 KeyValueDB::WholeSpaceIterator i = store->get_iterator();
124 for (i->seek_to_first(); i->valid(); i->next()) {
125 std::cout << i->raw_key() << std::endl;
126 i->value().hexdump(std::cout);
127 }
128 return 0;
129 } else if (cmd == "dump-objects") {
130 vector<ghobject_t> objects;
131 r = omap.list_objects(&objects);
132 if (r < 0) {
133 std::cerr << "list_objects got: " << cpp_strerror(r) << std::endl;
134 return r;
135 }
136 for (vector<ghobject_t>::iterator i = objects.begin();
137 i != objects.end();
138 ++i) {
139 if (vm.count("oid") != 0 && i->hobj.oid.name != oid)
140 continue;
141 std::cout << *i << std::endl;
142 }
143 return 0;
144 } else if (cmd == "dump-objects-with-keys") {
145 vector<ghobject_t> objects;
146 r = omap.list_objects(&objects);
147 if (r < 0) {
148 std::cerr << "list_objects got: " << cpp_strerror(r) << std::endl;
149 return r;
150 }
151 for (vector<ghobject_t>::iterator i = objects.begin();
152 i != objects.end();
153 ++i) {
154 if (vm.count("oid") != 0 && i->hobj.oid.name != oid)
155 continue;
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);
161 }
162 }
163 return 0;
164 } else if (cmd == "check" || cmd == "repair") {
165 ostringstream ss;
166 bool repair = (cmd == "repair");
167 r = omap.check(ss, repair);
168 if (r) {
169 std::cerr << ss.str() << std::endl;
170 if (r > 0) {
171 std::cerr << "check got " << r << " error(s)" << std::endl;
172 return 1;
173 }
174 }
175 std::cout << (repair ? "repair" : "check") << " succeeded" << std::endl;
176 return 0;
177 } else if (cmd == "dump-headers") {
178 vector<DBObjectMap::_Header> headers;
179 r = omap.list_object_headers(&headers);
180 if (r < 0) {
181 std::cerr << "list_object_headers got: " << cpp_strerror(r) << std::endl;
182 return 1;
183 }
184 for (auto i : headers)
185 std::cout << i << std::endl;
186 return 0;
187 } else {
188 std::cerr << "Did not recognize command " << cmd << std::endl;
189 return 1;
190 }
191 }