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://github.com/libguestfs/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>.
69 Your script has access to the following methods in the C<Nbdkit>
70 package (in fact, attempting to C<use Nbdkit> will fail, the methods
71 are already available):
73 Nbdkit::set_error($err);
75 Record C<$err> as the reason you are about to throw an exception. C<$err>
76 should correspond to usual errno values, where it may help to
81 Instead of returning error codes as in C, Perl callbacks should
82 indicate problems by throwing Perl exceptions (ie. C<die>, C<croak>
83 etc). The Perl error message is captured and printed by nbdkit.
84 Remember to use C<Nbdkit::set_error> if you need to control which
85 error is sent back to the client; if omitted, the client will see an
90 It is likely that Perl plugins won't work well, or maybe won't work at
91 all, on 32 bit platforms. This is simply because Perl doesn't have an
92 easy way to use 64 bit integers on 32 bit platforms, and 64 bit
93 integers (eg. file offsets, disk sizes) are required for many nbdkit
98 This just documents the arguments to the callbacks in Perl, and any
99 way that they differ from the C callbacks. In all other respects they
100 work the same way as the C callbacks, so you should go and read
109 There are no arguments or return value.
122 =item C<config_complete>
126 There are no arguments or return value.
134 my $readonly = shift;
139 The C<readonly> flag is a boolean.
141 You can return any Perl value as the handle. It is passed back to
142 subsequent calls. It's usually convenient to use a hashref, since
143 that lets you store arbitrary fields.
155 After C<close> returns, the reference count of the handle is
156 decremented in the C part, which usually means that the handle and its
157 contents will be garbage collected.
166 my $i64 = .. the size of the disk ..;
170 This returns the size of the disk. You can return any Perl object
171 that evaluates to an integer.
184 Return a boolean indicating whether the disk is writable.
197 Return a boolean indicating whether flush can be performed.
199 =item C<is_rotational>
210 Return a boolean indicating whether the disk is rotational.
223 Return a boolean indicating whether trim/discard can be performed.
234 # Construct a buffer of length $count bytes and return it.
238 The body of your C<pread> function should construct a buffer of length
239 (at least) C<$count> bytes. You should read C<$count> bytes from the
240 disk starting at C<$offset>.
242 NBD only supports whole reads, so your function should try to read the
243 whole region (perhaps requiring a loop). If the read fails or is
244 partial, your function should C<die>, optionally using
245 C<Nbdkit::set_error> first.
255 my $count = length ($buf);
260 The body of your C<pwrite> function should write the C<$buf> string to
261 the disk. You should write C<$count> bytes to the disk starting at
264 NBD only supports whole writes, so your function should try to write
265 the whole region (perhaps requiring a loop). If the write fails or is
266 partial, your function should C<die>, optionally using
267 C<Nbdkit::set_error> first.
279 The body of your C<flush> function should do a L<sync(2)> or
280 L<fdatasync(2)> or equivalent on the backing store.
282 If there is an error, the function should call C<die>, optionally using
283 C<Nbdkit::set_error> first.
297 The body of your C<trim> function should "punch a hole" in the backing
300 If there is an error, the function should call C<die>, optionally using
301 C<Nbdkit::set_error> first.
312 my $may_trim = shift;
316 The body of your C<zero> function should ensure that C<$count> bytes
317 of the disk, starting at C<$offset>, will read back as zero. If
318 C<$may_trim> is true, the operation may be optimized as a trim as long
319 as subsequent reads see zeroes.
321 NBD only supports whole writes, so your function should try to write
322 the whole region (perhaps requiring a loop). If the write fails or is
323 partial, your function should C<die>, optionally using
324 C<Nbdkit::set_error> first. In particular, if you would like to
325 automatically fall back to C<pwrite> (perhaps because there is nothing
326 to optimize if C<$may_trim> is false), use
327 C<Nbdkit::set_error(POSIX::EOPNOTSUPP)>.
331 =head2 Missing callbacks
335 =item Missing: C<load> and C<unload>
337 These are not needed because you can just use regular Perl C<BEGIN>
338 and C<END> constructs.
340 =item Missing: C<name>, C<version>, C<longname>, C<description>,
341 C<config_help>, C<can_fua>, C<can_cache>, C<cache>
343 These are not yet supported.
349 The thread model for Perl callbacks currently cannot be set from Perl.
350 It is hard-coded in the C part to
351 C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>. This may change or be
358 =item F<$plugindir/nbdkit-perl-plugin.so>
362 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
368 C<nbdkit-perl-plugin> first appeared in nbdkit 1.2.
384 Copyright (C) 2013-2018 Red Hat Inc.