1
0
Fork
You've already forked pyparted
1

Can't set partition location, probably due to constraint #114

Open
opened 2025年06月23日 22:53:32 +02:00 by ukoda · 3 comments
ukoda commented 2025年06月23日 22:53:32 +02:00 (Migrated from github.com)
Copy link

I'm trying to create a backup and restore utility for Raspberry Pi systems. I am using the version of pyparted from pip. When I try restore the partitions to a new disk the partition alignment is not as I specify which appears to be due to the constraint setting and leaves the last partition grossly undersized.

The simplest example of this issue can be seen when trying to create a single partition that would be the boot partition on a RPi system. Using a NVMe drive on a USB adaptor I can do this fine manually with fdisk:

root@san:/backup/san# fdisk -l /dev/sda
Disk /dev/sda: 119.24 GiB, 128035676160 bytes, 250069680 sectors
Disk model: Tech 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x957e6c15
Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 1056767 1048576 512M c W95 FAT32 (LBA)

This is the test code I wrote, based on the example code in this repo, to show the issue:

import parted
device = parted.getDevice('/dev/sda')
device.clobber()
disk = parted.freshDisk(device, "msdos")
geometry = parted.Geometry(
 device=device,
 start=8192,
 end=1056767
)
filesystem = parted.FileSystem('fat32', geometry=geometry)
partition = parted.Partition(
 disk=disk,
 type=parted.PARTITION_NORMAL,
 fs=filesystem,
 geometry=geometry
)
disk.addPartition(
 partition=partition, constraint=device.optimalAlignedConstraint
)
disk.commit()

However it results in:

Disk /dev/sda: 119.24 GiB, 128035676160 bytes, 250069680 sectors
Disk model: Tech 
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x5f47ce36
Device Boot Start End Sectors Size Id Type
/dev/sda1 522240 1044479 522240 255M b W95 FAT32

I can get closer if I change the constraint to:

 partition=partition, constraint=device.minimalAlignedConstraint

to get:

Device Boot Start End Sectors Size Id Type
/dev/sda1 8160 1056719 1048560 512M b W95 FAT32

But this still adds up to a problem over 3 partitions.

I have tried several other options but can't get it to honor the actual values I want.

Also of note is the partition Id is being set to 'b', not 'c'. Not sure if this will cause problems with the restored system yet.

I'm trying to create a backup and restore utility for Raspberry Pi systems. I am using the version of pyparted from pip. When I try restore the partitions to a new disk the partition alignment is not as I specify which appears to be due to the constraint setting and leaves the last partition grossly undersized. The simplest example of this issue can be seen when trying to create a single partition that would be the boot partition on a RPi system. Using a NVMe drive on a USB adaptor I can do this fine manually with fdisk: ``` root@san:/backup/san# fdisk -l /dev/sda Disk /dev/sda: 119.24 GiB, 128035676160 bytes, 250069680 sectors Disk model: Tech Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disklabel type: dos Disk identifier: 0x957e6c15 Device Boot Start End Sectors Size Id Type /dev/sda1 8192 1056767 1048576 512M c W95 FAT32 (LBA) ``` This is the test code I wrote, based on the example code in this repo, to show the issue: ``` import parted device = parted.getDevice('/dev/sda') device.clobber() disk = parted.freshDisk(device, "msdos") geometry = parted.Geometry( device=device, start=8192, end=1056767 ) filesystem = parted.FileSystem('fat32', geometry=geometry) partition = parted.Partition( disk=disk, type=parted.PARTITION_NORMAL, fs=filesystem, geometry=geometry ) disk.addPartition( partition=partition, constraint=device.optimalAlignedConstraint ) disk.commit() ``` However it results in: ``` Disk /dev/sda: 119.24 GiB, 128035676160 bytes, 250069680 sectors Disk model: Tech Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disklabel type: dos Disk identifier: 0x5f47ce36 Device Boot Start End Sectors Size Id Type /dev/sda1 522240 1044479 522240 255M b W95 FAT32 ``` I can get closer if I change the constraint to: ``` partition=partition, constraint=device.minimalAlignedConstraint ``` to get: ``` Device Boot Start End Sectors Size Id Type /dev/sda1 8160 1056719 1048560 512M b W95 FAT32 ``` But this still adds up to a problem over 3 partitions. I have tried several other options but can't get it to honor the actual values I want. Also of note is the partition Id is being set to 'b', not 'c'. Not sure if this will cause problems with the restored system yet.
ukoda commented 2025年06月28日 01:42:36 +02:00 (Migrated from github.com)
Copy link

