-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.cpp
81 lines (67 loc) · 2.2 KB
/
main.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
#include <liblp/liblp.h>
#include <iostream>
#include <sstream>
#include <string_view>
using namespace android::fs_mgr;
using std::cerr;
using std::cout;
using std::endl;
constexpr std::string_view kDmPrefix = "dynpart-";
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: dmsetup create --concise \"$(" << argv[0] << " device)\""
<< endl;
return 1;
}
auto metadata = ReadMetadata(argv[1], 0);
if (!metadata) {
cerr << "Failed to parse metadata from \"" << argv[1] << "\"" << endl;
return 1;
}
std::string table;
// Code structure taken from Android's system/core/fs_mgr/fs_mgr_dm_linear.cpp
for (auto partition : metadata->partitions) {
if (!partition.num_extents) {
cerr << "Skipping zero-length logical partition: "
<< GetPartitionName(partition) << endl;
continue;
}
if (partition.attributes & LP_PARTITION_ATTR_DISABLED) {
cerr << "Skipping disabled partition: " << GetPartitionName(partition)
<< endl;
continue;
}
std::ostringstream line;
bool read_only = partition.attributes & LP_PARTITION_ATTR_READONLY;
line << kDmPrefix << GetPartitionName(partition) << ",,,"
<< (read_only ? "ro" : "rw");
uint64_t sector = 0;
for (size_t i = 0; i < partition.num_extents; i++) {
const auto& extent = metadata->extents[partition.first_extent_index + i];
switch (extent.target_type) {
case LP_TARGET_TYPE_ZERO:
line << "," << sector << " " << extent.num_sectors << " zero";
break;
case LP_TARGET_TYPE_LINEAR: {
if (extent.target_source != 0) {
cerr << "This utility does not yet support multiple block devices"
<< endl;
return 1;
}
line << "," << sector << " " << extent.num_sectors << " linear "
<< argv[1] << " " << extent.target_data;
break;
}
default:
cerr << "Unknown target type in metadata: " << extent.target_type
<< endl;
return false;
}
sector += extent.num_sectors;
}
if (!table.empty()) table += ";";
table += line.str();
}
cout << table << endl;
return 0;
}