Update Red Hat Copyright Notices
[nbdkit.git] / plugins / perl / nbdkit-perl-plugin.pod
blob89a9b10ef590f392f30c204535acf9e8f58ea755
1 =head1 NAME
3 nbdkit-perl-plugin - nbdkit perl plugin
5 =head1 SYNOPSIS
7  nbdkit perl /path/to/plugin.pl [arguments...]
9 =head1 DESCRIPTION
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
17 like this:
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:
35  sub open
36  {
37    # see below
38  }
39  sub get_size
40  {
41    # see below
42  }
43  sub pread
44  {
45    # see below
46  }
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"
58 at the top:
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
84 C<use POSIX()>.
86 =head2 Exceptions
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
93 error of C<EIO>.
95 =head2 32 vs 64 bit
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
101 operations.
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
108 L<nbdkit-plugin(3)>.
110 =over 4
112 =item C<dump_plugin>
114 (Optional)
116 There are no arguments or return value.
118 =item C<config>
120 (Optional)
122  sub config
124      my $key = shift;
125      my $value = shift;
126      # No return value.
129 =item C<config_complete>
131 (Optional)
133 There are no arguments or return value.
135 =item C<open>
137 (Required)
139  sub open
141      my $readonly = shift;
142      my $handle = {};
143      return $handle;
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.
152 =item C<close>
154 (Optional)
156  sub close
158      my $handle = shift;
159      # No return value
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.
166 =item C<get_size>
168 (Required)
170  sub get_size
172      my $handle = shift;
173      my $i64 = .. the size of the disk ..;
174      return $i64;
177 This returns the size of the disk.  You can return any Perl object
178 that evaluates to an integer.
180 =item C<can_write>
182 (Optional)
184  sub can_write
186      my $handle = shift;
187      my $bool = ...;
188      return $bool;
191 Return a boolean indicating whether the disk is writable.
193 =item C<can_flush>
195 (Optional)
197  sub can_flush
199      my $handle = shift;
200      my $bool = ...;
201      return $bool;
204 Return a boolean indicating whether flush can be performed.
206 =item C<is_rotational>
208 (Optional)
210  sub is_rotational
212      my $handle = shift;
213      my $bool = ...;
214      return $bool;
217 Return a boolean indicating whether the disk is rotational.
219 =item C<can_trim>
221 (Optional)
223  sub can_trim
225      my $handle = shift;
226      my $bool = ...;
227      return $bool;
230 Return a boolean indicating whether trim/discard can be performed.
232 =item C<pread>
234 (Required)
236  sub pread
238     my $handle = shift;
239     my $count = shift;
240     my $offset = shift;
241     my $flags = shift;
242     # Construct a buffer of length $count bytes and return it.
243     return $buf;
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.
255 =item C<pwrite>
257 (Optional)
259  sub pwrite
261     my $handle = shift;
262     my $buf = shift;
263     my $count = length ($buf);
264     my $offset = shift;
265     my $flags = shift;
266     # No return value
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
271 C<$offset>.
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.
278 =item C<flush>
280 (Optional)
282  sub flush
284     my $handle = shift;
285     my $flags = shift;
286     # No return value
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.
295 =item C<trim>
297 (Optional)
299  sub trim
301     my $handle = shift;
302     my $count = shift;
303     my $offset = shift;
304     my $flags = shift;
305     # No return value
308 The body of your C<trim> function should "punch a hole" in the backing
309 store.
311 If there is an error, the function should call C<die>, optionally using
312 C<Nbdkit::set_error> first.
314 =item C<zero>
316 (Optional)
318  sub zero
320     my $handle = shift;
321     my $count = shift;
322     my $offset = shift;
323     my $flags = shift;
324     # No return value
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)>.
338 =back
340 =head2 Missing callbacks
342 =over 4
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.
354 =back
356 =head2 Threads
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
361 settable in future.
363 =head1 FILES
365 =over 4
367 =item F<$plugindir/nbdkit-perl-plugin.so>
369 The plugin.
371 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
373 =back
375 =head1 VERSION
377 C<nbdkit-perl-plugin> first appeared in nbdkit 1.2.
379 =head1 SEE ALSO
381 L<nbdkit(1)>,
382 L<nbdkit-plugin(3)>,
383 L<perl(1)>.
385 =head1 AUTHORS
387 Eric Blake
389 Richard W.M. Jones
391 =head1 COPYRIGHT
393 Copyright Red Hat