Helpful context managers for managing decryption and mounts of storage devices
Project description
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, compression: ValidCompressions | None = None) -> Iterator[Path]- Mounts a device to a temporary directory, auto-detecting the file system type. For BtrFS, optional compression settings are supported.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file storage_device_managers-2.0.0.tar.gz.
File metadata
- Download URL: storage_device_managers-2.0.0.tar.gz
- Upload date:
- Size: 20.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e0389f3dd3fb9ec22b90c1520a5f32a36090ac930bf61a0c9c0554c7aecb1a3
|
|
| MD5 |
e619f1ceabb9359094bac2fec30943f6
|
|
| BLAKE2b-256 |
abb582e05db0f1327a3254955c3bf4a69df64205640de22d732690733485bcfb
|
Provenance
The following attestation bundles were made for storage_device_managers-2.0.0.tar.gz:
Publisher:
python-publish.yml on MaxG87/storage-device-managers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
storage_device_managers-2.0.0.tar.gz -
Subject digest:
0e0389f3dd3fb9ec22b90c1520a5f32a36090ac930bf61a0c9c0554c7aecb1a3 - Sigstore transparency entry: 1463174889
- Sigstore integration time:
-
Permalink:
MaxG87/storage-device-managers@87100e89308ad07e0af7343f8a27359071501091 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/MaxG87
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@87100e89308ad07e0af7343f8a27359071501091 -
Trigger Event:
release
-
Statement type:
File details
Details for the file storage_device_managers-2.0.0-py3-none-any.whl.
File metadata
- Download URL: storage_device_managers-2.0.0-py3-none-any.whl
- Upload date:
- Size: 21.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0f9b2f48c15c1496078e78a0289d983d79284162f72c197260b7c85f654e80b
|
|
| MD5 |
0e412f47fff41c87b8111fb70f83e560
|
|
| BLAKE2b-256 |
f31f9969a68034120058df9992479a37015c45f1cb06900ac68be9d2b17f45af
|
Provenance
The following attestation bundles were made for storage_device_managers-2.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on MaxG87/storage-device-managers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
storage_device_managers-2.0.0-py3-none-any.whl -
Subject digest:
d0f9b2f48c15c1496078e78a0289d983d79284162f72c197260b7c85f654e80b - Sigstore transparency entry: 1463174987
- Sigstore integration time:
-
Permalink:
MaxG87/storage-device-managers@87100e89308ad07e0af7343f8a27359071501091 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/MaxG87
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@87100e89308ad07e0af7343f8a27359071501091 -
Trigger Event:
release
-
Statement type: