[Libguestfs] Curious sgdisk behavior

Nikolay Ivanets stenavin at gmail.com
Tue May 1 07:54:32 UTC 2018


FYI

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.

How to reproduce (esiest way is to use DISPART command line tool in Windows
but you can do the same via Disk Manager UI):
1. Have an empty disk in Windows
2. Launch DISKPART (with elevated privileges)
3. Select corresponding disk by number (SELECT DISK N)
4. Create GPT partition table (CONVERT GPT)
5. Convert to MBR partition table (CONVERT MBR)
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).

In both cases you will get "Invalid partition data!".

Why it happens?

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.

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.

I think it is a bug (or extra safety) in gdisk code because by standard (
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc739412(v=ws.10)#disk-sectors-on-gpt-disks)
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.

However other tools (gparted for example or Windows itself) will treat this
situation as a disk with MBR partition.

This way or other but it might create real troubles for libguestfs and
related tools like guestfish.

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.

--
    Mykola Ivanets
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20180501/c03944c1/attachment.htm>


More information about the Libguestfs mailing list