Skip to main content

Python libparted bindings

Project description

Reparted is my attempt at learning python ctypes module to create bindings for c api's so use it
at your own risk. I was a bit confused on the way parted and pyparted interface works, my aim was
to create a simple interface. It does not have the full set of features parted has to offer, only
enough to create and delete partitions and set the labels. Feel free to check the code and point
out enhancements. For any questions my email is rq.sysadmin@gmail.com, have fun!

Quick Introduction to add partitions:

from reparted import *

dev = device.Device("/dev/sdb") # or whatever your preferred dev is. If you call it without any
# arguments it will probe all standard devices and default to the
# first one it finds
mydisk = disk.Disk(dev)
sz = size.Size(dev, 200, "MB")
new = disk.Partition(mydisk, sz) # this defaults the filesystem to ext3 and partition type "NORMAL"
#new = disk.Partition(mydisk, sz, fs="ext4") # set the filesystem blocks to ext4
#new = disk.Partition(mydisk, sz, fs="ext4", name="myname") # set a name
#new = disk.Partition(mydisk, sz, align='minimal') # sets the partition alignment to minimal.
mydisk.add_partition(new)
mydisk.commit()


Partition

Now you can access partition properties and methods from the partition instance "new":

disk -Returns an instance of Disk
device -Returns an instance of Device
geom -Returns a 3-tuple (start_sector, end_sector, length_in_sectors)
num -Returns the partition number
type -Returns the partition type (EXTENDED, NORMAL, LOGICAL, etc...)
fs_type -Returns the filesystem block geometry type (ext3, ext4, etc...)
name -The partition name if set
set_name -If the partition table supports names you can change it with this method,
you will need to commit the changes to disk though.
set_flag -Sets the partition flag, it takes a string argument ("BOOT", "LVM",
"BOOT_LEGACY", etc...) and a boolean (True or False, to toggle the state.
alignment -Returns either 'minimal' or 'optimal' depending on the partition start sector.
Notice, however, that although you may specify a minimal alignment on a
new partition, this may return optimal depending if the partition start
sector turns to be aligned.

Disk

You can create an instance of a disk by providing a device:


dev = device.Device("/dev/sdb") # or whatever your preferred dev is.
mydisk = disk.Disk(dev)

These are the properties and methods for disk instances:

type_name -Returns the disk partition table name (gpt, dvh, etc...)
type_features -Returns either PARTITION_NAME (supports naming partitions)
or EXTENDED (lvm stuff, no naming partitions for you!)
block_sizes -the disk block sizes.
needs_clobber -True or False.
update_mode -True or False
partitions -Returns a list of partitions in disk (as Partition instances).
METADATA, FREESPACE and PROTECTED partitions are not included.
add_partition -Takes a Partition instance and adds it to the disk partition table.
NOTE: You must call commit to save your changes to disk.
delete_partition -Takes either a Partition instance or partition number and deletes it.
delete_all -Deletes all partitions
commit -Commits changes to disk.
_ped_disk -Returns the ctypes disk structure.
_ped_device -Returns the ctypes device structure.
get_partition -Returns a Partition instance given a partition number.
set_label -Takes a string (gpt or msdos) and sets the disk partition table.


Device

You can create an instance of a device by providing the path (ie. /dev/sda). If the path
is not provided, it will probe all standard devices until it finds one and default to it.
dev = device.Device("/dev/sdb") # or whatever your preferred dev is.

OR

dev = device.Device()

These are the properties and methods for device instances:

_ped_device -Returns the ctypes device structure.
length -Returns the device length in sectors.
path -Returns the device path (ie. /dev/sda).
model -Returns the device model (ie. 'ATA HARDDISK').
type -Returns the device type (ie. 'SCSI').
sector_size -Returns the sector size.
phys_sector_size -Returns the physical sector size.
open_count -How many times the device has been opened (per session).
read_only -True or False.
external_mode -True or False.
dirty -True or False.
boot_dirty -True or False.
hw_geom -Returns a 3-tuple (cylinders, heads, sectors)
bios_geom -Returns a 3-tuple (cylinders, heads, sectors)
host -Returns the device host id.
did -I have no idea what this is.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reparted-1.0.tar.gz (19.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page