3 nbdkit-python-plugin - nbdkit python plugin
7 nbdkit python /path/to/plugin.py [arguments...]
11 C<nbdkit-python-plugin> is an embedded Python interpreter for
12 L<nbdkit(1)>, allowing you to write nbdkit plugins in Python.
14 =head2 If you have been given an nbdkit Python plugin
16 Assuming you have a Python script which is an nbdkit plugin, you run it
19 nbdkit python /path/to/plugin.py
21 You may have to add further C<key=value> arguments to the command
22 line. Read the Python script to see if it requires any.
24 =head1 WRITING A PYTHON NBDKIT PLUGIN
26 For an example plugin written in Python, see:
27 L<https://github.com/libguestfs/nbdkit/blob/master/plugins/python/example.py>
29 Broadly speaking, Python nbdkit plugins work like C ones, so you should
30 read L<nbdkit-plugin(3)> first.
32 To write a Python nbdkit plugin, you create a Python file which
33 contains at least the following required functions (in the top level
41 def pread(h, buf, offset, flags):
44 Note that the subroutines must have those literal names (like C<open>),
45 because the C part looks up and calls those functions directly. You
46 may want to include documentation and globals (eg. for storing global
47 state). Any other top level statements are run when the script is
48 loaded, just like ordinary Python.
50 =head2 Python versions
52 In B<nbdkit E<le> 1.14>, either Python 2 or 3 could be used. It was
53 selected at compile time by either:
57 which selected the version of Python by looking at the C<python>
58 interpreter found on the C<$PATH>. Or:
60 ./configure PYTHON=/usr/bin/python3
62 which allowed you to select a different interpreter and hence a
63 different version of Python.
65 B<nbdkit E<ge> 1.16> drops all support for Python 2, since Python 2
66 has reached its end of life.
68 The new behaviour is that C<./configure> looks for C<python3> or
69 C<python> (in that order) on the C<$PATH>. It will fail if the first
70 interpreter it finds is a Python 2 interpreter. You may also still
71 choose a Python interpreter by setting the C<PYTHON> variable at
72 configure time as above.
74 If you wish to continue using nbdkit plugins written in Python 2 then
75 you must use nbdkit E<le> 1.14, but we would advise you to update your
78 To find out which version the Python plugin was compiled for, use the
79 I<--dump-plugin> option, eg:
81 $ nbdkit python --dump-plugin
84 python_pep_384_abi_version=3
88 The nbdkit API has evolved and new versions are released periodically.
89 To ensure backwards compatibility plugins have to opt in to the new
90 version. From Python you do this by declaring a constant in your
95 (where 2 is the latest version at the time this documentation was
96 written). All newly written Python modules must have this constant.
98 =head2 Executable script
100 If you want you can make the script executable and include a "shebang"
103 #!/usr/sbin/nbdkit python
105 See also L<nbdkit(1)/Shebang scripts>.
107 These scripts can also be installed in the C<$plugindir>. See
108 L<nbdkit-plugin(3)/WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES>.
112 Your script may use C<import nbdkit> to have access to the following
113 methods in the C<nbdkit> module:
115 nbdkit.set_error(err)
117 Record C<err> as the reason you are about to throw an exception. C<err>
118 should correspond to usual errno values, where it may help to
123 Python callbacks should throw exceptions to indicate errors. Remember
124 to use C<nbdkit.set_error> if you need to control which error is sent
125 back to the client; if omitted, the client will see an error of C<EIO>.
127 =head2 Python callbacks
129 This just documents the arguments to the callbacks in Python, and any
130 way that they differ from the C callbacks. In all other respects they
131 work the same way as the C callbacks, so you should go and read
140 There are no arguments or return value.
146 def config(key, value):
149 =item C<config_complete>
153 There are no arguments or return value.
162 You can return any non-NULL Python value as the handle. It is passed
163 back in subsequent calls.
172 After C<close> returns, the reference count of the handle is
173 decremented in the C part, which usually means that the handle and its
174 contents will be garbage collected.
181 # return the size of the disk
183 =item C<is_rotational>
187 def is_rotational(h):
190 =item C<can_multi_conn>
194 def can_multi_conn(h):
225 =item C<can_fast_zero>
229 def can_fast_zero(h):
237 # return nbdkit.FUA_NONE or nbdkit.FUA_EMULATE
238 # or nbdkit.FUA_NATIVE
245 # return nbdkit.CACHE_NONE or nbdkit.CACHE_EMULATE
246 # or nbdkit.CACHE_NATIVE
266 def pread(h, buf, offset, flags):
267 # read into the buffer
269 The body of your C<pread> function should read exactly C<len(buf)>
270 bytes of data starting at disk C<offset> and write it into the buffer
271 C<buf>. C<flags> is always 0.
273 NBD only supports whole reads, so your function should try to read
274 the whole region (perhaps requiring a loop). If the read fails or
275 is partial, your function should throw an exception, optionally using
276 C<nbdkit.set_error> first.
282 def pwrite(h, buf, offset, flags):
286 The body of your C<pwrite> function should write the buffer C<buf> to
287 the disk. You should write C<count> bytes to the disk starting at
288 C<offset>. C<flags> may contain C<nbdkit.FLAG_FUA>.
290 NBD only supports whole writes, so your function should try to
291 write the whole region (perhaps requiring a loop). If the write
292 fails or is partial, your function should throw an exception,
293 optionally using C<nbdkit.set_error> first.
302 The body of your C<flush> function should do a L<sync(2)> or
303 L<fdatasync(2)> or equivalent on the backing store.
304 C<flags> is always 0.
306 If the flush fails, your function should throw an exception, optionally
307 using C<nbdkit.set_error> first.
313 def trim(h, count, offset, flags):
316 The body of your C<trim> function should "punch a hole" in the backing
317 store. C<flags> may contain C<nbdkit.FLAG_FUA>. If the trim fails,
318 your function should throw an exception, optionally using
319 C<nbdkit.set_error> first.
325 def zero(h, count, offset, flags):
328 The body of your C<zero> function should ensure that C<count> bytes of
329 the disk, starting at C<offset>, will read back as zero. C<flags> is
330 a bitmask which may include C<nbdkit.FLAG_MAY_TRIM>,
331 C<nbdkit.FLAG_FUA>, C<nbdkit.FLAG_FAST_ZERO>.
333 NBD only supports whole writes, so your function should try to
334 write the whole region (perhaps requiring a loop).
336 If the write fails or is partial, your function should throw an
337 exception, optionally using C<nbdkit.set_error> first. In particular,
338 if you would like to automatically fall back to C<pwrite> (perhaps
339 because there is nothing to optimize if
340 S<C<flags & nbdkit.FLAG_MAY_TRIM>> is false), use
341 S<C<nbdkit.set_error (errno.EOPNOTSUPP)>>.
347 def cache(h, count, offset, flags):
350 The body of your C<cache> function should prefetch data in the
353 If the cache operation fails, your function should throw an exception,
354 optionally using C<nbdkit.set_error> first.
358 =head2 Missing callbacks
362 =item Missing: C<load> and C<unload>
364 These are not needed because you can just use ordinary Python
367 =item Missing: C<thread_model>
369 See L</Threads> below.
381 These are not yet supported.
387 The thread model for Python callbacks currently cannot be set from
388 Python. It is hard-coded in the C part to
389 C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>. This may change or be
396 =item F<$plugindir/nbdkit-python-plugin.so>
400 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
406 C<nbdkit-python-plugin> first appeared in nbdkit 1.2.
424 Copyright (C) 2013-2019 Red Hat Inc.