Subject: Re: Parted destroys mbr_signature (Disk Identifier) when adding MBR bootcode

From: Matthijs Kooijman <[email-masked]>
Date: Mon, 13 May 2019 16:30:32 +0200

Hi folks,

one more addition: Below is an example of reproducing this problem on an
Ubuntu Disco system running parted 3.2-25. I haven't tried git master,
but looking at the code, I'm pretty sure the problem stil exists there.

Gr.

Matthijs

# Use fdisk to create an empty partition table without any boot code
matthijs@grubby:~$ truncate -s 8M tmp.img
matthijs@grubby:~$ echo -e 'o\nw' | fdisk tmp.img

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x0298967f.

Command (m for help): Created a new DOS disklabel with disk identifier 0x94bfb6ca.

Command (m for help): The partition table has been altered.
Syncing disks.

matthijs@grubby:~$ fdisk -l tmp.img | grep identifier
Disk identifier: 0x94bfb6ca
matthijs@grubby:~$ head -c 512 tmp.img | hd
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  ca b6 bf 94 00 00 00 00  |................|
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

# Then use parted to add a partition
matthijs@grubby:~$ parted tmp.img mkpart p fat32 4096s 8191s
/sys/firmware/dmi/tables/smbios_entry_point: Permission denied
/dev/mem: Permission denied
WARNING: You are not superuser.  Watch out for permissions.

# This has changed the disk identifier (and added bootcode)
matthijs@grubby:~$ fdisk -l tmp.img | grep identifier
Disk identifier: 0x5d7de418
matthijs@grubby:~$ head -c 512 tmp.img | hd
00000000  fa b8 00 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |................|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  18 e4 7d 5d 00 00 00 00  |..........}]....|
000001c0  01 20 0c 03 20 3f 00 10  00 00 00 10 00 00 00 00  |. .. ?..........|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200