OSDN Git Service

lpdump: print partitions in super in order
authorYifan Hong <elsk@google.com>
Tue, 24 Sep 2019 01:01:20 +0000 (18:01 -0700)
committerYifan Hong <elsk@google.com>
Tue, 24 Sep 2019 02:54:21 +0000 (19:54 -0700)
Easier to understand where are all the extents.
Test: run it

Change-Id: I88511fe4ed0d23882872c7434422f0a42cd3ee1f

partition_tools/lpdump.cc

index 1b8f65e..2eb9f1f 100644 (file)
@@ -23,6 +23,7 @@
 #include <sysexits.h>
 #include <unistd.h>
 
+#include <algorithm>
 #include <iostream>
 #include <optional>
 #include <regex>
@@ -274,6 +275,16 @@ public:
     }
 };
 
+std::optional<std::tuple<std::string, uint64_t>>
+ParseLinearExtentData(const LpMetadata& pt, const LpMetadataExtent& extent) {
+    if (extent.target_type != LP_TARGET_TYPE_LINEAR) {
+        return std::nullopt;
+    }
+    const auto& block_device = pt.block_devices[extent.target_source];
+    std::string device_name = GetBlockDevicePartitionName(block_device);
+    return std::make_tuple(std::move(device_name), extent.target_data);
+}
+
 static void PrintMetadata(const LpMetadata& pt, std::ostream& cout) {
     cout << "Metadata version: " << pt.header.major_version << "." << pt.header.minor_version
          << "\n";
@@ -283,6 +294,8 @@ static void PrintMetadata(const LpMetadata& pt, std::ostream& cout) {
     cout << "Partition table:\n";
     cout << "------------------------\n";
 
+    std::vector<std::tuple<std::string, const LpMetadataExtent*>> extents;
+
     for (const auto& partition : pt.partitions) {
         std::string name = GetPartitionName(partition);
         std::string group_name = GetPartitionGroupName(pt.groups[partition.group_index]);
@@ -303,11 +316,29 @@ static void PrintMetadata(const LpMetadata& pt, std::ostream& cout) {
             } else if (extent.target_type == LP_TARGET_TYPE_ZERO) {
                 cout << "zero";
             }
+            extents.push_back(std::make_tuple(name, &extent));
             cout << "\n";
         }
         cout << "------------------------\n";
     }
 
+    std::sort(extents.begin(), extents.end(), [&](const auto& x, const auto& y) {
+        auto x_data = ParseLinearExtentData(pt, *std::get<1>(x));
+        auto y_data = ParseLinearExtentData(pt, *std::get<1>(y));
+        return x_data < y_data;
+    });
+
+    cout << "Super partition layout:\n";
+    cout << "------------------------\n";
+    for (auto&& [name, extent] : extents) {
+        auto data = ParseLinearExtentData(pt, *extent);
+        if (!data) continue;
+        auto&& [block_device, offset] = *data;
+        cout << block_device << ": " << offset << " .. " << (offset + extent->num_sectors)
+             << ": " << name << " (" << extent->num_sectors << " sectors)\n";
+    }
+    cout << "------------------------\n";
+
     cout << "Block device table:\n";
     cout << "------------------------\n";
     for (const auto& block_device : pt.block_devices) {