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. Pre-built binary wheels are available on PyPI — no compiler needed.

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.

All blocking operations (ioctl calls, sync, mkfs, mount/umount) release the GIL, so they can safely run in parallel from multiple threads without blocking the interpreter.

Requirements

  • Linux with btrfs support
  • Python 3.10+

Installation

Pre-built wheels for x86_64 and aarch64 (manylinux_2_28):

pip install pybtrfs

Releases with wheels are also available on GitHub.

To build from source (requires GCC and Python headers):

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.

This project statically compiles code from btrfs-progs (GPL-2.0), including libbtrfsutil (LGPL-2.1) and substantial portions of the core btrfs-progs codebase (kernel-shared, mkfs, crypto, etc.) which are GPL-2.0. Since the resulting binaries are derivative works of GPL-2.0 code, this project is licensed under GPL-2.0 to comply with the upstream license terms.

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.4.tar.gz (449.2 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.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

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

pybtrfs-0.5.4-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

pybtrfs-0.5.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

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

pybtrfs-0.5.4-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

pybtrfs-0.5.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

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

pybtrfs-0.5.4-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

pybtrfs-0.5.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

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

pybtrfs-0.5.4-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (1.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

File details

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

File metadata

  • Download URL: pybtrfs-0.5.4.tar.gz
  • Upload date:
  • Size: 449.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pybtrfs-0.5.4.tar.gz
Algorithm Hash digest
SHA256 fb22f2820469686dcac72f0a5b248a57de785a8271cc96a2d139bb07f790e702
MD5 e248d4c70a32dc049dbcd8ebe1fbed6a
BLAKE2b-256 4667b70eb78500dce2290cafaaf3d1404bbb5f570d927ac9c2217c10072201b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4.tar.gz:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybtrfs-0.5.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6de86586774a543c5dc7bd062eb691833d2d9375790c775baca847ff04eb27e5
MD5 89cf9b94282776c33761de5ebd40856e
BLAKE2b-256 2d45e16f0060f3fd3a8b5eb68000ce9a37880e9a4daa2c84ea61963006fe1465

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybtrfs-0.5.4-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.4-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 b30aa802ad63f4d6e7f5d71db71dcc3f89bd41fc2e67225ea8f9745651e74723
MD5 f5440278a0813a53eb4be4fb336cea3c
BLAKE2b-256 d95a1a1e008cda0180126ade370c5423e4d71bc80dae553add88827210100696

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybtrfs-0.5.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cea4003ea306ea483c489cd8b7b436b8e4e0114e1102c6f6d90d79e59c9b0115
MD5 9ac9c510417205046bc9e6ee71eb4a44
BLAKE2b-256 d12f856aeb9bfb49a9dc3e588538e4919b56539fae85262ae5d68427c1a18e5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybtrfs-0.5.4-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.4-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c0bbbb02ef847590a63035ee120b2265ca39d434781941777134999433ab3119
MD5 afb1e654d113d8289d4d11a516073221
BLAKE2b-256 74bf6aa93a08dd5ad4452f8688606b86fed4363183ec16875d0f864962e9d31c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybtrfs-0.5.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 02c72875d57e7275bde67cc2dcc2493a021fdf0ee7e71dcda054ac6856a9d7ea
MD5 ae731b096db853d2aaf320c76a77a4dc
BLAKE2b-256 56b1f84de51725ff6f8579553a64a6b1cf0c51a6c4914a2ba10d941cf27ad3ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybtrfs-0.5.4-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.4-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 e046b7a8807e37a01923e868a821469f5b5d4c66b47eec1d64fa3800d79aa851
MD5 6296debd59eb6a9c25a6a23df0c99068
BLAKE2b-256 2aa22c6a4c2f9b9607fedc94ffeb053d6050fe1986c58c44449c569c86aa20f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybtrfs-0.5.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ef14943eb052b72c4c91c7f48440a1cc8e3fd37087160713f65dbad676cf688c
MD5 2441c5141e5dbabe08bc2c2b83b20f69
BLAKE2b-256 b44e3a8f6aca2c23172369e2c1d1e160a4bebff7a0188c528ca0bd76a9114cb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybtrfs-0.5.4-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pybtrfs-0.5.4-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 01af57edbda4dd6b5374ae2fc0b787a372543901b07c2c8d93cfcfb95e79ad2c
MD5 c9115caa67f4769a3e02a00dfc3dab31
BLAKE2b-256 2fb8db447d33c21488ddd484537f59750cce1bf6ec360c7bb266e286dc4feaa4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybtrfs-0.5.4-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: wheels.yml on mosquito/pybtrfs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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