3 nbdkit-lua-plugin - nbdkit Lua plugin
7 nbdkit lua /path/to/plugin.lua [arguments...]
11 C<nbdkit-lua-plugin> is an embedded Lua interpreter for
12 L<nbdkit(1)>, allowing you to write nbdkit plugins in Lua.
14 =head2 If you have been given an nbdkit Lua plugin
16 Assuming you have a Lua script which is an nbdkit plugin, you run it
19 nbdkit lua /path/to/plugin.lua
21 You may have to add further C<key=value> arguments to the command
22 line. Read the Lua script to see if it requires any.
24 =head1 WRITING A LUA NBDKIT PLUGIN
26 For an example plugin written in Lua, see:
27 L<https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/lua/example.lua>
29 Broadly speaking, Lua nbdkit plugins work like C ones, so you should
30 read L<nbdkit-plugin(3)> first.
32 To write a Lua nbdkit plugin, you create a Lua file which contains
33 at least the following required functions:
35 function open (readonly)
43 function pread (h, count, offset)
48 Note that the subroutines must have those literal names (like
49 C<open>), because the C part looks up and calls those functions
50 directly. You may want to include documentation and globals (eg. for
51 storing global state). Also any top-level statements are run when
54 =head2 Executable script
56 If you want you can make the script executable and include a "shebang"
59 #!/usr/sbin/nbdkit lua
61 See also L<nbdkit(1)/Shebang scripts>.
63 These scripts can also be installed in the C<$plugindir>. See
64 L<nbdkit-plugin(3)/WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES>.
68 Lua plugin methods can indicate an error by calling C<error> or
69 C<assert>. The error message will contain the method name, filename
70 and line number where the error occurred, eg:
72 error ("could not open " .. filename)
73 --> nbdkit: error: open: myplugin.lua:123: could not open disk.img
77 This just documents the arguments to the callbacks in Lua, and any
78 way that they differ from the C callbacks. In all other respects they
79 work the same way as the C callbacks, so you should go and read
88 There are no arguments or return value.
94 function config (key, value)
98 =item C<config_complete>
102 There are no arguments or return value.
108 function open (readonly)
114 The C<readonly> flag is a boolean.
116 You can return any Lua string or object as the handle. It is passed
117 back to subsequent calls.
127 After C<close> returns, the reference count of the handle is
128 decremented in the C part, which usually means that the handle and its
129 contents will be garbage collected.
135 function get_size (h)
137 size= .. the size of the disk ..
141 This returns the size of the disk.
147 function can_write (h)
151 Return a boolean indicating whether the disk is writable.
157 function can_flush (h)
161 Return a boolean indicating whether flush can be performed.
163 =item C<is_rotational>
167 function is_rotational (h)
171 Return a boolean indicating whether the disk is rotational.
177 function can_trim (h)
181 Return a boolean indicating whether trim/discard can be performed.
187 function pread (h, count, offset)
188 -- Construct a buffer of length count bytes and return it.
192 The body of your C<pread> function should construct a buffer of length
193 (at least) C<count> bytes. You should read C<count> bytes from the
194 disk starting at C<offset>.
196 NBD only supports whole reads, so your function should try to read the
197 whole region (perhaps requiring a loop). If the read fails or is
198 partial, your function should call C<error>.
204 function pwrite (h, buf, offset)
208 The body of your C<pwrite> function should write the C<buf> string to
209 the disk. You should write C<count> bytes to the disk starting at
212 NBD only supports whole writes, so your function should try to write
213 the whole region (perhaps requiring a loop). If the write fails or is
214 partial, your function should call C<error>.
224 The body of your C<flush> function should do a L<sync(2)> or
225 L<fdatasync(2)> or equivalent on the backing store.
231 function trim (h, count, offset)
235 The body of your C<trim> function should "punch a hole" in the backing
242 function zero (h, count, offset, may_trim)
246 The body of your C<zero> function should ensure that C<count> bytes
247 of the disk, starting at C<offset>, will read back as zero. If
248 C<may_trim> is true, the operation may be optimized as a trim as long
249 as subsequent reads see zeroes.
251 NBD only supports whole writes, so your function should try to write
252 the whole region (perhaps requiring a loop). If the write fails or is
253 partial, your function should call C<error>.
257 =head2 Missing callbacks
261 =item Missing: C<load>, C<unload>, C<name>, C<version>, C<longname>,
262 C<description>, C<config_help>, C<can_zero>, C<can_fua>, C<can_cache>,
265 These are not yet supported.
271 The thread model for Lua callbacks currently cannot be set from Lua.
272 It is hard-coded in the C part to
273 C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>. This may change or be
280 =item F<$plugindir/nbdkit-lua-plugin.so>
284 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
290 C<nbdkit-lua-plugin> first appeared in nbdkit 1.6.