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: object

Represents a disk

A disk is a collection of partitions.

property active_partitions: List[Partition]

ACTIVE partitions of the disk

add_partition(partition: Partition, constr: Constraint | None = None) None[source]

Adds the given partition to the disk.

Parameters:
Raises:

Note

This is a wrapper around the ped_disk_add_partition function

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_check function

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_dev function

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_os function

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:

Partition

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_all function

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:
property dev: Device

The device of the disk

duplicate() Disk[source]

Duplicates the disk

Returns:

duplicated disk

Return type:

Disk

property flags: Set[DiskFlag]

The flags of the disk

property free_partitions: List[Partition]

FREE partitions of the disk

get_extended_partition() Partition[source]

Returns the extended partition

Returns:

extended partition

Return type:

Partition

Raises:

exceptions.InvalidPartitionError – if no extended partition exists

Note

This is a wrapper around the ped_disk_extended_partition function

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:
Returns:

maximum geometry for the given partition

Return type:

geom.Geometry

Note

This is a wrapper around the ped_disk_get_max_partition_geometry function

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:

Partition

Note

This is a wrapper around the ped_disk_get_partition function

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:

Partition

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:
Raises:

exceptions.PartedException – if the operation failed

minimize_extended_partition() None[source]

Minimizes the extended partition

Raises:
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:

Partition

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_disk method of the partition or the add_partition method of the disk to add it to the disk.

Note

This is a wrapper around the ped_partition_new function

property obj: cffi.FFI.CData

Wrapped PedDisk* object

property partitions: List[Partition]

ALL partitions of the disk (valid an “virtual”)

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_partition function. 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_print function

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_flag function. 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:
Raises:

exceptions.PartedException – if the operation failed

property type: DiskType

The type of the disk

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
static from_string(flag: str) DiskFlag[source]

Returns the DiskFlag from a string

Parameters:

flag (str) – The flag to get

Returns:

The flag

Return type:

DiskFlag

Raises:

ValueError – If the flag is not found

class parted.disk.DiskType(disktype: str | DiskType.WNT | cffi.FFI.CData | None = None)[source]

Bases: object

Represents a disk type

class Feature(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Flag

Represents a disk type feature

EXTENDED = 1
ID = 4
NAME = 2
NONE = 0
UUID = 8
static from_string(feature: str) Feature[source]

Returns the feature from a string

Parameters:

feature (str) – The feature to get

Returns:

The feature

Return type:

DiskType.Feature

Raises:

ValueError – If the feature is not found

class WNT(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Represents 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 from_string(wnt: str) WNT[source]

Returns the WNT from a string

Parameters:

wnt (str) – The WNT to get

Returns:

The WNT

Return type:

DiskType.WNT

Raises:

ValueError – If the WNT is not found

static enumerate() Iterable[DiskType][source]

Enumerates all disk types

Yields:

DiskType – An iterable of all disk types

property features: Feature

The features of the disk type

static first_type() DiskType[source]

First disk type in the parted list

Returns:

The first disk type

Return type:

DiskType

Note

This is a wrapper around the ped_disk_type_get_next function 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:

DiskType

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:

DiskType

property obj: cffi.FFI.CData

Wrapped PedDiskType* object

class parted.disk.Partition(partition: cffi.FFI.CData | None)[source]

Bases: object

Wrapper 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_dev method of the disk

property disk: Disk

The disk this partition belongs to

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 geometry: Geometry

The geometry 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:

geom.Geometry

Raises:

exceptions.InvalidPartitionError – If the partition is not valid for this operation

Note

This is a wrapper around the ped_partition_maximize function

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_maximize function

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_flag function

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_geometry function

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: IntEnum

Partition 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:

PartitionFlag

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: Flag

Type 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:

PartitionType

Raises:

ValueError – If the name is not a valid PartitionType name

property is_valid: bool