Skip to main content

Static Python bindings for btrfs ioctl operations

Project description

pybtrfs

Native Python bindings for btrfs — create filesystems, manage subvolumes and snapshots, mount and unmount. No shell commands, no subprocess, no runtime dependencies beyond libc.

Built as C extensions directly from vendored btrfs-progs source. pip install . is all you need.

Why not the existing btrfs package?

The btrfs package on PyPI requires libbtrfsutil installed as a system library. That means distro packages, broken virtualenvs, and pain on minimal containers or custom builds.

pybtrfs statically compiles everything from vendored source — the only build-time requirement is a C compiler and Python headers. The resulting .so files link only to libc.

Requirements

  • Linux with btrfs support
  • Python 3.12+
  • GCC and Python headers for building (apt install python3-dev)

Installation

pip install .

Or with make:

make build      # compile extensions
make install    # pip install .

Usage

Subvolumes and snapshots

import pybtrfs

# Create a subvolume
pybtrfs.create_subvolume("/mnt/data/project")

# Snapshot before a risky operation
pybtrfs.create_snapshot("/mnt/data/project", "/mnt/data/project-snap",
                        read_only=True)

# Check snapshot relationship
src = pybtrfs.subvolume_info("/mnt/data/project")
snap = pybtrfs.subvolume_info("/mnt/data/project-snap")
assert snap.parent_uuid == src.uuid

# Clean up
pybtrfs.delete_subvolume("/mnt/data/project-snap")

List all subvolumes

with pybtrfs.SubvolumeIterator("/mnt/data", info=True) as it:
    for path, info in it:
        print(f"{path} (id={info.id}, gen={info.generation})")

Create a filesystem

from pybtrfs import mkfs, CsumType, RaidProfile

# Single device
result = mkfs("/dev/sdb", label="data", force=True)
print(result["uuid"])

# RAID1 mirror with xxhash checksums
result = mkfs("/dev/sdb", "/dev/sdc",
              data_profile=RaidProfile.RAID1,
              metadata_profile=RaidProfile.RAID1,
              csum_type=CsumType.XXHASH,
              force=True)

Mount and unmount

from pybtrfs import mount, umount, mount_data, MountFlags

mount("/dev/sdb", "/mnt/data",
      data=mount_data(compress="zstd", space_cache="v2"))

mount("/dev/sdb", "/mnt/readonly", flags=MountFlags.RDONLY)

umount("/mnt/data")

Error handling

import pybtrfs

try:
    pybtrfs.sync("/not/btrfs")
except pybtrfs.BtrfsUtilError as e:
    print(e.btrfsutil_errno)  # e.g. BtrfsUtilErrno.NOT_BTRFS
    print(e.errno)            # OS errno

API reference

The package ships with .pyi stubs — full signatures and docstrings are available via help(pybtrfs), help(pybtrfs.mkfs), help(pybtrfs.mount), and your IDE's autocomplete.

Testing

Tests require root and a mounted btrfs filesystem:

truncate -s 1G /tmp/btrfs.img
mkfs.btrfs /tmp/btrfs.img
mkdir -p ~/btrfs
mount /tmp/btrfs.img ~/btrfs

sudo BTRFS=~/btrfs PYTHONPATH=. pytest -v

License

GPL-2.0 — see LICENSE.

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

