diff options
author | Maxim Devaev <[email protected]> | 2022-11-07 13:56:39 +0300 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2022-11-07 13:56:39 +0300 |
commit | 941b3bbd75b298d69b40a838dd149833594394ae (patch) | |
tree | fef9a8f55648d0f0f322730b623cdaa0e47bcac4 | |
parent | 317a127f58ef41f4b9fd31af76e742db3179703d (diff) |
refactoring
-rw-r--r-- | kvmd/fstab.py | 20 | ||||
-rw-r--r-- | kvmd/helpers/remount/__init__.py | 19 | ||||
-rw-r--r-- | kvmd/plugins/msd/otg/__init__.py | 2 |
3 files changed, 25 insertions, 16 deletions
diff --git a/kvmd/fstab.py b/kvmd/fstab.py index b8c26e68..0dfb8d52 100644 --- a/kvmd/fstab.py +++ b/kvmd/fstab.py @@ -25,14 +25,24 @@ import dataclasses # ===== +class PartitionType: + MSD = "otgmsd" + PST = "pst" + ALL = ( + MSD, + PST, + ) + + @dataclasses.dataclass(frozen=True) -class FstabStorage: +class Partition: mount_path: str root_path: str user: str -def find_storage(target: str) -> FstabStorage: +def find_partition(part_type: str) -> Partition: + assert part_type in PartitionType.ALL fstab_path = "/etc/fstab" with open(fstab_path) as file: for line in file.read().split("\n"): @@ -40,11 +50,11 @@ def find_storage(target: str) -> FstabStorage: if line and not line.startswith("#"): parts = line.split() if len(parts) == 6: - options = dict(re.findall(r"X-kvmd\.%s-(root|user)(?:=([^,]+))?" % (target), parts[3])) + options = dict(re.findall(r"X-kvmd\.%s-(root|user)(?:=([^,]+))?" % (part_type), parts[3])) if options: - return FstabStorage( + return Partition( mount_path=parts[1], root_path=(options.get("root", "") or parts[1]), user=options.get("user", ""), ) - raise RuntimeError(f"Can't find {target!r} mountpoint in {fstab_path}") + raise RuntimeError(f"Can't find {part_type!r} mountpoint in {fstab_path}") diff --git a/kvmd/helpers/remount/__init__.py b/kvmd/helpers/remount/__init__.py index acbbadd9..aeedfa26 100644 --- a/kvmd/helpers/remount/__init__.py +++ b/kvmd/helpers/remount/__init__.py @@ -64,27 +64,26 @@ def main() -> None: if len(sys.argv) != 2 or sys.argv[1] not in ["ro", "rw"]: raise SystemExit(f"Usage: {sys.argv[0]} [ro|rw]") - target = "" + part_type = "" dirs: list[str] = [] app = os.path.basename(sys.argv[0]) if app == "kvmd-helper-otgmsd-remount": - target = "otgmsd" + part_type = fstab.PartitionType.MSD dirs = ["images", "meta"] elif app == "kvmd-helper-pst-remount": - target = "pst" + part_type = fstab.PartitionType.PST dirs = ["data"] else: raise SystemExit("Unknown application target") rw = (sys.argv[1] == "rw") - assert target - storage = fstab.find_storage(target) - _remount(storage.mount_path, rw) - if rw and storage.root_path: + part = fstab.find_partition(part_type) + _remount(part.mount_path, rw) + if rw and part.root_path: for name in dirs: - path = os.path.join(storage.root_path, name) + path = os.path.join(part.root_path, name) _mkdir(path) - if storage.user: - _chown(path, storage.user) + if part.user: + _chown(path, part.user) _log(f"Storage in the {'RW' if rw else 'RO'}-mode now") diff --git a/kvmd/plugins/msd/otg/__init__.py b/kvmd/plugins/msd/otg/__init__.py index fa6c1fe9..06615ac9 100644 --- a/kvmd/plugins/msd/otg/__init__.py +++ b/kvmd/plugins/msd/otg/__init__.py @@ -160,7 +160,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes self.__initial_cdrom: bool = initial["cdrom"] self.__drive = Drive(gadget, instance=0, lun=0) - self.__storage = Storage(fstab.find_storage("otgmsd").root_path) + self.__storage = Storage(fstab.find_partition(fstab.PartitionType.MSD).root_path) self.__reader: (MsdFileReader | None) = None self.__writer: (MsdFileWriter | None) = None |