nbdkit-lua-plugin(3)
π¦ nbdkit-lua-plugin(3) β the nbdkit Lua plugin, bestie edition
okay so real talk: this is the manual page for nbdkit-lua-plugin from the nbdkit manual, and it's genuinely iconic β it's the embedded Lua interpreter that lets you write nbdkit plugins in Lua. No cap. Let's get into it π
β¨ SYNOPSIS β¨
nbdkit lua /path/to/plugin.lua [arguments...]
πΈ DESCRIPTION πΈ
nbdkit-lua-plugin is an embedded Lua interpreter for nbdkit(1), and it's literally the whole reason you can write nbdkit plugins in Lua. Obsessed.
π If you have been given an nbdkit Lua plugin
So like, assuming you've been handed a Lua script that is an nbdkit plugin, you run it like this:
nbdkit lua /path/to/plugin.lua
You may have to add further key=value arguments to the command line. Read the Lua script to see if it requires any β iykyk, the script will tell you what it needs π
π§ WRITING A LUA NBDKIT PLUGIN π§
For an example plugin written in Lua, see: https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/lua/example.lua
Broadly speaking, Lua nbdkit plugins work like C ones, so bestie, you should read nbdkit-plugin(3) first. Fr fr, go read it.
To write a Lua nbdkit plugin, you create a Lua file which contains at least the following required functions:
function open (readonly)
-- see below
return h
end
function get_size (h)
-- see below
return size
end
function pread (h, count, offset)
-- see below
return buf
end
Note that the subroutines must have those literal names (like open), because the C part looks up and calls those functions directly. Like, it's not gonna guess, okay? 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 lua
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). Slay.
π Errors
Lua plugin methods can indicate an error by calling error or assert. The error message will contain the method name, filename and line number where the error occurred, eg:
error ("could not open " .. filename)
--> nbdkit: error: open: myplugin.lua:123: could not open disk.img
Not me getting a full stack trace in my error message π β it's giving helpful, honestly.
π Lua callbacks
This just documents the arguments to the callbacks in Lua, 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). Periodt.
dump_plugin
(Optional)
There are no arguments or return value.
config
(Optional)
function config (key, value)
-- No return value.
end
config_complete
(Optional)
There are no arguments or return value.
open
(Required)
function open (readonly)
local handle
handle=...
return handle
end
The readonly flag is a boolean.
You can return any Lua string or object as the handle. It is passed back to subsequent calls. π«Ά
close
(Optional)
function close (h)
-- No return value
end
After 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)
function get_size (h)
local size
size= .. the size of the disk ..
return size
end
This returns the size of the disk.
can_write
(Optional)
function can_write (h)
return bool
end
Return a boolean indicating whether the disk is writable.
can_flush
(Optional)
function can_flush (h)
return bool
end
Return a boolean indicating whether flush can be performed.
is_rotational
(Optional)
function is_rotational (h)
return bool
end
Return a boolean indicating whether the disk is rotational.
can_trim
(Optional)
function can_trim (h)
return bool
end
Return a boolean indicating whether trim/discard can be performed.
pread
(Required)
function pread (h, count, offset)
-- Construct a buffer of length count bytes and return it.
return buf
end
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 half-reads, okay? π
pwrite
(Optional)
function pwrite (h, buf, offset)
-- No return value
end
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.
flush
(Optional)
function flush (h)
-- No return value
end
The body of your flush function should do a sync(2) or fdatasync(2) or equivalent on the backing store. Get those writes on disk, bestie β¨
trim
(Optional)
function trim (h, count, offset)
-- No return value
end
The body of your trim function should "punch a hole" in the backing store.
zero
(Optional)
function zero (h, count, offset, may_trim)
-- No return value
end
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 read, okay? π
π§΅ Threads
The thread model for Lua callbacks currently cannot be set from Lua. It is hard-coded in the C part to NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS. This may change or be settable in future. It's giving "we'll see" energy, ngl.
π FILES π
$plugindir/nbdkit-lua-plugin.soβ the plugin. Usenbdkit --dump-configto find the location of$plugindir.
π VERSION π
nbdkit-lua-plugin first appeared in nbdkit 1.6. A true icon of its time π
π SEE ALSO π
π 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.
lowkey the legal bit is the only part that isn't giving group chat, but we keep it exact because that's how licenses work π
and that's the whole page, bestie π«Ά this version dropped as part of nbdkit-1.48.0, dated 2026-08-22 β go write something iconic with it β¨π
π¦ where this page comes from
- ubuntu stonking: nbdkit-plugin-lua 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