[Libguestfs] [PATCH nbdkit 2/4] partition: Support MBR logical partitions.

Richard W.M. Jones rjones at redhat.com
Sun Jan 20 20:57:06 UTC 2019


---
 filters/partition/nbdkit-partition-filter.pod |   5 -
 filters/partition/partition-mbr.c             | 108 ++++++++++++++++--
 tests/test-partitioning1.sh                   |  22 +++-
 3 files changed, 117 insertions(+), 18 deletions(-)

diff --git a/filters/partition/nbdkit-partition-filter.pod b/filters/partition/nbdkit-partition-filter.pod
index 4a615b6..ccd1b52 100644
--- a/filters/partition/nbdkit-partition-filter.pod
+++ b/filters/partition/nbdkit-partition-filter.pod
@@ -19,11 +19,6 @@ This works like the C<qemu-nbd -P> option.
 The opposite of this filter is L<nbdkit-partitioning-plugin(1)> which
 adds a virtual partition table to a file or files.
 
-=head1 NOTE
-
-Only MBR primary partitions and GPT partition tables are supported.
-MBR logical partitions are B<not> supported.
-
 =head1 PARAMETERS
 
 =over 4
diff --git a/filters/partition/partition-mbr.c b/filters/partition/partition-mbr.c
index f679db0..8e61128 100644
--- a/filters/partition/partition-mbr.c
+++ b/filters/partition/partition-mbr.c
@@ -36,14 +36,20 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <inttypes.h>
 #include <string.h>
+#include <errno.h>
 
 #include <nbdkit-filter.h>
 
 #include "byte-swapping.h"
+#include "isaligned.h"
 
 #include "partition.h"
 
+/* See also linux.git/block/partitions/msdos.c:is_extended_partition */
+#define is_extended(byte) ((byte) == 0x5 || (byte) == 0xf || (byte) == 0x85)
+
 struct mbr_partition {
   uint8_t part_type_byte;
   uint32_t start_sector;
@@ -69,20 +75,98 @@ find_mbr_partition (struct nbdkit_next_ops *next_ops, void *nxdata,
 {
   int i;
   struct mbr_partition partition;
+  uint32_t ep_start_sector, ep_nr_sectors;
+  uint64_t ebr, next_ebr;
+  uint8_t sector[512];
 
-  if (partnum > 4) {
-    nbdkit_error ("MBR logical partitions are not supported");
+  if (partnum <= 4) {           /* Primary partition. */
+    for (i = 0; i < 4; ++i) {
+      get_mbr_partition (mbr, i, &partition);
+      if (partition.nr_sectors > 0 &&
+          partition.part_type_byte != 0 &&
+          !is_extended (partition.part_type_byte) &&
+          partnum == i+1) {
+        *offset_r = partition.start_sector * 512;
+        *range_r = partition.nr_sectors * 512;
+        return 0;
+      }
+    }
+  }
+  else {                        /* Logical partition. */
+    /* Find the extended partition. */
+    for (i = 0; i < 4; ++i) {
+      get_mbr_partition (mbr, i, &partition);
+      if (partition.nr_sectors > 0 &&
+          is_extended (partition.part_type_byte)) {
+        goto found_extended;
+      }
+    }
+    nbdkit_error ("MBR logical partition %d (>= 5) selected, but there is no extended partition in the partition table",
+                  partnum);
     return -1;
-  }
-
-  for (i = 0; i < 4; ++i) {
-    get_mbr_partition (mbr, i, &partition);
-    if (partition.nr_sectors > 0 &&
-        partition.part_type_byte != 0 &&
-        partnum == i+1) {
-      *offset_r = partition.start_sector * 512;
-      *range_r = partition.nr_sectors * 512;
-      return 0;
+
+  found_extended:
+    ep_start_sector = partition.start_sector;
+    ep_nr_sectors = partition.nr_sectors;
+    ebr = ep_start_sector * UINT64_C(512);
+
+    /* This loop will terminate eventually because we only accept
+     * links which strictly increase the EBR pointer.
+     */
+    for (i = 5; ; ++i) {
+      /* Read the EBR sector. */
+      if (next_ops->pread (nxdata, sector, sizeof sector, ebr, 0,
+                           &errno) == -1)
+        return -1;
+
+      if (i == partnum) {
+        uint64_t offset, range;
+
+        /* First entry in EBR points to the logical partition. */
+        get_mbr_partition (sector, 0, &partition);
+
+        /* The first entry start sector is relative to the EBR. */
+        offset = ebr + partition.start_sector * UINT64_C(512);
+        range = partition.nr_sectors * UINT64_C(512);
+
+        /* Logical partition cannot be before the corresponding EBR,
+         * and it cannot extend beyond the enclosing extended
+         * partition.
+         */
+        if (offset <= ebr ||
+            offset + range >
+              ((uint64_t)ep_start_sector + ep_nr_sectors) * 512) {
+          nbdkit_error ("logical partition start or size out of range "
+                        "(offset=%" PRIu64 ", range=%" PRIu64 ", "
+                        "ep:startsect=%" PRIu32 ", ep:nrsects=%" PRIu32 ")",
+                        offset, range, ep_start_sector, ep_nr_sectors);
+          return -1;
+        }
+        *offset_r = offset;
+        *range_r = range;
+        return 0;
+      }
+
+      /* Second entry in EBR links to the next EBR. */
+      get_mbr_partition (sector, 1, &partition);
+
+      /* All zeroes means the end of the chain. */
+      if (partition.start_sector == 0 && partition.nr_sectors == 0)
+        break;
+
+      /* The second entry start sector is relative to the start to the
+       * extended partition.
+       */
+      next_ebr = ((uint64_t)ep_start_sector + partition.start_sector) * 512;
+
+      /* Make sure the next EBR > current EBR. */
+      if (next_ebr <= ebr) {
+        nbdkit_error ("invalid EBR chain: "
+                      "next EBR %" PRIu64 " <= current EBR %" PRIu64,
+                      next_ebr, ebr);
+        return -1;
+      }
+      ebr = next_ebr;
     }
   }
 
diff --git a/tests/test-partitioning1.sh b/tests/test-partitioning1.sh
index 76ab43b..8aa45b9 100755
--- a/tests/test-partitioning1.sh
+++ b/tests/test-partitioning1.sh
@@ -77,7 +77,27 @@ nbdkit -f -v -D partitioning.regions=1 -U - \
 # Contents of partitioning1.out should be identical to file-data.
 cmp file-data partitioning1.out
 
-# Same test with GPT and more partitions.
+# Same test with > 4 MBR partitions.
+# Note we select partition 6 because partition 4 is the extended partition.
+nbdkit -f -v -D partitioning.regions=1 -U - \
+       --filter=partition \
+       partitioning \
+       partitioning1-p1 \
+       partitioning1-p2 \
+       partitioning1-p3 \
+       partitioning1-p4 \
+       type-guid=A2A0D0EB-E5B9-3344-87C0-68B6B72699C7 \
+       file-data \
+       type-guid=AF3DC60F-8384-7247-8E79-3D69D8477DE4 \
+       partitioning1-p5 \
+       partitioning1-p6 \
+       partition-type=mbr \
+       partition=6 \
+       --run 'qemu-img convert $nbd partitioning1.out'
+
+cmp file-data partitioning1.out
+
+# Same test with GPT.
 nbdkit -f -v -D partitioning.regions=1 -U - \
        --filter=partition \
        partitioning \
-- 
2.20.1




More information about the Libguestfs mailing list