0
# pyparted
1
2
Python bindings for GNU parted library providing comprehensive disk partition management capabilities. pyparted offers both a high-level Pythonic interface and low-level direct access to libparted functionality, making it suitable for system administration tools, installers, and disk management applications.
3
4
## Package Information
5
6
- **Package Name**: pyparted
7
- **Package Type**: pypi
8
- **Language**: Python
9
- **Installation**: `pip install pyparted`
10
11
## Core Imports
12
13
```python
14
import parted
15
```
16
17
For specific classes and functions:
18
19
```python
20
from parted import Device, Disk, Partition, Geometry, Alignment, Constraint, FileSystem
21
```
22
23
## Basic Usage
24
25
```python
26
import parted
27
28
# Get a device by path
29
device = parted.getDevice('/dev/sda')
30
print(f"Device: {device.model}, Size: {device.length} sectors")
31
32
# Read existing partition table
33
disk = parted.newDisk(device)
34
print(f"Disk type: {disk.type}")
35
36
# List partitions
37
for partition in disk.partitions:
38
print(f"Partition {partition.number}: {partition.getSize('GB'):.1f} GB")
39
40
# Create new partition table (CAUTION: destroys existing data)
41
# disk = parted.freshDisk(device, 'gpt')
42
# disk.commitToDevice()
43
```
44
45
## Architecture
46
47
pyparted uses a dual-layer architecture:
48
49
- **High-level parted module**: Pythonic classes with advanced functionality
50
- **Low-level _ped module**: Direct 1:1 mapping to libparted C functions
51
52
Core classes follow a hierarchical design:
53
- **Device**: Physical storage device representation
54
- **Disk**: Partition table container on a device
55
- **Partition**: Individual partition within a disk
56
- **Geometry**: Sector range definitions for regions
57
- **Alignment**: Sector alignment constraints
58
- **Constraint**: Complex operation restrictions
59
- **FileSystem**: Filesystem type and properties
60
61
## Capabilities
62
63
### Device Management
64
65
Device discovery, properties access, and low-level I/O operations for physical storage devices.
66
67
```python { .api }
68
def getDevice(path: str) -> Device
69
def getAllDevices() -> list[Device]
70
def freeAllDevices() -> None
71
72
class Device:
73
# Properties
74
model: str
75
path: str
76
type: int
77
sectorSize: int
78
physicalSectorSize: int
79
length: int
80
busy: bool
81
readOnly: bool
82
83
# Methods
84
def open() -> None
85
def close() -> None
86
def clobber() -> None
87
def getConstraint() -> Constraint
88
def getMinimumAlignment() -> Alignment
89
def getOptimumAlignment() -> Alignment
90
```
91
92
[Device Management](./device-management.md)
93
94
### Disk and Partition Table Operations
95
96
Reading, creating, and modifying partition tables including support for various formats like GPT, MBR, and other disk label types.
97
98
```python { .api }
99
def newDisk(device: Device) -> Disk
100
def freshDisk(device: Device, ty: str) -> Disk
101
102
class Disk:
103
# Properties
104
device: Device
105
type: str
106
partitions: list[Partition]
107
primaryPartitionCount: int
108
maxPrimaryPartitionCount: int
109
110
# Methods
111
def commit() -> None
112
def commitToDevice() -> None
113
def commitToOS() -> None
114
def addPartition(partition: Partition, constraint: Constraint) -> None
115
def deletePartition(partition: Partition) -> None
116
def maximizePartition(partition: Partition, constraint: Constraint) -> None
117
```
118
119
[Disk Operations](./disk-operations.md)
120
121
### Partition Management
122
123
Creating, modifying, and managing individual partitions including setting flags, names, and filesystem types.
124
125
```python { .api }
126
class Partition:
127
# Properties
128
disk: Disk
129
geometry: Geometry
130
fileSystem: FileSystem
131
number: int
132
path: str
133
type: int
134
name: str
135
active: bool
136
busy: bool
137
138
# Methods
139
def getFlag(flag: int) -> bool
140
def setFlag(flag: int, state: bool) -> None
141
def isFlagAvailable(flag: int) -> bool
142
def setSystem(fs: FileSystem) -> None
143
def setName(name: str) -> None
144
def getSize(unit: str = 'MB') -> float
145
```
146
147
[Partition Management](./partition-management.md)
148
149
### Geometry and Alignment
150
151
Sector-based geometry operations and alignment constraint management for precise partition positioning.
152
153
```python { .api }
154
class Geometry:
155
# Properties
156
device: Device
157
start: int
158
end: int
159
length: int
160
161
# Methods
162
def intersect(geometry: Geometry) -> Geometry
163
def overlapsWith(geometry: Geometry) -> bool
164
def contains(geometry: Geometry) -> bool
165
def containsSector(sector: int) -> bool
166
167
class Alignment:
168
# Properties
169
offset: int
170
grainSize: int
171
172
# Methods
173
def alignUp(geometry: Geometry, sector: int) -> int
174
def alignDown(geometry: Geometry, sector: int) -> int
175
def isAligned(geometry: Geometry, sector: int) -> bool
176
```
177
178
[Geometry and Alignment](./geometry-alignment.md)
179
180
### Constraint Solving
181
182
Advanced constraint system for ensuring partition operations respect device limitations, alignment requirements, and size restrictions.
183
184
```python { .api }
185
class Constraint:
186
# Properties
187
minSize: int
188
maxSize: int
189
190
# Methods
191
def intersect(constraint: Constraint) -> Constraint
192
def solveMax(geometry: Geometry) -> Geometry
193
def solveNearest(geometry: Geometry) -> Geometry
194
def isSolution(geometry: Geometry) -> bool
195
```
196
197
[Constraint Solving](./constraint-solving.md)
198
199
### Filesystem Operations
200
201
Filesystem detection, probing, and type management for partition formatting and identification.
202
203
```python { .api }
204
def probeFileSystem(geometry: Geometry) -> str
205
def probeForSpecificFileSystem(fstype: str, geometry: Geometry) -> Geometry
206
207
class FileSystem:
208
# Properties
209
type: str
210
geometry: Geometry
211
checked: bool
212
```
213
214
[Filesystem Operations](./filesystem-operations.md)
215
216
### Utility Functions
217
218
Helper functions for unit conversion, version information, and system compatibility.
219
220
```python { .api }
221
def formatBytes(bytes_: int, unit: str) -> float
222
def sizeToSectors(bytes_: int, unit: str, sector_size: int) -> int
223
def version() -> dict[str, str]
224
def getLabels(arch: str = None) -> set[str]
225
```
226
227
[Utility Functions](./utility-functions.md)
228
229
## Constants and Enumerations
230
231
pyparted provides extensive constants for device types, partition types, partition flags, disk flags, units, and exception handling.
232
233
[Constants Reference](./constants-reference.md)
234
235
## Exception Handling
236
237
Comprehensive exception system with specific exception types for different error conditions.
238
239
[Exception Handling](./exception-handling.md)