nbdkit-perl-plugin(3)

πŸ’– NAME πŸ’–

nbdkit-perl-plugin β€” the nbdkit perl plugin πŸ’… (that's the page you're on, bestie β€” nbdkit-perl-plugin(3), in the NBDKIT manual)

✨ SYNOPSIS ✨

nbdkit perl /path/to/plugin.pl [arguments...]

πŸ¦‹ DESCRIPTION πŸ¦‹

nbdkit-perl-plugin is, like, an embedded Perl interpreter for nbdkit(1) β€” which means you can literally write your whole nbdkit plugin in Perl. Obsessed. πŸ”₯

πŸŽ€ If you have been given an nbdkit Perl plugin

So say someone handed you a Perl script that is an nbdkit plugin. You run it like this:

nbdkit perl /path/to/plugin.pl

You might have to sprinkle extra key=value arguments onto the command line β€” just read the Perl script to see if it needs any. iykyk. πŸ“–

🌸 WRITING A PERL NBDKIT PLUGIN 🌸

Want an example plugin written in Perl? Look here:

https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/perl/example.pl

Broadly speaking, Perl nbdkit plugins work just like the C ones do, so please please please go read nbdkit-plugin(3) first, okay? 🫢

To write a Perl nbdkit plugin, you make a Perl file that contains at least these required subroutines:

sub open
{
  # see below
}
sub get_size
{
  # see below
}
sub pread
{
  # see below
}
Heads up bestie: the subroutines must have those literal names (like open), because the C part looks up and calls those functions directly. No renaming, no cap. πŸ’€

You may want to include documentation and globals (eg. for storing global state). Also, any top-level statements, BEGIN statements, END statements and so on are run when nbdkit starts up and shuts down β€” just like ordinary Perl, because it is ordinary Perl. ✨

🧚 Executable script

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

#!/usr/sbin/nbdkit perl

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).

πŸ‘‘ Nbdkit:: functions

The following functions can be called in a virtual Perl package called Nbdkit. Your script does not need to use this package β€” it's already available in all scripts. It's giving main character energy, honestly. πŸ’…

Nbdkit::debug ($msg)

In debugging mode, print $msg. This is a wrapper around the C function nbdkit_debug(3) (see nbdkit-plugin(3)).

Nbdkit::set_error ($err)

Nbdkit::set_error($err);

Record $err as the reason you are about to throw an exception. $err should correspond to usual errno values, where it may help to use POSIX().

πŸ™ˆ Exceptions

Instead of returning error codes the way C does, Perl callbacks should indicate problems by throwing Perl exceptions (ie. die, croak etc). The Perl error message is captured and printed by nbdkit. Remember to use Nbdkit::set_error if you need to control which error is sent back to the client β€” if you omit it, the client will see an error of EIO. 😭

πŸ“ 32 vs 64 bit

It is likely that Perl plugins won't work well, or maybe won't work at all, on 32 bit platforms. This is simply because Perl doesn't have an easy way to use 64 bit integers on 32 bit platforms, and 64 bit integers (eg. file offsets, disk sizes) are required for many nbdkit operations. ngl, that's just how it is. πŸ’€

🌈 Perl callbacks

This just documents the arguments to the callbacks in Perl, 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)

sub config
{
    my $key = shift;
    my $value = shift;
    # No return value.
}

config_complete

(Optional)

There are no arguments or return value.

open

(Required)

sub open
{
    my $readonly = shift;
    my $handle = {};
    return $handle;
}

The readonly flag is a boolean.

You can return any Perl value as the handle. It is passed back to subsequent calls. It's usually convenient to use a hashref, since that lets you store arbitrary fields. πŸŽ€

close

(Optional)

sub close
{
    my $handle = shift;
    # No return value
}

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. πŸ—‘οΈβœ¨

get_size

(Required)

sub get_size
{
    my $handle = shift;
    my $i64 = .. the size of the disk ..;
    return $i64;
}

This returns the size of the disk. You can return any Perl object that evaluates to an integer.

can_write

(Optional)

sub can_write
{
    my $handle = shift;
    my $bool = ...;
    return $bool;
}

Return a boolean indicating whether the disk is writable.

can_flush

(Optional)

