nbdkit-tcl-plugin - Man Page

nbdkit Tcl plugin

Synopsis

 nbdkit tcl /path/to/plugin.tcl [arguments...]

Description

nbdkit-tcl-plugin is an embedded Tcl interpreter for nbdkit(1), allowing you to write nbdkit plugins in Tcl.

If you have been given an nbdkit Tcl plugin

Assuming you have a Tcl script which is an nbdkit plugin, you run it like this:

 nbdkit tcl /path/to/plugin.tcl

You may have to add further key=value arguments to the command line.  Read the Tcl script to see if it requires any.

Writing a Tcl Nbdkit Plugin

For an example plugin written in Tcl, see: https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/tcl/example.tcl

Broadly speaking, Tcl nbdkit plugins work like C ones, so you should read nbdkit-plugin(3) first.

To write a Tcl nbdkit plugin, you create a Tcl file which contains at least the following required subroutines:

 proc plugin_open {readonly} {
     # see below
     return $h
 }
 proc get_size {h} {
     # see below
     return $size
 }
 proc pread {h count offset} {
     # see below
     return $buf
 }

Note that the subroutines must have those literal names (like plugin_open), because the C part looks up and calls those functions directly.  You may want to include documentation and globals (eg. for storing global state).  Also any top-level statements are run when nbdkit starts up.

Executable script

If you want you can make the script executable and include a "shebang" at the top:

 #!/usr/sbin/nbdkit tcl

See also "Shebang scripts" in nbdkit(1).

These scripts can also be installed in the $plugindir.  See "WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES" in nbdkit-plugin(3).

Exceptions

Tcl plugin methods can indicate an error by calling error.

Binary data

When writing your Tcl script, be careful to ensure that it is processing binary data (not Unicode).  If reading and writing from local disk files, you should use:

 fconfigure $fp -translation binary

Note also that the value returned from pread should convertible to a byte array, and the buffer passed to pwrite is also a byte array.

See also: https://wiki.tcl.tk/1180

Tcl callbacks

This just documents the arguments to the callbacks in Tcl, and any way that they differ from the C callbacks.  In all other respects they work the same way as the C callbacks, so you should go and read nbdkit-plugin(3).

dump_plugin

(Optional)

There are no arguments or return value.

config

(Optional)

 proc config {key value} {
     # No return value.
 }
config_complete

(Optional)

There are no arguments or return value.

plugin_open

(Required)

 proc plugin_open {readonly} {
     set handle ...
     return $handle
 }

The readonly flag is a boolean.

You can return any Tcl string or object as the handle.  It is passed back to subsequent calls.

plugin_close

(Optional)

 proc plugin_close {h} {
     # No return value
 }

After plugin_close returns, the reference count of the handle is decremented in the C part, which usually means that the handle and its contents will be garbage collected.

get_size

(Required)

 proc get_size {h} {
     set size .. the size of the disk ..
     return $size
 }

This returns the size of the disk.

can_write

(Optional)

 proc can_write {h} {
     return $bool
 }

Return a boolean indicating whether the disk is writable.

can_flush

(Optional)

 proc can_flush {h} {
     return $bool
 }

Return a boolean indicating whether flush can be performed.

is_rotational

(Optional)

 proc is_rotational {h} {
     return $bool
 }

Return a boolean indicating whether the disk is rotational.

can_trim

(Optional)

 proc can_trim {h} {
     return $bool
 }

Return a boolean indicating whether trim/discard can be performed.

pread

(Required)

 proc pread {h count offset} {
    # Construct a buffer of length $count bytes and return it.
    return $buf
 }

The body of your pread function should construct a buffer of length (at least) $count bytes.  You should read $count bytes from the disk starting at $offset.

NBD only supports whole reads, so your function should try to read the whole region (perhaps requiring a loop).  If the read fails or is partial, your function should call error.

pwrite

(Optional)

 proc pwrite {h buf offset} {
    # No return value
 }

The body of your pwrite function should write the $buf string to the disk.  You should write $count bytes to the disk starting at $offset.

NBD only supports whole writes, so your function should try to write the whole region (perhaps requiring a loop).  If the write fails or is partial, your function should call error.

plugin_flush

(Optional)

 proc plugin_flush {h} {
     # No return value
 }

The body of your plugin_flush function should do a sync(2) or fdatasync(2) or equivalent on the backing store.

trim

(Optional)

 proc trim {h count offset} {
     # No return value
 }

The body of your trim function should "punch a hole" in the backing store.

zero

(Optional)

 proc zero {h count offset may_trim} {
    # No return value
 }

The body of your zero function should ensure that $count bytes of the disk, starting at $offset, will read back as zero.  If $may_trim is true, the operation may be optimized as a trim as long as subsequent reads see zeroes.

NBD only supports whole writes, so your function should try to write the whole region (perhaps requiring a loop).  If the write fails or is partial, your function should call error.

Missing callbacks

Missing: load, unload, name, version, longname, description, config_help, can_zero, can_fua, can_cache, cache

These are not yet supported.

Threads

The thread model for Tcl callbacks currently cannot be set from Tcl. It is hard-coded in the C part to NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS.  This may change or be settable in future.

Files

$plugindir/nbdkit-tcl-plugin.so

The plugin.

Use nbdkit --dump-config to find the location of $plugindir.

Version

nbdkit-tcl-plugin first appeared in nbdkit 1.4.

See Also

nbdkit(1), nbdkit-plugin(3).

Authors

Richard W.M. Jones

License

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Referenced By

nbdkit(1), nbdkit-plugin(3), nbdkit-release-notes-1.4(1).

2024-10-14 nbdkit-1.41.8