0

I would like to add an empty partition of type 0x00 to the beginning of the protective MBR of a GPT-formatted disk. I've found a way to do this, but the method relies on fdisk enhancements not present in Linux versions (specifically, the -p/-f options available on fdisk on FreeBSD but not Linux). I've also found a way to add an empty partition after the first partition.

My protective MBR currently has one entry of type 0xEE. I would like to make this the second entry in the partition table and to add an empty entry of type 0x00 in front of it, without disturbing the GPT partition table or any of the GPT partitions on the disk. How can this be accomplished? The protective MBR can be edited with fdisk -t dos <device>, but fdisk refuses to add partitions because there are no free sectors available. And I'm unsure whether deleting the protective MBR partition and remaking two new partitions will affect the GPT partitions on the disk.

6
  • Perhaps sfdisk can do something similar to what the BSD version can do... cyberciti.biz/faq/… Commented Jan 25, 2022 at 1:50
  • Are you trying to create a hybrid MBR/gpt which is not recommended. rodsbooks.com/gdisk/hybrid.html It was used in past for BIOS install of Windows on gpt when Windows did not have UEFI boot but Mac computers did. If MBR & gpt get out of sync you can have major issues.
    – oldfred
    Commented Jan 25, 2022 at 17:47
  • @rfmodulator: Thanks, the --dump and --backup options of sfdisk look promising. @oldfred: No, I'm trying to work around a badly written UEFI+BIOS that refuses to boot from GPT disks.
    – user001
    Commented Jan 25, 2022 at 20:24
  • @rfmodulator: sfdisk checks whether the sectors are valid and refuses to write a table with invalid values, as must be the case for the desired partition table. Partial output of sfdisk --label-nested dos /dev/sdb < pmbr.dump: ">>> Script header accepted.\n [...] Created a new DOS disklabel with disk identifier 0x00000000.\n /dev/sdb1: Created a new partition 1 of type 'Empty' and of size 2 TiB.\n /dev/sdb2: Start sector 1 out of range.\n Failed to add #2 partition: Numerical result out of range.\n Leaving."
    – user001
    Commented Jan 25, 2022 at 23:07
  • Even if you -f/--force it? Commented Jan 25, 2022 at 23:14

1 Answer 1

1

This can be accomplished using dd.

Save the MBR in case it needs to be restored:

# dd bs=1 count=512 status=none if=/dev/sdb > foo

View the MBR partition table (64 bytes starting at byte offset 446 on sector 1). The example output is formatted by hexdump so that each partition table entry occupies one line.

# dd if=/dev/sdb bs=1 skip=446 count=64 status=none | hexdump -ve '16/1 " %02X" "\n"'
    00 01 00 00 EE FF FF FF 01 00 00 00 FF FF FF FF
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Copy the line for first partition and change the zeroeth byte to 0x80 to set the boot flag for that partition, if needed. Change the fourth byte to 0x00 to set the partition to empty type. (The significance of each byte is described on Wikipedia.) Replace the first partition with the edited line and move the original first partition to the second position.

    80 01 00 00 00 FF FF FF 01 00 00 00 FF FF FF FF
    00 01 00 00 EE FF FF FF 01 00 00 00 FF FF FF FF
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Write the modified hexadecimal substring to the disk to add an empty partition before the 0xEE partition:

# printf '\x80\x01\x00\x00\x00\xFF\xFF\xFF\x01\x00\x00\x00\xFF\xFF\xFF\xFF\x00\x01\x00\x00\xEE\xFF\xFF\xFF\x01\x00\x00\x00\xFF\xFF\xFF\xFF' | dd bs=1 seek=446 of=/dev/sdb

Verify the changes:

# dd if=/dev/sdb bs=1 skip=446 count=64 status=none | hexdump -ve '16/1 " %02X" "\n"'
80 01 00 00 00 FF FF FF 01 00 00 00 FF FF FF FF
00 01 00 00 EE FF FF FF 01 00 00 00 FF FF FF FF
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .