python-libfdisk

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.

Releases

Installation

Download the latest tarball and install using pip. A virtual environment is recommended (venv).
$ pip install python-libfdisk-1.2.tar.gz

Source code

https://git.48k.eu/python-libfdisk/

Example

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()
	

License

python-libfdisk is released under the terms of the GNU Lesser General Public License 2.1 or (at your option) any later version, same license as libfdisk which is included in the util-linux package.

Authors

Users

ogClient is a component of the OpenGnsys project that uses python-libfdisk for remote partitioning of Linux computers.

Sponsors

python-libfdisk development is sponsored by Soleta Networks