import ctypes import ctypes.util import os from pathlib import Path libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True) libc.mount.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong, ctypes.c_char_p) libc.umount2.argtypes = (ctypes.c_char_p, ctypes.c_ulong) def mount(source: Path | str, target: Path | str, format_: str, flags: int = 0, data: str = ""): result = libc.mount( ctypes.c_char_p(str(source).encode('utf-8')), ctypes.c_char_p(str(target).encode('utf-8')), ctypes.c_char_p(str(format_).encode('utf-8')), flags, ctypes.c_char_p(data.encode('utf-8')) if data is not None else None ) if result != 0: raise OSError(f"Could not perform mount: {os.strerror(ctypes.get_errno())}") def unmount(target: Path | str, flags: int = 0): return libc.umount2( ctypes.c_char_p(str(target).encode('utf-8')), flags )