-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageConfig.cpp
More file actions
608 lines (499 loc) · 13.9 KB
/
Copy pathStorageConfig.cpp
File metadata and controls
608 lines (499 loc) · 13.9 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include "StorageConfig.h"
#include <libutils/Logger.h>
#include <libopi/SysInfo.h>
#include <libopi/DiskHelper.h>
using namespace Utils;
using namespace OPI;
#include <iostream>
using namespace std;
namespace KGP
{
namespace Storage
{
namespace Model
{
const vector<TypeEntry<enum Type> > &Model::TypeEntries()
{
static vector<struct TypeEntry<enum Type>> tt=
{
{"undefined", "Undefined", Undefined},
{"static", "Static", Static},
{"dynamic", "Dynamic", Dynamic},
{"unknown", "Unknown", Unknown}
};
return tt;
}
} // NS Model
namespace Physical
{
const vector<TypeEntry<enum Type> > &Physical::TypeEntries()
{
static vector<struct TypeEntry<enum Type>> tt=
{
{"undefined", "Undefined", Undefined},
{"none", "Use local OS partition", None},
{"partition", "Use partition(s) on OS disk", Partition},
{"block", "Use block device(s)", Block},
{"unknown", "Unknown", Unknown}
};
return tt;
}
} // NS Phys
namespace Logical
{
const vector<TypeEntry<enum Type> > &Logical::TypeEntries()
{
static vector<struct TypeEntry<enum Type>> tt=
{
{"undefined", "Undefined", Undefined},
{"none", "Don't use logical volume storage", None},
{"lvm", "Use logical volume to group storage", LVM},
{"unknown", "Unknown", Unknown}
};
return tt;
}
} // NS Logical
namespace Encryption
{
const vector<TypeEntry<enum Type> > &Encryption::TypeEntries()
{
static vector<struct TypeEntry<enum Type>> tt=
{
{"undefined", "Undefined", Undefined},
{"none", "Don't use encryption", None},
{"luks", "Use LUKS encryption on storage", LUKS},
{"unknown", "Unknown", Unknown}
};
return tt;
}
} // NS Encryption
} // NS Storage
static void initStorageConfig()
{
logg << Logger::Notice << "Migrate/create sysconfig entries to use storage section" << lend;
SysConfig cfg(true);
if( SysInfo::isOP() )
{
// OP-Device
logg << Logger::Debug << "Migrating OP Device" << lend;
cfg.PutKey("storage", "model", "static" );
cfg.PutKey("storage", "physical", "block" );
cfg.PutKey("storage", "block_devices", list<string>{ sysinfo.StorageDevice() } );
if( SysInfo::isOpi() )
{
cfg.PutKey("storage", "logical", "none" );
}
else
{
cfg.PutKey("storage", "logical", "lvm" );
cfg.PutKey("storage", "lvm_device", cfg.GetKeyAsString("filesystem", "lvmdevice") );
cfg.PutKey("storage", "lvm_lv", cfg.GetKeyAsString("filesystem", "lvmlv") );
cfg.PutKey("storage", "lvm_vg", cfg.GetKeyAsString("filesystem", "lvmvg"));
}
cfg.PutKey("storage", "encryption", "luks" );
cfg.PutKey("storage", "luks_device", cfg.GetKeyAsString("filesystem", "luksdevice") );
}
else
{
logg << Logger::Debug << "Migrating/creating storage config for none OP Device" << lend;
cfg.PutKey("storage", "model", "dynamic" );
cfg.PutKey("storage", "physical", "undefined" );
cfg.PutKey("storage", "logical", "undefined" );
cfg.PutKey("storage", "encryption", "undefined" );
}
logg << Logger::Notice << "Migration completed" << lend;
}
StorageConfig::StorageConfig():
model(Storage::Model::Undefined),
physical(Storage::Physical::Undefined),
logical(Storage::Logical::Undefined),
encryption(Storage::Encryption::Undefined)
{
if( !this->syscfg.HasScope("storage") )
{
// Migrate config, might belong in a ccheck script but fix here for now
initStorageConfig();
}
this->parseConfig();
}
bool StorageConfig::isStatic()
{
return this->model.Type() == Storage::Model::Static;
}
bool StorageConfig::isValid()
{
if( this->model.Type() == Storage::Model::Static )
{
// Always correct config in static case
return true;
}
// Will recursively check logical and physical layers
return this->encryptionValid();
}
/********************************************************************************************
*
*
*
* Physical storage implementation
*
*
*
*******************************************************************************************/
list<Storage::Physical::Type> StorageConfig::QueryPhysicalStorage()
{
using namespace Storage::Physical;
if( SysInfo::isOpi() || SysInfo::isArmada() )
{
return { Block };
}
return { None, Partition, Block };
}
Storage::Physical::Physical StorageConfig::PhysicalStorage()
{
return this->physical;
}
void StorageConfig::PhysicalStorage(Storage::Physical::Type type)
{
this->physical =Storage::Physical::Physical(type);
SysConfig cfg(true);
cfg.PutKey("storage", "physical", Storage::Physical::Physical::toName(type) );
switch(type)
{
case Storage::Physical::Undefined:
case Storage::Physical::Unknown:
case Storage::Physical::None:
cfg.RemoveKey("storage", "partition_path");
cfg.RemoveKey("storage", "block_devices");
break;
case Storage::Physical::Partition:
cfg.RemoveKey("storage", "block_devices");
break;
case Storage::Physical::Block:
cfg.RemoveKey("storage", "partition_path");
break;
}
}
bool StorageConfig::UsePhysicalStorage(Storage::Physical::Type type)
{
return this->physical.Type() == type;
}
list<string> StorageConfig::PhysicalDevices()
{
if( this->physical.Type() == Storage::Physical::Partition )
{
return { this->syscfg.GetKeyAsString("storage","partition_path") };
}
if( this->physical.Type() == Storage::Physical::Block )
{
return this->syscfg.GetKeyAsStringList("storage","block_devices");
}
return {};
}
void StorageConfig::PhysicalStorage(const string &partition)
{
if( this->physical.Type() != Storage::Physical::Partition )
{
throw std::runtime_error("Illegal Physical storage type for partition "s + this->physical.Name() );
}
SysConfig cfg(true);
cfg.PutKey("storage", "partition_path", partition);
cfg.RemoveKey("storage", "block_devices");
}
void StorageConfig::PhysicalStorage(const list<string> &devices)
{
if( this->physical.Type() != Storage::Physical::Block )
{
throw std::runtime_error("Illegal Physical storage type for block devices "s + this->physical.Name() );
}
SysConfig cfg(true);
cfg.PutKey("storage", "block_devices", devices);
cfg.RemoveKey("storage", "partition_path");
}
/********************************************************************************************
*
*
*
* Logical storage implementation
*
*
*
*******************************************************************************************/
list<Storage::Logical::Type> StorageConfig::QueryLogicalStorage(Storage::Physical::Type type)
{
using namespace Storage;
using namespace Storage::Logical;
if( SysInfo::isOpi() )
{
return {None};
}
if( SysInfo::isArmada() )
{
return {LVM};
}
switch (type) {
case Physical::Undefined:
case Physical::Unknown:
case Physical::None:
return {};
break;
case Physical::Partition:
case Physical::Block:
return {None, LVM};
break;
}
// Should never get here
return {};
}
Storage::Logical::Logical StorageConfig::LogicalStorage()
{
return this->logical;
}
void StorageConfig::LogicalStorage(Storage::Logical::Type type)
{
using namespace Storage::Logical;
this->logical = type;
SysConfig cfg(true);
cfg.PutKey("storage","logical", Logical::toName(type));
switch( type )
{
case Undefined:
case Unknown:
case None:
cfg.RemoveKey("storage", "lvm_device");
cfg.RemoveKey("storage", "lvm_lv");
cfg.RemoveKey("storage", "lvm_vg");
break;
case LVM:
cfg.PutKey("storage", "lvm_device", Storage::Logical::DefaultLVMDevice);
cfg.PutKey("storage", "lvm_lv", Storage::Logical::DefaultLV);
cfg.PutKey("storage", "lvm_vg", Storage::Logical::DefaultVG);
break;
}
}
bool StorageConfig::UseLogicalStorage(Storage::Logical::Type type)
{
return this->logical.Type() == type;
}
list<string> StorageConfig::LogicalDevices()
{
if( this->logical.Type() == Storage::Logical::LVM )
{
return { this->syscfg.GetKeyAsString("storage","lvm_device") };
}
return {};
}
void StorageConfig::LogicalDevices(const list<string> &devices)
{
if( this->logical.Type() != Storage::Logical::LVM )
{
throw std::runtime_error("Illegal set logical devices when type is "s + this->logical.Name() );
}
if( devices.size() != 1 )
{
throw std::runtime_error("Only one logical device currently supported provided: "s + std::to_string(devices.size()));
}
SysConfig cfg(true);
cfg.PutKey("storage", "lvm_device",devices.front() );
}
void StorageConfig::LogicalDefaults()
{
this->LogicalStorage(Storage::Logical::LVM);
}
/********************************************************************************************
*
*
*
* Encryption storage implementation
*
*
*
*******************************************************************************************/
list<Storage::Encryption::Type> StorageConfig::QueryEncryptionStorage(Storage::Physical::Type phys, Storage::Logical::Type logical)
{
using namespace Storage;
using namespace Storage::Encryption;
// OPI & Keep, static config
if( SysInfo::isOpi() || SysInfo::isArmada() )
{
return { LUKS };
}
// Currently need some block storage to be able to encrypt
if( phys != Physical::Partition && phys != Physical::Block )
{
return { None };
}
if( logical == Logical::Undefined || logical == Logical::Unknown )
{
return { None };
}
return {None, LUKS};
}
Storage::Encryption::Encryption StorageConfig::EncryptionStorage()
{
return this->encryption;
}
void StorageConfig::EncryptionStorage(Storage::Encryption::Type type)
{
this->encryption = type;
SysConfig cfg(true);
cfg.PutKey("storage", "encryption", Storage::Encryption::Encryption::toName(type) );
switch( type )
{
case Storage::Encryption::None:
case Storage::Encryption::Undefined:
case Storage::Encryption::Unknown:
{
cfg.RemoveKey("storage", "luks_device");
break;
}
case Storage::Encryption::LUKS:
SysConfig(true).PutKey("storage", "luks_device", Storage::Encryption::DefaultEncryptionDevice);
break;
}
}
bool StorageConfig::UseEncryption(Storage::Encryption::Type type)
{
return this->encryption.Type() == type;
}
list<string> StorageConfig::EncryptionDevices()
{
if( this->encryption.Type() == Storage::Encryption::LUKS )
{
return {this->syscfg.GetKeyAsString("storage","luks_device") };
}
return {};
}
void StorageConfig::EncryptionDevices(const list<string> &devices)
{
if( this->encryption.Type() != Storage::Encryption::LUKS )
{
throw std::runtime_error("Illegal encryption devices when type is "s + this->encryption.Name() );
}
if( devices.size() != 1)
{
throw std::runtime_error("Only one crypto device currently supported provided: "s + std::to_string(devices.size()));
}
SysConfig(true).PutKey("storage", "luks_device", devices.front());
}
void StorageConfig::EncryptionDefaults()
{
this->EncryptionStorage(Storage::Encryption::LUKS);
}
/********************************************************************************************
*
*
*
* Remaining functionality
*
*
*
*******************************************************************************************/
string StorageConfig::StorageDevice()
{
// If we use encryption, that is top device.
if( this->UseEncryption(Storage::Encryption::LUKS) )
{
return this->syscfg.GetKeyAsString("storage","luks_device");
}
//If no encryption, logical device could be top device
if( this->UseLogicalStorage(Storage::Logical::LVM) )
{
return this->syscfg.GetKeyAsString("storage","lvm_device");
}
// Use unencrypted external drive? Can only manage one device!
// This device is expected to have one partition that should be used
if( this->UsePhysicalStorage( Storage::Physical::Block ) )
{
list<string> devs = this->syscfg.GetKeyAsStringList("storage", "block_devices");
if( devs.size() != 1 )
{
logg << Logger::Error << "Unable to deterimine storage device. Got " << devs.size() << " disks" << lend;
//TODO: Throw exception? This is a faulty config
return "";
}
return DiskHelper::PartitionName(devs.front());
}
//Uses separate partition?
if( this->UsePhysicalStorage( Storage::Physical::Partition ) )
{
return this->syscfg.GetKeyAsString("storage","partition_path");
}
logg << Logger::Notice << "Unable to determine final storage device path" << lend;
return "";
}
void StorageConfig::parseConfig()
{
using namespace Storage;
this->model = Model::Model::fromName( this->syscfg.GetKeyAsString("storage","model").c_str() );
this->physical = Physical::Physical::fromName( this->syscfg.GetKeyAsString("storage","physical").c_str() );
this->logical = Logical::Logical::fromName( this->syscfg.GetKeyAsString("storage","logical").c_str() );
this->encryption = Encryption::Encryption::fromName( this->syscfg.GetKeyAsString("storage","encryption").c_str() );
}
bool StorageConfig::encryptionValid()
{
using namespace Storage;
using namespace Storage::Encryption;
switch (this->encryption.Type() )
{
case None:
return this->logicalValid();
break;
case LUKS:
if( SysConfig().HasKey("storage", "luks_device") )
{
// Has underlaying block device and a valid logical config
return (this->UsePhysicalStorage(Physical::Partition) || this->UsePhysicalStorage(Physical::Block)) && this->logicalValid();
}
break;
default:
break;
}
return false;
}
bool StorageConfig::logicalValid()
{
using namespace Storage;
using namespace Storage::Logical;
switch( this->logical.Type() )
{
case None:
return this->physicalValid();
break;
case LVM:
{
SysConfig cfg;
if( cfg.HasKey("storage", "lvm_device") && cfg.HasKey("storage", "lvm_lv") && cfg.HasKey("storage", "lvm_vg") )
{
// Has underlaying block storage and a valid physical config
return (this->UsePhysicalStorage(Physical::Partition) || this->UsePhysicalStorage(Physical::Block)) && this->physicalValid();
}
break;
}
default:
break;
}
return false;
}
bool StorageConfig::physicalValid()
{
using namespace Storage::Physical;
SysConfig cfg;
bool partition = cfg.HasKey("storage", "partition_path");
bool block = cfg.HasKey("storage", "block_devices");
switch (this->physical.Type())
{
case None:
return !partition && !block;
break;
case Partition:
return partition && !block;
break;
case Block:
return !partition && block;
break;
default:
break;
}
return false;
}
} // NS KGP