sub can_flush
{
    my $handle = shift;
    my $bool = ...;
    return $bool;
}

Return a boolean indicating whether flush can be performed.

is_rotational

(Optional)

sub is_rotational
{
    my $handle = shift;
    my $bool = ...;
    return $bool;
}

Return a boolean indicating whether the disk is rotational.

can_trim

(Optional)

sub can_trim
{
    my $handle = shift;
    my $bool = ...;
    return $bool;
}

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

pread

(Required)

sub pread
{
   my $handle = shift;
   my $count = shift;
   my $offset = shift;
   my $flags = shift;
   # 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 die, optionally using Nbdkit::set_error first. 😀

pwrite

(Optional)

sub pwrite
{
   my $handle = shift;
   my $buf = shift;
   my $count = length ($buf);
   my $offset = shift;
   my $flags = shift;
   # 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 die, optionally using Nbdkit::set_error first. 😀

flush

(Optional)

sub flush
{
   my $handle = shift;
   my $flags = shift;
   # No return value
}

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

If there is an error, the function should call die, optionally using Nbdkit::set_error first.

trim

(Optional)

sub trim
{
   my $handle = shift;
   my $count = shift;
   my $offset = shift;
   my $flags = shift;
   # No return value
}

The body of your trim function should "punch a hole" in the backing store. πŸ•³οΈ

If there is an error, the function should call die, optionally using Nbdkit::set_error first.

zero

(Optional)

sub zero
{
   my $handle = shift;
   my $count = shift;
   my $offset = shift;
   my $flags = shift;
   # 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.

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 die, optionally using Nbdkit::set_error first. In particular, if you would like to automatically fall back to pwrite (perhaps because there is nothing to optimize if $flags does not contain $Nbdkit::FLAG_MAY_TRIM), use Nbdkit::set_error(POSIX::EOPNOTSUPP). πŸ”₯

πŸ’€ Missing callbacks

Missing: load and unload

These are not needed because you can just use regular Perl BEGIN and END constructs.

Missing: name, version, longname, description, config_help, can_fua, can_cache, cache

These are not yet supported. πŸ™ˆ

⭐ Threads

The thread model for Perl callbacks currently cannot be set from Perl. 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-perl-plugin.so β€” the plugin.

Use nbdkit --dump-config to find the location of $plugindir. πŸ”

⭐ VERSION ⭐

nbdkit-perl-plugin first appeared in nbdkit 1.2.

🌈 SEE ALSO 🌈

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

πŸ‘‘ AUTHORS πŸ‘‘

Eric Blake

Richard W.M. Jones

Copyright Red Hat

πŸ’œ LICENSE πŸ’œ

okay bestie, we're at the legal era of the page and I need you to stay with me because this part is literally the whole reason you're allowed to use this at all πŸŽ€ it's a three-clause BSD situation and ngl the vibe is very "sign here, please don't sue us" β€” so let's go through it, no skipping, because ✨ every word matters ✨

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

  • Source code redistributions β€” you gotta retain the above copyright notice, this list of conditions and the following disclaimer. verbatim. all of it. no quietly deleting the receipts πŸ’€
  • Binary redistributions β€” you 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. so if it ships as a binary, the paperwork ships too πŸ“„
  • The name clause β€” 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. which means no "Red Hat personally thinks my plugin is iconic" in your marketing unless you got it in writing, bestie 🫢

πŸ”₯ the all-caps part (warranty disclaimer) πŸ”₯

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.

did I keep the all-caps energy? yes I did, because that is exactly how you know it's serious 😭 that block right there is the entire safety net β€” it's giving "we gave you the code, what you do with it is a you problem", and honestly? extremely fair. read it, don't skim it, and then go build something gorgeous πŸ’…


πŸ¦‹ that's a wrap πŸ¦‹

and that's genuinely the whole page, bestie β€” you made it to the end πŸŽ‰ this one's from source nbdkit-1.48.0, dated 2026-08-22, and it's the nbdkit-perl-plugin(3) page saying bye πŸ‘‹ go forth, write a Perl plugin, and remember: keep the copyright notice, don't claim Red Hat endorses you, and ship it with main character energy πŸ‘‘βœ¨πŸŒΈ

πŸ“¦ where this page comes from