-
Notifications
You must be signed in to change notification settings - Fork 1
/
hewlett-read.cpp
353 lines (313 loc) · 9.71 KB
/
hewlett-read.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
#include "BUSE/buse.h"
#include <argp.h>
#include <string>
#include <memory.h>
#include <memory>
#include <iostream>
#include <exception>
#include <limits.h>
#include "smart_array_raid_0_reader.hpp"
#include "smart_array_raid_1_reader.hpp"
#include "smart_array_raid_5_reader.hpp"
#include "smart_array_raid_6_reader.hpp"
#include "smart_array_raid_10_reader.hpp"
#include "smart_array_raid_50_reader.hpp"
#include "smart_array_raid_60_reader.hpp"
using namespace sg;
std::unique_ptr<DriveReader> reader;
struct ProgramOptions
{
/// @brief Stripe size in kilobytes
u32 stripeSize;
u16 parityDelay;
u16 raidLevel;
u16 parityGroups;
u64 size;
u64 offset;
std::vector<std::string> drives;
std::string outputDevice;
};
int read(void *buf, u32 len, u64 offset, void *userdata)
{
try
{
return reader->read(buf, len, offset);
}
catch (std::runtime_error& ex)
{
std::cerr << "Runtime error occured while reading data: " << ex.what() << std::endl;
return -1;
}
}
// Argument parsing
static argp_option options[] = {
{ "stripe-size", 's', "256", 0, "Stripe size in KiB. Default: 256", 0 },
{ "parity-delay", 'p', "16", 0, "Parity delay, P420 Controllers use parity delay for their RAID 5, read more: https://www.freeraidrecovery.com/library/delayed-parity.aspx. Default: 16", 0 },
{ "delay", 'p', 0, OPTION_ALIAS, 0, 0 },
{ "raid", 'r', "<level>", 0, "Raid level, required!", 0 },
{ "output", 'o', "/dev/nbd0", 0, "Output device, it has to be /dev/nbdx. Default: /dev/nbd0", 0 },
{ "parity-groups", 'g', "2", 0, "Parity groups in RAID 50 and 60", 0 },
{ "size", 'S', "0", 0, "Size of logical drives, 0 is maximum possible. Default: 0" },
{ "offset", 'O', "0", 0, "Offset on each physical drive, Default: 0" },
{0}
};
u16 argToU16(std::string arg, std::string argName)
{
try
{
unsigned num = std::stoul(arg);
if (num > USHRT_MAX)
{
throw std::out_of_range("Argument exceeds range");
}
return num;
}
catch(std::invalid_argument const& ex)
{
throw std::invalid_argument("Argument " + argName + " (value:" + arg + ") is invalid. It has to be unsigned 16 bit integer.");
}
catch(std::out_of_range const& ex)
{
throw std::out_of_range("Argument " + argName + " (value:" + arg + ") is too large!. It has to be unsigned 16 bit integer.");
}
}
u32 argToU32(std::string arg, std::string argName)
{
try
{
return std::stoul(arg);
}
catch(std::invalid_argument const& ex)
{
throw std::invalid_argument("Argument " + argName + " (value:" + arg + ") is invalid. It has to be unsigned 32 bit integer.");
}
catch(std::out_of_range const& ex)
{
throw std::out_of_range("Argument " + argName + " (value:" + arg + ") is too large!. It has to be unsigned 32 bit integer.");
}
}
u64 argToU64(std::string arg, std::string argName)
{
try
{
return std::stoull(arg);
}
catch(std::invalid_argument const& ex)
{
throw std::invalid_argument("Argument " + argName + " (value:" + arg + ") is invalid. It has to be unsigned 64 bit integer.");
}
catch(std::out_of_range const& ex)
{
throw std::out_of_range("Argument " + argName + " (value:" + arg + ") is too large!. It has to be unsigned 64 bit integer.");
}
}
error_t parseOpt(int key, char *arg, argp_state *state)
{
ProgramOptions* options = reinterpret_cast<ProgramOptions*>(state->input);
std::string argStr;
switch (key)
{
case 's':
options->stripeSize = argToU32(arg, "stripe-size");
break;
case 'p':
options->parityDelay = argToU16(arg, "parity-delay");
break;
case 'r':
options->raidLevel = argToU16(arg, "raid");
break;
case 'o':
options->outputDevice = arg;
break;
case 'g':
options->parityGroups = argToU16(arg, "parity-groups");
break;
case 'S':
options->size = argToU64(arg, "size");
break;
case 'O':
options->offset = argToU64(arg, "offset");
break;
case ARGP_KEY_ARG:
if (state->arg_num > 256)
{
throw std::invalid_argument("Too many arguments.");
}
argStr = arg;
if (argStr == "X")
{
argStr.clear();
}
options->drives.push_back(argStr);
break;
case ARGP_KEY_END:
if (state->arg_num < 1)
{
std::cerr << "Not enough arguments." << std::endl;
argp_usage(state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
void drivesPathVectorToDeviceReaderVector(
std::vector<std::string>& paths,
std::vector<std::shared_ptr<DriveReader>>& out)
{
for (auto& path : paths)
{
if (!path.empty())
{
out.push_back(std::make_shared<BlockDeviceReader>(path));
}
else
{
// Reader should handle this
out.push_back(nullptr);
}
}
}
void initForRaid0(ProgramOptions &opts)
{
SmartArrayRaid0ReaderOptions readerOpts {
.stripeSize = opts.stripeSize,
.size = opts.size,
.offset = opts.offset
};
drivesPathVectorToDeviceReaderVector(opts.drives, readerOpts.driveReaders);
reader = std::make_unique<SmartArrayRaid0Reader>(readerOpts);
}
void initForRaid1(ProgramOptions &opts)
{
SmartArrayRaid1ReaderOptions readerOpts {
.size = opts.size,
.offset = opts.offset
};
drivesPathVectorToDeviceReaderVector(opts.drives, readerOpts.driveReaders);
reader = std::make_unique<SmartArrayRaid1Reader>(readerOpts);
}
void initForRaid5(ProgramOptions &opts)
{
SmartArrayRaid5ReaderOptions readerOpts {
.stripeSize = opts.stripeSize,
.parityDelay = opts.parityDelay,
.size = opts.size,
.offset = opts.offset
};
drivesPathVectorToDeviceReaderVector(opts.drives, readerOpts.driveReaders);
reader = std::make_unique<SmartArrayRaid5Reader>(readerOpts);
}
void initForRaid6(ProgramOptions &opts)
{
SmartArrayRaid6ReaderOptions readerOpts {
.stripeSize = opts.stripeSize,
.parityDelay = opts.parityDelay,
.size = opts.size,
.offset = opts.offset
};
drivesPathVectorToDeviceReaderVector(opts.drives, readerOpts.driveReaders);
reader = std::make_unique<SmartArrayRaid6Reader>(readerOpts);
}
void initForRaid10(ProgramOptions &opts)
{
SmartArrayRaid10ReaderOptions readerOpts {
.stripeSize = opts.stripeSize,
.size = opts.size,
.offset = opts.offset
};
drivesPathVectorToDeviceReaderVector(opts.drives, readerOpts.driveReaders);
reader = std::make_unique<SmartArrayRaid10Reader>(readerOpts);
}
void initForRaid50(ProgramOptions &opts)
{
SmartArrayRaid50ReaderOptions readerOpts {
.stripeSize = opts.stripeSize,
.parityDelay = opts.parityDelay,
.parityGroups = opts.parityGroups,
.size = opts.size,
.offset = opts.offset
};
drivesPathVectorToDeviceReaderVector(opts.drives, readerOpts.driveReaders);
reader = std::make_unique<SmartArrayRaid50Reader>(readerOpts);
}
void initForRaid60(ProgramOptions &opts)
{
SmartArrayRaid60ReaderOptions readerOpts {
.stripeSize = opts.stripeSize,
.parityDelay = opts.parityDelay,
.parityGroups = opts.parityGroups,
.size = opts.size,
.offset = opts.offset
};
drivesPathVectorToDeviceReaderVector(opts.drives, readerOpts.driveReaders);
reader = std::make_unique<SmartArrayRaid60Reader>(readerOpts);
}
int main(int argc, char** argv)
{
ProgramOptions opts = {
.stripeSize = 256,
.parityDelay = 16,
.raidLevel = 2137,
.outputDevice = "/dev/nbd0"
};
static argp argp = {
.options = options,
.parser = parseOpt,
.args_doc = "DRIVE1 DRIVE2 ...DRIVEN",
.doc = "HP Smart Array Raid Reader.\n\n"
"Drives must be provided in valid order. If you forgot order of drives in the array then use "
"a program packard-tell included with hewlett-read:\n"
"\tpackard-tell /dev/sda /dev/sdb /dev/sdc /dev/sdd\n"
"For missing drives type X instead of their path.\n"
"--raid is required option.\n\n"
"Examples:\n"
"\thewlett-read --raid 5 /dev/sda /dev/sdb /dev/sdc /dev/sdd\n"
"\thewlett-read --raid 5 /dev/sda X /dev/sdc /dev/sdd\n"
"\thewlett-read --raid 6 /dev/sda X /dev/sdc X"
};
int parseRet = argp_parse(&argp, argc, argv, 0, 0, &opts);
if (parseRet != 0)
{
std::cerr << "Arguments parse error, see above." << std::endl;
return -1;
}
switch (opts.raidLevel)
{
case 0:
initForRaid0(opts);
break;
case 1:
initForRaid1(opts);
break;
case 5:
initForRaid5(opts);
break;
case 6:
initForRaid6(opts);
break;
case 10:
initForRaid10(opts);
break;
case 50:
initForRaid50(opts);
break;
case 60:
initForRaid60(opts);
break;
case 2137:
std::cerr << "Error: No RAID level was provided." << std::endl;
return -1;
default:
std::cerr << "RAID " << opts.raidLevel << " is not supported." << std::endl;
return -1;
}
buse_operations ops = {
.read = read,
.size = reader->driveSize(),
.blksize = 512
};
std::cout << "Attaching to " << opts.outputDevice << "..." << std::endl;
buse_main(opts.outputDevice.c_str(), &ops, NULL);
}