-
Notifications
You must be signed in to change notification settings - Fork 26
/
client.cpp
412 lines (367 loc) · 13.3 KB
/
client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (c) 2020 SUSE LLC
*
* Licensed under LGPL-2.1 (see LICENSE)
*/
#include <wnbd.h>
#include "client.h"
#include "cmd.h"
#include "usage.h"
#include "version.h"
#include <boost/exception/diagnostic_information.hpp>
#include <iostream>
#include <iomanip>
namespace po = boost::program_options;
using namespace std;
vector<Client::Command*> Client::commands;
Client::Command* Client::get_command(string name)
{
for (Client::Command *command : Client::commands) {
if (command->name == name)
return command;
for (auto alias: command->aliases) {
if (alias == name)
return command;
}
}
return nullptr;
}
// Make sure to pass the EXACT SAME type that was used when defining the option,
// otherwise Boost will throw an exception.
template <class T>
T safe_get_param(const po::variables_map& vm, string name, T default_val = T())
{
if (vm.count(name)) {
return vm[name].as<T>();
}
return default_val;
}
void Client::get_common_options(po::options_description &options)
{
options.add_options()
("debug", po::bool_switch(), "Enable debug logging.");
}
void handle_common_options(po::variables_map &vm)
{
bool debug = safe_get_param<bool>(vm, "debug");
WnbdLogLevel log_level = debug ? WnbdLogLevelDebug : WnbdLogLevelInfo;
WnbdSetLogLevel(log_level);
}
DWORD Client::execute(int argc, const char** argv)
{
vector<string> args;
args.insert(args.end(), argv + 1, argv + argc);
// The first must argument must be the command name.
if (args.size() == 0) {
args.push_back("help");
}
string command_name = args[0];
Client::Command* command = get_command(command_name);
if (!command) {
cerr << "Unknown command: " << command_name << endl << endl
<< "See wnbd-client --help." << endl;
return ERROR_INVALID_PARAMETER;
}
// Remove the command from the list of arguments.
args.erase(args.begin());
try {
po::positional_options_description positional_opts;
po::options_description named_opts;
if (command->get_options) {
command->get_options(positional_opts, named_opts);
}
get_common_options(named_opts);
po::variables_map vm;
po::store(po::command_line_parser(args)
.options(named_opts)
.positional(positional_opts)
.run(), vm);
po::notify(vm);
handle_common_options(vm);
return command->execute(vm);
}
catch (po::required_option& e) {
cerr << "wnbd-client: " << e.what() << endl;
return ERROR_INVALID_PARAMETER;
}
catch (po::too_many_positional_options_error&) {
cerr << "wnbd-client: too many arguments" << endl;
return ERROR_INVALID_PARAMETER;
}
catch (po::error& e) {
cerr << "wnbd-client: " << e.what() << endl;
return ERROR_INVALID_PARAMETER;
}
catch (...) {
cerr << "wnbd-client: Caught unexpected exception." << endl << endl;
cerr << boost::current_exception_diagnostic_information() << endl;
return ERROR_INTERNAL_ERROR;
}
}
DWORD execute_version(const po::variables_map &vm)
{
return CmdVersion();
}
DWORD execute_help(const po::variables_map &vm)
{
if (vm.count("command-name")) {
string command_name = vm["command-name"].as<string>();
return print_command_help(command_name);
}
print_commands();
return 0;
}
void get_help_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
positonal_opts.add("command-name", 1);
named_opts.add_options()
("command-name", po::value<string>(), "Command name.");
}
DWORD execute_list(const po::variables_map &vm)
{
return CmdList();
}
void get_show_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
positonal_opts.add("instance-name", 1);
named_opts.add_options()
("instance-name", po::value<string>()->required(), "Disk identifier.");
}
DWORD execute_show(const po::variables_map &vm)
{
return CmdShow(safe_get_param<string>(vm, "instance-name").c_str());
}
void get_map_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
positonal_opts.add("instance-name", 1)
.add("hostname", 1);
named_opts.add_options()
("instance-name", po::value<string>()->required(), "Disk identifier.")
("hostname", po::value<string>()->required(), "NBD server hostname.")
("port", po::value<DWORD>()->default_value(10809),
"NBD server port number. Default: 10809.")
("export-name", po::value<string>(),
"NBD export name, defaults to <instance-name>.")
("skip-handshake", po::bool_switch(),
"Skip NBD handshake and jump right into the transmission phase. "
"This is useful for minimal NBD servers. If set, the disk size "
"and block size must be provided.")
("disk-size", po::value<UINT64>(),
"The disk size. Ignored when using NBD handshake.")
("block-size", po::value<UINT32>(),
"The block size. Ignored when using NBD handshake.")
("read-only", po::bool_switch(), "Enable disk read-only mode.");
}
DWORD execute_map(const po::variables_map& vm)
{
return CmdMap(
safe_get_param<string>(vm, "instance-name").c_str(),
safe_get_param<string>(vm, "hostname").c_str(),
safe_get_param<DWORD>(vm, "port"),
safe_get_param<string>(vm, "export-name").c_str(),
safe_get_param<UINT64>(vm, "disk-size"),
safe_get_param<UINT32>(vm, "block-size"),
safe_get_param<bool>(vm, "skip-handshake"),
safe_get_param<bool>(vm, "read-only"));
}
void get_unmap_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
positonal_opts.add("instance-name", 1);
named_opts.add_options()
("instance-name", po::value<string>()->required(), "Disk identifier.")
("hard-disconnect", po::bool_switch(),
"Perform a hard disconnect. Warning: pending IO operations as "
"well as unflushed data will be discarded.")
("no-hard-disconnect-fallback", po::bool_switch(),
"Immediately return an error if the soft disconnect fails instead "
"of attempting a hard disconnect as fallback.")
("soft-disconnect-timeout",
po::value<DWORD>()->default_value(WNBD_DEFAULT_RM_TIMEOUT_MS / 1000),
("Soft disconnect timeout in seconds. The soft disconnect operation "
"uses PnP to notify the Windows storage stack that the device is "
"going to be disconnected. Storage drivers can block this operation "
"if there are pending operations, unflushed caches or open handles. "
"Default: " + to_string(WNBD_DEFAULT_RM_TIMEOUT_MS / 1000) + ".").c_str())
("soft-disconnect-retry-interval",
po::value<DWORD>()->default_value(WNBD_DEFAULT_RM_RETRY_INTERVAL_MS / 1000),
("Soft disconnect retry interval in seconds. "
"Default: " + to_string(WNBD_DEFAULT_RM_RETRY_INTERVAL_MS / 1000) + ".").c_str());
}
DWORD execute_unmap(const po::variables_map& vm)
{
return CmdUnmap(
safe_get_param<string>(vm, "instance-name").c_str(),
safe_get_param<bool>(vm, "hard-disconnect"),
safe_get_param<bool>(vm, "no-hard-disconnect-fallback"),
safe_get_param<DWORD>(vm, "soft-disconnect-timeout"),
safe_get_param<DWORD>(vm, "soft-disconnect-retry-interval"));
}
DWORD execute_stats(const po::variables_map& vm)
{
return CmdStats(
safe_get_param<string>(vm, "instance-name").c_str());
}
void get_stats_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
positonal_opts.add("instance-name", 1);
named_opts.add_options()
("instance-name", po::value<string>()->required(), "Disk identifier.");
}
void get_list_opt_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
named_opts.add_options()
("persistent", po::bool_switch(), "List persistent options only.");
}
DWORD execute_list_opt(const po::variables_map& vm)
{
return CmdListOpt(
safe_get_param<bool>(vm, "persistent"));
}
void get_opt_getter_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
positonal_opts.add("name", 1);
named_opts.add_options()
("name", po::value<string>()->required(), "Option name.")
("persistent", po::bool_switch(), "Get the persistent value, if set.");
}
DWORD execute_get_opt(const po::variables_map& vm)
{
return CmdGetOpt(
safe_get_param<string>(vm, "name").c_str(),
safe_get_param<bool>(vm, "persistent"));
}
void get_opt_setter_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
positonal_opts.add("name", 1)
.add("value", 1);
named_opts.add_options()
("name", po::value<string>()->required(), "Option name.")
("value", po::value<string>()->required(), "Option value.")
("persistent", po::bool_switch(), "Persist the value across reboots.");
}
DWORD execute_set_opt(const po::variables_map& vm)
{
return CmdSetOpt(
safe_get_param<string>(vm, "name").c_str(),
safe_get_param<string>(vm, "value").c_str(),
safe_get_param<bool>(vm, "persistent"));
}
void get_reset_opt_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
positonal_opts.add("name", 1);
named_opts.add_options()
("name", po::value<string>()->required(), "Option name.")
("persistent", po::bool_switch(), "Persist the value across reboots.");
}
DWORD execute_reset_opt(const po::variables_map& vm)
{
return CmdResetOpt(
safe_get_param<string>(vm, "name").c_str(),
safe_get_param<bool>(vm, "persistent"));
}
DWORD execute_install(const po::variables_map& vm)
{
return CmdInstall(
safe_get_param<string>(vm, "filename").c_str());
}
void get_install_args(
po::positional_options_description& positonal_opts,
po::options_description& named_opts)
{
positonal_opts.add("filename", 1);
named_opts.add_options()
("filename", po::value<string>()->required(),
"The OEM driver information (wnbd.inf) path.");
}
DWORD execute_uninstall(const po::variables_map& vm)
{
return CmdUninstall();
}
void get_reset_adapter_args(
po::positional_options_description &positonal_opts,
po::options_description &named_opts)
{
named_opts.add_options()
("hard-disconnect-mappings", po::bool_switch(),
"Forcefully removes existing disk mappings.")
("reset-timeout",
po::value<DWORD>()->default_value(10),
"Adapter reset timeout in seconds. The PnP reset request can be "
"vetoed if the device is still in use. Default: 10s.")
("reset-retry-interval",
po::value<DWORD>()->default_value(1),
"Adapter reset retry interval in seconds. Default: 1s.");
}
DWORD execute_reset_adapter(const po::variables_map &vm)
{
return CmdResetAdapter(
safe_get_param<bool>(vm, "hard-disconnect-mappings"),
safe_get_param<DWORD>(vm, "reset-timeout"),
safe_get_param<DWORD>(vm, "reset-retry-interval"));
}
Client::Command commands[] = {
Client::Command(
"version", {"-v"}, "Get the client, library and driver version.",
execute_version),
Client::Command(
"help", {"-h", "--help"},
"List all commands or get more details about a specific command.",
execute_help, get_help_args),
Client::Command(
"list", {"ls"}, "List WNBD disks.",
execute_list),
Client::Command(
"show", {}, "Show detailed disk information.",
execute_show, get_show_args),
Client::Command(
"map", {}, "Create a new disk mapping, connecting to the "
"specified NBD server.",
execute_map, get_map_args),
Client::Command(
"unmap", {"rm"}, "Remove disk mapping.",
execute_unmap, get_unmap_args),
Client::Command(
"stats", {}, "Get disk stats.",
execute_stats, get_stats_args),
Client::Command(
"list-opt", {}, "List driver options.",
execute_list_opt, get_list_opt_args),
Client::Command(
"get-opt", {}, "Get driver option.",
execute_get_opt, get_opt_getter_args),
Client::Command(
"set-opt", {}, "Set driver option.",
execute_set_opt, get_opt_setter_args),
Client::Command(
"reset-opt", {}, "Reset driver option.",
execute_reset_opt, get_reset_opt_args),
Client::Command(
"install-driver", {}, "Install WNBD driver and create its adapter.",
execute_install, get_install_args),
Client::Command(
"uninstall-driver", {}, "Hard remove all disk mappings and adapters"
" and uninstall all WNBD driver instances.",
execute_uninstall),
Client::Command(
"reset-adapter", {}, "Resets the WNBD adapter using PnP. Existing "
"disk mappings need to be removed.",
execute_reset_adapter, get_reset_adapter_args),
};