3 nbdkit-perl-plugin - nbdkit perl plugin
7 nbdkit perl /path/to/plugin.pl [arguments...]
11 C<nbdkit-perl-plugin> is an embedded Perl interpreter for
12 L<nbdkit(1)>, allowing you to write nbdkit plugins in Perl.
14 =head2 If you have been given an nbdkit Perl plugin
16 Assuming you have a Perl script which is an nbdkit plugin, you run it
19 nbdkit perl /path/to/plugin.pl
21 You may have to add further C<key=value> arguments to the command
22 line. Read the Perl script to see if it requires any.
24 =head1 WRITING A PERL NBDKIT PLUGIN
26 For an example plugin written in Perl, see:
27 L<https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/perl/example.pl>
29 Broadly speaking, Perl nbdkit plugins work like C ones, so you should
30 read L<nbdkit-plugin(3)> first.
32 To write a Perl nbdkit plugin, you create a Perl file which contains
33 at least the following required subroutines:
48 Note that the subroutines must have those literal names (like
49 C<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, C<BEGIN>
52 statements, C<END> statements and so on are run when nbdkit starts up
53 and shuts down, just like ordinary Perl.
55 =head2 Executable script
57 If you want you can make the script executable and include a "shebang"
60 #!/usr/sbin/nbdkit perl
62 See also L<nbdkit(1)/Shebang scripts>.
64 These scripts can also be installed in the C<$plugindir>. See
65 L<nbdkit-plugin(3)/WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES>.
67 =head2 C<Nbdkit::> functions
69 The following functions can be called in a virtual Perl package called
70 C<Nbdkit>. Your script does not need to "use" this package, it is
71 already available in all scripts.
73 =head3 C<Nbdkit::debug ($msg)>
75 In debugging mode, print C<$msg>. This is a wrapper around the C
76 function C<nbdkit_debug> (see L<nbdkit-plugin(3)>).
78 =head3 C<Nbdkit::set_error ($err)>
80 Nbdkit::set_error($err);
82 Record C<$err> as the reason you are about to throw an exception.
83 C<$err> should correspond to usual errno values, where it may help to
88 Instead of returning error codes as in C, Perl callbacks should
89 indicate problems by throwing Perl exceptions (ie. C<die>, C<croak>
90 etc). The Perl error message is captured and printed by nbdkit.
91 Remember to use C<Nbdkit::set_error> if you need to control which
92 error is sent back to the client; if omitted, the client will see an
97 It is likely that Perl plugins won't work well, or maybe won't work at
98 all, on 32 bit platforms. This is simply because Perl doesn't have an
99 easy way to use 64 bit integers on 32 bit platforms, and 64 bit
100 integers (eg. file offsets, disk sizes) are required for many nbdkit
103 =head2 Perl callbacks
105 This just documents the arguments to the callbacks in Perl, and any
106 way that they differ from the C callbacks. In all other respects they
107 work the same way as the C callbacks, so you should go and read
116 There are no arguments or return value.
129 =item C<config_complete>
133 There are no arguments or return value.
141 my $readonly = shift;
146 The C<readonly> flag is a boolean.
148 You can return any Perl value as the handle. It is passed back to
149 subsequent calls. It's usually convenient to use a hashref, since
150 that lets you store arbitrary fields.
162 After C<close> returns, the reference count of the handle is
163 decremented in the C part, which usually means that the handle and its
164 contents will be garbage collected.
173 my $i64 = .. the size of the disk ..;
177 This returns the size of the disk. You can return any Perl object
178 that evaluates to an integer.
191 Return a boolean indicating whether the disk is writable.
204 Return a boolean indicating whether flush can be performed.
206 =item C<is_rotational>
217 Return a boolean indicating whether the disk is rotational.
230 Return a boolean indicating whether trim/discard can be performed.
242 # Construct a buffer of length $count bytes and return it.
246 The body of your C<pread> function should construct a buffer of length
247 (at least) C<$count> bytes. You should read C<$count> bytes from the
248 disk starting at C<$offset>.
250 NBD only supports whole reads, so your function should try to read the
251 whole region (perhaps requiring a loop). If the read fails or is
252 partial, your function should C<die>, optionally using
253 C<Nbdkit::set_error> first.
263 my $count = length ($buf);
269 The body of your C<pwrite> function should write the C<$buf> string to
270 the disk. You should write C<$count> bytes to the disk starting at
273 NBD only supports whole writes, so your function should try to write
274 the whole region (perhaps requiring a loop). If the write fails or is
275 partial, your function should C<die>, optionally using
276 C<Nbdkit::set_error> first.
289 The body of your C<flush> function should do a L<sync(2)> or
290 L<fdatasync(2)> or equivalent on the backing store.
292 If there is an error, the function should call C<die>, optionally using
293 C<Nbdkit::set_error> first.
308 The body of your C<trim> function should "punch a hole" in the backing
311 If there is an error, the function should call C<die>, optionally using
312 C<Nbdkit::set_error> first.
327 The body of your C<zero> function should ensure that C<$count> bytes
328 of the disk, starting at C<$offset>, will read back as zero.
330 NBD only supports whole writes, so your function should try to write
331 the whole region (perhaps requiring a loop). If the write fails or is
332 partial, your function should C<die>, optionally using
333 C<Nbdkit::set_error> first. In particular, if you would like to
334 automatically fall back to C<pwrite> (perhaps because there is nothing
335 to optimize if C<$flags> does not contain C<$Nbdkit::FLAG_MAY_TRIM>), use
336 C<Nbdkit::set_error(POSIX::EOPNOTSUPP)>.
340 =head2 Missing callbacks
344 =item Missing: C<load> and C<unload>
346 These are not needed because you can just use regular Perl C<BEGIN>
347 and C<END> constructs.
349 =item Missing: C<name>, C<version>, C<longname>, C<description>,
350 C<config_help>, C<can_fua>, C<can_cache>, C<cache>
352 These are not yet supported.
358 The thread model for Perl callbacks currently cannot be set from Perl.
359 It is hard-coded in the C part to
360 C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>. This may change or be
367 =item F<$plugindir/nbdkit-perl-plugin.so>
371 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
377 C<nbdkit-perl-plugin> first appeared in nbdkit 1.2.