summaryrefslogtreecommitdiff
path: root/kvmd/helpers
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2022-11-07 03:23:06 +0300
committerMaxim Devaev <[email protected]>2022-11-07 03:23:06 +0300
commit53f8b052de48843ece433362ba66633c8b16b7f6 (patch)
tree1d3ced9fd2f44bbb91c1604478eacd7bace1e6f5 /kvmd/helpers
parent648316931a8c5204945a5100a5530c15f7515a23 (diff)
common fstab and libc funcs
Diffstat (limited to 'kvmd/helpers')
-rw-r--r--kvmd/helpers/remount/__init__.py38
-rw-r--r--kvmd/helpers/swapfiles/__init__.py16
2 files changed, 6 insertions, 48 deletions
diff --git a/kvmd/helpers/remount/__init__.py b/kvmd/helpers/remount/__init__.py
index 80e683f5..acbbadd9 100644
--- a/kvmd/helpers/remount/__init__.py
+++ b/kvmd/helpers/remount/__init__.py
@@ -22,23 +22,10 @@
import sys
import os
-import re
import shutil
-import dataclasses
import subprocess
-
-# ====
-_MOUNT_PATH = "/bin/mount"
-_FSTAB_PATH = "/etc/fstab"
-
-
-# =====
[email protected](frozen=True)
-class _Storage:
- mount_path: str
- root_path: str
- user: str
+from ... import fstab
# =====
@@ -46,29 +33,11 @@ def _log(msg: str) -> None:
print(msg, file=sys.stderr)
-def _find_storage(target: str) -> _Storage:
- assert target
- with open(_FSTAB_PATH) as fstab_file:
- for line in fstab_file.read().split("\n"):
- line = line.strip()
- 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]))
- if options:
- return _Storage(
- mount_path=parts[1],
- root_path=(options.get("root", "") or parts[1]),
- user=options.get("user", ""),
- )
- raise SystemExit(f"Can't find {target!r} mountpoint in {_FSTAB_PATH}")
-
-
def _remount(path: str, rw: bool) -> None:
mode = ("rw" if rw else "ro")
_log(f"Remounting {path} to {mode.upper()}-mode ...")
try:
- subprocess.check_call([_MOUNT_PATH, "--options", f"remount,{mode}", path])
+ subprocess.check_call(["/bin/mount", "--options", f"remount,{mode}", path])
except subprocess.CalledProcessError as err:
raise SystemExit(f"Can't remount: {err}")
@@ -109,7 +78,8 @@ def main() -> None:
rw = (sys.argv[1] == "rw")
- storage = _find_storage(target)
+ assert target
+ storage = fstab.find_storage(target)
_remount(storage.mount_path, rw)
if rw and storage.root_path:
for name in dirs:
diff --git a/kvmd/helpers/swapfiles/__init__.py b/kvmd/helpers/swapfiles/__init__.py
index 3675d709..c0e271c4 100644
--- a/kvmd/helpers/swapfiles/__init__.py
+++ b/kvmd/helpers/swapfiles/__init__.py
@@ -22,12 +22,8 @@
import sys
import os
-import ctypes
-import ctypes.util
-from ctypes import c_int
-from ctypes import c_uint
-from ctypes import c_char_p
+from ... import libc
# =====
@@ -35,14 +31,6 @@ def main() -> None:
if len(sys.argv) != 3:
raise SystemExit(f"Usage: {sys.argv[0]} <file1> <file2>")
- path = ctypes.util.find_library("c")
- if not path:
- raise SystemExit("Where is libc?")
- assert path
- libc = ctypes.CDLL(path)
- libc.renameat2.restype = c_int
- libc.renameat2.argtypes = [c_int, c_char_p, c_int, c_char_p, c_uint]
-
result = libc.renameat2(
-100, # AT_FDCWD
os.fsencode(sys.argv[1]),
@@ -52,4 +40,4 @@ def main() -> None:
)
if result != 0:
- raise SystemExit(f"{sys.argv[0]}: {os.strerror(ctypes.get_errno())}")
+ raise SystemExit(f"{sys.argv[0]}: {os.strerror(libc.get_errno())}")