parted.disk module
This module contains Disk and Partition related classes
- author:
Adolfo Gómez, dkmaster at dkmon dot com
- class parted.disk.Disk(disk: device.Device | cffi.FFI.CData | None = None)[source]
Bases:
objectRepresents a disk
A disk is a collection of partitions.
- add_partition(partition: Partition, constr: Constraint | None = None) None[source]
Adds the given partition to the disk.
- Parameters:
partition (Partition) – partition to add
constraint (constraint.Constraint) – constraint to use
- Raises:
exceptions.InvalidPartitionError – if the partition is invalid
exceptions.PartedException – if the operation failed
Note
This is a wrapper around the
ped_disk_add_partitionfunction
- check() bool[source]
Checks the disk for consistency and returns True if it is consistent.
- Returns:
whether the disk is consistent
- Return type:
bool
Note
This is a wrapper around the
ped_disk_checkfunction
- commit_to_dev() None[source]
Write the changes made to the in-memory description of a partition table to the device. As a protection, device must be opened in read-write mode to be able to commit changes.
- Warning: Ensure to invoke this method after you are made changes to the disk. The destruction of the disk object
will write them on “most cases” as indicated by the parted documentation, but calling this method explicitly will ensure that the changes are written to the device.
- Returns:
whether the operation succeeded
- Return type:
bool
Note
This is a wrapper around the
ped_disk_commit_to_devfunction
- commit_to_os() bool[source]
Tell the operating system kernel about the partition table layout of disk.
This is rather loosely defined: for example, on old versions of Linux, it simply calls the BLKRRPART ioctl, which tells the kernel to reread the partition table. On newer versions (2.4.x), it will use the new blkpg interface to tell Linux where each partition starts/ends, etc. In this case, Linux does not need to have support for a specific type of partition table.
- Returns:
whether the operation succeeded
- Return type:
bool
Note
This is a wrapper around the
ped_disk_commit_to_osfunction
- create_partition(part_type: PartitionType, fs_type: str | FileSystemType | WNT, start: int, end: int, constraint: Constraint | None = None) Partition[source]
Creates a new partition and adds it to the disk.
- Parameters:
disk (Disk) – The disk to use
part_type (PartitionType) – The type of the partition
fs_type (str or FileSystemType) – The file system type
start (int) – The start sector
end (int) – The end sector
constraint (constraint.Constraint) – constraint to use
- Returns:
The new partition
- Return type:
- Raises:
exceptions.InvalidDiskError if the disk is not valid for this operation –
exceptions.InvalidFileSystemType if the file system type is not valid –
exceptions.PartedException if the operation failed –
- debug() str[source]
Prints the partition table to the given stream.
- Parameters:
out_stream (TextIO) – stream to print to, defaults to stdout
- delete_all_partitions() None[source]
Removes all partitions from the disk.
- Raises:
exceptions.PartedException – if the operation failed
Note
This is a wrapper around the
ped_disk_delete_allfunction
- delete_partition(partition: Partition) None[source]
Removes the specified partition from the disk, and destroys it.
The reference to the partition is no longer valid after this call
- Parameters:
partition (Partition) – partition to remove
- Raises:
exceptions.InvalidPartitionError – if the partition is invalid
exceptions.PartedException – if the operation failed
- get_extended_partition() Partition[source]
Returns the extended partition
- Returns:
extended partition
- Return type:
- Raises:
exceptions.InvalidPartitionError – if no extended partition exists
Note
This is a wrapper around the
ped_disk_extended_partitionfunction
- get_flag(flag: DiskFlag) bool[source]
Returns the state of the given flag
- Parameters:
flag (DiskFlag) – flag to get the state of
- Returns:
state of the given flag
- Return type:
bool
- get_max_partition_geometry(partition: Partition, constraint: Constraint) Geometry[source]
Returns the maximum geometry for the given partition
- Parameters:
partition (Partition) – partition
constraint (constraint.Constraint) – constraint
- Returns:
maximum geometry for the given partition
- Return type:
Note
This is a wrapper around the
ped_disk_get_max_partition_geometryfunction
- get_partition(num: int) Partition[source]
Returns the partition with the given number
- Parameters:
num (int) – partition number
- Returns:
partition with the given number
- Return type:
Note
This is a wrapper around the
ped_disk_get_partitionfunction
- get_partition_by_sector(sector: int) Partition[source]
Returns the partition containing the given sector
- Parameters:
sector (int) – sector
- Returns:
partition containing the given sector
- Return type:
- is_flag_available(flag: DiskFlag) bool[source]
Returns whether the given flag is available
- Parameters:
flag (DiskFlag) – flag to check
- Returns:
whether the given flag is available
- Return type:
bool
- property last_partition_num: int
The number of the last partition
- property max_primary_partition_count: int
The maximum number of primary partitions
- maximize_partition_geometry(partition: Partition, constraint: Constraint) None[source]
Maximizes the geometry of the given partition
- Parameters:
partition (Partition) – partition to maximize
constraint (constraint.Constraint) – constraint to use
- Raises:
exceptions.PartedException – if the operation failed
- minimize_extended_partition() None[source]
Minimizes the extended partition
- Raises:
exceptions.InvalidPartitionError – if no extended partition exists
exceptions.PartedException – if the operation failed
- new_partition(part_type: PartitionType, fs_type: str | FileSystemType | WNT, start: int, end: int) Partition[source]
Creates a new partition
- Parameters:
disk (Disk) – The disk to use
part_type (PartitionType) – The type of the partition
fs_type (str or FileSystemType) – The file system type
start (int) – The start sector
end (int) – The end sector
- Returns:
The new partition
- Return type:
- Raises:
exceptions.InvalidDiskError if the disk is not valid for this operation –
exceptions.InvalidFileSystemType if the file system type is not valid –
Important
The created partition is not added to the disk. You have to call the
add_to_diskmethod of the partition or theadd_partitionmethod of the disk to add it to the disk.Note
This is a wrapper around the
ped_partition_newfunction
- property obj: cffi.FFI.CData
Wrapped
PedDisk*object
- partitions_list(type: Literal['all', 'active', 'free']) Iterable[Partition][source]
Gets the list of filtered partitions
- Parameters:
type (str) – type of partitions to get
- Yields:
Partition – partition with the given type
- Note: This is a wrapper around the
ped_disk_next_partitionfunction. if Disk is not initialized, it will yield nothing.
- print() None[source]
Prints the partition table to stdout.
Note
This is a wrapper around the
ped_disk_printfunction
- set_flag(flag: DiskFlag, state: bool) None[source]
Sets the given flag to the given state
- Parameters:
flag (DiskFlag) – flag to set
state (bool) – state to set the flag to
- Raises:
exceptions.PartedException – if the operation failed
Note
This is a wrapper around the
ped_disk_set_flagfunction. Unsupported flags will be ignored.
- set_partition_geometry(partition: Partition, const: Constraint, sector_start: int, sector_end: int) None[source]
Sets a new geometry for a partition.
- Parameters:
partition (Partition) – partition to set the geometry for
geom (geom.Geometry) – new geometry
constraint (constraint.Constraint) – constraint to use
- Raises:
exceptions.PartedException – if the operation failed
- class parted.disk.DiskFlag(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnum- CYLINDER_ALIGNMENT = 1
- GPT_PMBR_BOOT = 2
- class parted.disk.DiskType(disktype: str | DiskType.WNT | cffi.FFI.CData | None = None)[source]
Bases:
objectRepresents a disk type
- class Feature(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
FlagRepresents a disk type feature
- EXTENDED = 1
- ID = 4
- NAME = 2
- NONE = 0
- UUID = 8
- class WNT(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumRepresents Well Known Type names
- AIX = 'aix'
- AMIGA = 'amiga'
- BSD = 'bsd'
- DASD = 'dasd'
- DM = 'dm'
- GPT = 'gpt'
- LOOP = 'loop'
- MAC = 'mac'
- MSDOS = 'msdos'
- PC98 = 'pc98'
- SUN = 'sun'
- UNKNOWN = 'unknown'
- static enumerate() Iterable[DiskType][source]
Enumerates all disk types
- Yields:
DiskType – An iterable of all disk types
- static first_type() DiskType[source]
First disk type in the parted list
- Returns:
The first disk type
- Return type:
Note
This is a wrapper around the
ped_disk_type_get_nextfunction with a NULL argument
- static from_name(name: WNT | str) DiskType[source]
Returns the disk type with the given name
- Parameters:
name (str) – disk type name (e.g. ‘gpt’, ‘msdos’, ‘bsd’, …)
- Returns:
disk type structure for the given name
- Return type:
- property name: str
The name of the disk type
- next_type() DiskType[source]
Next disk type in the parted list
This is mean to be used for enumerating all disk types
- Returns:
The next disk type
- Return type:
- property obj: cffi.FFI.CData
Wrapped
PedDiskType*object
- class parted.disk.Partition(partition: cffi.FFI.CData | None)[source]
Bases:
objectWrapper for PedPartition A partition is a contiguous region of a disk. It has a type, a filesystem type, a name, and a geometry. It also has flags.
- property active: bool
True if the partition is active, False otherwise A partition is active is it not a metadata or free space partition
- add_to_disk(constraint: Constraint) None[source]
Adds this partition to the disk
- Parameters:
constraint (Constraint) – The constraint to use
- property busy: bool
Whether this partition is busy
- delete() None[source]
Deletes the partition
Note
After deleting the partition, the object will be set to NULL. The changes are done “in memory” and will be written to disk only if you call the
commit_to_devmethod of the disk
- property extended_list: List[Partition]
List of extended partitions
- Returns:
List of extended partitions
- Return type:
List[Partition]
Note
Maybe this parition is not an extended partition. In this case, the list will be empty
- property extended_list_active: List[Partition]
List of active extended partitions
- Returns:
List of active extended partitions
- Return type:
List[Partition]
Note
Maybe this parition is not an extended partition. In this case, the list will be empty
- property extended_list_free: List[Partition]
List of free extended partitions
- Returns:
List of free extended partitions
- Return type:
List[Partition]
Note
Maybe this parition is not an extended partition. In this case, the list will be empty
- property flags: Set[PartitionFlag]
The flags of this partition
Only valid partitions, with valid disk and type of partitions that support flags will be considered
- Returns:
set of PartitionFlag
- property fs_type: FileSystemType
The filesystem type of this partition
- property is_valid: bool
True if the partition is validself. A partition will be considered valid if it’s not freespace, is not metadata, is attached to a disk and can be operated)
- max_geometry(constraint: Constraint) Geometry[source]
Returns the maximum geometry of this partition
- Parameters:
constraint (Constraint) – The constraint to use
- Returns:
The maximum geometry
- Return type:
- Raises:
exceptions.InvalidPartitionError – If the partition is not valid for this operation
Note
This is a wrapper around the
ped_partition_maximizefunction
- maximize(constraint: Constraint) None[source]
Maximizes the geometry of this partition
- Parameters:
constraint (Constraint) – The constraint to use
- Raises:
exceptions.InvalidPartitionError – If the partition is not valid for this operation
Note
This is a wrapper around the
ped_partition_maximizefunction
- property name: str
The name of this partition
- property num: int
The number of this partition
- property obj: cffi.FFI.CData
Wrapped
PedPartition*object
- property path: str
The path of this partition
- set_flag(flag: PartitionFlag, state: bool = True) None[source]
Sets the flag of this partition
- Parameters:
flag (PartitionFlag) – The flag to set
state (bool) – The state of the flag
- Raises:
exceptions.InvalidPartitionError – If the partition is not valid for this operation
Note
This is a wrapper around the
ped_partition_set_flagfunction
- set_geometry(const: Constraint, sector_start: int, sector_end: int) None[source]
Sets the geometry of this partition
- Parameters:
const (Constraint) – The constraint to use
sector_start (int) – The new start sector
sector_end (int) – The new end sector
- Raises:
exceptions.InvalidPartitionError – If the partition is not valid for this operation
Note
This is a wrapper around the
ped_partition_set_geometryfunction
- property type: PartitionType
The type of this partition
- class parted.disk.PartitionFlag(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnumPartition flags
- APPLE_TV_RECOVERY = 13
- BIOS_GRUB = 12
- BLS_BOOT = 20
- BOOT = 1
- CHROMEOS_KERNEL = 19
- DIAG = 14
- ESP = 18
- HIDDEN = 4
- HPSERVICE = 8
- IRST = 17
- LBA = 7
- LEGACY_BOOT = 15
- LINUX_HOME = 21
- LVM = 6
- MSFT_DATA = 16
- MSFT_RESERVED = 11
- PALO = 9
- PREP = 10
- RAID = 5
- ROOT = 2
- SWAP = 3
- static from_string(flag: str) PartitionFlag[source]
Returns a PartitionFlag from its name if exits, else raises an exception
- Parameters:
name (str) – Name of the PartitionFlag to get
- Returns:
The PartitionFlag with the given name
- Return type:
- Raises:
ValueError – If the name is not a valid PartitionFlag name
- class parted.disk.PartitionType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
FlagType of partition
- EXTENDED = 2
- FREE = 4
- LOGICAL = 1
- LOGICAL_FREE = 5
- LOGICAL_METADATA = 9
- METADATA = 8
- NORMAL = 0
- PROTECTED = 16
- static from_string(ptype: str) PartitionType[source]
Returns a PartitionType from a name
- Parameters:
name (str) – Name of the PartitionType to get
- Returns:
The PartitionType with the given name
- Return type:
- Raises:
ValueError – If the name is not a valid PartitionType name
- property is_valid: bool