Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / action / Nbd.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "tools/rbd/ArgumentTypes.h"
5 #include "tools/rbd/Shell.h"
6 #include "tools/rbd/Utils.h"
7 #include "include/stringify.h"
8 #include "common/SubProcess.h"
9 #include <iostream>
10 #include <boost/algorithm/string/predicate.hpp>
11 #include <boost/scope_exit.hpp>
12 #include <boost/program_options.hpp>
13
14 namespace rbd {
15 namespace action {
16 namespace nbd {
17
18 namespace at = argument_types;
19 namespace po = boost::program_options;
20
21 static int call_nbd_cmd(const po::variables_map &vm,
22                         const std::vector<const char*> &args)
23 {
24   char exe_path[PATH_MAX];
25   ssize_t exe_path_bytes = readlink("/proc/self/exe", exe_path,
26                                     sizeof(exe_path) - 1);
27   if (exe_path_bytes < 0) {
28     strcpy(exe_path, "rbd-nbd");
29   } else {
30     if (snprintf(exe_path + exe_path_bytes,
31                  sizeof(exe_path) - exe_path_bytes,
32                  "-nbd") < 0) {
33       return -EOVERFLOW;
34     }
35   }
36
37   SubProcess process(exe_path, SubProcess::KEEP, SubProcess::KEEP, SubProcess::KEEP);
38
39   if (vm.count("conf")) {
40     process.add_cmd_arg("--conf");
41     process.add_cmd_arg(vm["conf"].as<std::string>().c_str());
42   }
43   if (vm.count("cluster")) {
44     process.add_cmd_arg("--cluster");
45     process.add_cmd_arg(vm["cluster"].as<std::string>().c_str());
46   }
47   if (vm.count("id")) {
48     process.add_cmd_arg("--id");
49     process.add_cmd_arg(vm["id"].as<std::string>().c_str());
50   }
51   if (vm.count("name")) {
52     process.add_cmd_arg("--name");
53     process.add_cmd_arg(vm["name"].as<std::string>().c_str());
54   }
55   if (vm.count("mon_host")) {
56     process.add_cmd_arg("--mon_host");
57     process.add_cmd_arg(vm["mon_host"].as<std::string>().c_str());
58   }
59   if (vm.count("keyfile")) {
60     process.add_cmd_arg("--keyfile");
61     process.add_cmd_arg(vm["keyfile"].as<std::string>().c_str());
62   }
63   if (vm.count("keyring")) {
64     process.add_cmd_arg("--keyring");
65     process.add_cmd_arg(vm["keyring"].as<std::string>().c_str());
66   }
67
68   for (std::vector<const char*>::const_iterator p = args.begin();
69        p != args.end(); ++p)
70     process.add_cmd_arg(*p);
71
72   if (process.spawn()) {
73     std::cerr << "rbd: failed to run rbd-nbd: " << process.err() << std::endl;
74     return -EINVAL;
75   } else if (process.join()) {
76     std::cerr << "rbd: rbd-nbd failed with error: " << process.err() << std::endl;
77     return -EINVAL;
78   }
79
80   return 0;
81 }
82
83 void get_show_arguments(po::options_description *positional,
84                         po::options_description *options)
85 { }
86
87 int execute_show(const po::variables_map &vm)
88 {
89   std::vector<const char*> args;
90
91   args.push_back("list-mapped");
92
93   return call_nbd_cmd(vm, args);
94 }
95
96 void get_map_arguments(po::options_description *positional,
97                        po::options_description *options)
98 {
99   at::add_image_or_snap_spec_options(positional, options,
100                                      at::ARGUMENT_MODIFIER_NONE);
101   options->add_options()
102     ("read-only", po::bool_switch(), "map read-only")
103     ("exclusive", po::bool_switch(), "forbid writes by other clients")
104     ("device", po::value<std::string>(), "specify nbd device")
105     ("nbds_max", po::value<std::string>(), "override module param nbds_max")
106     ("max_part", po::value<std::string>(), "override module param max_part");
107 }
108
109 int execute_map(const po::variables_map &vm)
110 {
111   size_t arg_index = 0;
112   std::string pool_name;
113   std::string image_name;
114   std::string snap_name;
115   int r = utils::get_pool_image_snapshot_names(
116     vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
117     &snap_name, utils::SNAPSHOT_PRESENCE_PERMITTED,
118     utils::SPEC_VALIDATION_NONE);
119   if (r < 0) {
120     return r;
121   }
122
123   std::vector<const char*> args;
124
125   args.push_back("map");
126   std::string img;
127   img.append(pool_name);
128   img.append("/");
129   img.append(image_name);
130   if (!snap_name.empty()) {
131     img.append("@");
132     img.append(snap_name);
133   }
134   args.push_back(img.c_str());
135
136   if (vm["read-only"].as<bool>())
137     args.push_back("--read-only");
138
139   if (vm["exclusive"].as<bool>())
140     args.push_back("--exclusive");
141
142   if (vm.count("device")) {
143     args.push_back("--device");
144     args.push_back(vm["device"].as<std::string>().c_str());
145   }
146   if (vm.count("nbds_max")) {
147     args.push_back("--nbds_max");
148     args.push_back(vm["nbds_max"].as<std::string>().c_str());
149   }
150   if (vm.count("max_part")) {
151     args.push_back("--max_part");
152     args.push_back(vm["max_part"].as<std::string>().c_str());
153   }
154
155   return call_nbd_cmd(vm, args);
156 }
157
158 void get_unmap_arguments(po::options_description *positional,
159                    po::options_description *options)
160 {
161   positional->add_options()
162     ("device-spec", "specify nbd device");
163 }
164
165 int execute_unmap(const po::variables_map &vm)
166 {
167   std::string device_name = utils::get_positional_argument(vm, 0);
168   if (!boost::starts_with(device_name, "/dev/")) {
169     device_name.clear();
170   }
171
172   if (device_name.empty()) {
173     std::cerr << "rbd: nbd unmap requires device path" << std::endl;
174     return -EINVAL;
175   }
176
177   std::vector<const char*> args;
178
179   args.push_back("unmap");
180   args.push_back(device_name.c_str());
181
182   return call_nbd_cmd(vm, args);
183 }
184
185 Shell::SwitchArguments switched_arguments({"read-only"});
186
187 Shell::Action action_show(
188   {"nbd", "list"}, {"nbd", "ls"}, "List the nbd devices already used.", "",
189   &get_show_arguments, &execute_show);
190
191 Shell::Action action_map(
192   {"nbd", "map"}, {}, "Map image to a nbd device.", "",
193   &get_map_arguments, &execute_map);
194
195 Shell::Action action_unmap(
196   {"nbd", "unmap"}, {}, "Unmap a nbd device.", "",
197   &get_unmap_arguments, &execute_unmap);
198
199 } // namespace nbd
200 } // namespace action
201 } // namespace rbd