For now I am working around the problem by using sfdisk from a subprocess call. Not ideal but it does give me the partitions I need to move forward with my utility.

For now I am working around the problem by using sfdisk from a subprocess call. Not ideal but it does give me the partitions I need to move forward with my utility.
dcantrell commented 2025年12月02日 19:59:25 +01:00 (Migrated from github.com)
Copy link

What are the units for Start and End in the fdisk output? I forget what it displays by default.

The Geometry in libparted is expecting sectors for start and length.

What are the units for Start and End in the fdisk output? I forget what it displays by default. The Geometry in libparted is expecting sectors for start and length.
ukoda commented 2025年12月02日 20:38:33 +01:00 (Migrated from github.com)
Copy link

@dcantrell fdisk by default shows logical sectors and sector size is at the top of the output, in this case 512 bytes. Running the maths it does look right for the 512MB partition I created in the first example using fdisk directly.

@dcantrell fdisk by default shows logical sectors and sector size is at the top of the output, in this case 512 bytes. Running the maths it does look right for the 512MB partition I created in the first example using fdisk directly.
Sign in to join this conversation.
No Branch/Tag specified
main
stable-3.9.x
master-distutils
master-staging
test-cases
v3.13.0
v3.12.0
v3.11.7
v3.11.6
v3.11.5
v3.11.4
v3.11.3
v3.11.2
v3.11.1
v3.11.0
v3.10.7
v3.10.6
v3.10.5
pyparted-3.10.4
pyparted-3.10.3
pyparted-3.10.2
pyparted-3.10.1
pyparted-3.10.0
pyparted-3.9.5
pyparted-3.9.4
pyparted-3.9.3
pyparted-3.9.2
pyparted-3.10
pyparted-3.10a1
v3.10a1
v3.10a0
hayseed
pyparted-3.9
pyparted-3.8
pyparted-3.7
pyparted-3.6
pyparted-3.5
pyparted-3.4
pyparted-3.3
pyparted-3.2
pyparted-3.1
pyparted-3.0
pyparted-2.5
pyparted-2.4
pyparted-2.3
pyparted-2.2
pyparted-2.1.2
pyparted-2.1.1
pyparted-2.1.0
pyparted-2.0.12
pyparted-2.0.11
pyparted-2.0.10
pyparted-2.0.9
pyparted-2.0.8
pyparted-2.0.7
pyparted-2.0.6
pyparted-2.0.5
pyparted-2.0.4
pyparted-2.0.3
pyparted-2.0.2
pyparted-2.0.1
pyparted-2.0.0
pyparted-1_8_9-1
start
pyparted-1.8.8-1
pyparted-1.8.7-1
pyparted-1.8.6-2
pyparted-1.8.6-1
pyparted-1.8.5-1
pyparted-1.8.4-1
pyparted-1.8.3-1
pyparted-1.8.2-1
pyparted-1.8.1-1
pyparted-1.8.0-1
pyparted-1.7.3-1
pyparted-1.6.9-1
pyparted-1.6.7-1
pyparted-1.6.6-2
pyparted-1.6.6-1
pyparted-1.6.10-1
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dcantrell/pyparted#114
Reference in a new issue
dcantrell/pyparted
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?