File: nbdkit-perl-plugin.pod

package info (click to toggle)
nbdkit 1.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,504 kB
  • sloc: ansic: 63,658; sh: 18,717; makefile: 6,814; python: 1,848; cpp: 1,143; perl: 504; ml: 504; tcl: 62
file content (393 lines) | stat: -rw-r--r-- 8,531 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
=head1 NAME

nbdkit-perl-plugin - nbdkit perl plugin

=head1 SYNOPSIS

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

=head1 DESCRIPTION

C<nbdkit-perl-plugin> is an embedded Perl interpreter for
L<nbdkit(1)>, allowing you to write nbdkit plugins in Perl.

=head2 If you have been given an nbdkit Perl plugin

Assuming you have a Perl script which is an nbdkit plugin, you run it
like this:

 nbdkit perl /path/to/plugin.pl

You may have to add further C<key=value> arguments to the command
line.  Read the Perl script to see if it requires any.

=head1 WRITING A PERL NBDKIT PLUGIN

For an example plugin written in Perl, see:
L<https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/perl/example.pl>

Broadly speaking, Perl nbdkit plugins work like C ones, so you should
read L<nbdkit-plugin(3)> first.

To write a Perl nbdkit plugin, you create a Perl file which contains
at least the following required subroutines:

 sub open
 {
   # see below
 }
 sub get_size
 {
   # see below
 }
 sub pread
 {
   # see below
 }

Note that the subroutines must have those literal names (like
C<open>), because the C part looks up and calls those functions
directly.  You may want to include documentation and globals (eg. for
storing global state).  Also any top-level statements, C<BEGIN>
statements, C<END> statements and so on are run when nbdkit starts up
and shuts down, just like ordinary Perl.

=head2 Executable script

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

 #!/usr/sbin/nbdkit perl

See also L<nbdkit(1)/Shebang scripts>.

These scripts can also be installed in the C<$plugindir>.  See
L<nbdkit-plugin(3)/WRITING PLUGINS IN OTHER PROGRAMMING LANGUAGES>.

=head2 C<Nbdkit::> functions

The following functions can be called in a virtual Perl package called
C<Nbdkit>.  Your script does not need to "use" this package, it is
already available in all scripts.

=head3 C<Nbdkit::debug ($msg)>

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

=head3 C<Nbdkit::set_error ($err)>

 Nbdkit::set_error($err);

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

=head2 Exceptions

Instead of returning error codes as in C, Perl callbacks should
indicate problems by throwing Perl exceptions (ie. C<die>, C<croak>
etc).  The Perl error message is captured and printed by nbdkit.
Remember to use C<Nbdkit::set_error> if you need to control which
error is sent back to the client; if omitted, the client will see an
error of C<EIO>.

=head2 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.

=head2 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
L<nbdkit-plugin(3)>.

=over 4

=item C<dump_plugin>

(Optional)

There are no arguments or return value.

=item C<config>

(Optional)

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

=item C<config_complete>

(Optional)

There are no arguments or return value.

=item C<open>

(Required)

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

The C<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.

=item C<close>

(Optional)

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

After C<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.

=item C<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.

=item C<can_write>

(Optional)

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

Return a boolean indicating whether the disk is writable.

=item C<can_flush>

(Optional)

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

Return a boolean indicating whether flush can be performed.

=item C<is_rotational>

(Optional)

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

Return a boolean indicating whether the disk is rotational.

=item C<can_trim>

(Optional)

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

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

=item C<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 C<pread> function should construct a buffer of length
(at least) C<$count> bytes.  You should read C<$count> bytes from the
disk starting at C<$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 C<die>, optionally using
C<Nbdkit::set_error> first.

=item C<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 C<pwrite> function should write the C<$buf> string to
the disk.  You should write C<$count> bytes to the disk starting at
C<$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 C<die>, optionally using
C<Nbdkit::set_error> first.

=item C<flush>

(Optional)

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

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

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

=item C<trim>

(Optional)

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

The body of your C<trim> function should "punch a hole" in the backing
store.

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

=item C<zero>

(Optional)

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

The body of your C<zero> function should ensure that C<$count> bytes
of the disk, starting at C<$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 C<die>, optionally using
C<Nbdkit::set_error> first.  In particular, if you would like to
automatically fall back to C<pwrite> (perhaps because there is nothing
to optimize if C<$flags> does not contain C<$Nbdkit::FLAG_MAY_TRIM>), use
C<Nbdkit::set_error(POSIX::EOPNOTSUPP)>.

=back

=head2 Missing callbacks

=over 4

=item Missing: C<load> and C<unload>

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

=item Missing: C<name>, C<version>, C<longname>, C<description>,
C<config_help>, C<can_fua>, C<can_cache>, C<cache>

These are not yet supported.

=back

=head2 Threads

The thread model for Perl callbacks currently cannot be set from Perl.
It is hard-coded in the C part to
C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>.  This may change or be
settable in future.

=head1 FILES

=over 4

=item F<$plugindir/nbdkit-perl-plugin.so>

The plugin.

Use C<nbdkit --dump-config> to find the location of C<$plugindir>.

=back

=head1 VERSION

C<nbdkit-perl-plugin> first appeared in nbdkit 1.2.

=head1 SEE ALSO

L<nbdkit(1)>,
L<nbdkit-plugin(3)>,
L<perl(1)>.

=head1 AUTHORS

Eric Blake

Richard W.M. Jones

=head1 COPYRIGHT

Copyright Red Hat