Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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, oid, backend;
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     ("backend", po::value<string>(&backend),
42      "DB backend (default rocksdb)")
43     ;
44   po::positional_options_description p;
45   p.add("command", 1);
46
47   vector<string> ceph_option_strings;
48   po::variables_map vm;
49   try {
50     po::parsed_options parsed =
51       po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run();
52     po::store(
53               parsed,
54               vm);
55     po::notify(vm);
56
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;
61     return 1;
62   }
63
64   vector<const char *> ceph_options, def_args;
65   ceph_options.reserve(ceph_option_strings.size());
66   for (vector<string>::iterator i = ceph_option_strings.begin();
67        i != ceph_option_strings.end();
68        ++i) {
69     ceph_options.push_back(i->c_str());
70   }
71
72   if (vm.count("debug")) debug = true;
73
74   auto cct = global_init(
75     &def_args, ceph_options, CEPH_ENTITY_TYPE_OSD,
76     CODE_ENVIRONMENT_UTILITY_NODOUT, 0);
77   common_init_finish(g_ceph_context);
78   g_ceph_context->_conf->apply_changes(NULL);
79   g_conf = g_ceph_context->_conf;
80   if (debug) {
81     g_conf->set_val_or_die("log_to_stderr", "true");
82     g_conf->set_val_or_die("err_to_stderr", "true");
83   }
84   g_conf->apply_changes(NULL);
85
86   if (vm.count("help")) {
87     std::cerr << desc << std::endl;
88     return 1;
89   }
90
91   if (vm.count("omap-path") == 0) {
92     std::cerr << "Required argument --omap-path" << std::endl;
93     return 1;
94   }
95
96   if (vm.count("command") == 0) {
97     std::cerr << "Required argument --command" << std::endl;
98     return 1;
99   }
100
101   if (vm.count("backend") == 0) {
102     backend = "rocksdb";
103   }
104
105   KeyValueDB* store(KeyValueDB::create(g_ceph_context, backend, store_path));
106   if (store == NULL) {
107     std::cerr << "Invalid backend '" << backend << "' specified" << std::endl;
108     return 1;
109   }
110   /*if (vm.count("paranoid")) {
111     std::cerr << "Enabling paranoid checks" << std::endl;
112     store->options.paranoid_checks = true;
113     }*/
114   DBObjectMap omap(cct.get(), store);
115   stringstream out;
116   int r = store->open(out);
117   if (r < 0) {
118     std::cerr << "Store open got: " << cpp_strerror(r) << std::endl;
119     std::cerr << "Output: " << out.str() << std::endl;
120     return r;
121   }
122   // We don't call omap.init() here because it will repair
123   // the DBObjectMap which we might want to examine for diagnostic
124   // reasons.  Instead use --command repair.
125
126   omap.get_state();
127   std::cout << "Version: " << (int)omap.state.v << std::endl;
128   std::cout << "Seq: " << omap.state.seq << std::endl;
129   std::cout << "legacy: " << (omap.state.legacy ? "true" : "false") << std::endl;
130
131   if (cmd == "dump-raw-keys") {
132     KeyValueDB::WholeSpaceIterator i = store->get_iterator();
133     for (i->seek_to_first(); i->valid(); i->next()) {
134       std::cout << i->raw_key() << std::endl;
135     }
136     return 0;
137   } else if (cmd == "dump-raw-key-vals") {
138     KeyValueDB::WholeSpaceIterator i = store->get_iterator();
139     for (i->seek_to_first(); i->valid(); i->next()) {
140       std::cout << i->raw_key() << std::endl;
141       i->value().hexdump(std::cout);
142     }
143     return 0;
144   } else if (cmd == "dump-objects") {
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 << *i << std::endl;
157     }
158     return 0;
159   } else if (cmd == "dump-objects-with-keys") {
160     vector<ghobject_t> objects;
161     r = omap.list_objects(&objects);
162     if (r < 0) {
163       std::cerr << "list_objects got: " << cpp_strerror(r) << std::endl;
164       return r;
165     }
166     for (vector<ghobject_t>::iterator i = objects.begin();
167          i != objects.end();
168          ++i) {
169       if (vm.count("oid") != 0 && i->hobj.oid.name != oid)
170         continue;
171       std::cout << "Object: " << *i << std::endl;
172       ObjectMap::ObjectMapIterator j = omap.get_iterator(ghobject_t(i->hobj));
173       for (j->seek_to_first(); j->valid(); j->next()) {
174         std::cout << j->key() << std::endl;
175         j->value().hexdump(std::cout);
176       }
177     }
178     return 0;
179   } else if (cmd == "check" || cmd == "repair") {
180     ostringstream ss;
181     bool repair = (cmd == "repair");
182     r = omap.check(ss, repair, true);
183     if (r) {
184       std::cerr << ss.str() << std::endl;
185       if (r > 0) {
186         std::cerr << "check got " << r << " error(s)" << std::endl;
187         return 1;
188       }
189     }
190     std::cout << (repair ? "repair" : "check") << " succeeded" << std::endl;
191     return 0;
192   } else if (cmd == "dump-headers") {
193     vector<DBObjectMap::_Header> headers;
194     r = omap.list_object_headers(&headers);
195     if (r < 0) {
196       std::cerr << "list_object_headers got: " << cpp_strerror(r) << std::endl;
197       return 1;
198     }
199     for (auto i : headers)
200       std::cout << i << std::endl;
201     return 0;
202   } else if (cmd == "resetv2") {
203     omap.state.v = 2;
204     omap.state.legacy = false;
205     omap.set_state();
206   } else {
207     std::cerr << "Did not recognize command " << cmd << std::endl;
208     return 1;
209   }
210 }