parted.device module
This module contains Device and Device related classes
- author:
Adolfo Gómez, dkmaster at dkmon dot com
- class parted.device.CHSGeometry(cylinders: int, heads: int, sectors: int)[source]
Bases:
objectCylinders, heads and sectors geometry of a device
- cylinders: int
- static from_partitiontable_msdos(data: bytes) CHSGeometry[source]
Creates a CHSGeometry from a partition table msdos data
- heads: int
- sectors: int
- property total_sectors: int
Total number of sectors in the device
- class parted.device.Device(device: str | cffi.FFI.CData | Device | None = None)[source]
Bases:
objectWrapper for PedDevice*
This class is a wrapper for the PedDevice* type, and provides a more pythonic interface to it.
- begin_external_access(read_only: bool = False) None[source]
Begins external access to the device Invokes the
ped_device_begin_external_accessfunction- Parameters:
read_only (bool) – Whether the device is opened read-only
- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
exceptions.PartedException – If the access could not be granted
Note
This is a wrapper around the
ped_device_begin_external_accessfunction
- property bios_geom: CHSGeometry
Returns: CHSGeometry: The device BIOS geometry
- property boot_dirty: bool
Returns: bool: True if the device is set to boot dirty
- clobber() None[source]
Clobber the partition table on the device. Does not need to be opened before.
As a protection, the device must be opened for writing before calling this function. :param device: The device to clobber :type device: device.Device
- Returns:
True on success, False on error
- Return type:
bool
Warning
Handle with care! will destroy all the partions on the device!!. Data will be written to the device as soon as this function is called.
Note
This is a wrapper around the
ped_disk_clobberfunction
- close() None[source]
Closes the device, disallowing read/write access.
- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
exceptions.IOError – If the device cannot be closed
Note
This is a wrapper around the
ped_device_closefunction
- copy(source: geom.Geometry, to: int, tm: timer.Timer | None = None, sectors_block: int = 32) None[source]
Copies the data from one region to another
- Parameters:
from (geom.Geometry) – The geometry to copy from
to (int) – The sector to start copying to
- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
exceptions.NotOpenedError – If the device is not opened
exceptions.IOError – If the copy fails
Important
The device needs to be opened before calling this function.
- property did: int
The device id of the device
- property dirty: bool
if the device is dirty
- end_external_access() None[source]
Ends external access to the device
Invokes the
ped_device_end_external_accessfunction- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
exceptions.PartedException – If the access could not be ended
Note
This is a wrapper around the
ped_device_end_external_accessfunction
- static enumerate() Iterator[Device][source]
Enumerate all devices.
- Yields:
Device – Device object.
- Raises:
exceptions.PartedException – If the device could not be enumerated (e.g. permission denied)
Note
This is a wrapper around the
ped_device_get_nextfunction
- property external_mode: bool
if the device is in external access mode
- static free_all() None[source]
Clear all devices from memory
Note
This is a wrapper around the
ped_device_free_allfunction
- static get(name: str) Device[source]
Get device by name.
- Parameters:
name (str) – Name of device. (i.e. filename, ‘/dev/sda’, etc.)
- Returns:
Device object.
- Return type:
Note
This is a wrapper around the
ped_device_getfunction
- property host: int
The host number of the device
- property hw_geom: CHSGeometry
Returns: CHSGeometry: The device hardware geometry
- property is_busy: bool
If the device
Note
This is a wrapper around the
ped_device_is_busyfunction
- property is_opened: bool
Returns True if the device is opened
- Returns:
True if the device is opened
- Return type:
bool
- property length: int
Device length in sectors
- property model: str
device model(e.g. “”ATA ST4000VN008-2DR1”, “Linux device-mapper (linear)”, “QEMU HARDDISK”, “Unknown”)
- new_table(type: str | DiskType | WNT) Disk[source]
Creates a new partition table on the disk.
- Parameters:
dev (device.Device) – The device to create the partition table on.
type (DiskType) – The type of partition table to create. (e.g. ‘gpt’, ‘msdos’, ‘bsd’, …)
- Returns:
The new partition.
- Return type:
Important
The new table is created “in memory” only. You must call the commit_to_dev() method to write it to the device.
Note
This is a wrapper around the
ped_disk_new_freshfunction- Raises:
exceptions.InvalidDeviceError – if the device is invalid
- next() Device[source]
- Returns:
The next device in the list, if any, or a Device with _device set to NULL
- Return type:
Note
This is a wrapper around the
ped_device_get_nextfunction
- property obj: cffi.FFI.CData
Wrapped
PedDevice*object
- open(mode: Literal['r', 'rw'] = 'rw') OpenContext[source]
Opens the device, allowing read/write access
- Parameters:
for_writing (bool) – True if the device should be opened for writing (default: True)
- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
exceptions.IOError – If the device cannot be opened
Note
This is a wrapper around the
ped_device_openfunction
- property open_count: int
number of times the device has been opened
- property path: str
device path (e.g. /dev/sda, file.img, etc)
- property phys_sector_size: int
The device physical sector size (e.g. 512, 4096, etc)
- probe() DiskType[source]
Get the partition table type of the device.
- Parameters:
device (device.Device) – The device to probe
- Returns:
The disk type
- Return type:
Note
This is a wrapper around the
ped_disk_probefunction
- static probe_all() None[source]
Probe all devices. (needs to be root)
- Raises:
exceptions.PartedException – If the device could not be probed (e.g. permission denied)
Note
This is a wrapper around the
ped_device_probe_allfunction
- read(sector_start: int, sector_count: int) bytes[source]
Reads the specified number of sectors from the device, starting at the specified sector
- Parameters:
sector_start (int) – The sector to start reading from
sector_count (int) – The number of sectors to read
- Returns:
The data read from the device
- Return type:
bytes
- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
exceptions.IOError – If the read fails
Note
This is a wrapper around the
ped_device_readfunction
- property read_only: bool
if the device is read only
- read_table() Disk[source]
Reads the partition table from the device and returns a new Disk object.
- Parameters:
dev (device.Device) – The device to read the partition table from.
- Returns:
A new Disk object.
- Return type:
Warning
The device object cilinders, heads and sectors can be modified by this call if the partition table indicates that the existing values are incorrect.
Note
This is a wrapper around the
ped_disk_newfunction
- property sector_size: int
The device sector size (e.g. 512, 4096, etc)
- property size: int
Device size in bytes
- sync() None[source]
Flushes the device’s write cache
- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
exceptions.NotOpenedError – If the device is not opened
exceptions.IOError – If the sync fails
Important
The device needs to be opened before calling this function.
Note
This is a wrapper around the
ped_device_syncfunction
- sync_fast() None[source]
Flushes the device’s write cache
- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
Important
The device needs to be opened before calling this function
Note
This is a wrapper around the
ped_device_sync_fastfunction
- property type: DeviceType
Device type
- wants_access(for_writing: bool = False) None[source]
Raises an exception if the device cannot be accesed
The device is “accesible” if it’s opened and not is in external access mode. if “for_writing” is True, the device must be opened not read only.
- Parameters:
for_writing (bool, optional) – If the device is opened for writing. Defaults to False.
- Raises:
exceptions.InvalidDeviceError – If the device is not accesible
- write(buffer: bytes, sector_start: int, sector_count: int) None[source]
Write buffer to device.
- Parameters:
buffer (bytes) – Buffer to write to device.
sector_start (int) – Sector to start writing at.
sector_count (int) – Number of sectors to write.
- Raises:
exceptions.InvalidObjectError – If the wrapped device is not valid
exceptions.IOError – If the write fails
exceptions.NotOpenedError – If the device is not opened
Note
This is a wrapper around the
ped_device_writefunction
- class parted.device.DeviceType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
IntEnumDevice types
- AOE = 16
- ATARAID = 6
- CPQARRAY = 4
- DAC960 = 3
- DASD = 9
- DM = 12
- FILE = 5
- I2O = 7
- IDE = 2
- LOOP = 18
- MD = 17
- NVME = 19
- PMEM = 21
- RAM = 20
- SCSI = 1
- SDMMC = 14
- SX8 = 11
- UBD = 8
- UNKNOWN = 0
- VIODASD = 10
- VIRTBLK = 15
- XVD = 13
- static from_string(s: str) DeviceType[source]
Returns a DeviceType from a string
- Parameters:
s (str) – String to convert
- Returns:
DeviceType from string
- Return type:
- Raises:
ValueError – If string is not a valid DeviceType