-
Notifications
You must be signed in to change notification settings - Fork 1
/
3.291msd.patch
324 lines (298 loc) · 13.7 KB
/
3.291msd.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
diff -ruN kvmd/aiohelpers.py kvmd/aiohelpers.py
--- kvmd/aiohelpers.py 2024-04-29 03:07:25.000000000 +0800
+++ kvmd/aiohelpers.py 2024-04-29 03:16:05.629834800 +0800
@@ -38,11 +38,26 @@
]
logger.info("Remounting %s storage to %s: %s ...", name, mode.upper(), tools.cmdfmt(cmd))
try:
- proc = await aioproc.log_process(cmd, logger)
- if proc.returncode != 0:
- assert proc.returncode is not None
- raise subprocess.CalledProcessError(proc.returncode, cmd)
- except Exception as err:
- logger.error("Can't remount %s storage: %s", name, tools.efmt(err))
- return False
- return True
+ await _run_helper(cmd)
+ except Exception:
+ logger.error("Can't remount internal storage")
+ raise
+
+
+async def unlock_drive(base_cmd: list[str]) -> None:
+ logger = get_logger(0)
+ logger.info("Unlocking the drive ...")
+ try:
+ await _run_helper(base_cmd)
+ except Exception:
+ logger.error("Can't unlock the drive")
+ raise
+
+
+# =====
+async def _run_helper(cmd: list[str]) -> None:
+ logger = get_logger(0)
+ logger.info("Executing helper %s ...", cmd)
+ proc = await aioproc.log_process(cmd, logger)
+ if proc.returncode != 0:
+ logger.error(f"Error while helper execution: pid={proc.pid}; retcode={proc.returncode}")
diff -ruN kvmd/apps/otg/__init__.py kvmd/apps/otg/__init__.py
--- kvmd/apps/otg/__init__.py 2024-04-29 03:07:25.000000000 +0800
+++ kvmd/apps/otg/__init__.py 2024-04-29 03:17:55.231210000 +0800
@@ -185,7 +185,6 @@
_chown(join(func_path, "lun.0/cdrom"), user)
_chown(join(func_path, "lun.0/ro"), user)
_chown(join(func_path, "lun.0/file"), user)
- _chown(join(func_path, "lun.0/forced_eject"), user)
if start:
_symlink(func_path, join(self.__profile_path, func))
name = ("Mass Storage Drive" if self.__msd_instance == 0 else f"Extra Drive #{self.__msd_instance}")
@@ -296,7 +295,7 @@
logger.info("Disabling gadget %r ...", config.otg.gadget)
_write(join(gadget_path, "UDC"), "\n")
- _unlink(join(gadget_path, "os_desc", usb.G_PROFILE_NAME), optional=True)
+ _unlink(join(gadget_path, "os_desc", usb.G_PROFILE_NAME), True)
profile_path = join(gadget_path, usb.G_PROFILE)
for func in os.listdir(profile_path):
diff -ruN kvmd/apps/otgmsd/__init__.py kvmd/apps/otgmsd/__init__.py
--- kvmd/apps/otgmsd/__init__.py 2024-04-29 03:07:25.000000000 +0800
+++ kvmd/apps/otgmsd/__init__.py 2024-04-29 03:20:57.568139600 +0800
@@ -21,8 +21,10 @@
import os
+import signal
import errno
import argparse
+import psutil
from ...validators.basic import valid_bool
from ...validators.basic import valid_int_f0
@@ -52,6 +54,21 @@
raise SystemExit(f"Can't change {param!r} value because device is locked: {err}")
raise
+def _unlock() -> None:
+ # https://github.com/torvalds/linux/blob/3039fad/drivers/usb/gadget/function/f_mass_storage.c#L2924
+ found = False
+ for proc in psutil.process_iter():
+ attrs = proc.as_dict(attrs=["name", "exe", "pid"])
+ if attrs.get("name") == "file-storage" and not attrs.get("exe"):
+ try:
+ proc.send_signal(signal.SIGUSR1)
+ found = True
+ except Exception as err:
+ raise SystemExit(f"Can't send SIGUSR1 to MSD kernel thread with pid={attrs['pid']}: {err}")
+ if not found:
+ raise SystemExit("Can't find MSD kernel thread")
+
+
# =====
def main(argv: (list[str] | None)=None) -> None:
@@ -77,7 +94,7 @@
parser.add_argument("--eject", action="store_true",
help="Eject the image")
parser.add_argument("--unlock", action="store_true",
- help="Does nothing, just for backward compatibility")
+ help="Send SIGUSR1 to MSD kernel thread")
options = parser.parse_args(argv[1:])
if config.kvmd.msd.type != "otg":
@@ -87,8 +104,11 @@
set_param = (lambda param, value: _set_param(config.otg.gadget, options.instance, param, value))
get_param = (lambda param: _get_param(config.otg.gadget, options.instance, param))
+ if options.unlock:
+ _unlock()
+
if options.eject:
- set_param("forced_eject", "")
+ set_param("file", "")
if options.set_cdrom is not None:
set_param("cdrom", str(int(options.set_cdrom)))
diff -ruN kvmd/helpers/unlock/__init__.py kvmd/helpers/unlock/__init__.py
--- kvmd/helpers/unlock/__init__.py 1970-01-01 08:00:00.000000000 +0800
+++ kvmd/helpers/unlock/__init__.py 2024-04-29 03:25:59.388222500 +0800
@@ -0,0 +1,58 @@
+# ========================================================================== #
+# #
+# KVMD - The main PiKVM daemon. #
+# #
+# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> #
+# #
+# 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/>. #
+# #
+# ========================================================================== #
+
+
+import sys
+import signal
+
+import psutil
+
+
+# =====
+_PROCESS_NAME = "file-storage"
+
+
+# =====
+def _log(msg: str) -> None:
+ print(msg, file=sys.stderr)
+
+
+def _unlock() -> None:
+ # https://github.com/torvalds/linux/blob/3039fad/drivers/usb/gadget/function/f_mass_storage.c#L2924
+ found = False
+ for proc in psutil.process_iter():
+ attrs = proc.as_dict(attrs=["name", "exe", "pid"])
+ if attrs.get("name") == _PROCESS_NAME and not attrs.get("exe"):
+ _log(f"Sending SIGUSR1 to MSD {_PROCESS_NAME!r} kernel thread with pid={attrs['pid']} ...")
+ try:
+ proc.send_signal(signal.SIGUSR1)
+ found = True
+ except Exception as err:
+ raise SystemExit(f"Can't send SIGUSR1 to MSD kernel thread with pid={attrs['pid']}: {err}")
+ if not found:
+ raise SystemExit(f"Can't find MSD kernel thread {_PROCESS_NAME!r}")
+
+
+# =====
+def main() -> None:
+ if len(sys.argv) != 2 or sys.argv[1] != "unlock":
+ raise SystemExit(f"Usage: {sys.argv[0]} [unlock]")
+ _unlock()
diff -ruN kvmd/helpers/unlock/__main__.py kvmd/helpers/unlock/__main__.py
--- kvmd/helpers/unlock/__main__.py 1970-01-01 08:00:00.000000000 +0800
+++ kvmd/helpers/unlock/__main__.py 2024-04-29 03:29:56.556771300 +0800
@@ -0,0 +1,24 @@
+# ========================================================================== #
+# #
+# KVMD - The main PiKVM daemon. #
+# #
+# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> #
+# #
+# 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/>. #
+# #
+# ========================================================================== #
+
+
+from . import main
+main()
diff -ruN kvmd/plugins/msd/otg/__init__.py kvmd/plugins/msd/otg/__init__.py
--- kvmd/plugins/msd/otg/__init__.py 2024-04-29 03:07:25.000000000 +0800
+++ kvmd/plugins/msd/otg/__init__.py 2024-04-29 04:42:09.154649200 +0800
@@ -119,6 +119,7 @@
sync_chunk_size: int,
remount_cmd: list[str],
+ unlock_cmd: list[str],
initial: dict,
@@ -133,7 +134,7 @@
self.__initial_cdrom: bool = initial["cdrom"]
self.__drive = Drive(gadget, instance=0, lun=0)
- self.__storage = Storage(fstab.find_msd().root_path, remount_cmd)
+ self.__storage = Storage(fstab.find_msd().root_path, remount_cmd, unlock_cmd)
self.__reader: (MsdFileReader | None) = None
self.__writer: (MsdFileWriter | None) = None
@@ -157,6 +158,11 @@
"/usr/bin/kvmd-helper-otgmsd-remount", "{mode}",
], type=valid_command),
+ "unlock_cmd": Option([
+ "/usr/bin/sudo", "--non-interactive",
+ "/usr/bin/kvmd-helper-otgmsd-unlock", "unlock",
+ ], type=valid_command),
+
"initial": {
"image": Option("", type=valid_msd_image_name, if_empty=""),
"cdrom": Option(False, type=valid_bool),
@@ -212,6 +218,7 @@
async def reset(self) -> None:
async with self.__state.busy(check_online=False):
try:
+ await self.__storage.unlock_drive()
self.__drive.set_image_path("")
self.__drive.set_cdrom_flag(False)
self.__drive.set_rw_flag(False)
@@ -269,6 +276,7 @@
assert self.__state.vd.image.in_storage
+ await self.__state.vd.image.unlock_drive()
self.__drive.set_rw_flag(self.__state.vd.rw)
self.__drive.set_cdrom_flag(self.__state.vd.cdrom)
if self.__state.vd.rw:
@@ -276,6 +284,7 @@
self.__drive.set_image_path(self.__state.vd.image.path)
else:
+ await self.__storage.unlock_drive()
self.__STATE_check_connected()
self.__drive.set_image_path("")
await self.__storage.remount_rw(False, fatal=False)
@@ -491,6 +500,7 @@
if (await image.exists()):
logger.info("Setting up initial image %r ...", self.__initial_image)
try:
+ await self.__storage.unlock_drive()
self.__drive.set_rw_flag(False)
self.__drive.set_cdrom_flag(self.__initial_cdrom)
self.__drive.set_image_path(image.path)
diff -ruN kvmd/plugins/msd/otg/drive.py kvmd/plugins/msd/otg/drive.py
--- kvmd/plugins/msd/otg/drive.py 2024-04-29 03:07:25.000000000 +0800
+++ kvmd/plugins/msd/otg/drive.py 2024-04-29 03:30:56.697201300 +0800
@@ -51,10 +51,7 @@
# =====
def set_image_path(self, path: str) -> None:
- if path:
- self.__set_param("file", path)
- else:
- self.__set_param("forced_eject", "")
+ self.__set_param("file", path)
def get_image_path(self) -> str:
path = self.__get_param("file")
diff -ruN kvmd/plugins/msd/otg/storage.py kvmd/plugins/msd/otg/storage.py
--- kvmd/plugins/msd/otg/storage.py 2024-04-29 03:07:25.000000000 +0800
+++ kvmd/plugins/msd/otg/storage.py 2024-04-29 04:30:13.444208000 +0800
@@ -111,6 +111,11 @@
if not self.__adopted:
await self.__storage.remount_rw(rw, fatal)
+ async def unlock_drive(self) -> None:
+ assert self.__storage
+ if not self.__adopted:
+ await self.__storage.unlock_drive()
+
async def remove(self, fatal: bool) -> None:
assert self.__storage
removed = False
@@ -176,10 +181,11 @@
class Storage(_StorageDc):
- def __init__(self, path: str, remount_cmd: list[str]) -> None:
+ def __init__(self, path: str, remount_cmd: list[str], unlock_cmd: list[str]) -> None:
super().__init__()
self.__path = path
self.__remount_cmd = remount_cmd
+ self.__unlock_cmd = unlock_cmd
self.__watchable_paths: (list[str] | None) = None
self.__images: (dict[str, Image] | None) = None
@@ -291,5 +297,8 @@
async def remount_rw(self, rw: bool, fatal: bool=True) -> None:
if not (await aiohelpers.remount("MSD", self.__remount_cmd, rw)):
- if fatal:
- raise MsdError("Can't execute remount helper")
+ pass
+ #raise MsdError("Can't execute remount helper")
+
+ async def unlock_drive(self) -> None:
+ await aiohelpers.unlock_drive(self.__unlock_cmd)