-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnbd_http_server.py
executable file
·339 lines (278 loc) · 9.97 KB
/
nbd_http_server.py
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python
#
# Copyright (C) 2022 Vates SAS
#
# 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 __future__ import print_function
from contextlib import contextmanager
import argparse
import errno
import os
import signal
import subprocess
import sys
import threading
WORKING_DIR = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), '')
NBDKIT_PLUGIN = WORKING_DIR + '../lib64/nbdkit/plugins/nbdkit-multi-http-plugin.so'
# ==============================================================================
OUTPUT_PREFIX = ''
SIGTERM_RECEIVED = False
def handle_sigterm(*args):
global SIGTERM_RECEIVED
SIGTERM_RECEIVED = True
# -----------------------------------------------------------------------------
def pid_exists(pid):
try:
os.kill(pid, 0)
except OSError:
return False
return True
# -----------------------------------------------------------------------------
def remove_file(path):
try:
os.remove(path)
except OSError as e:
if e.errno != errno.ENOENT:
raise e
def run_or_ignore(fun):
try:
fun()
except Exception:
pass
# -----------------------------------------------------------------------------
THREAD_PRINT_LOCK = threading.Lock()
def thread_print(str):
with THREAD_PRINT_LOCK:
print(str)
def eprint(str):
thread_print(OUTPUT_PREFIX + str)
# -----------------------------------------------------------------------------
class CommandException(Exception):
def __init__(self, code, cmd, stdout, stderr):
self.code = code
self.cmd = cmd
self.stdout = stdout
self.stderr = stderr
Exception.__init__(self, os.strerror(abs(code)))
def __str__(self):
return 'Command exception: `{}` (code: `{}`, reason: `{}`)'.format(
self.cmd, self.code, self.stderr
)
def call(cmd, expected_returncode=0):
eprint('Call command: {}'.format(cmd))
p = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True,
universal_newlines=True
)
stdout, stderr = p.communicate()
if p.returncode != expected_returncode:
raise CommandException(p.returncode, str(cmd), stdout.strip(), stderr.strip())
return stdout
# ==============================================================================
class TimeoutException(Exception):
def __init__(self):
super(Exception, self).__init__('timeout')
@contextmanager
def timeout(seconds):
def handler(signum, frame):
raise TimeoutException
old_handler = signal.signal(signal.SIGALRM, handler)
try:
signal.alarm(seconds)
yield
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, old_handler)
# ==============================================================================
def get_nbd_pid(nbd_path):
try:
call(['nbd-client', '-c', nbd_path], expected_returncode=1)
except CommandException as e:
try:
return int(e.stdout)
except ValueError:
return -1
return None
def disconnect_nbd(nbd_path):
call(['nbd-client', '-d', nbd_path])
class Nbd:
__slots__ = ('name', 'nbd')
def __init__(self, nbd, name):
self.nbd = nbd
self.name = name
def disconnect(self):
try:
disconnect_nbd('/dev/' + self.nbd)
except Exception as e:
eprint('Failed to disconnect NBD {}: `{}`.'.format(self.nbd, e))
def attach_nbd(socket_path, nbd_name, pid_path):
def get_nbds():
return list(filter(lambda nbd: nbd.startswith('nbd'), os.listdir('/dev')))
nbds = get_nbds()
if not nbds:
call(['modprobe', 'nbd'])
nbds = get_nbds()
if not nbds:
raise Exception('No NBD available.')
for nbd in nbds:
nbd_path = '/dev/' + nbd
# Open to check if the device is not mounted.
try:
fd = os.open(nbd_path, os.O_EXCL)
os.close(fd)
except Exception:
continue
# Ensure device is free.
# Also we must do that, otherwise this error can be produced during the attach:
# "Ioctl NBD_SET_SOCK failed: Device or resource busy"
# And of course, this error kills our server.
pid = get_nbd_pid(nbd_path)
if pid is not None:
if not pid_exists(pid):
eprint(
'Potential leaked NBD device detected: `{}` used by dead process {}'
.format(nbd_path, pid)
)
continue
# Use free device.
try:
# Use an extreme timeout here, should never be triggered.
with timeout(30):
call(['nbd-client', '-unix', socket_path, nbd_path, '-b', '512'])
except Exception as e:
# We guess we have to try another device after that: this exception is probably
# caused by a concurrent attach and not with our nbdkit plugin.
eprint('Failed to attach socket `{}` to {}: {}.'.format(socket_path, nbd_path, e))
# Ensure the device is still free after failed command.
# Note: a race condition exists when we try to release this device,
# if the targeted pid is invalid. It can be used by another process.
pid = None
try:
with open(pid_path) as f:
pid = int(f.readline().strip('\n'))
except Exception:
continue
finally:
remove_file(pid_path)
targeted_pid = get_nbd_pid(nbd_path)
if (pid is not None and pid == targeted_pid) or (targeted_pid is not None and targeted_pid < 0):
run_or_ignore(lambda: disconnect_nbd(nbd_path))
continue
# NBD is now attached, try to modify scheduler + return NBD object.
eprint('NBD `{}` is now attached.'.format(nbd_path))
try:
with open('/sys/block/' + nbd + '/queue/scheduler', 'w') as fd:
fd.write('none')
except Exception as e:
eprint('Failed to modify scheduler of {}: `{}`.'.format(nbd_path, e))
return Nbd(nbd, nbd_name)
raise Exception('Cannot attach `{}` to NBD.'.format(socket_path))
def run_nbd_server(socket_path, nbd_name, urls, device_size):
pid_path = '/var/run/nbd-{}.pid'.format(nbd_name)
def clean_paths():
remove_file(socket_path)
remove_file(pid_path)
clean_paths()
arguments = [
'nbdkit',
'--verbose',
'--foreground',
'-U', socket_path,
'-e', nbd_name,
'-P', pid_path,
NBDKIT_PLUGIN,
'urls=' + urls
]
if device_size is not None:
arguments.append('device-size=' + str(device_size))
# Start nbdkit process.
server = subprocess.Popen(
arguments,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
env=dict(os.environ, PYTHONUNBUFFERED='1')
)
# Wait for init.
try:
with timeout(10):
while server.poll() is None:
line = server.stdout.readline().rstrip('\n')
if not line:
continue
print(line)
if 'written pidfile' in line:
break
except Exception as e:
raise Exception('Failed to start nbdkit server: {}.'.format(e))
# Continue to log server messages in stdout.
def log_server_messages():
while server.poll() is None:
line = server.stdout.readline().rstrip('\n')
if line:
thread_print(line)
server_stdout_thread = threading.Thread(target=log_server_messages)
server_stdout_thread.start()
nbd = None
try:
nbd = attach_nbd(socket_path, nbd_name, pid_path)
while True:
try:
if SIGTERM_RECEIVED:
eprint('SIGTERM received. Exiting server...')
break
signal.pause()
except KeyboardInterrupt:
eprint('Exiting server...')
break
finally:
if nbd:
nbd.disconnect()
server.send_signal(signal.SIGQUIT)
server.wait()
server_stdout_thread.join()
clean_paths()
# ==============================================================================
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--socket-path', action='store', dest='socket_path', default=None, required=True,
help='UNIX socket path to use'
)
parser.add_argument(
'--nbd-name', action='store', dest='nbd_name', default=None, required=True,
help='NBD export name'
)
parser.add_argument(
'--urls', action='store', dest='urls', default=None, required=True,
help='URLS to read/write data'
)
parser.add_argument(
'--device-size', action='store', dest='device_size', default=None, required=False, type=int,
help='Force the device size, instead of using an HTTP server to get it'
)
args = parser.parse_args()
global OUTPUT_PREFIX
OUTPUT_PREFIX = '[' + args.nbd_name + '] '
try:
signal.signal(signal.SIGTERM, handle_sigterm)
run_nbd_server(args.socket_path, args.nbd_name, args.urls, args.device_size)
except Exception as e:
eprint('Got exception: `{}`.'.format(e))
exit(1)
if __name__ == '__main__':
main()