<div dir="ltr"><div>FYI</div><div><br></div><div>Just found interesting behavior of sgdisk (it is used in daemon code for various things) which might surprise us: sgdisk won't work if it finds "valid" MBR and GPT partition tables on the disk. In this case sgdisk fails with "Invalid partition data!" message.</div><div><br></div><div>How to reproduce (esiest way is to use DISPART command line tool in Windows but you can do the same via Disk Manager UI):</div><div>1. Have an empty disk in Windows</div><div>2. Launch DISKPART (with elevated privileges)</div><div>3. Select corresponding disk by number (SELECT DISK N)</div><div>4. Create GPT partition table (CONVERT GPT)</div><div>5. Convert to MBR partition table (CONVERT MBR)</div><div>6. Attach disk to guestfish and try using, for example, part-get-disk-guid command on affected disk (or assign disk to a loop or nbd device and use sgdisk on affected disk directly from your host).</div><div><br></div><div>In both cases you will get "Invalid partition data!".</div><div><br></div><div>Why it happens?</div><div><br></div><div>After step #4: GPT partition table at LBA1 is created and Protective MBR partition table at LBA0 is created as well. More over Windows automatically creates Microsoft Reserved Partition but this actually doesn't matter: we can delete this partition or pretend it doesn't exist. At this step sgdisk works as expected: it finds both Protective MBR and GPT partitions. Because they both exist sgdisk correctly detects it is actually GPT partition table.</div><div><br></div><div>After step #5: Real MBR (with no partitions) partition table is created at LBA0 (overwriting early created Protective MBR). But neither primary no backup GPT partition tables is erased. Now we have two partition tables: MBR and GPT. And at this step sgdisk fails. It finds both partition tables and don't know which one to choise. If we run gdisk - it will display interactive question which one to use.</div><div><br></div><div>I think it is a bug (or extra safety) in gdisk code because by standard (<a href="https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc739412(v=ws.10)#disk-sectors-on-gpt-disks">https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc739412(v=ws.10)#disk-sectors-on-gpt-disks</a>) Protective MBR should be present for GPT partition to be valid. Or it does make sense for extra sefity if for the reason Protective MBR was not created or accedently erased.</div><div><br></div><div>However other tools (gparted for example or Windows itself) will treat this situation as a disk with MBR partition.</div><div><br></div><div>This way or other but it might create real troubles for libguestfs and related tools like guestfish.</div><div><br></div><div>It might be rare but real use-case when Windows user accedently creates GPT partition table and converts it immediately to MBR. I just fell into this trap myself.</div><div><br></div><div><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>--</div><div>    Mykola Ivanets</div></div></div></div></div>