https://github.com/maxg87/storage-device-managers
https://github.com/maxg87/storage-device-managers
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/maxg87/storage-device-managers
- Owner: MaxG87
- License: gpl-3.0
- Created: 2023-06-15T11:38:08.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-07-02T09:19:14.000Z (about 1 year ago)
- Last Synced: 2025-09-25T16:49:08.581Z (9 months ago)
- Language: Python
- Size: 1.1 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/psf/black)
[](https://pycqa.github.io/isort/)
[](http://mypy-lang.org/)
[](http://www.gnu.org/licenses/gpl-3.0)
# 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 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 compression settings
- Creating and removing symbolic links with root privileges
- Formatting and encrypting devices
- Changing file ownership securely
## Features
- **Device Decryption & Encryption**: Easily decrypt and encrypt storage devices using `cryptsetup`.
- **BtrFS Mount Management**: Mount and unmount BtrFS file systems with optional compression settings.
- **Symbolic Link Handling**: Create and remove symbolic links with elevated permissions.
- **File System Operations**: Format devices with BtrFS, manage ownership, and check mount status.
- **Secure Passphrase Handling**: Automatically generate safe passwords for encryption.
## Usage
### Decrypting and Mounting a Device
```python
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
```python
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
```python
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 `cryptsetup` and returns a context-managed path.
- `mounted_device(device: Path, compression: Optional[ValidCompressions] = None) -> Iterator[Path]`
- Mounts a BtrFS device with optional compression settings.
- `symbolic_link(src: Path, dest: Path) -> Iterator[Path]`
- Creates and removes a symbolic link with root privileges.
### Utility Functions
- `mount_btrfs_device(device: Path, mount_dir: Path, compression: Optional[ValidCompressions] = None) -> None`
- `is_mounted(device: Path) -> bool`
- `get_mounted_devices() -> Mapping[str, Mapping[Path, frozenset[str]]]`
- `unmount_device(device: Path) -> None`
- `open_encrypted_device(device: Path, pass_cmd: str) -> Path`
- `close_decrypted_device(device: Path) -> None`
- `encrypt_device(device: Path, password_cmd: str) -> UUID`
- `mkfs_btrfs(device: Path) -> None`
- `generate_passcmd() -> str`
- `chown(file_or_folder: Path, user: Union[int, str], group: Optional[Union[int, str]] = None, *, recursive: bool) -> None`
## Contributing
Contributions are welcome! Please submit issues and pull requests via GitHub.