-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathlvm.cpp
488 lines (423 loc) · 14.4 KB
/
lvm.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* Ext2read
* File: lvm.cpp
**/
/**
* Copyright (C) 2006 2010 by Manish Regmi (regmi dot manish at gmail.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**/
#include <stdlib.h>
#include <sstream>
#include <QRegExp>
#include "lvm.h"
LVM::LVM(FileHandle handle, lloff_t offset, Ext2Read *rd)
{
pv_handle = handle;
pv_offset = offset;
ext2read = rd;
}
LVM::~LVM()
{
}
VolumeGroup *LVM::find_volgroup(QString &uuid)
{
VolumeGroup *grp;
list<VolumeGroup *>::iterator i;
list <VolumeGroup *> grouplist = ext2read->get_volgroups();
for(i = grouplist.begin(); i != grouplist.end(); i++)
{
grp = (*i);
if(grp->uuid.compare(uuid) == 0)
{
return grp;
}
}
return NULL;
}
VolumeGroup *LVM::add_volgroup(QString &uuid, QString &name, int seq, int size)
{
VolumeGroup *vol;
//list <VolumeGroup *> grouplist = ext2read->get_volgroups();
vol = new VolumeGroup(uuid, name, seq, size);
if(!vol)
return NULL;
ext2read->groups.push_back(vol);
LOG("Volgroup added \n");
return vol;
}
int LVM::scan_pv()
{
int ret;
PV_LABEL_HEADER *header;
PV_LABEL *label;
lloff_t sector;
int length;
char buffer[SECTOR_SIZE];
char *metadata;
for(int i = 0; i < 4; i++)
{
sector = pv_offset + i;
ret = read_disk(pv_handle, buffer, sector, 1, SECTOR_SIZE);
header = (PV_LABEL_HEADER *) &buffer[0];
if(strncmp(header->pv_name, "LABELONE", LVM_SIGLEN) != 0)
{
continue;
//LOG("Invalid label. The partition is not LVM2 volume\n");
//return -1;
}
LOG("PV Metadata: %s %UUID=%s offset %d \n",header->pv_name, header->pv_uuid, header->pv_labeloffset);
strncpy(uuid, header->pv_uuid, UUID_LEN);
uuid[UUID_LEN] = '\0';
sector = (header->pv_labeloffset/SECTOR_SIZE) + pv_offset;
read_disk(pv_handle, buffer, sector, 1, SECTOR_SIZE);
label = (PV_LABEL *) &buffer[0];
sector = pv_offset + ((label->pv_offset_low + label->pv_offset_high)/SECTOR_SIZE);
length = (label->pv_length + SECTOR_SIZE - 1)/SECTOR_SIZE;
metadata = new char[length * SECTOR_SIZE];
read_disk(pv_handle, metadata, sector, length, SECTOR_SIZE);
metadata[label->pv_length] = 0;
LOG("\n%s", metadata);
pv_metadata = QString::fromUtf8(metadata, label->pv_length);
parse_metadata();
delete [] metadata;
break;
}
return 0;
}
// NOTE: Do error checking
int LVM::parse_metadata()
{
int num, num2, numbase;
QString volname, suuid;
int seq = 0, size = 0;
bool ok;
VolumeGroup *grp;
QByteArray ba;
num = pv_metadata.indexOf("{");
volname = pv_metadata.left(num - 1);
num = pv_metadata.indexOf(QRegExp("[a-zA-Z0-9]*-{1,}[a-zA-Z0-9]*"), 0);
if(num > 0)
{
suuid = pv_metadata.mid(num, 38);
suuid.replace("-", "");
}
num = pv_metadata.indexOf(QRegExp("[0-9]"), num + 38);
if(num > 0)
{
seq = pv_metadata.mid(num, 1).toInt(&ok);
if(!ok)
{
LOG("Cannot Parse LVM Metadata :-( \n");
return -1;
}
}
num = pv_metadata.indexOf(QRegExp("[0-9]+"), num + 1);
if(num > 0)
{
size = pv_metadata.mid(num, 5).toInt(&ok);
if(!ok)
{
LOG("Cannot Parse LVM Metadata :-( \n");
return -1;
}
}
LOG("Volgroup found seq %d, extent_size %d\n",seq, size);
grp = find_volgroup(suuid);
if(!grp)
{
grp = add_volgroup(suuid, volname, seq, size);
grp->set_ext2read(ext2read);
}
// Parse Physical Volume
lloff_t dev_size;
uint32_t pe_start, pe_count;
num = pv_metadata.indexOf("physical_volumes", 0);
if(num < 0)
return -1;
while((num = pv_metadata.indexOf(QRegExp("pv[0-9\\s\\t]+\\{"), num)) > 0)
{
num = pv_metadata.indexOf(QRegExp("[a-zA-Z0-9]*-{1,}[a-zA-Z0-9]*"), num);
if(num < 0)
break;
suuid = pv_metadata.mid(num, 38);
suuid.replace("-", "");
num += 38;
numbase = num;
num = pv_metadata.indexOf(QRegExp("dev_size"), num);
num = pv_metadata.indexOf(QRegExp("[0-9]+"), num);
num2 = pv_metadata.indexOf(QRegExp("\\n"), num);
dev_size = pv_metadata.mid(num, num2-num).toULongLong(&ok);
if(!ok)
{
LOG("Cannot Parse LVM Metadata (Physical Volume) :-( \n");
return -1;
}
num = pv_metadata.indexOf(QRegExp("pe_start"), numbase);
num = pv_metadata.indexOf(QRegExp("[0-9]+"), num);
num2 = pv_metadata.indexOf(QRegExp("\\n"), num);
pe_start = pv_metadata.mid(num, num2-num).toUInt(&ok);
if(!ok)
{
LOG("Cannot Parse LVM Metadata :-( \n");
return -1;
}
num = pv_metadata.indexOf(QRegExp("pe_count"), numbase);
num = pv_metadata.indexOf(QRegExp("[0-9]+"), num);
num2 = pv_metadata.indexOf(QRegExp("\\n"), num);
pe_count = pv_metadata.mid(num, num2-num).toUInt(&ok);
if(!ok)
{
LOG("Cannot Parse LVM Metadata (Physical Volume) :-( \n");
return -1;
}
LOG("Physical Volume found. start %d, count %d, size %Ld\n", pe_start, pe_count, dev_size);
PhysicalVolume *pvol;
pvol = grp->find_physical_volume(suuid);
num2 = suuid.compare(uuid);
if(!pvol && (num2 == 0))
{
pvol = grp->add_physical_volume(suuid, dev_size, pe_start, pe_count, pv_handle, pv_offset);
}
}
// Parse Logical Volume
int nsegs;
num = pv_metadata.indexOf(QRegExp("logical_volumes"), 0);
if(num < 0)
return -1;
num = pv_metadata.indexOf(QRegExp("\\n"), num);
num += 2;
while((num = pv_metadata.indexOf(QRegExp("[a-zA-Z_0-9\\s\\t]+\\{"), num)) > 0)
{
QString lvolname = volname;
num2 = pv_metadata.indexOf(QRegExp("[\\s\\t]+\\{"), num);
lvolname.append("_");
lvolname.append(pv_metadata.mid(num+1, num2-num));
num = pv_metadata.indexOf(QRegExp("[a-zA-Z0-9]*-{1,}[a-zA-Z0-9]*"), num);
if(num < 0)
break;
suuid = pv_metadata.mid(num, 38);
suuid.replace("-", "");
num += 38;
num = pv_metadata.indexOf("flags", num);
num = pv_metadata.indexOf(QRegExp("[0-9]+"), num);
num2 = pv_metadata.indexOf(QRegExp("\\n"), num);
nsegs = pv_metadata.mid(num, num2-num).toInt(&ok);
if(!ok)
{
LOG("Cannot Parse LVM Metadata (Logical Volume) :-( \n");
return -1;
}
LogicalVolume *lvol;
uint32_t start_extent, extent_count;
lvol = grp->find_logical_volume(suuid);
if(!lvol)
lvol = grp->add_logical_volume(suuid, nsegs, lvolname);
LOG("Logical Volume found. Name %s, segments %d\n", lvol->volname.toUtf8().data(), nsegs);
for(int i = 0; i < nsegs; i++)
{
num = pv_metadata.indexOf(QRegExp("segment[0-9]+"), num);
num += 8;
numbase = num;
num = pv_metadata.indexOf("start_extent", num);
num = pv_metadata.indexOf(QRegExp("[0-9]+"), num);
num2 = pv_metadata.indexOf(QRegExp("\\n"), num);
start_extent = pv_metadata.mid(num, num2-num).toInt(&ok);
num = pv_metadata.indexOf("extent_count", numbase);
num = pv_metadata.indexOf(QRegExp("[0-9]+"), num);
num2 = pv_metadata.indexOf(QRegExp("\\n"), num);
extent_count = pv_metadata.mid(num, num2-num).toInt(&ok);
// Multiple stripes NOT Implemented: we only support linear for now.
lv_segment *seg = new lv_segment(start_extent, extent_count);
seg->stripe = new struct stripe;
seg->stripe->stripe_pv = 0;
num = pv_metadata.indexOf(QRegExp("pv[0-9]+"), num);
num += 4;
num = pv_metadata.indexOf(QRegExp("[0-9]+"), num);
num2 = pv_metadata.indexOf(QRegExp("\\n"), num);
seg->stripe->stripe_start_extent = pv_metadata.mid(num, num2-num).toInt(&ok);
num = num2;
seg->pvolumes = NULL; // we do the segment -> pv mapping later because this pv might not be found yet
lvol->segments.push_back(seg);
LOG("Segment found. start %d, count %d\n", start_extent + seg->stripe->stripe_start_extent, extent_count);
}
}
return 0;
}
VolumeGroup::VolumeGroup(QString &id, QString &name, int seq, int size)
{
uuid = id;
volname = name;
seqno = seq;
extent_size = size;
}
VolumeGroup::~VolumeGroup()
{
list<PhysicalVolume *>::iterator i;
list<LogicalVolume *>::iterator j;
for(i = pvolumes.begin(); i != pvolumes.end(); i++)
{
delete (*i);
}
for(j = lvolumes.begin(); j != lvolumes.end(); j++)
{
delete (*j);
}
}
PhysicalVolume *VolumeGroup::find_physical_volume(QString &id)
{
PhysicalVolume *pvol;
list<PhysicalVolume *>::iterator i;
for(i = pvolumes.begin(); i != pvolumes.end(); i++)
{
pvol = (*i);
if(pvol->uuid.compare(id) == 0)
{
return pvol;
}
}
return NULL;
}
PhysicalVolume *VolumeGroup::add_physical_volume(QString &id, lloff_t devsize, uint32_t start, uint32_t count, FileHandle file, lloff_t dsk_offset)
{
PhysicalVolume *pvol;
pvol = new PhysicalVolume(id, devsize, start, count, file, dsk_offset);
if(!pvol)
return NULL;
pvolumes.push_back(pvol);
return pvol;
}
LogicalVolume *VolumeGroup::find_logical_volume(QString &id)
{
LogicalVolume *lvol;
list<LogicalVolume *>::iterator i;
for(i = lvolumes.begin(); i != lvolumes.end(); i++)
{
lvol = (*i);
if(lvol->uuid.compare(id) == 0)
{
return lvol;
}
}
return NULL;
}
LogicalVolume *VolumeGroup::add_logical_volume(QString &id, int count, QString &vname)
{
LogicalVolume *lvol;
lvol = new LogicalVolume(id, count, vname, this);
if(!lvol)
return NULL;
lvolumes.push_back(lvol);
return lvol;
}
void VolumeGroup::logical_mount()
{
//EXT2_SUPER_BLOCK sblock;
LogicalVolume *lvol;
//PhysicalVolume *pvol;
Ext2Partition *partition;
lv_segment *seg;
lv_segment *root = NULL;
list<LogicalVolume *>::iterator i;
list<lv_segment *>::iterator j;
list<PhysicalVolume *>::iterator k;
lloff_t start;
int index = 0;
LOG("Mouning Logical VOLUMES\n");
for(i = lvolumes.begin(); i != lvolumes.end(); i++)
{
lvol = (*i);
for(j = lvol->segments.begin(); j != lvol->segments.end(); j++)
{
seg = (*j);
if(seg->start_extent == 0)
root = seg;
index = 0;
for(k = pvolumes.begin(); k != pvolumes.end(); k++)
{
if(seg->stripe->stripe_pv == index)
{
seg->pvolumes = (*k);
break;
}
index++;
}
}
if(!root)
continue;
int off = ((root->start_extent + root->stripe->stripe_start_extent) * lvol->this_group->extent_size);
LOG("PE start %d offset %d extoff %d B=%d A=%d %s\n", root->pvolumes->pe_start,
root->pvolumes->offset, lvol->this_group->extent_size, root->start_extent, root->stripe->stripe_start_extent, lvol->volname.toUtf8().data());
start = root->pvolumes->pe_start + root->pvolumes->offset + off;
partition = new Ext2Partition(root->pvolumes->dev_size, start, SECTOR_SIZE, root->pvolumes->handle, lvol);
if(partition->is_valid)
{
QByteArray ba;
ba = lvol->volname.toUtf8();
partition->set_image_name(ba.data());
LOG("adding %s\n", partition->get_linux_name().c_str());
ext2read->add_partition(partition);
}
else
{
LOG("Invalid Partition %s\n", partition->get_linux_name().c_str());
delete partition;
}
}
}
PhysicalVolume::PhysicalVolume(QString &id, lloff_t devsize, uint32_t start, uint32_t count, FileHandle file, lloff_t dsk_offset)
{
uuid = id;
dev_size = devsize;
pe_start = start;
pe_count = count;
handle = file;
offset = dsk_offset;
}
LogicalVolume::LogicalVolume(QString &id, int nsegs, QString &vname, VolumeGroup *vol)
{
uuid = id;
segment_count = nsegs;
this_group = vol;
volname = vname;
}
LogicalVolume::~LogicalVolume()
{
}
lloff_t LogicalVolume::lvm_mapper(lloff_t sectno)
{
lv_segment *seg;
lloff_t sect_mapped = 0;
uint32_t extent_no, extent_offset;
list<lv_segment *>::iterator iterate;
extent_no = sectno / this_group->extent_size;
extent_offset = sectno % this_group->extent_size;
for(iterate = segments.begin(); iterate != segments.end(); iterate++)
{
seg = (*iterate);
if((extent_no >= seg->start_extent) && (extent_no < (seg->start_extent + seg->extent_count)))
{
sect_mapped = (lloff_t)(extent_no * this_group->extent_size) + extent_offset;
sect_mapped += seg->pvolumes->pe_start + seg->pvolumes->offset + seg->stripe->stripe_start_extent;
break;
}
}
if(sect_mapped == 0)
{
LOG("Error in LVM Mapping \n");
}
return sect_mapped;
}