Update Red Hat Copyright Notices
[nbdkit.git] / plugins / sh / nbdkit-sh-plugin.pod
blobb2c946a05fdf3b3b1dac849c1c2be68721f9093b
1 =head1 NAME
3 nbdkit-sh-plugin - nbdkit shell, script or executable plugin
5 =head1 SYNOPSIS
7  nbdkit sh /path/to/script [arguments...]
9 =for paragraph
11  nbdkit sh - <<'EOF'
12  ... shell script ...
13  EOF
15 =head1 DESCRIPTION
17 C<nbdkit-sh-plugin> allows you to write plugins for L<nbdkit(1)> using
18 arbitrary scripting languages, including shells like L<bash(1)>,
19 L<dash(1)>, L<csh(1)>, L<zsh(1)> etc., other scripting environments,
20 or any executable.
22 Note if you want to use an established scripting language like Perl or
23 Python, then nbdkit has specific plugins to handle those languages and
24 those will be more efficient (see L<nbdkit(1)> for a complete list).
26 To use shell script fragments from the nbdkit command line (rather
27 than a separate script) see L<nbdkit-eval-plugin(1)>.
29 =head2 If you have been given an nbdkit sh plugin
31 Assuming you have a shell script which is an nbdkit plugin, you run it
32 like this:
34  nbdkit sh /path/to/script
36 You may have to add further C<key=value> arguments to the command
37 line.  The script must be executable (C<chmod +x>).
39 =head2 Inline shell scripts
41 It is also possible to write a shell script plugin "inline" using C<->
42 as the name of the script, like this:
44  nbdkit sh - <<'EOF'
45    case "$1" in
46      get_size) echo 1M ;;
47      pread) dd if=/dev/zero count=$3 iflag=count_bytes ;;
48      *) exit 2 ;;
49    esac
50  EOF
52 By default the inline script runs under F</bin/sh>.  You can add a
53 shebang (C<#!>) to use other scripting languages.  Of course, reading
54 an inline script from stdin is incompatible with the I<-s>
55 (I<--single>) mode of nbdkit that connects a client on stdin.
57 =head1 WRITING AN NBDKIT SH PLUGIN
59 For an example plugin written in Bash, see:
60 L<https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/sh/example.sh>
62 Broadly speaking, nbdkit shell plugins work like C ones, so you should
63 read L<nbdkit-plugin(3)> first.
65 =head2 Programming model
67 This plugin has a simple programming model: For every plugin method
68 that needs to be called, the external script is invoked with
69 parameters describing the method and its arguments.  The first
70 parameter is always the method name.  For example:
72  /path/to/script config file disk.img
73                    │      │   │
74                    │      │   └─ value ($3)
75                    │      └── key ($2)
76                method ($1)
78 =for paragraph
80  /path/to/script pread <handle> <count> <offset>
81                    │       │       │       │
82                    │       │       │       └─ offset in bytes ($4)
83                    │       │       └── request size in bytes ($3)
84                method ($1) └── handle ($2) ─ see "Handles" below
86 Scripts should ignore extra parameters that they don't understand
87 since we may add new parameters in future.
89 =head2 Exit codes
91 The script should exit with specific exit codes:
93 =over 4
95 =item S<0>
97 The method was executed successfully.
99 =item 1 and 16-255
101 There was an error.  The script may print on stderr an errno name,
102 optionally followed by whitespace and a message, for example:
104  echo 'ENOSPC Out of space' >&2
105  exit 1
107 or if you don't need the log message:
109  echo ENOSPC >&2
110  exit 1
112 If the script doesn't print anything or the output cannot be parsed
113 then nbdkit assumes error C<EIO>.  Note that output to stderr is
114 ignored if the command succeeds, so it is acceptable to output a
115 potential error message prefix prior to attempting a command which
116 will add further details if a failure occurs.
118 =item S<2>
120 The requested method is not supported by the script.
122 =item S<3>
124 For methods which return booleans, this code indicates false.
126 =item 4 and 5
128 Triggers a call to the C function C<nbdkit_shutdown>, which requests
129 an asynchronous exit of the nbdkit server (disconnecting all clients).
130 The client will usually get a response before shutdown is complete
131 (although this is racy); so once the shutdown is requested, code 4
132 then behaves like code 0 (stderr is ignored, and the server tries to
133 return success), and code 5 behaves like code 1 (the server tries to
134 return an error to the client parsed from stderr, although a missing
135 error defaults to C<ESHUTDOWN> instead of C<EIO>).
137 =item S<6>
139 Triggers a call to the C function C<nbdkit_disconnect> with C<force>
140 set to true, which requests an abrupt disconnect of the current
141 client.  The contents of stderr are irrelevant with this status, since
142 the client will not get a response.
144 =item 7 and 8
146 Triggers a call to the C function C<nbdkit_disconnect> with C<force>
147 set to false, which requests a soft disconnect of the current client
148 (future client requests are rejected with C<ESHUTDOWN> without calling
149 into the plugin, but current requests may complete).  Since the client
150 will likely get the response to this command, code 7 then behaves like
151 code 0 (stderr is ignored, and the server tries to return success),
152 and code 8 behaves like code 1 (the server tries to return an error to
153 the client parsed from stderr, although a missing error defaults to
154 C<ESHUTDOWN> instead of C<EIO>).
156 =item 9-15
158 These exit codes are reserved for future use.  Note that versions of
159 nbdkit E<lt> 1.34 documented that codes 8 through 15 behaved like code
160 1; although it is unlikely that many scripts relied on this similarity
161 in practice.
163 =back
165 In nbdkit E<gt> 1.34, it is possible to probe whether additional exit
166 codes have been assigned meaning, by looking for the line
167 B<max_known_status=> in the output of B<nbdkit --dump-plugin sh>.  If
168 this line is not present, exit codes 4 and above behave like status 1.
170 =head2 Temporary directory
172 A fresh script is invoked for each method call (ie. scripts are
173 stateless), so if the script needs to store state it has to store it
174 somewhere in the filesystem in a format and location which is left up
175 to the author of the script.
177 However nbdkit helps by creating a randomly named, empty directory for
178 the script.  This directory persists for the lifetime of nbdkit and is
179 deleted when nbdkit exits.  The name of the directory is passed to
180 each script invocation in the C<$tmpdir> environment variable.
182 =head2 Handles
184 Handles are arbitrary strings, but it is best to limit them to short
185 alphanumeric strings.
187 =head3 Per-connection state
189 The temporary directory described above can be used for state for the
190 lifetime of the nbdkit instance (across multiple connections).  If you
191 want to store state per connection then one way to do it is to create
192 a randomly named subdirectory under the temporary directory:
194  case "$1" in
195    ...
196    open)
197      mktemp -d $tmpdir/handle-XXXXXX ;;
199 The handle will be the subdirectory name, returned to the script as
200 C<$2> in all connected calls (eg. C<pread>, C<get_size>).  You can
201 delete the subdirectory explicitly in C<close>:
203  case "$1" in
204    ...
205    close)
206      rm -rf "$2" ;;
208 or rely on nbdkit deleting the whole temporary directory including all
209 per-handle subdirectories when it exits.
211 =head2 Performance
213 This plugin has to fork on every request, so performance will never be
214 great.  For best performance, consider using the L<nbdkit-plugin(3)>
215 API directly.  Having said that, if you have a sh plugin and want to
216 improve performance then the following tips may help:
218 =over 4
220 =item Relax the thread model.
222 The default C<thread_model> is C<serialize_all_requests> meaning that
223 two instances of the script can never be running at the same time.
224 This is safe but slow.  If your script is safe to be called in
225 parallel, set this to C<parallel>.
227 =item Implement the C<zero> method.
229 If the C<zero> method is not implemented then nbdkit will fall back to
230 using C<pwrite> which is considerably slower because nbdkit has to
231 send blocks of zeroes to the script.
233 =item You don't have to write shell scripts.
235 This plugin can run any external binary, not only shell scripts.  You
236 should get more performance by rewriting the shell script as a program
237 in a compiled language.
239 =back
241 =head2 Methods
243 This just documents the arguments to the script corresponding to each
244 plugin method, and any way that they differ from the C callbacks.  In
245 all other respects they work the same way as the C callbacks, so you
246 should go and read L<nbdkit-plugin(3)>.
248 =over 4
250 =item C<load>
252  /path/to/script load
254 =item C<unload>
256  /path/to/script unload
258 This is called just before nbdkit exits.  Errors from this method are
259 ignored.
261 =item C<dump_plugin>
263  /path/to/script dump_plugin
265 =item C<config>
267  /path/to/script config <key> <value>
269 =item C<config_complete>
271  /path/to/script config_complete
273 =item C<magic_config_key>
275  /path/to/script magic_config_key
277 If a magic config key is needed, this should echo it to stdout.
278 See L<nbdkit(1)/Magic parameters>.
280 =item C<thread_model>
282  /path/to/script thread_model
284 On success this should print the desired thread model of the script,
285 one of C<"serialize_connections">, C<"serialize_all_requests">,
286 C<"serialize_requests">, or C<"parallel">.
288 This method is I<not> required; if omitted, then the plugin will be
289 executed under the safe C<"serialize_all_requests"> model.  However,
290 this means that this method B<must> be provided if you want to use the
291 C<"parallel"> or C<"serialize_requests"> model.  Even then your
292 request may be restricted for other reasons; look for C<thread_model>
293 in the output of C<nbdkit --dump-plugin sh script> to see what
294 actually gets selected.
296 If an error occurs, the script should output an error message and exit
297 with status C<1>; unrecognized output is ignored.
299 =item C<get_ready>
301  /path/to/script get_ready
303 =item C<after_fork>
305  /path/to/script after_fork
307 =item C<preconnect>
309  /path/to/script preconnect <readonly>
311 =item C<list_exports>
313  /path/to/script list_exports <readonly> <tls>
315 The C<readonly> and C<tls> parameters will be C<true> or C<false>.
317 The first line of output informs nbdkit how to parse the rest of the
318 output, the remaining lines then supply the inputs of the C
319 C<nbdkit_add_export> function (see L<nbdkit-plugin(3)>), as follows:
321 =over 4
323 =item NAMES
325 The remaining output provides one export name per line, and no export
326 will be given a description.  For convenience, this form is also
327 assumed if the first output line does not match one of the recognized
328 parse modes.
330 =item INTERLEAVED
332 The remaining output provides pairs of lines, the first line being an
333 export name, and the second the corresponding description.
335 =item NAMES+DESCRIPTIONS
337 The number of remaining lines is counted, with the first half being
338 used as export names, and the second half providing descriptions to
339 pair with names from the first half.
341 An example of using this form to list files in the current directory,
342 followed by their L<ls(1)> long description, would be:
344  echo NAMES+DESCRIPTIONS
345  ls
346  ls -l
348 =back
350 Note that other output modes might be introduced in the future; in
351 particular, none of the existing modes allow a literal newline in an
352 export name or description, although this could be possible under a
353 new mode supporting escape sequences.
355 This method is I<not> required; if it is absent, the list of exports
356 advertised by nbdkit will be the single name result of C<default_export>
357 and no description.
359 =item C<default_export>
361  /path/to/script default_export <readonly> <tls>
363 The C<readonly> and C<tls> parameters will be C<true> or C<false>.
365 On success this should print a name on stdout to use in place of the
366 default export C<"">, then exit with code C<0>.  For convenience, the
367 output can be any of the list forms recognized by C<list_exports>, in
368 which case the first listed export name is used, and where an empty
369 list uses C<"">.  Given the current set of recognized export lists, it
370 is not possible for the resulting name to include a newline.
372 This method is I<not> required; if it is absent, the default export
373 name will be the empty string, C<"">.
375 =item C<open>
377  /path/to/script open <readonly> <exportname> <tls>
379 The C<readonly> parameter will be C<true> or C<false>.  The
380 C<exportname> parameter, if present, is the export name passed to the
381 server from the client.  The C<tls> parameter, if present, will be
382 C<true> or C<false> depending on whether the client is using TLS.
384 On success this should print the handle (any string) on stdout and
385 exit with code C<0>.  If the handle ends with a newline character then
386 the newline is removed.
388 Unlike C plugins, this method is I<not> required.  If omitted then the
389 handle will be C<""> (empty string).
391 =item C<close>
393  /path/to/script close <handle>
395 =item C<export_description>
397  /path/to/script export_description <handle>
399 The script should print a human-readable description of the disk image
400 on stdout.  If the description ends with a newline character then the
401 newline is removed.
403 This method is I<not> required; if it is absent, no export description
404 will be provided to the client.
406 =item C<get_size>
408  /path/to/script get_size <handle>
410 The script should print the size of the disk image on stdout.  You can
411 print the size in bytes, or use any format understood by
412 C<nbdkit_parse_size> such as C<1M> (see
413 L<nbdkit-plugin(3)/PARSING SIZE PARAMETERS>).
415 This method is required.
417 =item C<block_size>
419  /path/to/script block_size <handle>
421 This script should print three numbers on stdout, separated by
422 whitespace.  These are (in order) the minimum block size, the
423 preferred block size, and the maximum block size.  You can print the
424 sizes in bytes or use any format understood by C<nbdkit_parse_size>
425 such as C<1M> (see L<nbdkit-plugin(3)/PARSING SIZE PARAMETERS>).
427 =item C<can_write>
429 =item C<can_flush>
431 =item C<can_trim>
433 =item C<can_zero>
435 =item C<can_extents>
437 Unlike in other languages, you B<must> provide the C<can_*> methods
438 otherwise they are assumed to all return false and your C<pwrite>,
439 C<flush>, C<trim>, C<zero> and C<extents> methods will never be
440 called.  The reason for this is obscure: In other languages we can
441 detect if (eg) a C<pwrite> method is defined and synthesize an
442 appropriate response if no actual C<can_write> method is defined.
443 However detecting if methods are present without running them is not
444 possible with this plugin.
446  /path/to/script can_write <handle>
447  /path/to/script can_flush <handle>
448  /path/to/script can_trim <handle>
449  /path/to/script can_zero <handle>
450  /path/to/script can_extents <handle>
452 The script should exit with code C<0> for true or code C<3> for false.
454 =item C<is_rotational>
456 =item C<can_fast_zero>
458  /path/to/script is_rotational <handle>
459  /path/to/script can_fast_zero <handle>
461 The script should exit with code C<0> for true or code C<3> for false.
463 =item C<can_fua>
465 =item C<can_cache>
467  /path/to/script can_fua <handle>
468  /path/to/script can_cache <handle>
470 These control Forced Unit Access (FUA) and caching behaviour of the
471 core server.
473 Unlike the other C<can_*> callbacks, these two are I<not> a boolean.
474 They must print either "none", "emulate" or "native" to stdout.  The
475 meaning of these is described in L<nbdkit-plugin(3)>.  Furthermore,
476 you B<must> provide a C<can_cache> method if you desire the C<cache>
477 callback to be utilized, similar to the reasoning behind requiring
478 C<can_write> to utilize C<pwrite>.
480 =item C<can_multi_conn>
482  /path/to/script can_multi_conn <handle>
484 The script should exit with code C<0> for true or code C<3> for false.
486 =item C<pread>
488  /path/to/script pread <handle> <count> <offset>
490 The script should print the requested binary data on stdout.  Exactly
491 C<count> bytes must be printed.
493 This method is required.
495 =item C<pwrite>
497  /path/to/script pwrite <handle> <count> <offset> <flags>
499 The script should read the binary data to be written from stdin.
501 The C<flags> parameter can be an empty string or C<"fua">.  In the
502 future, a comma-separated list of flags may be present.
504 Unlike in other languages, if you provide a C<pwrite> method you
505 B<must> also provide a C<can_write> method which exits with code C<0>
506 (true).
508 =item C<flush>
510  /path/to/script flush <handle>
512 Unlike in other languages, if you provide a C<flush> method you
513 B<must> also provide a C<can_flush> method which exits with code C<0>
514 (true).
516 =item C<trim>
518  /path/to/script trim <handle> <count> <offset> <flags>
520 The C<flags> parameter can be an empty string or C<"fua">.  In the
521 future, a comma-separated list of flags may be present.
523 Unlike in other languages, if you provide a C<trim> method you B<must>
524 also provide a C<can_trim> method which exits with code C<0> (true).
526 =item C<zero>
528  /path/to/script zero <handle> <count> <offset> <flags>
530 The C<flags> parameter can be an empty string or a comma-separated
531 list of the flags: C<"fua">, C<"may_trim">, and C<"fast"> (eg. C<"">,
532 C<"fua">, C<"fua,may_trim,fast"> are some of the 8 possible values).
534 Unlike in other languages, if you provide a C<zero> method you B<must>
535 also provide a C<can_zero> method which exits with code C<0> (true).
537 To trigger a fallback to <pwrite> on a normal zero request, or to
538 respond quickly to the C<"fast"> flag that a specific zero request is
539 no faster than a corresponding write, the script must output
540 C<ENOTSUP> or C<EOPNOTSUPP> to stderr (possibly followed by a
541 description of the problem) before exiting with code C<1> (failure).
543 =item C<extents>
545  /path/to/script extents <handle> <count> <offset> <flags>
547 The C<flags> parameter can be an empty string or C<"req_one">.
549 This must print, one per line on stdout, a list of one or more extents
550 in the format:
552  offset length type
554 which correspond to the inputs of the C C<nbdkit_add_extent> function
555 (see L<nbdkit-plugin(3)>).  The C<offset> and C<length> fields may use
556 any format understood by C<nbdkit_parse_size>.  The optional C<type>
557 field may be an integer, missing (same as 0), or a comma-separated
558 list of the words C<hole> and C<zero>.  An example of a valid set of
559 extents covering a C<10M> disk where the first megabyte only is
560 allocated data:
562  0  1M
563  1M 9M  hole,zero
565 Unlike in other languages, if you provide an C<extents> method you
566 B<must> also provide a C<can_extents> method which exits with code
567 C<0> (true).
569 =item C<cache>
571  /path/to/script cache <handle> <count> <offset>
573 Unlike in other languages, if you provide a C<cache> method you
574 B<must> also provide a C<can_cache> method which prints "native" and
575 exits with code C<0> (true).
577 =back
579 =head2 Missing callbacks
581 =over 4
583 =item Missing: C<name>, C<version>, C<longname>,
584 C<description>, C<config_help>
586 These are not yet supported.
588 =back
590 =head1 FILES
592 =over 4
594 =item F<$plugindir/nbdkit-sh-plugin.so>
596 The plugin.
598 Use C<nbdkit --dump-config> to find the location of C<$plugindir>.
600 =back
602 =head1 VERSION
604 C<nbdkit-sh-plugin> first appeared in nbdkit 1.8.
606 =head1 SEE ALSO
608 L<nbdkit(1)>,
609 L<nbdkit-plugin(3)>,
610 L<nbdkit-eval-plugin(1)>,
611 L<nbdkit-cc-plugin(1)>.
613 =head1 AUTHORS
615 Richard W.M. Jones
617 =head1 COPYRIGHT
619 Copyright Red Hat