Skip to contentSkip to conversation

tools: pygrub: if partition table is empty, try treating as a whole disk

PATCH In review Ian Campbell 3 participants 4 messages Nov 5, 2015 – Nov 16, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: pygrub: if partition table is empty, try treating as a whole disk

pygrub (in identify_disk_image()) detects a DOS style partition table via the presence of the 0xaa55 signature at the end of the first sector of the disk.

However this signature is also present in whole-disk configurations when there is an MBR on the disk. Many filesystems (e.g. ext[234]) include leading padding in their on disk format specifically to enable this.

So if we think we have a DOS partition table but do not find any actual partition table entries we may as well try looking at it as a whole disk image. Worst case is we probe and find there isn't anything there.

This was reported by Sjors Gielen in Debian bug #745419. The fix was inspired by a patch by Adi Kriegisch in
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=745419#27

Tested by genext2fs'ing my /boot into a new raw image (works) and then:
dd if=/usr/lib/grub/i386-pc/g2ldr.mbr of=img conv=notrunc bs=512 count=1

to add an MBR (with 0xaa55 signature) to it, which after this patch also works.

---
 tools/pygrub/src/pygrub | 5 +++++
 1 file changed, 5 insertions(+)
tools/pygrub/src/pygrub +5 -0
index e4aedda..40f9584 100755
@@ -156,6 +156,11 @@ def get_partition_offsets(file):
         else:
             part_offs.append(offset)
 
+    # We thought we had a DOS partition table, but didn't find any
+    # actual valid partition entries. This can happen because an MBR
+    # (e.g. grubs) may contain the same signature.
+    if not part_offs: part_offs = [0]
+
     return part_offs
 
 class GrubLineEditor(curses.textpad.Textbox):
Signature · 1 line
2.1.4
Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines · Ian Campbell

pygrub (in identify_disk_image()) detects a DOS style partition table via the presence of the 0xaa55 signature at the end of the first sector of the disk.

However this signature is also present in whole-disk configurations when there is an MBR on the disk. Many filesystems (e.g. ext[234]) include leading padding in their on disk format specifically to enable this.

So if we think we have a DOS partition table but do not find any actual partition table entries we may as well try looking at it as a whole disk image. Worst case is we probe and find there isn't anything there.

This was reported by Sjors Gielen in Debian bug #745419. The fix was inspired by a patch by Adi Kriegisch in
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=745419#27

Tested by genext2fs'ing my /boot into a new raw image (works) and then:

Quoted 7 lines
   dd if=/usr/lib/grub/i386-pc/g2ldr.mbr of=img conv=notrunc bs=512 count=1

to add an MBR (with 0xaa55 signature) to it, which after this patch also works.

Quoted 23 lines
---
 tools/pygrub/src/pygrub | 5 +++++
 1 file changed, 5 insertions(+)
tools/pygrub/src/pygrub +5 -0
index e4aedda..40f9584 100755
@@ -156,6 +156,11 @@ def get_partition_offsets(file):
         else:
             part_offs.append(offset)
 
+    # We thought we had a DOS partition table, but didn't find any
+    # actual valid partition entries. This can happen because an MBR
+    # (e.g. grubs) may contain the same signature.
+    if not part_offs: part_offs = [0]
+
     return part_offs
 
 class GrubLineEditor(curses.textpad.Textbox):
Signature · 1 line
2.1.4
Acked-by
#2 lore raw

Wei Liu writes ("Re: [PATCH] tools: pygrub: if partition table is empty, try treating as a whole disk"):

Quoted 4 lines · Ian Campbell
Quoted 3 lines

pygrub (in identify_disk_image()) detects a DOS style partition table via the presence of the 0xaa55 signature at the end of the first sector of the disk.

...

Quoted 1 line

In line with my usual policy about pygrub compatibility problems, I intend to backport this to stable trees.

Ian.

author Applied
#3 lore raw

Control: tag -1 +fixed-upstream

Quoted 10 lines · Ian Jackson

Wei Liu writes ("Re: [PATCH] tools: pygrub: if partition table is empty, try treating as a whole disk"):

Quoted 4 lines · Ian Campbell
Quoted 3 lines

pygrub (in identify_disk_image()) detects a DOS style partition table via the presence of the 0xaa55 signature at the end of the first sector of the disk.

...

Quoted 1 line

applied, thanks.

Quoted 2 lines

In line with my usual policy about pygrub compatibility problems, I intend to backport this to stable trees.

Good idea.

Ian.