1nbdkit-python-plugin(3)             NBDKIT             nbdkit-python-plugin(3)
2
3
4

NAME

6       nbdkit-python-plugin - nbdkit python plugin
7

SYNOPSIS

9        nbdkit python /path/to/plugin.py [arguments...]
10

DESCRIPTION

12       "nbdkit-python-plugin" is an embedded Python interpreter for nbdkit(1),
13       allowing you to write nbdkit plugins in Python 3.
14
15   If you have been given an nbdkit Python plugin
16       Assuming you have a Python script which is an nbdkit plugin, you run it
17       like this:
18
19        nbdkit python /path/to/plugin.py
20
21       You may have to add further "key=value" arguments to the command line.
22       Read the Python script to see if it requires any.
23

WRITING A PYTHON NBDKIT PLUGIN

25       For example plugins written in Python, see:
26       https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/python/examples
27
28       Broadly speaking, Python nbdkit plugins work like C ones, so you should
29       read nbdkit-plugin(3) first.
30
31       To write a Python nbdkit plugin, you create a Python file which
32       contains at least the following required functions (in the top level
33       "__main__" module):
34
35        API_VERSION = 2
36        def open(readonly):
37          # see below
38        def get_size(h):
39          # see below
40        def pread(h, buf, offset, flags):
41          # see below
42
43       Note that the subroutines must have those literal names (like "open"),
44       because the C part looks up and calls those functions directly.  You
45       may want to include documentation and globals (eg. for storing global
46       state).  Any other top level statements are run when the script is
47       loaded, just like ordinary Python.
48
49   Python versions
50       Since nbdkit ≥ 1.16 only Python 3 is supported.  If you wish to
51       continue using nbdkit plugins written in Python 2 then you must use
52       nbdkit ≤ 1.14, but we advise you to update your plugins.
53
54       The version of Python 3 is chosen when nbdkit is built.  This is
55       compiled in and can't be changed at runtime.  "./configure" looks for
56       (in order):
57
58       •   the "PYTHON" variable (eg "./configure PYTHON=/usr/bin/python3.9")
59
60python3 on $PATH
61
62python on $PATH
63
64       "./configure" will fail if the first interpreter found is a Python 2
65       interpreter.
66
67       To find out which version of Python "nbdkit-python-plugin" was compiled
68       for, use the --dump-plugin option:
69
70        $ nbdkit python --dump-plugin
71        ...
72        python_version=3.7.0
73        python_pep_384_abi_version=3
74
75   API versions
76       The nbdkit API has evolved and new versions are released periodically.
77       To ensure backwards compatibility plugins have to opt in to the new
78       version.  From Python you do this by declaring a constant in your
79       module:
80
81        API_VERSION = 2
82
83       (where 2 is the latest version at the time this documentation was
84       written).  All newly written Python modules must have this constant.
85
86   Executable script
87       If you want you can make the script executable and include a "shebang"
88       at the top:
89
90        #!/usr/sbin/nbdkit python
91
92       See also "Shebang scripts" in nbdkit(1).
93
94       These scripts can also be installed in the $plugindir.  See "WRITING
95       PLUGINS IN OTHER PROGRAMMING LANGUAGES" in nbdkit-plugin(3).
96
97   Module functions
98       Your script may use "import nbdkit" to have access to the following
99       methods in the "nbdkit" module:
100
101       "nbdkit.debug(msg)"
102
103       Send a debug message to stderr or syslog if verbose messages are
104       enabled.
105
106       "nbdkit.export_name()"
107
108       Return the export name negotiated with the client as a Unicode string.
109       Note this should not be trusted because the client can send whatever it
110       wants.
111
112       "nbdkit.set_error(err)"
113
114       Record "err" as the reason you are about to throw an exception. "err"
115       should correspond to usual errno values, where it may help to "import
116       errno".
117
118       "nbdkit.shutdown()"
119
120       Request asynchronous server shutdown.
121
122   Module constants
123       After "import nbdkit" the following constants are available.  These are
124       used in the callbacks below.
125
126       "nbdkit.THREAD_MODEL_SERIALIZE_CONNECTIONS"
127       "nbdkit.THREAD_MODEL_SERIALIZE_ALL_REQUESTS"
128       "nbdkit.THREAD_MODEL_SERIALIZE_REQUESTS"
129       "nbdkit.THREAD_MODEL_PARALLEL"
130           Possible return values from "thread_model()".
131
132       "nbdkit.FLAG_MAY_TRIM"
133       "nbdkit.FLAG_FUA"
134       "nbdkit.FLAG_REQ_ONE"
135       "nbdkit.FLAG_FAST_ZERO"
136           Flags bitmap passed to certain plugin callbacks.  Not all callbacks
137           with a flags parameter use all of these flags, consult the
138           documentation below and nbdkit-plugin(3).
139
140       "nbdkit.FUA_NONE"
141       "nbdkit.FUA_EMULATE"
142       "nbdkit.FUA_NATIVE"
143           Possible return values from "can_fua()".
144
145       "nbdkit.CACHE_NONE"
146       "nbdkit.CACHE_EMULATE"
147       "nbdkit.CACHE_NATIVE"
148           Possible return values from "can_cache()".
149
150       "nbdkit.EXTENT_HOLE"
151       "nbdkit.EXTENT_ZERO"
152           Used in the "type" field returned by "extents()".
153
154   Threads
155       The thread model for Python callbacks defaults to
156       "nbdkit.THREAD_MODEL_SERIALIZE_ALL_REQUESTS".
157
158       Since nbdkit 1.22 it has been possible to set this by implementing a
159       "thread_model()" function which returns one of the constants
160       "nbdkit.THREAD_MODEL_*".
161
162       The Python Global Interpreter Lock (GIL) is still used, so Python code
163       does not run in parallel.  However if a plugin callback calls a library
164       which blocks (eg. to make an HTTP request), then another callback might
165       be executed in parallel.  Plugins which use
166       "nbdkit.THREAD_MODEL_SERIALIZE_REQUESTS" or
167       "nbdkit.THREAD_MODEL_PARALLEL" may need to use locks on shared data.
168
169   Exceptions
170       Python callbacks should throw exceptions to indicate errors.  Remember
171       to use "nbdkit.set_error" if you need to control which error is sent
172       back to the client; if omitted, the client will see an error of "EIO".
173
174   Python callbacks
175       This just documents the arguments to the callbacks in Python, and any
176       way that they differ from the C callbacks.  In all other respects they
177       work the same way as the C callbacks, so you should go and read
178       nbdkit-plugin(3).
179
180       "dump_plugin"
181           (Optional)
182
183           There are no arguments or return value.
184
185       "config"
186           (Optional)
187
188            def config(key, value):
189              # no return value
190
191       "config_complete"
192           (Optional)
193
194           There are no arguments or return value.
195
196       "thread_model"
197           (Optional, nbdkit ≥ 1.22)
198
199            def thread_model():
200              return nbdkit.THEAD_MODEL_SERIALIZE_ALL_REQUESTS
201
202           See "Threads" above.
203
204       "get_ready"
205           (Optional)
206
207           There are no arguments or return value.
208
209       "after_fork"
210           (Optional, nbdkit ≥ 1.26)
211
212           There are no arguments or return value.
213
214       "cleanup"
215           (Optional, nbdkit ≥ 1.28)
216
217           There are no arguments or return value.
218
219       "list_exports"
220           (Optional)
221
222            def list_exports(readonly, is_tls):
223              # return an iterable object (eg. list) of
224              # (name, description) tuples or bare names:
225              return [ (name1, desc1), name2, (name3, desc3), ... ]
226
227       "default_export"
228           (Optional)
229
230            def default_export(readonly, is_tls):
231              # return a string
232              return "name"
233
234       "preconnect"
235           (Optional, nbdkit ≥ 1.26)
236
237            def preconnect(readonly):
238              # no return value
239
240       "open"
241           (Required)
242
243            def open(readonly):
244              # return handle
245
246           You can return any non-NULL Python value as the handle.  It is
247           passed back in subsequent calls.
248
249       "close"
250           (Optional)
251
252            def close(h):
253              # no return value
254
255           After "close" returns, the reference count of the handle is
256           decremented in the C part, which usually means that the handle and
257           its contents will be garbage collected.
258
259       "export_description"
260           (Optional)
261
262            def export_description(h):
263              # return a string
264              return "description"
265
266       "get_size"
267           (Required)
268
269            def get_size(h):
270              # return the size of the disk
271
272       "block_size"
273           (Option)
274
275            def block_size(h):
276              # return triple (minimum, preferred, maximum) block size
277
278       "is_rotational"
279           (Optional)
280
281            def is_rotational(h):
282              # return a boolean
283
284       "can_multi_conn"
285           (Optional)
286
287            def can_multi_conn(h):
288              # return a boolean
289
290       "can_write"
291           (Optional)
292
293            def can_write(h):
294              # return a boolean
295
296       "can_flush"
297           (Optional)
298
299            def can_flush(h):
300              # return a boolean
301
302       "can_trim"
303           (Optional)
304
305            def can_trim(h):
306              # return a boolean
307
308       "can_zero"
309           (Optional)
310
311            def can_zero(h):
312              # return a boolean
313
314       "can_fast_zero"
315           (Optional)
316
317            def can_fast_zero(h):
318              # return a boolean
319
320       "can_fua"
321           (Optional)
322
323            def can_fua(h):
324              # return nbdkit.FUA_NONE or nbdkit.FUA_EMULATE
325              # or nbdkit.FUA_NATIVE
326
327       "can_cache"
328           (Optional)
329
330            def can_cache(h):
331              # return nbdkit.CACHE_NONE or nbdkit.CACHE_EMULATE
332              # or nbdkit.CACHE_NATIVE
333
334       "can_extents"
335           (Optional)
336
337            def can_extents(h):
338              # return a boolean
339
340       "pread"
341           (Required)
342
343            def pread(h, buf, offset, flags):
344              # read into the buffer
345
346           The body of your "pread" function should read exactly "len(buf)"
347           bytes of data starting at disk "offset" and write it into the
348           buffer "buf".  "flags" is always 0.
349
350           NBD only supports whole reads, so your function should try to read
351           the whole region (perhaps requiring a loop).  If the read fails or
352           is partial, your function should throw an exception, optionally
353           using "nbdkit.set_error" first.
354
355       "pwrite"
356           (Optional)
357
358            def pwrite(h, buf, offset, flags):
359              length = len(buf)
360              # no return value
361
362           The body of your "pwrite" function should write the buffer "buf" to
363           the disk.  You should write "count" bytes to the disk starting at
364           "offset".  "flags" may contain "nbdkit.FLAG_FUA".
365
366           NBD only supports whole writes, so your function should try to
367           write the whole region (perhaps requiring a loop).  If the write
368           fails or is partial, your function should throw an exception,
369            optionally using "nbdkit.set_error" first.
370
371       "flush"
372           (Optional)
373
374            def flush(h, flags):
375              # no return value
376
377           The body of your "flush" function should do a sync(2) or
378           fdatasync(2) or equivalent on the backing store.  "flags" is always
379           0.
380
381           If the flush fails, your function should throw an exception,
382           optionally using "nbdkit.set_error" first.
383
384       "trim"
385           (Optional)
386
387            def trim(h, count, offset, flags):
388              # no return value
389
390           The body of your "trim" function should "punch a hole" in the
391           backing store.  "flags" may contain "nbdkit.FLAG_FUA".  If the trim
392           fails, your function should throw an exception, optionally using
393           "nbdkit.set_error" first.
394
395       "zero"
396           (Optional)
397
398            def zero(h, count, offset, flags):
399              # no return value
400
401           The body of your "zero" function should ensure that "count" bytes
402           of the disk, starting at "offset", will read back as zero.  "flags"
403           is a bitmask which may include "nbdkit.FLAG_MAY_TRIM",
404           "nbdkit.FLAG_FUA", "nbdkit.FLAG_FAST_ZERO".
405
406           NBD only supports whole writes, so your function should try to
407           write the whole region (perhaps requiring a loop).
408
409           If the write fails or is partial, your function should throw an
410           exception, optionally using "nbdkit.set_error" first.  In
411           particular, if you would like to automatically fall back to
412           "pwrite" (perhaps because there is nothing to optimize if
413           "flags & nbdkit.FLAG_MAY_TRIM" is false), use
414           "nbdkit.set_error(errno.EOPNOTSUPP)".
415
416       "cache"
417           (Optional)
418
419            def cache(h, count, offset, flags):
420              # no return value
421
422           The body of your "cache" function should prefetch data in the
423           indicated range.
424
425           If the cache operation fails, your function should throw an
426           exception, optionally using "nbdkit.set_error" first.
427
428       "extents"
429           (Optional)
430
431            def extents(h, count, offset, flags):
432              # return an iterable object (eg. list) of
433              # (offset, length, type) tuples:
434              return [ (off1, len1, type1), (off2, len2, type2), ... ]
435
436   Missing callbacks
437       Missing: "load"
438           This is not needed since you can use regular Python mechanisms like
439           top level statements to run code when the module is loaded.
440
441       Missing: "unload"
442           This is missing, but in nbdkit ≥ 1.28 you can put code in the
443           "cleanup()" function to have it run when nbdkit exits.  In earlier
444           versions of nbdkit, using a Python atexit handler is recommended.
445
446       Missing: "name", "version", "longname", "description", "config_help",
447       "magic_config_key".
448           These are not yet supported.
449

