-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackard-tell.cpp
285 lines (240 loc) · 8.4 KB
/
packard-tell.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <cmath>
#include "drive_reader.hpp"
#include "metadata_parser.hpp"
#include "types.hpp"
using namespace sg;
// #define DEBUG
struct DriveNumPair
{
std::string path;
u8 num;
};
const int DRIVE_METADATA_NEGATIVE_OFFSET = 31 * 1024 * 1024;
void readMetadata(std::string path, u8* out, u32 bytesToLoad)
{
BlockDeviceReader reader(path);
reader.read(out, bytesToLoad, reader.driveSize() - DRIVE_METADATA_NEGATIVE_OFFSET);
}
u8 readDriveNumber(std::string path)
{
BlockDeviceReader reader(path);
u8 ret;
reader.read(&ret, 1, reader.driveSize() - DRIVE_METADATA_NEGATIVE_OFFSET);
return ret;
}
std::string findDrivePathByNum(std::vector<DriveNumPair>& drives, u8 num)
{
auto res = std::find_if(
drives.begin(),
drives.end(),
[num](DriveNumPair& x) {
return x.num == num;
}
);
return res == drives.end() ? "" : res->path;
}
std::string findDriveSerialByBayNumber(P420Metadata& metadata, u8 baynum)
{
auto res = std::find_if(
metadata.physicalDrives.begin(),
metadata.physicalDrives.end(),
[baynum](PhysicalDrive& x) {
return x.bayNumber == baynum;
}
);
return res == metadata.physicalDrives.end() ? "" : res->serialNumber;
}
std::string sizeToHuman(u64 size)
{
static const std::string suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB" };
double s = size;
int i = 0;
for (; i < 6 && s >= 1024.0; i++)
{
s = s / 1024;
}
std::stringstream ss;
if (s == static_cast<u32>(s))
{
ss << static_cast<u32>(s);
}
else
{
ss << std::setprecision(2) << std::fixed << s;
}
ss << suffixes[i];
return ss.str();
}
std::string formatSize(u64 size)
{
if (size > 1024)
{
return std::to_string(size) + "B (" + sizeToHuman(size) + ")";
}
return std::to_string(size) + "B";
}
std::string join(std::vector<std::string> vec, std::string delimiter)
{
return vec.empty() ? "" : std::accumulate(
std::next(vec.begin()),
vec.end(),
vec[0],
[delimiter](std::string a, std::string b) {
return a + delimiter + b;
}
);
}
std::string generateCommandToMount(LogicalDrive& ld, std::vector<DriveNumPair>& drives)
{
std::vector<std::string> arguments;
arguments.push_back("./hewlett-read");
u8 missingDrives = 0;
for (auto& pd : ld.physicalDrives)
{
std::string path = findDrivePathByNum(drives, pd);
if (path.empty())
{
missingDrives++;
path = "X";
}
arguments.push_back(path);
}
switch (ld.raidLevel)
{
case 0:
case 1:
case 10:
case 5:
case 6:
if (ld.physicalDriveCount - missingDrives < ld.physicalDrivesNeededToRun)
{
return "Error: Too many missing drives";
}
break;
case 50:
case 60:
arguments.push_back("--parity-groups=" + std::to_string(ld.parityGroups));
// I won't calculate here missing drives because I would have to
// tell it for every parity group. hewlett-read will fail anyway
// if certain drives will be missing.
break;
default:
return "Error: Uknown RAID level";
}
arguments.push_back("--raid=" + std::to_string(ld.raidLevel));
if (ld.raidLevel != 1)
{
arguments.push_back("--stripe-size=" + std::to_string(ld.stripeSizeInBytes / 1024));
}
arguments.push_back("--size=" + std::to_string(ld.logicalDriveSizeInBytes));
if (ld.offsetOnEachPhysicalDriveInBytes > 0)
{
arguments.push_back("--offset=" + std::to_string(ld.offsetOnEachPhysicalDriveInBytes));
}
return join(arguments, " ");
}
void printLogicalDrive(LogicalDrive& ld, std::vector<DriveNumPair>& drives, P420Metadata& metadata, std::string indent)
{
std::string raidLevel = ld.raidLevel != 255 ? std::to_string(ld.raidLevel) : "UNKNOWN";
std::cout << "Label: " << ld.label << ":" << std::endl;
std::cout << indent << "Raid level: " << raidLevel << std::endl;
if (ld.raidLevel == 50 || ld.raidLevel == 60)
{
std::cout << indent << "Parity groups: " << ld.parityGroups << std::endl;
}
std::cout << indent << "Size: " << formatSize(ld.logicalDriveSizeInBytes) << std::endl;
std::cout << indent << "Stripe size: " << formatSize(ld.stripeSizeInBytes) << std::endl;
std::cout << indent << "Offset on physical drive: " << formatSize(ld.offsetOnEachPhysicalDriveInBytes) << std::endl;
std::cout << indent << "Space taken on physical drive: " << formatSize(ld.spaceTakenOnEachPhysicalDriveInBytes) << std::endl;
std::cout << indent << "Physical drives:" << std::endl;
u8 drivesFound = 0;
for (auto pd : ld.physicalDrives)
{
std::string path = findDrivePathByNum(drives, pd);
path = path.empty() ? "MISSING" : path;
// From my observation drive number in logical drive is bayNumber + 7
// But I am not sure, so serial number is experimental feature
std::string serial = findDriveSerialByBayNumber(metadata, pd - 7);
std::cout << indent << indent << std::setw(3) << pd << ": " << path << "\tSerial?: " << serial << std::endl;
}
std::cout << indent << "Command: " << generateCommandToMount(ld, drives) << std::endl;
}
std::string trimRight(std::string s)
{
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
int main(int argc, char** argv)
{
const std::string INDENT = " ";
if (argc < 2)
{
std::cout << "Scuro Guardiano's Smart Array Order Teller v1.3.3.7" << std::endl;
std::cout << "Have you forgotten drives order in your raid array? Don't worry!" << std::endl;
std::cout << "Just give me the drives and I will tell everything I know about logical drives on them!" << std::endl;
std::cout << "Provide at least 1 drive, logical, right?" << std::endl << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << "\tpackard-tell drive1 drive2 ...driveN" << std::endl;
std::cout << "Example:" << std::endl;
std::cout << "\tpackard-tell /dev/sda /dev/sdb /dev/sdc /dev/sdd" << std::endl << std::endl;
return 0;
}
std::vector<DriveNumPair> drives;
for (int i = 1; i < argc; i++)
{
std::string drivePath = argv[i];
drives.push_back({
.path = drivePath,
.num = readDriveNumber(drivePath)
});
}
std::sort(
drives.begin(),
drives.end(),
[](auto& p1, auto& p2) {
return p1.num < p2.num;
}
);
std::cout << "Correct drives order: ";
for (auto& drive : drives)
{
std::cout << drive.path << " ";
}
std::cout << std::endl << std::endl;
P420Metadata metadata;
u8 metadataBin[0x28000];
readMetadata(drives[0].path, metadataBin, 0x28000);
parseMetadata(metadataBin, &metadata);
std::cout << "======== PARSED CONTROLLER METADATA ========" << std::endl;
std::cout << "Controller Serial Number: " << metadata.controllerSerialNumber << std::endl;
std::cout << "Logical drives count: " << metadata.logicalDrives.size() << std::endl;
std::cout << "Physical drives count: " << metadata.physicalDrives.size() << std::endl << std::endl;
std::cout << "Physical drives:" << std::endl;
for (auto& pd : metadata.physicalDrives)
{
// From my observation drive number in logical drive is bayNumber + 7
// But I am not sure, so path is experimental feature.
// I am also not sure about Box number.
std::string path = findDrivePathByNum(drives, pd.bayNumber + 7);
std::cout << INDENT
<< "Box?: " << std::setw(3) << pd.boxNumber
<< INDENT << "Bay: " << std::setw(3) << pd.bayNumber
<< INDENT << "Serial: " << trimRight(pd.serialNumber)
<< INDENT << "Path?: " << (path.empty() ? "MISSING" : path)
<< std::endl;
}
std::cout << std::endl;
std::cout << "Logical drives:" << std::endl;
for (auto& ld : metadata.logicalDrives)
{
printLogicalDrive(ld, drives, metadata, INDENT);
std::cout << std::endl;
}
}