CPython bindings for the libfdisk library that is included in the util-linux project.
This binding allows you to manipulate partition tables from a python program on Linux.
$ pip install python-libfdisk-1.2.tar.gz
import fdisk
SIZE = {
"B": 1,
"KB": 1<<10,
"MB": 1<<20,
"GB": 1<<30,
}
# Create context object
cxt = fdisk.Context(device='./disk.bin', readonly=False)
# Create label (in-memory)
cxt.create_disklabel('gpt')
# Find EFI partition type from the context's label
efi_ptype = cxt.label.get_parttype_from_string('C12A7328-F81F-11D2-BA4B-00A0C93EC93B')
# Create a EFI partition
pa = fdisk.Partition(start_follow_default=True,
partno_follow_default=True) # Create partition object
pa.size = 512 * SIZE['KB'] // cxt.sector_size # Set size (number of sectors)
pa.type = efi_ptype # Set partition type
cxt.add_partition(pa) # Add partition to context
# Create another partition, default parttype is 'Linux filesystem'
size = 24 * SIZE['MB']
pa = fdisk.Partition(start_follow_default=True,
partno_follow_default=True)
pa.size = size // cxt.sector_size
cxt.add_partition(pa)
# Write in-memory changes to disk
cxt.write_disklabel()
ogClient
is a component of the OpenGnsys
project that uses python-libfdisk for remote partitioning
of Linux computers.