Storage Device Managers - Helpful context managers for managing decryption and mounts of storage devices
Overview
The storage_device_managers module provides a set of utilities to manage
encrypted storage devices, handle BtrFS and ext4 mounts, and perform file
system operations in a secure and structured way. It is designed to support
common storage-related tasks such as:
- Decrypting and mounting encrypted devices
- Managing BtrFS and ext4 mounts (with automatic file system detection)
- Creating and removing symbolic links with root privileges
- Formatting and encrypting devices (BtrFS and ext4)
- Changing file ownership securely
Requires Python 3.11 or newer.
Features
- Device Decryption & Encryption: Easily decrypt and encrypt storage devices using
cryptsetup.PassCmdErroris raised when the password command fails, distinct from other shell errors. - BtrFS and ext4 Mount Management: Mount and unmount BtrFS file systems with optional compression settings, or ext4 file systems.
- Automatic File System Detection: Use
get_filesystemto detect a device's file system type, andmount_deviceto mount it without specifying the type manually. - Symbolic Link Handling: Create and remove symbolic links with elevated permissions.
- File System Operations: Format devices with BtrFS or ext4 using dedicated functions or the unified
mkfshelper, manage ownership, and check mount status. - Secure Passphrase Handling: Automatically generate safe passwords for encryption.
Usage
Decrypting and Mounting a Device
from pathlib import Path
from storage_device_managers import decrypted_device, mounted_device
# Decrypt and mount a device
with decrypted_device(Path("/dev/sdb1"), "cat /path/to/password-file") as dev:
with mounted_device(dev) as mount_point:
print(f"Device mounted at {mount_point}")
Encrypting a Device
from pathlib import Path
from storage_device_managers import encrypt_device
uuid = encrypt_device(Path("/dev/sdb1"), "cat /path/to/password-file")
print(f"Device encrypted with UUID: {uuid}")
Creating a Symbolic Link
from pathlib import Path
from storage_device_managers import symbolic_link
src = Path("/path/to/source")
dest = Path("/path/to/destination")
with symbolic_link(src, dest) as link:
print(f"Symbolic link created at {link}")
API Reference
Context Managers
decrypted_device(device: Path, pass_cmd: str) -> Iterator[Path]- Decrypts a device using
cryptsetupand returns a context-managed path. - Raises
shell_interface.PassCmdErrorif the password command fails. - Raises
shell_interface.ShellInterfaceErroron other shell-related errors.
- Decrypts a device using
mounted_device(device: Path, destination: Path | None = None, *, compression: ValidCompressions | None = None) -> Iterator[Path]- Mounts a device, auto-detecting the file system type. For BtrFS, optional compression settings are supported. If
destinationis provided, the device is mounted there and the directory is left intact after the context exits; otherwise a temporary directory is used and removed afterwards.
- Mounts a device, auto-detecting the file system type. For BtrFS, optional compression settings are supported. If
symbolic_link(src: Path, dest: Path) -> Iterator[Path]- Creates and removes a symbolic link with root privileges.
Utility Functions
get_filesystem(device: Path) -> strmount_device(device: Path, mount_dir: Path, compression: ValidCompressions | None = None) -> Nonemount_btrfs_device(device: Path, mount_dir: Path, compression: ValidCompressions | None = None) -> Nonemount_ext4_device(device: Path, mount_dir: Path) -> Noneis_mounted(device: Path) -> boolget_mounted_devices() -> Mapping[str, Mapping[Path, frozenset[str]]]unmount_device(device: Path) -> Noneopen_encrypted_device(device: Path, pass_cmd: str) -> Path- Raises
shell_interface.PassCmdErrorif the password command fails. - Raises
shell_interface.ShellInterfaceErroron other shell-related errors.
- Raises
close_decrypted_device(device: Path) -> Noneencrypt_device(device: Path, password_cmd: str) -> UUID- Raises
shell_interface.PassCmdErrorif the password command fails. - Raises
shell_interface.ShellInterfaceErroron other shell-related errors.
- Raises
mkfs(device: Path, filesystem: ValidFileSystems) -> Nonemkfs_btrfs(device: Path) -> Nonemkfs_ext4(device: Path) -> Nonegenerate_passcmd() -> strchown(file_or_folder: Path, user: int | str, group: int | str | None = None, *, recursive: bool) -> None
Contributing
Contributions are welcome! Please submit issues and pull requests via GitHub.
Metadata
Release files for storage-device-managers 3.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| storage_device_managers-3.0.1.tar.gz | 29.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| storage_device_managers-3.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 50.9 kB
Release files / storage_device_managers-3.0.1.tar.gz
| Download URL | storage_device_managers-3.0.1.tar.gz |
|---|---|
| Size | 29.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
77b6616075645ffb6c3780b5c8fd6bf8ef41f4aff65fc5092ca8770837373914
|
|
BLAKE2b-256 checksum How to use checksums |
1d0036a825d63f5a56ab0688a40ee0977e6fdb14235eca0c4dd7e47a2d393a00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.
Transparency logRelease files / storage_device_managers-3.0.1-py3-none-any.whl
| Download URL | storage_device_managers-3.0.1-py3-none-any.whl |
|---|---|
| Size | 21.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0be028dfe3581c812f6ed1fdbd3bf29d00675d8a760ff2bacd5997ac9659eec8
|
|
BLAKE2b-256 checksum How to use checksums |
08359feae0970a888e671e115f497e36b5e63e34d8a9ab68a9064c93c1e18f1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.
Transparency log