FILES

451       $plugindir/nbdkit-python-plugin.so
452           The plugin.
453
454           Use "nbdkit --dump-config" to find the location of $plugindir.
455

VERSION

457       "nbdkit-python-plugin" first appeared in nbdkit 1.2.
458

SEE ALSO

460       nbdkit(1), nbdkit-plugin(3), python(1).
461

AUTHORS

463       Eric Blake
464
465       Richard W.M. Jones
466
467       Nir Soffer
468
470       Copyright (C) 2013-2021 Red Hat Inc.
471

LICENSE

473       Redistribution and use in source and binary forms, with or without
474       modification, are permitted provided that the following conditions are
475       met:
476
477       •   Redistributions of source code must retain the above copyright
478           notice, this list of conditions and the following disclaimer.
479
480       •   Redistributions in binary form must reproduce the above copyright
481           notice, this list of conditions and the following disclaimer in the
482           documentation and/or other materials provided with the
483           distribution.
484
485       •   Neither the name of Red Hat nor the names of its contributors may
486           be used to endorse or promote products derived from this software
487           without specific prior written permission.
488
489       THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY
490       EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
491       IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
492       PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE
493       LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
494       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
495       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
496       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
497       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
498       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
499       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
500
501
502
503nbdkit-1.30.7                     2022-07-10           nbdkit-python-plugin(3)
Impressum