nbdkit-tcl-plugin(3)
π nbdkit-tcl-plugin(3) β the NBDKIT manual, bestie edition π
okay so this is nbdkit-tcl-plugin(3), part of the NBDKIT manual set β and no cap, it's giving "write your own nbdkit plugin in Tcl" main character energy. let's get into it π
β¨ NAME β¨
nbdkit-tcl-plugin β the nbdkit Tcl plugin. that's it, that's the name π¦
β¨ SYNOPSIS β¨
nbdkit tcl /path/to/plugin.tcl [arguments...]
πΈ DESCRIPTION πΈ
nbdkit-tcl-plugin is literally an embedded Tcl interpreter for nbdkit(1), which lets you write nbdkit plugins in Tcl. obsessed. π«Ά
π if you have been given an nbdkit Tcl plugin
so like, assuming you've been handed a Tcl script that 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 β iykyk, the script tells you everything π
π¦ WRITING A TCL NBDKIT PLUGIN π¦
for an example plugin written in Tcl, peep this:
https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/tcl/example.tcl
broadly speaking, Tcl nbdkit plugins work like the C ones, so you should read nbdkit-plugin(3) first. fr fr, read it 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. no renaming, no vibes-based naming, literal names only π
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. simple as that π
π Binary data π
when writing your Tcl script, be careful to ensure that it is processing binary data (not Unicode). if you're reading and writing from local disk files, you should use:
fconfigure $fp -translation binary
also note that the value returned from pread should be convertible to a byte array, and the buffer passed to pwrite is also a byte array. like, byte array both ways, periodt β¨
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). go on, we'll wait π«Ά
π
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. iconic β¨
π
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. bye bestie π
π
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. no partial sends, no cap π«
π
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. flush it, bestie π§
π
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. they're on the "we don't have it yet" list, not the "we secretly have it" list π
β 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. so like, not your call rn π
πΈ FILES πΈ
$plugindir/nbdkit-tcl-plugin.soβ the plugin. usenbdkit --dump-configto find the location of$plugindir. π
π VERSION π
nbdkit-tcl-plugin first appeared in nbdkit 1.4. vintage, honestly π¦
π SEE ALSO π
(go read both, they're the prerequisites, not optional reading, i'm so serious π«Ά)
π AUTHORS π
Richard W.M. Jones
π COPYRIGHT π
Copyright Red Hat
π LICENSE π
redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- neither the name of Red Hat nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
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.
and that's the whole page babes π
this cutie was served up as nbdkit-1.48.0, dated 2026-08-22, and it's the nbdkit-tcl-plugin(3) manual page β go forth and write the most iconic Tcl plugin the world has ever seen πβ¨π¦
π¦ where this page comes from
- ubuntu stonking: nbdkit-plugin-tcl 1.48.0-1ubuntu2 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms