storaged-project / udisks

The UDisks project provides a daemon, tools and libraries to access and manipulate disks, storage devices and technologies.
https://storaged.org/doc/udisks2-api/latest/
Other
342 stars 143 forks source link

CreatePartition size on MBR table type #959

Open krab opened 2 years ago

krab commented 2 years ago

tl;dr is i'm writing usb formatting tool on Qt and have found strange udisks2 behavior. When windows create partition on usb flash drive it creates MBR with partition starting offset 2048 sectors (1 mb for 512bytes) and partition ends 2047 sectors before device total secotres Here is gparted shows info https://i.imgur.com/1HpE4q7.png

On Linux I'm using udisks2 via d-bus: http://storaged.org/doc/udisks2-api/latest/gdbus-org.freedesktop.UDisks2.PartitionTable.html#gdbus-method-org-freedesktop-UDisks2-PartitionTable.CreatePartition

IN t offset: The desired offset where the partition should be created, in bytes.
IN t size: The desired size of the partition, in bytes (0 for maximal size).

So, for MBR i have to specify 2048 offset (when GPT can work with 0 offset parameter it will create 2048 anyways), that's not an issue. When i specify size 0 (for maximum device empty space, according to udisks2 d-bus spec) for GPT and MBR (works in both cases) udisks2 creates partition with correct offset sector, but it lasts sector points over total device sectors by 2047. https://i.imgur.com/kUYOjaD.png

Basically, size parameter not aware of offset? It's looks like udisks2 bug.

OS: Linux Mint Udisks2 version: 2.8.4

tbzatek commented 2 years ago

Could you please post fdisk -l outputs before and after? In your screenshots I see equal numbers in both cases. There's some aligning going on with several rules.

krab commented 2 years ago

Yeah, looks like you are right. "Total sectors" is partition total sectors not device i miss understood gparted information.

Windows creates like this on usb

krab@mintvm:~$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 7,24 GiB, 7767851008 bytes, 15171584 sectors
Disk model: Silicon-Power8G 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xb8dc834f

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        2048 15169535 15167488  7,2G  7 HPFS/NTFS/exFAT

2049 free sectors at the end.

Udisks2 via Gnome Disks Utility

krab@mintvm:~$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 7,24 GiB, 7767851008 bytes, 15171584 sectors
Disk model: Silicon-Power8G 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d3738cb

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        2048 15171583 15169536  7,2G  7 HPFS/NTFS/exFAT

1 sector free?

Basically, my starting issue was this sgdisk warning output

krab@mintvm:~$ sudo sgdisk -i1 /dev/sdb

***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. 
***************************************************************

Warning! Secondary partition table overlaps the last partition by
33 blocks!
You will need to delete this partition or resize it in another utility.
Partition GUID code: EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 (Microsoft basic data)
Partition unique GUID: FE154D11-26B7-4362-B5FC-A7193ADC454D
First sector: 2048 (at 1024.0 KiB)
Last sector: 15171583 (at 7.2 GiB)
Partition size: 15169536 sectors (7.2 GiB)
Attribute flags: 0000000000000000
Partition name: 'Microsoft basic data'
krab commented 2 years ago

GPT table:

Windows:

krab@mintvm:~$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 7,24 GiB, 7767851008 bytes, 15171584 sectors
Disk model: Silicon-Power8G 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 278FEBB0-CE26-47D7-965F-10F21E220A24

Device     Start      End  Sectors  Size Type
/dev/sdb1   2048 15169535 15167488  7,2G Microsoft basic data

Udisks2:

krab@mintvm:~$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 7,24 GiB, 7767851008 bytes, 15171584 sectors
Disk model: Silicon-Power8G 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 278FEBB0-CE26-47D7-965F-10F21E220A24

Device     Start      End  Sectors  Size Type
/dev/sdb1   2048 15169535 15167488  7,2G Microsoft basic data

Both left 2049 sectors.

krab commented 2 years ago

sgdisk warns

Warning! Secondary partition table overlaps the last partition by
33 blocks!

when creating partition on mbr table with udisks2, i understand that sgdisk is for gpt but still doesn't warn for partition created under windows.

krab commented 2 years ago

Just tested with 3 different usb flash drives, every time udisks2/mbr lefts only 1 sector alignment at the end. This will make conversion to gpt difficult as sgdisk warns above?

tbzatek commented 2 years ago

Well I guess the source of the problem is that sgdisk converts the MBR table to GPT as it tells you and there isn't sufficient free space for a backup GPT table at the end of the disk. It is still a valid MBR layout. But yeah, it makes sense to leave some space at the end of the disk.

Partitioning has been rewritten in libblockdev-3, have to check how it's done there.

vojtechtrefny commented 2 years ago

Well I guess the source of the problem is that sgdisk converts the MBR table to GPT as it tells you and there isn't sufficient free space for a backup GPT table at the end of the disk.

I think this is by design. sgdisk works only with GPT so if you try to use it with MBR it will try to convert it to GPT.

From sgdisk man page:

Upon start, sgdisk attempts to identify the partition type in use on the disk. If it finds valid GPT data, sgdisk will use it. If sgdisk finds a valid MBR or BSD disklabel but no GPT data, it will attempt to convert the MBR or disklabel into GPT form.

Both fdisk and GParted leave only one sector free at the end with MBR so I don't think we need to things differently in UDisks (or libblockdev). Converting MBR to GPT probably isn't a common use case and you can always shrink the last partition if you really want to do that.