Update Red Hat Copyright Notices
[nbdkit.git] / plugins / lua / nbdkit-lua-plugin.pod
blobebdbdc948b99119b7a66a934ea8f6343272bfe05
1 =head1 NAME
3 nbdkit-lua-plugin - nbdkit Lua plugin
5 =head1 SYNOPSIS
7  nbdkit lua /path/to/plugin.lua [arguments...]
9 =head1 DESCRIPTION
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
17 like this:
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)
36      -- see below
37      return h
38  end
39  function get_size (h)
40      -- see below
41      return size
42  end
43  function pread (h, count, offset)
44      -- see below
45      return buf
46  end
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
52 nbdkit starts up.
54 =head2 Executable script
56 If you want you can make the script executable and include a "shebang"
57 at the top:
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>.
66 =head2 Errors
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
75 =head2 Lua callbacks
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
80 L<nbdkit-plugin(3)>.
82 =over 4
84 =item C<dump_plugin>
86 (Optional)
88 There are no arguments or return value.
90 =item C<config>
92 (Optional)
94  function config (key, value)
95      -- No return value.
96  end
98 =item C<config_complete>
100 (Optional)
102 There are no arguments or return value.
104 =item C<open>
106 (Required)
108  function open (readonly)
109      local handle
110      handle=...
111      return handle
112  end
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.
119 =item C<close>
121 (Optional)
123  function close (h)
124      -- No return value
125  end
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.
131 =item C<get_size>
133 (Required)
135  function get_size (h)
136      local size
137      size= .. the size of the disk ..
138      return size
139  end
141 This returns the size of the disk.
143 =item C<can_write>
145 (Optional)
147  function can_write (h)
148      return bool
149  end
151 Return a boolean indicating whether the disk is writable.
153 =item C<can_flush>
155 (Optional)
157  function can_flush (h)
158      return bool
159  end
161 Return a boolean indicating whether flush can be performed.
163 =item C<is_rotational>
165 (Optional)
167  function is_rotational (h)
168      return bool
169  end
171 Return a boolean indicating whether the disk is rotational.
173 =item C<can_trim>
175 (Optional)
177  function can_trim (h)
178      return bool
179  end
181 Return a boolean indicating whether trim/discard can be performed.
183 =item C<pread>
185 (Required)
187  function pread (h, count, offset)
188     -- Construct a buffer of length count bytes and return it.
189     return buf
190  end
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>.
200 =item C<pwrite>
202 (Optional)
204  function pwrite (h, buf, offset)
205     -- No return value
206  end
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
210 C<offset>.
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>.
216 =item C<flush>
218 (Optional)
220  function flush (h)
221      -- No return value
222  end
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.
227 =item C<trim>
229 (Optional)
231  function trim (h, count, offset)
232      -- No return value
233  end
235 The body of your C<trim> function should "punch a hole" in the backing
236 store.
238 =item C<zero>
240 (Optional)
242  function zero (h, count, offset, may_trim)
243     -- No return value
244  end
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>.
255 =back
257 =head2 Missing callbacks
259 =over 4
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>,
263 C<cache>
265 These are not yet supported.
267 =back
269 =head2 Threads
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
274 settable in future.
276 =head1 FILES
278 =over 4
280 =item F<$plugindir/nbdkit-lua-plugin.so>
282 The plugin.
284 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
286 =back
288 =head1 VERSION
290 C<nbdkit-lua-plugin> first appeared in nbdkit 1.6.
292 =head1 SEE ALSO
294 L<nbdkit(1)>,
295 L<nbdkit-plugin(3)>.
297 =head1 AUTHORS
299 Richard W.M. Jones
301 =head1 COPYRIGHT
303 Copyright Red Hat