3 nbdkit-tcl-plugin - nbdkit Tcl plugin
7 nbdkit tcl /path/to/plugin.tcl [arguments...]
11 C<nbdkit-tcl-plugin> is an embedded Tcl interpreter for
12 L<nbdkit(1)>, allowing you to write nbdkit plugins in Tcl.
14 =head2 If you have been given an nbdkit Tcl plugin
16 Assuming you have a Tcl script which is an nbdkit plugin, you run it
19 nbdkit tcl /path/to/plugin.tcl
21 You may have to add further C<key=value> arguments to the command
22 line. Read the Tcl script to see if it requires any.
24 =head1 WRITING A TCL NBDKIT PLUGIN
26 For an example plugin written in Tcl, see:
27 L<https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/tcl/example.tcl>
29 Broadly speaking, Tcl nbdkit plugins work like C ones, so you should
30 read L<nbdkit-plugin(3)> first.
32 To write a Tcl nbdkit plugin, you create a Tcl file which contains
33 at least the following required subroutines:
35 proc plugin_open {readonly} {
43 proc pread {h count offset} {
48 Note that the subroutines must have those literal names (like
49 C<plugin_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 tcl
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 Tcl plugin methods can indicate an error by calling C<error>.
72 When writing your Tcl script, be careful to ensure that it is
73 processing binary data (not Unicode). If reading and writing from
74 local disk files, you should use:
76 fconfigure $fp -translation binary
78 Note also that the value returned from C<pread> should convertible to
79 a byte array, and the buffer passed to C<pwrite> is also a byte array.
81 See also: L<https://wiki.tcl.tk/1180>
85 This just documents the arguments to the callbacks in Tcl, and any
86 way that they differ from the C callbacks. In all other respects they
87 work the same way as the C callbacks, so you should go and read
96 There are no arguments or return value.
102 proc config {key value} {
106 =item C<config_complete>
110 There are no arguments or return value.
116 proc plugin_open {readonly} {
121 The C<readonly> flag is a boolean.
123 You can return any Tcl string or object as the handle. It is passed
124 back to subsequent calls.
126 =item C<plugin_close>
130 proc plugin_close {h} {
134 After C<plugin_close> returns, the reference count of the handle is
135 decremented in the C part, which usually means that the handle and its
136 contents will be garbage collected.
143 set size .. the size of the disk ..
147 This returns the size of the disk.
157 Return a boolean indicating whether the disk is writable.
167 Return a boolean indicating whether flush can be performed.
169 =item C<is_rotational>
173 proc is_rotational {h} {
177 Return a boolean indicating whether the disk is rotational.
187 Return a boolean indicating whether trim/discard can be performed.
193 proc pread {h count offset} {
194 # Construct a buffer of length $count bytes and return it.
198 The body of your C<pread> function should construct a buffer of length
199 (at least) C<$count> bytes. You should read C<$count> bytes from the
200 disk starting at C<$offset>.
202 NBD only supports whole reads, so your function should try to read the
203 whole region (perhaps requiring a loop). If the read fails or is
204 partial, your function should call C<error>.
210 proc pwrite {h buf offset} {
214 The body of your C<pwrite> function should write the C<$buf> string to
215 the disk. You should write C<$count> bytes to the disk starting at
218 NBD only supports whole writes, so your function should try to write
219 the whole region (perhaps requiring a loop). If the write fails or is
220 partial, your function should call C<error>.
222 =item C<plugin_flush>
226 proc plugin_flush {h} {
230 The body of your C<plugin_flush> function should do a L<sync(2)> or
231 L<fdatasync(2)> or equivalent on the backing store.
237 proc trim {h count offset} {
241 The body of your C<trim> function should "punch a hole" in the backing
248 proc zero {h count offset may_trim} {
252 The body of your C<zero> function should ensure that C<$count> bytes
253 of the disk, starting at C<$offset>, will read back as zero. If
254 C<$may_trim> is true, the operation may be optimized as a trim as long
255 as subsequent reads see zeroes.
257 NBD only supports whole writes, so your function should try to write
258 the whole region (perhaps requiring a loop). If the write fails or is
259 partial, your function should call C<error>.
263 =head2 Missing callbacks
267 =item Missing: C<load>, C<unload>, C<name>, C<version>, C<longname>,
268 C<description>, C<config_help>, C<can_zero>, C<can_fua>, C<can_cache>,
271 These are not yet supported.
277 The thread model for Tcl callbacks currently cannot be set from Tcl.
278 It is hard-coded in the C part to
279 C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>. This may change or be
286 =item F<$plugindir/nbdkit-tcl-plugin.so>
290 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
296 C<nbdkit-tcl-plugin> first appeared in nbdkit 1.4.