diff options
-rw-r--r-- | kvmd/apps/__init__.py | 38 | ||||
-rw-r--r-- | kvmd/apps/kvmd/__init__.py | 11 | ||||
-rw-r--r-- | kvmd/apps/otg/__init__.py | 45 | ||||
-rw-r--r-- | kvmd/helpers/otgmsd/remount/__init__.py | 4 |
4 files changed, 73 insertions, 25 deletions
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py index bf2b6270..04c1b621 100644 --- a/kvmd/apps/__init__.py +++ b/kvmd/apps/__init__.py @@ -54,6 +54,7 @@ from ..validators.basic import valid_number from ..validators.basic import valid_float_f0 from ..validators.basic import valid_float_f01 +from ..validators.auth import valid_user from ..validators.auth import valid_users_list from ..validators.os import valid_abs_path @@ -249,14 +250,26 @@ def _get_config_scheme() -> Dict: }, "otg": { - "gadget": Option("kvmd", type=valid_otg_gadget), - "vendor_id": Option(0x1D6B, type=valid_otg_id), # Linux Foundation - "product_id": Option(0x0104, type=valid_otg_id), # Multifunction Composite Gadget - "manufacturer": Option("Pi-KVM"), - "product": Option("Composite KVM Device"), - "serial_number": Option("CAFEBABE"), - "udc": Option(""), - "init_delay": Option(3.0, type=valid_float_f01), + "vendor_id": Option(0x1D6B, type=valid_otg_id), # Linux Foundation + "product_id": Option(0x0104, type=valid_otg_id), # Multifunction Composite Gadget + "manufacturer": Option("Pi-KVM"), + "product": Option("Composite KVM Device"), + "serial": Option("CAFEBABE"), + + "gadget": Option("kvmd", type=valid_otg_gadget), + "udc": Option("", type=(lambda arg: str(arg).strip())), + "init_delay": Option(3.0, type=valid_float_f01), + + "msd": { + "user": Option("kvmd", type=valid_user), + "default": { + "stall": Option(False, type=valid_bool), + "cdrom": Option(True, type=valid_bool), + "rw": Option(False, type=valid_bool), + "removable": Option(True, type=valid_bool), + "fua": Option(True, type=valid_bool), + }, + }, "acm": { "enabled": Option(True, type=valid_bool), @@ -264,7 +277,14 @@ def _get_config_scheme() -> Dict: "drives": { "enabled": Option(False, type=valid_bool), - "count": Option(1, type=(lambda arg: valid_number(arg, min=1))), + "count": Option(1, type=(lambda arg: valid_number(arg, min=1))), + "default": { + "stall": Option(False, type=valid_bool), + "cdrom": Option(False, type=valid_bool), + "rw": Option(True, type=valid_bool), + "removable": Option(True, type=valid_bool), + "fua": Option(True, type=valid_bool), + }, }, }, diff --git a/kvmd/apps/kvmd/__init__.py b/kvmd/apps/kvmd/__init__.py index d2c662a0..05e17dd4 100644 --- a/kvmd/apps/kvmd/__init__.py +++ b/kvmd/apps/kvmd/__init__.py @@ -50,10 +50,17 @@ def main(argv: Optional[List[str]]=None) -> None: load_hid=True, load_atx=True, load_msd=True, - )[2].kvmd + )[2] with gpio.bcm(): # pylint: disable=protected-access + + msd_kwargs = config.kvmd.msd._unpack(ignore=["type"]) + if config.kvmd.msd.type == "otg": + msd_kwargs["gadget"] = config.otg.gadget # XXX: Small crutch to pass gadget name to plugin + + config = config.kvmd + Server( auth_manager=AuthManager( internal_type=config.auth.internal.type, @@ -67,7 +74,7 @@ def main(argv: Optional[List[str]]=None) -> None: hid=get_hid_class(config.hid.type)(**config.hid._unpack(ignore=["type"])), atx=get_atx_class(config.atx.type)(**config.atx._unpack(ignore=["type"])), - msd=get_msd_class(config.msd.type)(**config.msd._unpack(ignore=["type"])), + msd=get_msd_class(config.msd.type)(**msd_kwargs), streamer=Streamer(**config.streamer._unpack()), ).run(**config.server._unpack()) diff --git a/kvmd/apps/otg/__init__.py b/kvmd/apps/otg/__init__.py index 277751bd..8f5444bc 100644 --- a/kvmd/apps/otg/__init__.py +++ b/kvmd/apps/otg/__init__.py @@ -22,6 +22,7 @@ import os import re +import shutil import time import argparse @@ -49,6 +50,11 @@ def _mkdir(path: str) -> None: os.mkdir(path) +def _chown(path: str, user: str) -> None: + get_logger().info("CHOWN --- %s - %s", user, path) + shutil.chown(path, user) + + def _symlink(src: str, dest: str) -> None: get_logger().info("SYMLINK - %s --> %s", dest, src) os.symlink(src, dest) @@ -104,7 +110,7 @@ def _create_acm(gadget_path: str, config_path: str) -> None: _symlink(func_path, join(config_path, "acm.usb0")) -def _create_hid(gadget_path: str, config_path: str, hid: Hid, instance: int) -> None: +def _create_hid(gadget_path: str, config_path: str, instance: int, hid: Hid) -> None: func_path = join(gadget_path, f"functions/hid.usb{instance}") _mkdir(func_path) _write(join(func_path, "protocol"), str(hid.protocol)) @@ -114,14 +120,29 @@ def _create_hid(gadget_path: str, config_path: str, hid: Hid, instance: int) -> _symlink(func_path, join(config_path, f"hid.usb{instance}")) -def _create_msd(gadget_path: str, config_path: str, instance: int, cdrom: bool, rw: bool) -> None: +def _create_msd( + gadget_path: str, + config_path: str, + instance: int, + user: str, + stall: bool, + cdrom: bool, + rw: bool, + removable: bool, + fua: bool, +) -> None: + func_path = join(gadget_path, f"functions/mass_storage.usb{instance}") _mkdir(func_path) - _write(join(func_path, "stall"), "0") - _write(join(func_path, "lun.0/cdrom"), ("1" if cdrom else "0")) - _write(join(func_path, "lun.0/ro"), ("0" if rw else "1")) - _write(join(func_path, "lun.0/removable"), "1") - _write(join(func_path, "lun.0/nofua"), "0") + _write(join(func_path, "stall"), str(int(stall))) + _write(join(func_path, "lun.0/cdrom"), str(int(cdrom))) + _write(join(func_path, "lun.0/ro"), str(int(not rw))) + _write(join(func_path, "lun.0/removable"), str(int(removable))) + _write(join(func_path, "lun.0/nofua"), str(int(not fua))) + if user != "root": + _chown(join(func_path, "lun.0/cdrom"), user) + _chown(join(func_path, "lun.0/ro"), user) + _chown(join(func_path, "lun.0/file"), user) _symlink(func_path, join(config_path, f"mass_storage.usb{instance}")) @@ -148,7 +169,7 @@ def _cmd_start(config: Section) -> None: _mkdir(lang_path) _write(join(lang_path, "manufacturer"), config.otg.manufacturer) _write(join(lang_path, "product"), config.otg.product) - _write(join(lang_path, "serialnumber"), config.otg.serial_number) + _write(join(lang_path, "serialnumber"), config.otg.serial) config_path = join(gadget_path, "configs/c.1") _mkdir(config_path) @@ -162,16 +183,16 @@ def _cmd_start(config: Section) -> None: if config.kvmd.hid.type == "otg": logger.info("Required HID") - _create_hid(gadget_path, config_path, KEYBOARD_HID, 0) - _create_hid(gadget_path, config_path, MOUSE_HID, 1) + _create_hid(gadget_path, config_path, 0, KEYBOARD_HID) + _create_hid(gadget_path, config_path, 1, MOUSE_HID) if config.kvmd.msd.type == "otg": logger.info("Required MSD") - _create_msd(gadget_path, config_path, 0, cdrom=True, rw=False) + _create_msd(gadget_path, config_path, 0, config.otg.msd.user, **config.otg.msd.default._unpack()) # pylint: disable=protected-access if config.otg.drives.enabled: logger.info("Required MSD extra drives: %d", config.otg.drives.count) for instance in range(config.otg.drives.count): - _create_msd(gadget_path, config_path, instance + 1, cdrom=False, rw=True) + _create_msd(gadget_path, config_path, instance + 1, "root", **config.otg.drives.default._unpack()) # pylint: disable=protected-access logger.info("Enabling the gadget ...") _write(join(gadget_path, "UDC"), udc) diff --git a/kvmd/helpers/otgmsd/remount/__init__.py b/kvmd/helpers/otgmsd/remount/__init__.py index 667e770e..4a83980e 100644 --- a/kvmd/helpers/otgmsd/remount/__init__.py +++ b/kvmd/helpers/otgmsd/remount/__init__.py @@ -74,7 +74,7 @@ def _remount(path: str, rw: bool) -> None: def _mkdir(path: str) -> None: if not os.path.exists(path): - _log(f"MKDIR {path} ...") + _log(f"MKDIR --- {path}") try: os.mkdir(path) except Exception as err: @@ -82,7 +82,7 @@ def _mkdir(path: str) -> None: def _chown(path: str, user: str) -> None: - _log(f"CHOWN {user} {path} ...") + _log(f"CHOWN --- {user} - {path}") try: shutil.chown(path, user) except Exception as err: |