diskman 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- diskman/__init__.py +2 -0
- diskman/cli.py +253 -0
- diskman/core.py +503 -0
- diskman/tui.py +211 -0
- diskman-0.2.1.dist-info/METADATA +192 -0
- diskman-0.2.1.dist-info/RECORD +10 -0
- diskman-0.2.1.dist-info/WHEEL +5 -0
- diskman-0.2.1.dist-info/entry_points.txt +2 -0
- diskman-0.2.1.dist-info/licenses/LICENSE +21 -0
- diskman-0.2.1.dist-info/top_level.txt +1 -0
diskman/__init__.py
ADDED
diskman/cli.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import getpass
|
|
5
|
+
import io
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from diskman.core import (
|
|
10
|
+
DEFAULT_BASE_DIR,
|
|
11
|
+
CommandError,
|
|
12
|
+
automount,
|
|
13
|
+
automount_async,
|
|
14
|
+
collect_partitions,
|
|
15
|
+
disable_persistent_mount,
|
|
16
|
+
enable_persistent_mount,
|
|
17
|
+
find_partition,
|
|
18
|
+
is_luks_open,
|
|
19
|
+
is_mountable,
|
|
20
|
+
is_root_partition,
|
|
21
|
+
lock_luks,
|
|
22
|
+
mount_partition,
|
|
23
|
+
mount_partition_async,
|
|
24
|
+
persistent_mount_map,
|
|
25
|
+
require_root,
|
|
26
|
+
root_sources,
|
|
27
|
+
smart_health,
|
|
28
|
+
umount_partition,
|
|
29
|
+
umount_partition_async,
|
|
30
|
+
unlock_luks,
|
|
31
|
+
)
|
|
32
|
+
from diskman.tui import run_tui
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _flags_for_part(p, roots, boot_map):
|
|
36
|
+
flags = []
|
|
37
|
+
if is_root_partition(p, roots):
|
|
38
|
+
flags.append("ROOT")
|
|
39
|
+
if p.mounted:
|
|
40
|
+
flags.append("MOUNTED")
|
|
41
|
+
if not is_mountable(p):
|
|
42
|
+
flags.append("SKIP")
|
|
43
|
+
if p.is_luks:
|
|
44
|
+
flags.append("LUKS_OPEN" if is_luks_open(p) else "LUKS_LOCKED")
|
|
45
|
+
if p.uuid and p.uuid in boot_map:
|
|
46
|
+
flags.append("AUTOBOOT")
|
|
47
|
+
return flags
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def render_table(parts) -> str:
|
|
51
|
+
roots = root_sources()
|
|
52
|
+
boot_map = persistent_mount_map()
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
from rich.console import Console
|
|
56
|
+
from rich.table import Table
|
|
57
|
+
|
|
58
|
+
table = Table(title="diskman partitions")
|
|
59
|
+
table.add_column("Device")
|
|
60
|
+
table.add_column("Type")
|
|
61
|
+
table.add_column("Disk")
|
|
62
|
+
table.add_column("FS")
|
|
63
|
+
table.add_column("Size")
|
|
64
|
+
table.add_column("Mountpoint")
|
|
65
|
+
table.add_column("SMART")
|
|
66
|
+
table.add_column("Flags")
|
|
67
|
+
|
|
68
|
+
for p in parts:
|
|
69
|
+
flags = _flags_for_part(p, roots, boot_map)
|
|
70
|
+
table.add_row(
|
|
71
|
+
p.path,
|
|
72
|
+
p.devtype or "-",
|
|
73
|
+
p.disk_kind or "-",
|
|
74
|
+
p.fstype or "-",
|
|
75
|
+
p.size or "-",
|
|
76
|
+
p.mountpoint or "-",
|
|
77
|
+
smart_health(p),
|
|
78
|
+
",".join(flags) or "-",
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
out = io.StringIO()
|
|
82
|
+
console = Console(file=out, force_terminal=False, color_system=None)
|
|
83
|
+
console.print(table)
|
|
84
|
+
return out.getvalue()
|
|
85
|
+
except Exception:
|
|
86
|
+
header = (
|
|
87
|
+
f"{'DEVICE':<18} {'TYPE':<7} {'DISK':<5} {'FS':<10} {'SIZE':<8} "
|
|
88
|
+
f"{'MOUNTPOINT':<22} {'SMART':<12} {'FLAGS'}"
|
|
89
|
+
)
|
|
90
|
+
lines = [header, "-" * len(header)]
|
|
91
|
+
for p in parts:
|
|
92
|
+
flags = _flags_for_part(p, roots, boot_map)
|
|
93
|
+
lines.append(
|
|
94
|
+
f"{p.path:<18} {p.devtype:<7} {p.disk_kind:<5} {p.fstype or '-':<10} {p.size or '-':<8} "
|
|
95
|
+
f"{p.mountpoint or '-':<22} {smart_health(p):<12} {','.join(flags) or '-'}"
|
|
96
|
+
)
|
|
97
|
+
return "\n".join(lines)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
101
|
+
parser = argparse.ArgumentParser(
|
|
102
|
+
prog="diskman",
|
|
103
|
+
description="CLI + TUI disk/partition manager with auto-mount (excluding root).",
|
|
104
|
+
)
|
|
105
|
+
parser.add_argument(
|
|
106
|
+
"--base-dir",
|
|
107
|
+
default=str(DEFAULT_BASE_DIR),
|
|
108
|
+
help="Base mount directory for auto/manual mounts (default: /mnt/auto)",
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
sub = parser.add_subparsers(dest="cmd", required=True)
|
|
112
|
+
sub.add_parser("list", help="List partitions and mount status")
|
|
113
|
+
|
|
114
|
+
auto = sub.add_parser("automount", help="Auto-mount all mountable partitions except root")
|
|
115
|
+
auto.add_argument("--dry-run", action="store_true", help="Show what would be mounted")
|
|
116
|
+
auto.add_argument("--async", dest="is_async", action="store_true", help="Run automount in a background worker")
|
|
117
|
+
|
|
118
|
+
mount_p = sub.add_parser("mount", help="Mount one device")
|
|
119
|
+
mount_p.add_argument("device", help="Device path, e.g. /dev/sdb1")
|
|
120
|
+
mount_p.add_argument("--async", dest="is_async", action="store_true", help="Run mount in a background worker")
|
|
121
|
+
|
|
122
|
+
umount_p = sub.add_parser("umount", help="Unmount one device")
|
|
123
|
+
umount_p.add_argument("device", help="Device path, e.g. /dev/sdb1")
|
|
124
|
+
umount_p.add_argument("--lock-luks", action="store_true", help="If device is LUKS, close mapper after unmount")
|
|
125
|
+
umount_p.add_argument("--async", dest="is_async", action="store_true", help="Run unmount in a background worker")
|
|
126
|
+
|
|
127
|
+
sub.add_parser("boot-list", help="List diskman reboot auto-mount entries in fstab")
|
|
128
|
+
|
|
129
|
+
boot_add = sub.add_parser("boot-add", help="Enable reboot auto-mount for one device")
|
|
130
|
+
boot_add.add_argument("device", help="Device path, e.g. /dev/sdb1")
|
|
131
|
+
|
|
132
|
+
boot_rm = sub.add_parser("boot-remove", help="Disable reboot auto-mount for one device")
|
|
133
|
+
boot_rm.add_argument("device", help="Device path, e.g. /dev/sdb1")
|
|
134
|
+
|
|
135
|
+
luks_unlock = sub.add_parser("luks-unlock", help="Unlock a LUKS device")
|
|
136
|
+
luks_unlock.add_argument("device", help="LUKS device path, e.g. /dev/sdb2")
|
|
137
|
+
|
|
138
|
+
luks_lock = sub.add_parser("luks-lock", help="Lock a LUKS device mapper")
|
|
139
|
+
luks_lock.add_argument("device", help="LUKS device path, e.g. /dev/sdb2")
|
|
140
|
+
|
|
141
|
+
sub.add_parser("tui", help="Start terminal UI")
|
|
142
|
+
return parser
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _prompt_luks_passphrase() -> str:
|
|
146
|
+
return getpass.getpass("LUKS passphrase: ")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def main(argv: list[str] | None = None) -> int:
|
|
150
|
+
parser = build_parser()
|
|
151
|
+
args = parser.parse_args(argv)
|
|
152
|
+
base_dir = Path(args.base_dir)
|
|
153
|
+
|
|
154
|
+
try:
|
|
155
|
+
if args.cmd == "list":
|
|
156
|
+
print(render_table(collect_partitions()))
|
|
157
|
+
return 0
|
|
158
|
+
|
|
159
|
+
if args.cmd == "automount":
|
|
160
|
+
if not args.dry_run:
|
|
161
|
+
require_root()
|
|
162
|
+
if args.is_async:
|
|
163
|
+
logs = automount_async(base_dir=base_dir, dry_run=args.dry_run).result()
|
|
164
|
+
else:
|
|
165
|
+
logs = automount(base_dir=base_dir, dry_run=args.dry_run)
|
|
166
|
+
for line in logs:
|
|
167
|
+
print(line)
|
|
168
|
+
return 0
|
|
169
|
+
|
|
170
|
+
if args.cmd == "mount":
|
|
171
|
+
require_root()
|
|
172
|
+
part = find_partition(collect_partitions(), args.device)
|
|
173
|
+
if not part:
|
|
174
|
+
raise CommandError(f"Device not found: {args.device}")
|
|
175
|
+
|
|
176
|
+
passphrase = None
|
|
177
|
+
if part.is_luks and not is_luks_open(part):
|
|
178
|
+
passphrase = _prompt_luks_passphrase()
|
|
179
|
+
|
|
180
|
+
if args.is_async:
|
|
181
|
+
_, msg = mount_partition_async(part, base_dir, root_sources(), passphrase).result()
|
|
182
|
+
else:
|
|
183
|
+
_, msg = mount_partition(part, base_dir, root_sources(), passphrase)
|
|
184
|
+
print(msg)
|
|
185
|
+
return 0
|
|
186
|
+
|
|
187
|
+
if args.cmd == "umount":
|
|
188
|
+
require_root()
|
|
189
|
+
part = find_partition(collect_partitions(), args.device)
|
|
190
|
+
if not part:
|
|
191
|
+
raise CommandError(f"Device not found: {args.device}")
|
|
192
|
+
if args.is_async:
|
|
193
|
+
_, msg = umount_partition_async(part, root_sources(), args.lock_luks).result()
|
|
194
|
+
else:
|
|
195
|
+
_, msg = umount_partition(part, root_sources(), args.lock_luks)
|
|
196
|
+
print(msg)
|
|
197
|
+
return 0
|
|
198
|
+
|
|
199
|
+
if args.cmd == "tui":
|
|
200
|
+
run_tui(base_dir)
|
|
201
|
+
return 0
|
|
202
|
+
|
|
203
|
+
if args.cmd == "boot-list":
|
|
204
|
+
boot_map = persistent_mount_map()
|
|
205
|
+
if not boot_map:
|
|
206
|
+
print("No diskman reboot auto-mount entries found.")
|
|
207
|
+
return 0
|
|
208
|
+
for uuid, mnt in boot_map.items():
|
|
209
|
+
print(f"UUID={uuid} -> {mnt}")
|
|
210
|
+
return 0
|
|
211
|
+
|
|
212
|
+
if args.cmd == "boot-add":
|
|
213
|
+
require_root()
|
|
214
|
+
part = find_partition(collect_partitions(), args.device)
|
|
215
|
+
if not part:
|
|
216
|
+
raise CommandError(f"Device not found: {args.device}")
|
|
217
|
+
print(enable_persistent_mount(part, base_dir, root_sources()))
|
|
218
|
+
return 0
|
|
219
|
+
|
|
220
|
+
if args.cmd == "boot-remove":
|
|
221
|
+
require_root()
|
|
222
|
+
part = find_partition(collect_partitions(), args.device)
|
|
223
|
+
if not part:
|
|
224
|
+
raise CommandError(f"Device not found: {args.device}")
|
|
225
|
+
print(disable_persistent_mount(part))
|
|
226
|
+
return 0
|
|
227
|
+
|
|
228
|
+
if args.cmd == "luks-unlock":
|
|
229
|
+
require_root()
|
|
230
|
+
part = find_partition(collect_partitions(), args.device)
|
|
231
|
+
if not part:
|
|
232
|
+
raise CommandError(f"Device not found: {args.device}")
|
|
233
|
+
passphrase = _prompt_luks_passphrase()
|
|
234
|
+
mapper = unlock_luks(part, passphrase)
|
|
235
|
+
print(f"Unlocked {part.path} -> {mapper}")
|
|
236
|
+
return 0
|
|
237
|
+
|
|
238
|
+
if args.cmd == "luks-lock":
|
|
239
|
+
require_root()
|
|
240
|
+
part = find_partition(collect_partitions(), args.device)
|
|
241
|
+
if not part:
|
|
242
|
+
raise CommandError(f"Device not found: {args.device}")
|
|
243
|
+
print(lock_luks(part))
|
|
244
|
+
return 0
|
|
245
|
+
|
|
246
|
+
raise CommandError(f"Unknown command: {args.cmd}")
|
|
247
|
+
except CommandError as exc:
|
|
248
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
249
|
+
return 1
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
if __name__ == "__main__":
|
|
253
|
+
raise SystemExit(main())
|
diskman/core.py
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
from concurrent.futures import Future, ThreadPoolExecutor
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import List, Tuple
|
|
10
|
+
|
|
11
|
+
LSBLK_COLUMNS = "NAME,KNAME,PATH,TYPE,PKNAME,FSTYPE,LABEL,UUID,SIZE,MOUNTPOINT,ROTA"
|
|
12
|
+
DEFAULT_BASE_DIR = Path("/mnt/auto")
|
|
13
|
+
DEFAULT_FSTAB_PATH = Path("/etc/fstab")
|
|
14
|
+
DISKMAN_FSTAB_TAG = "diskman:auto"
|
|
15
|
+
|
|
16
|
+
_smart_cache: dict[str, str] = {}
|
|
17
|
+
_executor = ThreadPoolExecutor(max_workers=4)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class Partition:
|
|
22
|
+
name: str
|
|
23
|
+
kname: str
|
|
24
|
+
path: str
|
|
25
|
+
devtype: str
|
|
26
|
+
pkname: str
|
|
27
|
+
disk_path: str
|
|
28
|
+
disk_kind: str
|
|
29
|
+
fstype: str
|
|
30
|
+
label: str
|
|
31
|
+
uuid: str
|
|
32
|
+
size: str
|
|
33
|
+
mountpoint: str
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def mounted(self) -> bool:
|
|
37
|
+
return bool(self.mountpoint)
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def is_luks(self) -> bool:
|
|
41
|
+
return self.fstype.lower() == "crypto_luks"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class CommandError(RuntimeError):
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def run_cmd(cmd: List[str], check: bool = True, input_text: str | None = None) -> str:
|
|
49
|
+
proc = subprocess.run(cmd, text=True, capture_output=True, input=input_text)
|
|
50
|
+
if check and proc.returncode != 0:
|
|
51
|
+
raise CommandError((proc.stderr or proc.stdout or "command failed").strip())
|
|
52
|
+
return proc.stdout.strip()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def run_cmd_proc(cmd: List[str], input_text: str | None = None) -> subprocess.CompletedProcess[str]:
|
|
56
|
+
return subprocess.run(cmd, text=True, capture_output=True, input=input_text)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def lsblk_json() -> dict:
|
|
60
|
+
out = run_cmd(["lsblk", "-J", "-p", "-o", LSBLK_COLUMNS])
|
|
61
|
+
return json.loads(out)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def iter_nodes(node: dict):
|
|
65
|
+
yield node
|
|
66
|
+
for child in node.get("children", []):
|
|
67
|
+
yield from iter_nodes(child)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _rota_to_kind(rota: object) -> str:
|
|
71
|
+
if rota is True or str(rota).strip() == "1":
|
|
72
|
+
return "HDD"
|
|
73
|
+
if rota is False or str(rota).strip() == "0":
|
|
74
|
+
return "SSD"
|
|
75
|
+
return "UNKNOWN"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _pick_mount_options(part: Partition, read_only: bool = False) -> str:
|
|
79
|
+
opts = ["defaults", "nofail", "noatime"]
|
|
80
|
+
if read_only:
|
|
81
|
+
opts.append("ro")
|
|
82
|
+
|
|
83
|
+
fs = part.fstype.lower()
|
|
84
|
+
if fs in {"ext4", "xfs", "btrfs", "f2fs"} and part.disk_kind == "SSD":
|
|
85
|
+
opts.append("discard")
|
|
86
|
+
if fs in {"vfat", "fat", "fat32", "exfat"}:
|
|
87
|
+
opts.extend([f"uid={os.getuid()}", f"gid={os.getgid()}", "umask=022"])
|
|
88
|
+
if fs == "ntfs3":
|
|
89
|
+
opts.extend([f"uid={os.getuid()}", f"gid={os.getgid()}", "windows_names"])
|
|
90
|
+
|
|
91
|
+
# Keep order stable and unique.
|
|
92
|
+
return ",".join(dict.fromkeys(opts))
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _mount_once(path: str, mountpoint: str, options: str | None = None) -> subprocess.CompletedProcess[str]:
|
|
96
|
+
cmd = ["mount"]
|
|
97
|
+
if options:
|
|
98
|
+
cmd.extend(["-o", options])
|
|
99
|
+
cmd.extend([path, mountpoint])
|
|
100
|
+
return run_cmd_proc(cmd)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _mount_with_read_only_fallback(part: Partition, mountpoint: Path) -> Tuple[bool, str]:
|
|
104
|
+
rw_opts = _pick_mount_options(part, read_only=False)
|
|
105
|
+
proc = _mount_once(part.path, str(mountpoint), rw_opts)
|
|
106
|
+
if proc.returncode == 0:
|
|
107
|
+
return True, f"Mounted {part.path} -> {mountpoint} ({rw_opts})"
|
|
108
|
+
|
|
109
|
+
ro_opts = _pick_mount_options(part, read_only=True)
|
|
110
|
+
ro_proc = _mount_once(part.path, str(mountpoint), ro_opts)
|
|
111
|
+
if ro_proc.returncode == 0:
|
|
112
|
+
reason = (proc.stderr or proc.stdout or "rw mount failed").strip()
|
|
113
|
+
return True, f"Mounted read-only {part.path} -> {mountpoint} ({ro_opts}); reason: {reason}"
|
|
114
|
+
|
|
115
|
+
err = (ro_proc.stderr or ro_proc.stdout or proc.stderr or proc.stdout or "mount failed").strip()
|
|
116
|
+
return False, f"Mount failed for {part.path}: {err}"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _luks_mapper_name(part: Partition) -> str:
|
|
120
|
+
safe = Path(part.kname).name.replace("/", "-")
|
|
121
|
+
return f"diskman-{safe}"
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _luks_mapper_path(mapper_name: str) -> str:
|
|
125
|
+
return f"/dev/mapper/{mapper_name}"
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def is_luks_open(part: Partition) -> bool:
|
|
129
|
+
mapper_name = _luks_mapper_name(part)
|
|
130
|
+
mapper_path = _luks_mapper_path(mapper_name)
|
|
131
|
+
return Path(mapper_path).exists()
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def unlock_luks(part: Partition, passphrase: str) -> str:
|
|
135
|
+
if not part.is_luks:
|
|
136
|
+
raise CommandError(f"Not a LUKS device: {part.path}")
|
|
137
|
+
|
|
138
|
+
mapper_name = _luks_mapper_name(part)
|
|
139
|
+
proc = run_cmd_proc(["cryptsetup", "open", part.path, mapper_name, "--key-file", "-"], input_text=passphrase)
|
|
140
|
+
if proc.returncode != 0:
|
|
141
|
+
raise CommandError((proc.stderr or proc.stdout or "cryptsetup open failed").strip())
|
|
142
|
+
return _luks_mapper_path(mapper_name)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def lock_luks(part: Partition) -> str:
|
|
146
|
+
if not part.is_luks:
|
|
147
|
+
raise CommandError(f"Not a LUKS device: {part.path}")
|
|
148
|
+
mapper_name = _luks_mapper_name(part)
|
|
149
|
+
run_cmd(["cryptsetup", "close", mapper_name])
|
|
150
|
+
return f"Closed LUKS mapper: {mapper_name}"
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _resolve_luks_inner(part: Partition) -> str:
|
|
154
|
+
mapper_name = _luks_mapper_name(part)
|
|
155
|
+
mapper_path = _luks_mapper_path(mapper_name)
|
|
156
|
+
if not Path(mapper_path).exists():
|
|
157
|
+
raise CommandError(f"LUKS device is locked: {part.path}. Unlock first.")
|
|
158
|
+
|
|
159
|
+
out = run_cmd(["lsblk", "-J", "-p", "-o", "PATH,FSTYPE", mapper_path])
|
|
160
|
+
data = json.loads(out)
|
|
161
|
+
nodes = data.get("blockdevices", [])
|
|
162
|
+
if not nodes:
|
|
163
|
+
return mapper_path
|
|
164
|
+
node = nodes[0]
|
|
165
|
+
children = node.get("children", [])
|
|
166
|
+
if children:
|
|
167
|
+
child_path = (children[0].get("path") or "").strip()
|
|
168
|
+
if child_path.startswith("/dev/"):
|
|
169
|
+
return child_path
|
|
170
|
+
return mapper_path
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _physical_disk_path(node: dict, top_path: str) -> str:
|
|
174
|
+
pkname = (node.get("pkname") or "").strip()
|
|
175
|
+
if pkname:
|
|
176
|
+
if pkname.startswith("/dev/"):
|
|
177
|
+
return pkname
|
|
178
|
+
return f"/dev/{pkname}"
|
|
179
|
+
return top_path
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def collect_partitions() -> List[Partition]:
|
|
183
|
+
data = lsblk_json()
|
|
184
|
+
partitions: List[Partition] = []
|
|
185
|
+
for top in data.get("blockdevices", []):
|
|
186
|
+
top_path = (top.get("path") or "").strip()
|
|
187
|
+
top_kind = _rota_to_kind(top.get("rota"))
|
|
188
|
+
for node in iter_nodes(top):
|
|
189
|
+
devtype = (node.get("type") or "").strip()
|
|
190
|
+
path = (node.get("path") or "").strip()
|
|
191
|
+
if not path.startswith("/dev/"):
|
|
192
|
+
continue
|
|
193
|
+
if devtype in {"disk", "loop", "rom"}:
|
|
194
|
+
continue
|
|
195
|
+
|
|
196
|
+
disk_path = _physical_disk_path(node, top_path)
|
|
197
|
+
partitions.append(
|
|
198
|
+
Partition(
|
|
199
|
+
name=(node.get("name") or "").strip(),
|
|
200
|
+
kname=(node.get("kname") or "").strip(),
|
|
201
|
+
path=path,
|
|
202
|
+
devtype=devtype,
|
|
203
|
+
pkname=(node.get("pkname") or "").strip(),
|
|
204
|
+
disk_path=disk_path,
|
|
205
|
+
disk_kind=top_kind,
|
|
206
|
+
fstype=(node.get("fstype") or "").strip(),
|
|
207
|
+
label=(node.get("label") or "").strip(),
|
|
208
|
+
uuid=(node.get("uuid") or "").strip(),
|
|
209
|
+
size=(node.get("size") or "").strip(),
|
|
210
|
+
mountpoint=(node.get("mountpoint") or "").strip(),
|
|
211
|
+
)
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
partitions.sort(key=lambda p: p.path)
|
|
215
|
+
return partitions
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def root_sources() -> set[str]:
|
|
219
|
+
source = run_cmd(["findmnt", "-n", "-o", "SOURCE", "/"])
|
|
220
|
+
source = source.strip()
|
|
221
|
+
if not source:
|
|
222
|
+
return set()
|
|
223
|
+
if source.startswith("/dev/"):
|
|
224
|
+
return {source.split("[", 1)[0]}
|
|
225
|
+
return set()
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def is_root_partition(part: Partition, root_devs: set[str]) -> bool:
|
|
229
|
+
return part.path in root_devs or part.mountpoint == "/"
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def is_mountable(part: Partition) -> bool:
|
|
233
|
+
if not part.fstype:
|
|
234
|
+
return False
|
|
235
|
+
if part.fstype.lower() == "swap":
|
|
236
|
+
return False
|
|
237
|
+
if part.is_luks:
|
|
238
|
+
# LUKS container needs unlock before mount.
|
|
239
|
+
return True
|
|
240
|
+
return True
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def target_mount_point(part: Partition, base_dir: Path) -> Path:
|
|
244
|
+
return base_dir / Path(part.path).name
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def ensure_dir(path: Path) -> None:
|
|
248
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def require_root() -> None:
|
|
252
|
+
if os.geteuid() != 0:
|
|
253
|
+
raise CommandError("This operation requires root. Re-run with sudo.")
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def find_partition(parts: List[Partition], device: str) -> Partition | None:
|
|
257
|
+
for part in parts:
|
|
258
|
+
if part.path == device:
|
|
259
|
+
return part
|
|
260
|
+
return None
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def _smart_health_for_disk(disk_path: str) -> str:
|
|
264
|
+
if disk_path in _smart_cache:
|
|
265
|
+
return _smart_cache[disk_path]
|
|
266
|
+
|
|
267
|
+
# smartctl may require root and may not exist on all systems.
|
|
268
|
+
try:
|
|
269
|
+
proc = run_cmd_proc(["smartctl", "-H", "-j", disk_path])
|
|
270
|
+
except FileNotFoundError:
|
|
271
|
+
_smart_cache[disk_path] = "NO_SMARTCTL"
|
|
272
|
+
return _smart_cache[disk_path]
|
|
273
|
+
|
|
274
|
+
if proc.returncode != 0 and not proc.stdout:
|
|
275
|
+
_smart_cache[disk_path] = "UNAVAILABLE"
|
|
276
|
+
return _smart_cache[disk_path]
|
|
277
|
+
|
|
278
|
+
try:
|
|
279
|
+
payload = json.loads(proc.stdout or "{}")
|
|
280
|
+
except json.JSONDecodeError:
|
|
281
|
+
_smart_cache[disk_path] = "UNKNOWN"
|
|
282
|
+
return _smart_cache[disk_path]
|
|
283
|
+
|
|
284
|
+
smart_status = payload.get("smart_status") or {}
|
|
285
|
+
passed = smart_status.get("passed")
|
|
286
|
+
if passed is True:
|
|
287
|
+
status = "PASSED"
|
|
288
|
+
elif passed is False:
|
|
289
|
+
status = "FAILED"
|
|
290
|
+
else:
|
|
291
|
+
# Fallback to short text status fields if present.
|
|
292
|
+
status = (
|
|
293
|
+
str(payload.get("ata_smart_data", {}).get("self_test", {}).get("status", {}).get("string") or "")
|
|
294
|
+
.strip()
|
|
295
|
+
.upper()
|
|
296
|
+
or "UNKNOWN"
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
_smart_cache[disk_path] = status
|
|
300
|
+
return status
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def smart_health(part: Partition) -> str:
|
|
304
|
+
return _smart_health_for_disk(part.disk_path)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def is_mount_read_only(part: Partition) -> bool:
|
|
308
|
+
if not part.mountpoint:
|
|
309
|
+
return False
|
|
310
|
+
proc = run_cmd_proc(["findmnt", "-n", "-o", "OPTIONS", "--target", part.mountpoint])
|
|
311
|
+
if proc.returncode != 0:
|
|
312
|
+
return False
|
|
313
|
+
options = (proc.stdout or "").strip().split(",")
|
|
314
|
+
return "ro" in options
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def mount_partition(
|
|
318
|
+
part: Partition,
|
|
319
|
+
base_dir: Path,
|
|
320
|
+
root_devs: set[str],
|
|
321
|
+
luks_passphrase: str | None = None,
|
|
322
|
+
) -> Tuple[bool, str]:
|
|
323
|
+
if is_root_partition(part, root_devs):
|
|
324
|
+
return False, f"Skipping root partition: {part.path}"
|
|
325
|
+
if not is_mountable(part):
|
|
326
|
+
return False, f"Not mountable: {part.path}"
|
|
327
|
+
if part.mounted:
|
|
328
|
+
return False, f"Already mounted: {part.path} -> {part.mountpoint}"
|
|
329
|
+
|
|
330
|
+
mount_source = part.path
|
|
331
|
+
if part.is_luks:
|
|
332
|
+
if not is_luks_open(part):
|
|
333
|
+
if luks_passphrase is None:
|
|
334
|
+
return False, f"LUKS device locked: {part.path}"
|
|
335
|
+
unlock_luks(part, luks_passphrase)
|
|
336
|
+
mount_source = _resolve_luks_inner(part)
|
|
337
|
+
|
|
338
|
+
mnt = target_mount_point(part, base_dir)
|
|
339
|
+
ensure_dir(mnt)
|
|
340
|
+
|
|
341
|
+
# Try filesystem-aware rw mount first; fallback to read-only.
|
|
342
|
+
effective = Partition(**{**part.__dict__, "path": mount_source, "fstype": part.fstype})
|
|
343
|
+
ok, msg = _mount_with_read_only_fallback(effective, mnt)
|
|
344
|
+
if ok:
|
|
345
|
+
return True, msg
|
|
346
|
+
return False, msg
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def umount_partition(part: Partition, root_devs: set[str], lock_luks_after: bool = False) -> Tuple[bool, str]:
|
|
350
|
+
if is_root_partition(part, root_devs):
|
|
351
|
+
return False, f"Skipping root partition: {part.path}"
|
|
352
|
+
if not part.mounted:
|
|
353
|
+
if part.is_luks and lock_luks_after and is_luks_open(part):
|
|
354
|
+
lock_msg = lock_luks(part)
|
|
355
|
+
return True, f"Not mounted; {lock_msg}"
|
|
356
|
+
return False, f"Not mounted: {part.path}"
|
|
357
|
+
|
|
358
|
+
run_cmd(["umount", part.path])
|
|
359
|
+
if part.is_luks and lock_luks_after and is_luks_open(part):
|
|
360
|
+
lock_msg = lock_luks(part)
|
|
361
|
+
return True, f"Unmounted {part.path}; {lock_msg}"
|
|
362
|
+
return True, f"Unmounted {part.path}"
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def automount(base_dir: Path, dry_run: bool = False) -> List[str]:
|
|
366
|
+
logs: List[str] = []
|
|
367
|
+
parts = collect_partitions()
|
|
368
|
+
roots = root_sources()
|
|
369
|
+
|
|
370
|
+
for part in parts:
|
|
371
|
+
if is_root_partition(part, roots):
|
|
372
|
+
logs.append(f"SKIP root: {part.path}")
|
|
373
|
+
continue
|
|
374
|
+
if not is_mountable(part):
|
|
375
|
+
logs.append(f"SKIP unsupported: {part.path}")
|
|
376
|
+
continue
|
|
377
|
+
if part.mounted:
|
|
378
|
+
logs.append(f"SKIP already mounted: {part.path} -> {part.mountpoint}")
|
|
379
|
+
continue
|
|
380
|
+
if part.is_luks and not is_luks_open(part):
|
|
381
|
+
logs.append(f"SKIP locked LUKS: {part.path}")
|
|
382
|
+
continue
|
|
383
|
+
|
|
384
|
+
mnt = target_mount_point(part, base_dir)
|
|
385
|
+
if dry_run:
|
|
386
|
+
logs.append(f"DRY-RUN mount {part.path} -> {mnt}")
|
|
387
|
+
continue
|
|
388
|
+
|
|
389
|
+
try:
|
|
390
|
+
ensure_dir(mnt)
|
|
391
|
+
_, msg = mount_partition(part, base_dir, roots)
|
|
392
|
+
logs.append(f"OK {msg}")
|
|
393
|
+
except Exception as exc:
|
|
394
|
+
logs.append(f"ERR mount {part.path}: {exc}")
|
|
395
|
+
|
|
396
|
+
return logs
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def automount_async(base_dir: Path, dry_run: bool = False) -> Future[List[str]]:
|
|
400
|
+
return _executor.submit(automount, base_dir, dry_run)
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def mount_partition_async(
|
|
404
|
+
part: Partition,
|
|
405
|
+
base_dir: Path,
|
|
406
|
+
root_devs: set[str],
|
|
407
|
+
luks_passphrase: str | None = None,
|
|
408
|
+
) -> Future[Tuple[bool, str]]:
|
|
409
|
+
return _executor.submit(mount_partition, part, base_dir, root_devs, luks_passphrase)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def umount_partition_async(
|
|
413
|
+
part: Partition,
|
|
414
|
+
root_devs: set[str],
|
|
415
|
+
lock_luks_after: bool = False,
|
|
416
|
+
) -> Future[Tuple[bool, str]]:
|
|
417
|
+
return _executor.submit(umount_partition, part, root_devs, lock_luks_after)
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
def unlock_luks_async(part: Partition, passphrase: str) -> Future[str]:
|
|
421
|
+
return _executor.submit(unlock_luks, part, passphrase)
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def lock_luks_async(part: Partition) -> Future[str]:
|
|
425
|
+
return _executor.submit(lock_luks, part)
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
def persistent_mount_map(fstab_path: Path = DEFAULT_FSTAB_PATH) -> dict[str, str]:
|
|
429
|
+
entries: dict[str, str] = {}
|
|
430
|
+
try:
|
|
431
|
+
lines = fstab_path.read_text(encoding="utf-8").splitlines()
|
|
432
|
+
except FileNotFoundError:
|
|
433
|
+
return entries
|
|
434
|
+
|
|
435
|
+
for line in lines:
|
|
436
|
+
stripped = line.strip()
|
|
437
|
+
if not stripped or stripped.startswith("#"):
|
|
438
|
+
continue
|
|
439
|
+
if DISKMAN_FSTAB_TAG not in stripped:
|
|
440
|
+
continue
|
|
441
|
+
|
|
442
|
+
fields = stripped.split()
|
|
443
|
+
if len(fields) < 2:
|
|
444
|
+
continue
|
|
445
|
+
spec = fields[0]
|
|
446
|
+
mountpoint = fields[1]
|
|
447
|
+
if spec.startswith("UUID="):
|
|
448
|
+
entries[spec.removeprefix("UUID=")] = mountpoint
|
|
449
|
+
|
|
450
|
+
return entries
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
def fstab_line_for_partition(part: Partition, base_dir: Path) -> str:
|
|
454
|
+
if not part.uuid:
|
|
455
|
+
raise CommandError(f"Missing UUID for {part.path}; cannot persist in fstab safely.")
|
|
456
|
+
if not is_mountable(part):
|
|
457
|
+
raise CommandError(f"Partition is not mountable: {part.path}")
|
|
458
|
+
if part.is_luks:
|
|
459
|
+
raise CommandError(f"Refusing to persist raw LUKS container in fstab: {part.path}")
|
|
460
|
+
mnt = target_mount_point(part, base_dir)
|
|
461
|
+
opts = _pick_mount_options(part, read_only=False)
|
|
462
|
+
return f"UUID={part.uuid} {mnt} {part.fstype} {opts} 0 2 # {DISKMAN_FSTAB_TAG} {part.path}"
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def enable_persistent_mount(
|
|
466
|
+
part: Partition, base_dir: Path, root_devs: set[str], fstab_path: Path = DEFAULT_FSTAB_PATH
|
|
467
|
+
) -> str:
|
|
468
|
+
if is_root_partition(part, root_devs):
|
|
469
|
+
raise CommandError(f"Refusing to persist root partition: {part.path}")
|
|
470
|
+
ensure_dir(target_mount_point(part, base_dir))
|
|
471
|
+
existing = persistent_mount_map(fstab_path)
|
|
472
|
+
if part.uuid in existing:
|
|
473
|
+
return f"Persistent mount already enabled for {part.path} -> {existing[part.uuid]}"
|
|
474
|
+
|
|
475
|
+
line = fstab_line_for_partition(part, base_dir)
|
|
476
|
+
with fstab_path.open("a", encoding="utf-8") as f:
|
|
477
|
+
f.write("\n" + line + "\n")
|
|
478
|
+
return f"Enabled reboot auto-mount for {part.path}"
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def disable_persistent_mount(part: Partition, fstab_path: Path = DEFAULT_FSTAB_PATH) -> str:
|
|
482
|
+
if not part.uuid:
|
|
483
|
+
raise CommandError(f"Missing UUID for {part.path}; cannot remove from fstab safely.")
|
|
484
|
+
try:
|
|
485
|
+
lines = fstab_path.read_text(encoding="utf-8").splitlines()
|
|
486
|
+
except FileNotFoundError:
|
|
487
|
+
raise CommandError(f"fstab not found: {fstab_path}")
|
|
488
|
+
|
|
489
|
+
kept: List[str] = []
|
|
490
|
+
removed = False
|
|
491
|
+
uuid_key = f"UUID={part.uuid}"
|
|
492
|
+
for line in lines:
|
|
493
|
+
stripped = line.strip()
|
|
494
|
+
if stripped and not stripped.startswith("#") and DISKMAN_FSTAB_TAG in stripped and uuid_key in stripped:
|
|
495
|
+
removed = True
|
|
496
|
+
continue
|
|
497
|
+
kept.append(line)
|
|
498
|
+
|
|
499
|
+
if not removed:
|
|
500
|
+
return f"Persistent mount was not enabled for {part.path}"
|
|
501
|
+
|
|
502
|
+
fstab_path.write_text("\n".join(kept).rstrip() + "\n", encoding="utf-8")
|
|
503
|
+
return f"Disabled reboot auto-mount for {part.path}"
|
diskman/tui.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import curses
|
|
4
|
+
from concurrent.futures import Future
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .core import (
|
|
8
|
+
automount_async,
|
|
9
|
+
collect_partitions,
|
|
10
|
+
disable_persistent_mount,
|
|
11
|
+
enable_persistent_mount,
|
|
12
|
+
is_mount_read_only,
|
|
13
|
+
is_luks_open,
|
|
14
|
+
is_mountable,
|
|
15
|
+
is_root_partition,
|
|
16
|
+
lock_luks_async,
|
|
17
|
+
mount_partition_async,
|
|
18
|
+
persistent_mount_map,
|
|
19
|
+
require_root,
|
|
20
|
+
root_sources,
|
|
21
|
+
smart_health,
|
|
22
|
+
unlock_luks_async,
|
|
23
|
+
umount_partition_async,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _prompt_hidden(stdscr, y: int, x: int, prompt: str) -> str:
|
|
28
|
+
curses.noecho()
|
|
29
|
+
stdscr.addstr(y, x, prompt)
|
|
30
|
+
stdscr.refresh()
|
|
31
|
+
buf = []
|
|
32
|
+
while True:
|
|
33
|
+
key = stdscr.getch()
|
|
34
|
+
if key in (10, 13):
|
|
35
|
+
break
|
|
36
|
+
if key in (curses.KEY_BACKSPACE, 127, 8):
|
|
37
|
+
if buf:
|
|
38
|
+
buf.pop()
|
|
39
|
+
stdscr.addstr(y, x + len(prompt) + len(buf), " ")
|
|
40
|
+
stdscr.move(y, x + len(prompt) + len(buf))
|
|
41
|
+
stdscr.refresh()
|
|
42
|
+
continue
|
|
43
|
+
if 32 <= key <= 126:
|
|
44
|
+
buf.append(chr(key))
|
|
45
|
+
stdscr.addstr(y, x + len(prompt) + len(buf) - 1, "*")
|
|
46
|
+
stdscr.refresh()
|
|
47
|
+
return "".join(buf)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def run_tui(base_dir: Path) -> None:
|
|
51
|
+
def tui(stdscr) -> None:
|
|
52
|
+
curses.curs_set(0)
|
|
53
|
+
stdscr.nodelay(True)
|
|
54
|
+
stdscr.keypad(True)
|
|
55
|
+
index = 0
|
|
56
|
+
nav = "r:refresh a:auto m:mount/unmount u:unlock l:lock p:boot q:quit"
|
|
57
|
+
status = "Ready"
|
|
58
|
+
parts = collect_partitions()
|
|
59
|
+
pending: Future | None = None
|
|
60
|
+
pending_action = ""
|
|
61
|
+
|
|
62
|
+
while True:
|
|
63
|
+
if pending and pending.done():
|
|
64
|
+
try:
|
|
65
|
+
result = pending.result()
|
|
66
|
+
if isinstance(result, tuple):
|
|
67
|
+
_, status = result
|
|
68
|
+
elif isinstance(result, list):
|
|
69
|
+
status = result[-1] if result else "No changes"
|
|
70
|
+
else:
|
|
71
|
+
status = str(result)
|
|
72
|
+
except Exception as exc:
|
|
73
|
+
status = f"Error: {exc}"
|
|
74
|
+
pending = None
|
|
75
|
+
pending_action = ""
|
|
76
|
+
parts = collect_partitions()
|
|
77
|
+
index = min(index, max(len(parts) - 1, 0))
|
|
78
|
+
|
|
79
|
+
roots = root_sources()
|
|
80
|
+
boot_map = persistent_mount_map()
|
|
81
|
+
h, w = stdscr.getmaxyx()
|
|
82
|
+
stdscr.erase()
|
|
83
|
+
stdscr.addnstr(0, 0, "diskman TUI", w - 1)
|
|
84
|
+
stdscr.addnstr(1, 0, nav, w - 1)
|
|
85
|
+
if pending:
|
|
86
|
+
stdscr.addnstr(2, 0, f"Result: {status} (running: {pending_action})", w - 1)
|
|
87
|
+
else:
|
|
88
|
+
stdscr.addnstr(2, 0, f"Result: {status}", w - 1)
|
|
89
|
+
stdscr.addnstr(3, 0, "-" * max(0, w - 1), w - 1)
|
|
90
|
+
|
|
91
|
+
for row, part in enumerate(parts[: max(0, h - 5)], start=4):
|
|
92
|
+
i = row - 4
|
|
93
|
+
if i >= len(parts):
|
|
94
|
+
break
|
|
95
|
+
marker = ">" if i == index else " "
|
|
96
|
+
flags = []
|
|
97
|
+
if is_root_partition(part, roots):
|
|
98
|
+
flags.append("ROOT")
|
|
99
|
+
if part.mounted:
|
|
100
|
+
flags.append("MOUNTED")
|
|
101
|
+
if is_mount_read_only(part):
|
|
102
|
+
flags.append("RO")
|
|
103
|
+
if not is_mountable(part):
|
|
104
|
+
flags.append("SKIP")
|
|
105
|
+
if part.is_luks:
|
|
106
|
+
flags.append("LUKS_OPEN" if is_luks_open(part) else "LUKS_LOCKED")
|
|
107
|
+
if part.uuid and part.uuid in boot_map:
|
|
108
|
+
flags.append("AUTOBOOT")
|
|
109
|
+
line = (
|
|
110
|
+
f"{marker} {part.path:<16} {part.disk_kind:<3} {part.fstype or '-':<10} {part.size or '-':<7} "
|
|
111
|
+
f"{smart_health(part):<8} {part.mountpoint or '-':<16} {','.join(flags) or '-'}"
|
|
112
|
+
)
|
|
113
|
+
stdscr.addnstr(row, 0, line, w - 1)
|
|
114
|
+
|
|
115
|
+
key = stdscr.getch()
|
|
116
|
+
if key == -1:
|
|
117
|
+
curses.napms(40)
|
|
118
|
+
continue
|
|
119
|
+
|
|
120
|
+
if key in (ord("q"), 27):
|
|
121
|
+
status = "Exiting..."
|
|
122
|
+
stdscr.addnstr(2, 0, f"Result: {status}", w - 1)
|
|
123
|
+
stdscr.refresh()
|
|
124
|
+
curses.napms(200)
|
|
125
|
+
break
|
|
126
|
+
|
|
127
|
+
if key in (curses.KEY_DOWN, ord("j")):
|
|
128
|
+
index = min(index + 1, max(len(parts) - 1, 0))
|
|
129
|
+
status = f"Selected {parts[index].path}" if parts else "Ready"
|
|
130
|
+
elif key in (curses.KEY_UP, ord("k")):
|
|
131
|
+
index = max(index - 1, 0)
|
|
132
|
+
status = f"Selected {parts[index].path}" if parts else "Ready"
|
|
133
|
+
elif key == ord("r"):
|
|
134
|
+
parts = collect_partitions()
|
|
135
|
+
status = "Refreshed"
|
|
136
|
+
index = min(index, max(len(parts) - 1, 0))
|
|
137
|
+
elif key == ord("a") and not pending:
|
|
138
|
+
try:
|
|
139
|
+
require_root()
|
|
140
|
+
pending = automount_async(base_dir=base_dir)
|
|
141
|
+
pending_action = "automount"
|
|
142
|
+
status = "Automount started"
|
|
143
|
+
except Exception as exc:
|
|
144
|
+
status = f"Error: {exc}"
|
|
145
|
+
elif key == ord("m") and parts and not pending:
|
|
146
|
+
part = parts[index]
|
|
147
|
+
try:
|
|
148
|
+
require_root()
|
|
149
|
+
if part.mounted:
|
|
150
|
+
pending = umount_partition_async(part, roots)
|
|
151
|
+
pending_action = f"unmount {part.path}"
|
|
152
|
+
else:
|
|
153
|
+
passphrase = None
|
|
154
|
+
if part.is_luks and not is_luks_open(part):
|
|
155
|
+
status = "Enter LUKS passphrase"
|
|
156
|
+
stdscr.addnstr(2, 0, f"Result: {status}", w - 1)
|
|
157
|
+
stdscr.refresh()
|
|
158
|
+
passphrase = _prompt_hidden(stdscr, min(h - 1, 3), 0, "LUKS passphrase: ")
|
|
159
|
+
pending = mount_partition_async(part, base_dir, roots, passphrase)
|
|
160
|
+
pending_action = f"mount {part.path}"
|
|
161
|
+
status = f"Started {pending_action}"
|
|
162
|
+
except Exception as exc:
|
|
163
|
+
status = f"Error: {exc}"
|
|
164
|
+
parts = collect_partitions()
|
|
165
|
+
elif key == ord("u") and parts and not pending:
|
|
166
|
+
part = parts[index]
|
|
167
|
+
try:
|
|
168
|
+
require_root()
|
|
169
|
+
if not part.is_luks:
|
|
170
|
+
status = f"Not LUKS: {part.path}"
|
|
171
|
+
elif is_luks_open(part):
|
|
172
|
+
status = f"Already unlocked: {part.path}"
|
|
173
|
+
else:
|
|
174
|
+
status = "Enter LUKS passphrase"
|
|
175
|
+
stdscr.addnstr(2, 0, f"Result: {status}", w - 1)
|
|
176
|
+
stdscr.refresh()
|
|
177
|
+
passphrase = _prompt_hidden(stdscr, min(h - 1, 3), 0, "LUKS passphrase: ")
|
|
178
|
+
pending = unlock_luks_async(part, passphrase)
|
|
179
|
+
pending_action = f"unlock {part.path}"
|
|
180
|
+
status = f"Started {pending_action}"
|
|
181
|
+
except Exception as exc:
|
|
182
|
+
status = f"Error: {exc}"
|
|
183
|
+
elif key == ord("l") and parts and not pending:
|
|
184
|
+
part = parts[index]
|
|
185
|
+
try:
|
|
186
|
+
require_root()
|
|
187
|
+
if not part.is_luks:
|
|
188
|
+
status = f"Not LUKS: {part.path}"
|
|
189
|
+
elif not is_luks_open(part):
|
|
190
|
+
status = f"Already locked: {part.path}"
|
|
191
|
+
else:
|
|
192
|
+
pending = lock_luks_async(part)
|
|
193
|
+
pending_action = f"lock {part.path}"
|
|
194
|
+
status = f"Started {pending_action}"
|
|
195
|
+
except Exception as exc:
|
|
196
|
+
status = f"Error: {exc}"
|
|
197
|
+
elif key == ord("p") and parts and not pending:
|
|
198
|
+
part = parts[index]
|
|
199
|
+
try:
|
|
200
|
+
require_root()
|
|
201
|
+
if not part.uuid:
|
|
202
|
+
status = f"Error: missing UUID for {part.path}"
|
|
203
|
+
elif part.uuid in boot_map:
|
|
204
|
+
status = disable_persistent_mount(part)
|
|
205
|
+
else:
|
|
206
|
+
status = enable_persistent_mount(part, base_dir, roots)
|
|
207
|
+
except Exception as exc:
|
|
208
|
+
status = f"Error: {exc}"
|
|
209
|
+
parts = collect_partitions()
|
|
210
|
+
|
|
211
|
+
curses.wrapper(tui)
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: diskman
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: CLI + TUI disk manager with SMART, LUKS, and non-blocking automount
|
|
5
|
+
Home-page: https://github.com/gaffer/disk-manager
|
|
6
|
+
Author: Gaffer
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/gaffer/disk-manager
|
|
9
|
+
Project-URL: Repository, https://github.com/gaffer/disk-manager
|
|
10
|
+
Project-URL: Issues, https://github.com/gaffer/disk-manager/issues
|
|
11
|
+
Keywords: linux,disk,partition,mount,luks,smart,tui,cli
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: System Administrators
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Topic :: System :: Systems Administration
|
|
23
|
+
Classifier: Topic :: Utilities
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: rich>=13.7.0
|
|
28
|
+
Provides-Extra: binary
|
|
29
|
+
Requires-Dist: pyinstaller>=6.0; extra == "binary"
|
|
30
|
+
Provides-Extra: release
|
|
31
|
+
Requires-Dist: build>=1.2.0; extra == "release"
|
|
32
|
+
Requires-Dist: twine>=5.0.0; extra == "release"
|
|
33
|
+
Dynamic: home-page
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
Dynamic: requires-python
|
|
36
|
+
|
|
37
|
+
# diskman
|
|
38
|
+
|
|
39
|
+
[](https://pypi.org/project/diskman/)
|
|
40
|
+
[](https://opensource.org/licenses/MIT)
|
|
41
|
+
[](https://www.python.org/downloads/)
|
|
42
|
+
|
|
43
|
+
**diskman** is a lightweight, terminal-first Linux disk and partition manager. It provides a powerful CLI and an interactive TUI for day-to-day storage administration, featuring SMART health monitoring, LUKS encryption support, and filesystem-aware automounting.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 🚀 Key Features
|
|
48
|
+
|
|
49
|
+
- **Dual Interface:** Full-featured Command Line Interface (CLI) and an interactive Terminal User Interface (TUI).
|
|
50
|
+
- **Smart Automount:** Filesystem-aware mount options (e.g., `discard` for SSDs, UID/GID mapping for FAT/NTFS).
|
|
51
|
+
- **LUKS Support:** Auto-detection, unlocking, and locking of encrypted partitions.
|
|
52
|
+
- **Health Monitoring:** Real-time SMART health status visibility (via `smartmontools`).
|
|
53
|
+
- **Safety First:** Automatically excludes root partitions from destructive operations and provides a **read-only fallback** if a writable mount fails.
|
|
54
|
+
- **Non-Blocking Ops:** Asynchronous mount, unmount, and LUKS operations to keep the TUI responsive.
|
|
55
|
+
- **Persistence:** Easily toggle reboot-persistent mounts via tagged `/etc/fstab` entries.
|
|
56
|
+
- **System Integration:** Ready-to-use `systemd` service and timer for periodic automounting.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## 📦 Installation
|
|
61
|
+
|
|
62
|
+
### From PyPI (Recommended)
|
|
63
|
+
|
|
64
|
+
Install the latest stable version directly from PyPI:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
python3 -m pip install --upgrade diskman
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### From Source
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
git clone https://github.com/SoyalIslam/disk-manager.git
|
|
74
|
+
cd disk-manager
|
|
75
|
+
python3 -m pip install .
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Requirements
|
|
79
|
+
|
|
80
|
+
- **OS:** Linux
|
|
81
|
+
- **Python:** 3.9+
|
|
82
|
+
- **System Tools:** `util-linux` (`lsblk`, `findmnt`, `mount`, `umount`)
|
|
83
|
+
- **Optional Tools:**
|
|
84
|
+
- `cryptsetup` (for LUKS support)
|
|
85
|
+
- `smartmontools` (for SMART health monitoring)
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## 🛠 Usage
|
|
90
|
+
|
|
91
|
+
### CLI Reference
|
|
92
|
+
|
|
93
|
+
| Command | Description |
|
|
94
|
+
| :--- | :--- |
|
|
95
|
+
| `diskman list` | List all partitions, filesystems, and mount status. |
|
|
96
|
+
| `diskman tui` | Launch the interactive Terminal User Interface. |
|
|
97
|
+
| `sudo diskman automount` | Auto-mount all available partitions (excluding root). |
|
|
98
|
+
| `sudo diskman mount /dev/sdb1` | Mount a specific device (prompts for LUKS if needed). |
|
|
99
|
+
| `sudo diskman umount /dev/sdb1` | Unmount a specific device. |
|
|
100
|
+
| `sudo diskman luks-unlock /dev/sdb2` | Unlock a LUKS encrypted partition. |
|
|
101
|
+
| `sudo diskman boot-add /dev/sdb1` | Enable reboot-persistent mount in `/etc/fstab`. |
|
|
102
|
+
|
|
103
|
+
*Note: Operations that modify system state (mount/unmount/LUKS/fstab) require `sudo`.*
|
|
104
|
+
|
|
105
|
+
### TUI Controls
|
|
106
|
+
|
|
107
|
+
Launch with `diskman tui` (or `sudo diskman tui` for full functionality):
|
|
108
|
+
|
|
109
|
+
- **`j` / `k`** or **Arrow Keys**: Navigate device list.
|
|
110
|
+
- **`m`**: Mount or unmount the selected partition.
|
|
111
|
+
- **`u` / `l`**: Unlock or lock a LUKS partition.
|
|
112
|
+
- **`a`**: Trigger a background automount of all devices.
|
|
113
|
+
- **`p`**: Toggle persistence (`/etc/fstab`) for the selected device.
|
|
114
|
+
- **`r`**: Refresh the device list.
|
|
115
|
+
- **`q`**: Exit.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## ⚙️ System Integration
|
|
120
|
+
|
|
121
|
+
### Periodic Automount (systemd)
|
|
122
|
+
|
|
123
|
+
You can automate mounting of external drives using the provided systemd units:
|
|
124
|
+
|
|
125
|
+
1. **Install Units:**
|
|
126
|
+
```bash
|
|
127
|
+
sudo install -Dm644 extras/systemd/diskman-automount.service /etc/systemd/system/
|
|
128
|
+
sudo install -Dm644 extras/systemd/diskman-automount.timer /etc/systemd/system/
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
2. **Enable Timer:**
|
|
132
|
+
```bash
|
|
133
|
+
sudo systemctl daemon-reload
|
|
134
|
+
sudo systemctl enable --now diskman-automount.timer
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## 🏗 Development
|
|
140
|
+
|
|
141
|
+
### Setup Environment
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
python3 -m venv .venv
|
|
145
|
+
source .venv/bin/activate
|
|
146
|
+
pip install -r requirements-dev.txt
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Build & Package
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
# Build distribution archives
|
|
153
|
+
python3 -m build
|
|
154
|
+
|
|
155
|
+
# Build standalone binary (via PyInstaller)
|
|
156
|
+
./scripts/build_binary.sh
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### GitHub Actions: Publish to PyPI
|
|
160
|
+
|
|
161
|
+
This repo includes:
|
|
162
|
+
|
|
163
|
+
- `.github/workflows/publish-pypi.yml`
|
|
164
|
+
|
|
165
|
+
Set these repository secrets in GitHub:
|
|
166
|
+
|
|
167
|
+
- `PYPI_API_TOKEN`
|
|
168
|
+
- `TEST_PYPI_API_TOKEN`
|
|
169
|
+
|
|
170
|
+
Workflow usage:
|
|
171
|
+
|
|
172
|
+
1. Manual publish to TestPyPI:
|
|
173
|
+
- Actions -> `Publish Python Package` -> `Run workflow` -> target `testpypi`
|
|
174
|
+
2. Manual publish to PyPI:
|
|
175
|
+
- Actions -> `Publish Python Package` -> `Run workflow` -> target `pypi`
|
|
176
|
+
3. Auto publish to PyPI on tag:
|
|
177
|
+
- Push tag `vX.Y.Z` matching `pyproject.toml` version
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
git tag v0.2.1
|
|
181
|
+
git push origin v0.2.1
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## 📄 License
|
|
187
|
+
|
|
188
|
+
This project is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for details.
|
|
189
|
+
|
|
190
|
+
## 🤝 Contributing
|
|
191
|
+
|
|
192
|
+
Contributions, issues, and feature requests are welcome! Feel free to check the [issues page](https://github.com/SoyalIslam/disk-manager/issues).
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
diskman/__init__.py,sha256=xJ2et1pA3q9-MEkIP1Cb8w1_Kgsp7XUCzIvDzn_Vup4,48
|
|
2
|
+
diskman/cli.py,sha256=BeYhRRSDzrY82kr39ojm4IF34JvUfRFoxarBqa0hecU,8734
|
|
3
|
+
diskman/core.py,sha256=LTCp_yvaTkAvMCvNdk7cO-G1bCDLZMWLB8DqlhitH-s,16379
|
|
4
|
+
diskman/tui.py,sha256=OSh1FTdRYsS58jRlvSTzUBsaLwTC-2Hqmib36Fipjhw,8440
|
|
5
|
+
diskman-0.2.1.dist-info/licenses/LICENSE,sha256=Ys5fmaQsRsB-8cJfy4e_CloHBDocl1gMixnZHOTpUfc,1063
|
|
6
|
+
diskman-0.2.1.dist-info/METADATA,sha256=irlYbb8CAqPcJVWr4y12QXX9fdE9drKjYZtC86coY3Y,6167
|
|
7
|
+
diskman-0.2.1.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
|
|
8
|
+
diskman-0.2.1.dist-info/entry_points.txt,sha256=VyD8ju6ss5UjWEKwVYWVX_IxBAlmqP9kIN2GcoKf5ls,45
|
|
9
|
+
diskman-0.2.1.dist-info/top_level.txt,sha256=lbH0DpopZMDL5RBuoRLl1oO-_ZS-7UXIh16XoTpKyzQ,8
|
|
10
|
+
diskman-0.2.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gaffer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
diskman
|