pybtrfs-0.5.0.tar.gz (449.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pybtrfs-0.5.0-cp314-cp314t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.26+ x86-64manylinux: glibc 2.28+ x86-64

pybtrfs-0.5.0-cp314-cp314-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.26+ x86-64manylinux: glibc 2.28+ x86-64

pybtrfs-0.5.0-cp313-cp313t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.13tmanylinux: glibc 2.26+ x86-64manylinux: glibc 2.28+ x86-64

pybtrfs-0.5.0-cp313-cp313-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.26+ x86-64manylinux: glibc 2.28+ x86-64

pybtrfs-0.5.0-cp312-cp312-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.26+ x86-64manylinux: glibc 2.28+ x86-64

pybtrfs-0.5.0-cp311-cp311-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.26+ x86-64manylinux: glibc 2.28+ x86-64

pybtrfs-0.5.0-cp310-cp310-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.26+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file pybtrfs-0.5.0.tar.gz.

File metadata

  • Download URL: pybtrfs-0.5.0.tar.gz
  • Upload date:
  • Size: 449.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for pybtrfs-0.5.0.tar.gz
Algorithm Hash digest
SHA256 383a7437830f2e31f1c063a33c7e7753b73e8cb58dc618809edd9229040c1a44
MD5 be67c1aaff9bd7241ba8392c1e01fd8b
BLAKE2b-256 70c4cde44f53178ea1fa6969574405fbfbde2e07a2f819b54f4786ee4d1d3998

See more details on using hashes here.

File details

Details for the file pybtrfs-0.5.0-cp314-cp314t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.0-cp314-cp314t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 219aa21078544540effb61d79b1db52d9ece81024d8def85f5c9fe019465fc8d
MD5 079ca5607468ff7d6289be7d645580a5
BLAKE2b-256 3eadafc02fdcdcaf16babe0cb5607ee1cd2bf2af06b1a42cea5d4f0ac9ad54b1

See more details on using hashes here.

File details

Details for the file pybtrfs-0.5.0-cp314-cp314-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.0-cp314-cp314-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7f161be3ad5f0c3e9a084c2c32942d74366396b8c241e7e650988a547554ba7c
MD5 376441d76b582c230766983c1e434030
BLAKE2b-256 9fbdc1a8a8b2c9c8505e3e827a74866c6d879886349f57245a7c4cce58d9fdb4

See more details on using hashes here.

File details

Details for the file pybtrfs-0.5.0-cp313-cp313t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.0-cp313-cp313t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ea6d8da5872734fe4e6029fa1a70dffb7c2f6ef73254e392fbdcb6fc2568341c
MD5 8e80e66a1462cd531e020df3a5bcf717
BLAKE2b-256 081d1ebb7b94106493b0f16026dbd549e8b3a5bd568e8e5d80a5f27ae42700bd

See more details on using hashes here.

File details

Details for the file pybtrfs-0.5.0-cp313-cp313-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.0-cp313-cp313-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c20c472eaff207aa88db2b6e40944fd43275d9ab3d51d2cf97f14c8a867a7744
MD5 3f268f8b6ba0e6d361dfa1920950762c
BLAKE2b-256 1eeb23f36e361f9d22e6758fae735bdd0b3c24210b2b2fd3222c9e9f58dd9d90

See more details on using hashes here.

File details

Details for the file pybtrfs-0.5.0-cp312-cp312-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.0-cp312-cp312-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b8a7bf7cb1f220299f23f84f89ade66f6c67f8a11e866087504683bbd0033648
MD5 bb4c55b2d28f6384ceeb10085650d200
BLAKE2b-256 43b303ab711ab4aab0c1ed68259f1226aa377ade8c9fadd3e7cf9b09a161d352

See more details on using hashes here.

File details

Details for the file pybtrfs-0.5.0-cp311-cp311-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.0-cp311-cp311-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b7adbe1627395309f70b4e37780969ee9b225977c11f6b37dc86758562e63da7
MD5 8c19652f309307b311e5aa25c9037094
BLAKE2b-256 d94b9e47ef72bfeec728b760377b5b907773a3157c7ac5f66430a6a989b22441

See more details on using hashes here.

File details

Details for the file pybtrfs-0.5.0-cp310-cp310-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.0-cp310-cp310-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4746205339a7ddb8be375f983bad6e508f8b048b0701342ae4a7e2b3b057c8cc
MD5 01a6d93d22e02b930dc4d9b4aecda19e
BLAKE2b-256 2e72804274a778e3e630585a3c55718e844541a1f2804be9170bf4f75e58d62c

See more details on using hashes here.

Supported by

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