common helpers

This commit is contained in:
Maxim Devaev 2022-04-05 20:51:53 +03:00
parent fa3aeb79ae
commit 5be17cb756
6 changed files with 21 additions and 30 deletions

View File

@ -1,20 +0,0 @@
# ========================================================================== #
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
# ========================================================================== #

View File

@ -27,6 +27,8 @@ import shutil
import dataclasses import dataclasses
import subprocess import subprocess
from typing import List
# ==== # ====
_MOUNT_PATH = "/bin/mount" _MOUNT_PATH = "/bin/mount"
@ -46,21 +48,22 @@ def _log(msg: str) -> None:
print(msg, file=sys.stderr) print(msg, file=sys.stderr)
def _find_storage() -> _Storage: def _find_storage(target: str) -> _Storage:
assert target
with open(_FSTAB_PATH) as fstab_file: with open(_FSTAB_PATH) as fstab_file:
for line in fstab_file.read().split("\n"): for line in fstab_file.read().split("\n"):
line = line.strip() line = line.strip()
if line and not line.startswith("#"): if line and not line.startswith("#"):
parts = line.split() parts = line.split()
if len(parts) == 6: if len(parts) == 6:
options = dict(re.findall(r"X-kvmd\.otgmsd-(root|user)=([^,]+)", parts[3])) options = dict(re.findall(r"X-kvmd\.%s-(root|user)=([^,]+)" % (target), parts[3]))
if options: if options:
return _Storage( return _Storage(
mount_path=parts[1], mount_path=parts[1],
root_path=options.get("root", ""), root_path=options.get("root", ""),
user=options.get("user", ""), user=options.get("user", ""),
) )
raise RuntimeError(f"Can't find MSD mountpoint in {_FSTAB_PATH}") raise SystemExit(f"Can't find {target!r} mountpoint in {_FSTAB_PATH}")
def _remount(path: str, rw: bool) -> None: def _remount(path: str, rw: bool) -> None:
@ -94,13 +97,22 @@ def main() -> None:
if len(sys.argv) != 2 or sys.argv[1] not in ["ro", "rw"]: if len(sys.argv) != 2 or sys.argv[1] not in ["ro", "rw"]:
raise SystemExit(f"Usage: {sys.argv[0]} [ro|rw]") raise SystemExit(f"Usage: {sys.argv[0]} [ro|rw]")
target = ""
dirs: List[str] = []
app = os.path.basename(sys.argv[0])
if app == "kvmd-helper-otgmsd-remount":
target = "otgmsd"
dirs = ["images", "meta"]
else:
raise SystemExit("Unknown application target")
rw = (sys.argv[1] == "rw") rw = (sys.argv[1] == "rw")
storage = _find_storage() storage = _find_storage(target)
_remount(storage.mount_path, rw) _remount(storage.mount_path, rw)
if rw: if rw:
if storage.root_path: if storage.root_path:
for name in ["images", "meta"]: for name in dirs:
path = os.path.join(storage.root_path, name) path = os.path.join(storage.root_path, name)
_mkdir(path) _mkdir(path)
if storage.user: if storage.user:

View File

@ -108,9 +108,8 @@ def main() -> None:
"kvmd.apps.janus", "kvmd.apps.janus",
"kvmd.apps.watchdog", "kvmd.apps.watchdog",
"kvmd.helpers", "kvmd.helpers",
"kvmd.helpers.otgmsd", "kvmd.helpers.unlock",
"kvmd.helpers.otgmsd.unlock", "kvmd.helpers.remount",
"kvmd.helpers.otgmsd.remount",
], ],
package_data={ package_data={
@ -130,8 +129,8 @@ def main() -> None:
"kvmd-vnc = kvmd.apps.vnc:main", "kvmd-vnc = kvmd.apps.vnc:main",
"kvmd-janus = kvmd.apps.janus:main", "kvmd-janus = kvmd.apps.janus:main",
"kvmd-watchdog = kvmd.apps.watchdog:main", "kvmd-watchdog = kvmd.apps.watchdog:main",
"kvmd-helper-otgmsd-unlock = kvmd.helpers.otgmsd.unlock:main", "kvmd-helper-otgmsd-unlock = kvmd.helpers.unlock:main",
"kvmd-helper-otgmsd-remount = kvmd.helpers.otgmsd.remount:main", "kvmd-helper-otgmsd-remount = kvmd.helpers.remount:main",
], ],
}, },