1nbdkit-sh-plugin(3) NBDKIT nbdkit-sh-plugin(3)
2
3
4
6 nbdkit-sh-plugin - nbdkit shell, script or executable plugin
7
9 nbdkit sh /path/to/script [arguments...]
10
11 nbdkit sh - <<'EOF'
12 ... shell script ...
13 EOF
14
16 "nbdkit-sh-plugin" allows you to write plugins for nbdkit(1) using
17 arbitrary scripting languages, including shells like bash(1), dash(1),
18 csh(1), zsh(1) etc., other scripting environments, or any executable.
19
20 Note if you want to use an established scripting language like Perl or
21 Python, then nbdkit has specific plugins to handle those languages and
22 those will be more efficient (see nbdkit(1) for a complete list).
23
24 To use shell script fragments from the nbdkit command line (rather than
25 a separate script) see nbdkit-eval-plugin(1).
26
27 If you have been given an nbdkit sh plugin
28 Assuming you have a shell script which is an nbdkit plugin, you run it
29 like this:
30
31 nbdkit sh /path/to/script
32
33 You may have to add further "key=value" arguments to the command line.
34 The script must be executable ("chmod +x").
35
36 Inline shell scripts
37 It is also possible to write a shell script plugin "inline" using "-"
38 as the name of the script, like this:
39
40 nbdkit sh - <<'EOF'
41 case "$1" in
42 get_size) echo 1M ;;
43 pread) dd if=/dev/zero count=$3 iflag=count_bytes ;;
44 *) exit 2 ;;
45 esac
46 EOF
47
48 By default the inline script runs under /bin/sh. You can add a shebang
49 ("#!") to use other scripting languages. Of course, reading an inline
50 script from stdin is incompatible with the "-s" ("--single") mode of
51 nbdkit that connects a client on stdin.
52
54 For an example plugin written in Bash, see:
55 https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/sh/example.sh
56
57 Broadly speaking, nbdkit shell plugins work like C ones, so you should
58 read nbdkit-plugin(3) first.
59
60 Programming model
61 This plugin has a simple programming model: For every plugin method
62 that needs to be called, the external script is invoked with parameters
63 describing the method and its arguments. The first parameter is always
64 the method name. For example:
65
66 /path/to/script config file disk.img
67 │ │ │
68 │ │ └─ value ($3)
69 │ └── key ($2)
70 method ($1)
71
72 /path/to/script pread <handle> <count> <offset>
73 │ │ │ │
74 │ │ │ └─ offset in bytes ($4)
75 │ │ └── request size in bytes ($3)
76 method ($1) └── handle ($2) ─ see "Handles" below
77
78 Scripts should ignore extra parameters that they don't understand since
79 we may add new parameters in future.
80
81 Exit codes
82 The script should exit with specific exit codes:
83
84 0 The method was executed successfully.
85
86 1 and 8-127
87 There was an error. The script may print on stderr an errno name,
88 optionally followed by whitespace and a message, for example:
89
90 echo 'ENOSPC Out of space' >&2
91 exit 1
92
93 or if you don't need the log message:
94
95 echo ENOSPC >&2
96 exit 1
97
98 If the script doesn't print anything or the output cannot be parsed
99 then nbdkit assumes error "EIO". Note that output to stderr is
100 ignored if the command succeeds, so it is acceptable to output a
101 potential error message prefix prior to attempting a command which
102 will add further details if a failure occurs.
103
104 2 The requested method is not supported by the script.
105
106 3 For methods which return booleans, this code indicates false.
107
108 4, 5, 6, 7
109 These exit codes are reserved for future use.
110
111 Temporary directory
112 A fresh script is invoked for each method call (ie. scripts are
113 stateless), so if the script needs to store state it has to store it
114 somewhere in the filesystem in a format and location which is left up
115 to the author of the script.
116
117 However nbdkit helps by creating a randomly named, empty directory for
118 the script. This directory persists for the lifetime of nbdkit and is
119 deleted when nbdkit exits. The name of the directory is passed to each
120 script invocation in the $tmpdir environment variable.
121
122 Handles
123 Handles are arbitrary strings, but it is best to limit them to short
124 alphanumeric strings.
125
126 Per-connection state
127
128 The temporary directory described above can be used for state for the
129 lifetime of the nbdkit instance (across multiple connections). If you
130 want to store state per connection then one way to do it is to create a
131 randomly named subdirectory under the temporary directory:
132
133 case "$1" in
134 ...
135 open)
136 mktemp -d $tmpdir/handle-XXXXXX ;;
137
138 The handle will be the subdirectory name, returned to the script as $2
139 in all connected calls (eg. "pread", "get_size"). You can delete the
140 subdirectory explicitly in "close":
141
142 case "$1" in
143 ...
144 close)
145 rm -rf "$2" ;;
146
147 or rely on nbdkit deleting the whole temporary directory including all
148 per-handle subdirectories when it exits.
149
150 Performance
151 This plugin has to fork on every request, so performance will never be
152 great. For best performance, consider using the nbdkit-plugin(3) API
153 directly. Having said that, if you have a sh plugin and want to
154 improve performance then the following tips may help:
155
156 Relax the thread model.
157 The default "thread_model" is "serialize_all_requests" meaning that
158 two instances of the script can never be running at the same time.
159 This is safe but slow. If your script is safe to be called in
160 parallel, set this to "parallel".
161
162 Implement the "zero" method.
163 If the "zero" method is not implemented then nbdkit will fall back
164 to using "pwrite" which is considerably slower because nbdkit has
165 to send blocks of zeroes to the script.
166
167 You don't have to write shell scripts.
168 This plugin can run any external binary, not only shell scripts.
169 You should get more performance by rewriting the shell script as a
170 program in a compiled language.
171
172 Methods
173 This just documents the arguments to the script corresponding to each
174 plugin method, and any way that they differ from the C callbacks. In
175 all other respects they work the same way as the C callbacks, so you
176 should go and read nbdkit-plugin(3).
177
178 "load"
179 /path/to/script load
180
181 "unload"
182 /path/to/script unload
183
184 This is called just before nbdkit exits. Errors from this method
185 are ignored.
186
187 "dump_plugin"
188 /path/to/script dump_plugin
189
190 "config"
191 /path/to/script config <key> <value>
192
193 "config_complete"
194 /path/to/script config_complete
195
196 "magic_config_key"
197 /path/to/script magic_config_key
198
199 If a magic config key is needed, this should echo it to stdout.
200 See "Magic parameters" in nbdkit(1).
201
202 "thread_model"
203 /path/to/script thread_model
204
205 On success this should print the desired thread model of the
206 script, one of "serialize_connections", "serialize_all_requests",
207 "serialize_requests", or "parallel".
208
209 This method is not required; if omitted, then the plugin will be
210 executed under the safe "serialize_all_requests" model. However,
211 this means that this method must be provided if you want to use the
212 "parallel" or "serialize_requests" model. Even then your request
213 may be restricted for other reasons; look for "thread_model" in the
214 output of "nbdkit --dump-plugin sh script" to see what actually
215 gets selected.
216
217 If an error occurs, the script should output an error message and
218 exit with status 1; unrecognized output is ignored.
219
220 "get_ready"
221 /path/to/script get_ready
222
223 "after_fork"
224 /path/to/script after_fork
225
226 "preconnect"
227 /path/to/script preconnect <readonly>
228
229 "list_exports"
230 /path/to/script list_exports <readonly> <tls>
231
232 The "readonly" and "tls" parameters will be "true" or "false".
233
234 The first line of output informs nbdkit how to parse the rest of
235 the output, the remaining lines then supply the inputs of the C
236 "nbdkit_add_export" function (see nbdkit-plugin(3)), as follows:
237
238 NAMES
239 The remaining output provides one export name per line, and no
240 export will be given a description. For convenience, this form
241 is also assumed if the first output line does not match one of
242 the recognized parse modes.
243
244 INTERLEAVED
245 The remaining output provides pairs of lines, the first line
246 being an export name, and the second the corresponding
247 description.
248
249 NAMES+DESCRIPTIONS
250 The number of remaining lines is counted, with the first half
251 being used as export names, and the second half providing
252 descriptions to pair with names from the first half.
253
254 An example of using this form to list files in the current
255 directory, followed by their ls(1) long description, would be:
256
257 echo NAMES+DESCRIPTIONS
258 ls
259 ls -l
260
261 Note that other output modes might be introduced in the future; in
262 particular, none of the existing modes allow a literal newline in
263 an export name or description, although this could be possible
264 under a new mode supporting escape sequences.
265
266 This method is not required; if it is absent, the list of exports
267 advertised by nbdkit will be the single name result of
268 "default_export" and no description.
269
270 "default_export"
271 /path/to/script default_export <readonly> <tls>
272
273 The "readonly" and "tls" parameters will be "true" or "false".
274
275 On success this should print a name on stdout to use in place of
276 the default export "", then exit with code 0. For convenience, the
277 output can be any of the list forms recognized by "list_exports",
278 in which case the first listed export name is used, and where an
279 empty list uses "". Given the current set of recognized export
280 lists, it is not possible for the resulting name to include a
281 newline.
282
283 This method is not required; if it is absent, the default export
284 name will be the empty string, "".
285
286 "open"
287 /path/to/script open <readonly> <exportname> <tls>
288
289 The "readonly" parameter will be "true" or "false". The
290 "exportname" parameter, if present, is the export name passed to
291 the server from the client. The "tls" parameter, if present, will
292 be "true" or "false" depending on whether the client is using TLS.
293
294 On success this should print the handle (any string) on stdout and
295 exit with code 0. If the handle ends with a newline character then
296 the newline is removed.
297
298 Unlike C plugins, this method is not required. If omitted then the
299 handle will be "" (empty string).
300
301 "close"
302 /path/to/script close <handle>
303
304 "export_description"
305 /path/to/script export_description <handle>
306
307 The script should print a human-readable description of the disk
308 image on stdout. If the description ends with a newline character
309 then the newline is removed.
310
311 This method is not required; if it is absent, no export description
312 will be provided to the client.
313
314 "get_size"
315 /path/to/script get_size <handle>
316
317 The script should print the size of the disk image on stdout. You
318 can print the size in bytes, or use any format understood by
319 "nbdkit_parse_size" such as "1M" (see "PARSING SIZE PARAMETERS" in
320 nbdkit-plugin(3)).
321
322 This method is required.
323
324 "can_write"
325 "can_flush"
326 "can_trim"
327 "can_zero"
328 "can_extents"
329 Unlike in other languages, you must provide the "can_*" methods
330 otherwise they are assumed to all return false and your "pwrite",
331 "flush", "trim", "zero" and "extents" methods will never be called.
332 The reason for this is obscure: In other languages we can detect if
333 (eg) a "pwrite" method is defined and synthesize an appropriate
334 response if no actual "can_write" method is defined. However
335 detecting if methods are present without running them is not
336 possible with this plugin.
337
338 /path/to/script can_write <handle>
339 /path/to/script can_flush <handle>
340 /path/to/script can_trim <handle>
341 /path/to/script can_zero <handle>
342 /path/to/script can_extents <handle>
343
344 The script should exit with code 0 for true or code 3 for false.
345
346 "is_rotational"
347 "can_fast_zero"
348 /path/to/script is_rotational <handle>
349 /path/to/script can_fast_zero <handle>
350
351 The script should exit with code 0 for true or code 3 for false.
352
353 "can_fua"
354 "can_cache"
355 /path/to/script can_fua <handle>
356 /path/to/script can_cache <handle>
357
358 These control Forced Unit Access (FUA) and caching behaviour of the
359 core server.
360
361 Unlike the other "can_*" callbacks, these two are not a boolean.
362 They must print either "none", "emulate" or "native" to stdout.
363 The meaning of these is described in nbdkit-plugin(3).
364 Furthermore, you must provide a "can_cache" method if you desire
365 the "cache" callback to be utilized, similar to the reasoning
366 behind requiring "can_write" to utilize "pwrite".
367
368 "can_multi_conn"
369 /path/to/script can_multi_conn <handle>
370
371 The script should exit with code 0 for true or code 3 for false.
372
373 "pread"
374 /path/to/script pread <handle> <count> <offset>
375
376 The script should print the requested binary data on stdout.
377 Exactly "count" bytes must be printed.
378
379 This method is required.
380
381 "pwrite"
382 /path/to/script pwrite <handle> <count> <offset> <flags>
383
384 The script should read the binary data to be written from stdin.
385
386 The "flags" parameter can be an empty string or "fua". In the
387 future, a comma-separated list of flags may be present.
388
389 Unlike in other languages, if you provide a "pwrite" method you
390 must also provide a "can_write" method which exits with code 0
391 (true).
392
393 "flush"
394 /path/to/script flush <handle>
395
396 Unlike in other languages, if you provide a "flush" method you must
397 also provide a "can_flush" method which exits with code 0 (true).
398
399 "trim"
400 /path/to/script trim <handle> <count> <offset> <flags>
401
402 The "flags" parameter can be an empty string or "fua". In the
403 future, a comma-separated list of flags may be present.
404
405 Unlike in other languages, if you provide a "trim" method you must
406 also provide a "can_trim" method which exits with code 0 (true).
407
408 "zero"
409 /path/to/script zero <handle> <count> <offset> <flags>
410
411 The "flags" parameter can be an empty string or a comma-separated
412 list of the flags: "fua", "may_trim", and "fast" (eg. "", "fua",
413 "fua,may_trim,fast" are some of the 8 possible values).
414
415 Unlike in other languages, if you provide a "zero" method you must
416 also provide a "can_zero" method which exits with code 0 (true).
417
418 To trigger a fallback to <pwrite> on a normal zero request, or to
419 respond quickly to the "fast" flag that a specific zero request is
420 no faster than a corresponding write, the script must output
421 "ENOTSUP" or "EOPNOTSUPP" to stderr (possibly followed by a
422 description of the problem) before exiting with code 1 (failure).
423
424 "extents"
425 /path/to/script extents <handle> <count> <offset> <flags>
426
427 The "flags" parameter can be an empty string or "req_one".
428
429 This must print, one per line on stdout, a list of one or more
430 extents in the format:
431
432 offset length type
433
434 which correspond to the inputs of the C "nbdkit_add_extent"
435 function (see nbdkit-plugin(3)). The "offset" and "length" fields
436 may use any format understood by "nbdkit_parse_size". The optional
437 "type" field may be an integer, missing (same as 0), or a comma-
438 separated list of the words "hole" and "zero". An example of a
439 valid set of extents covering a "10M" disk where the first megabyte
440 only is allocated data:
441
442 0 1M
443 1M 9M hole,zero
444
445 Unlike in other languages, if you provide an "extents" method you
446 must also provide a "can_extents" method which exits with code 0
447 (true).
448
449 "cache"
450 /path/to/script cache <handle> <count> <offset>
451
452 Unlike in other languages, if you provide a "cache" method you must
453 also provide a "can_cache" method which prints "native" and exits
454 with code 0 (true).
455
456 Missing callbacks
457 Missing: "name", "version", "longname", "description", "config_help"
458 These are not yet supported.
459
461 $plugindir/nbdkit-sh-plugin.so
462 The plugin.
463
464 Use "nbdkit --dump-config" to find the location of $plugindir.
465
467 "nbdkit-sh-plugin" first appeared in nbdkit 1.8.
468
470 nbdkit(1), nbdkit-plugin(3), nbdkit-eval-plugin(1),
471 nbdkit-cc-plugin(1).
472
474 Richard W.M. Jones
475
477 Copyright (C) 2018-2020 Red Hat Inc.
478
480 Redistribution and use in source and binary forms, with or without
481 modification, are permitted provided that the following conditions are
482 met:
483
484 • Redistributions of source code must retain the above copyright
485 notice, this list of conditions and the following disclaimer.
486
487 • Redistributions in binary form must reproduce the above copyright
488 notice, this list of conditions and the following disclaimer in the
489 documentation and/or other materials provided with the
490 distribution.
491
492 • Neither the name of Red Hat nor the names of its contributors may
493 be used to endorse or promote products derived from this software
494 without specific prior written permission.
495
496 THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY
497 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
498 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
499 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE
500 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
501 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
502 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
503 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
504 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
505 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
506 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
507
508
509
510nbdkit-1.25.8 2021-05-25 nbdkit-sh-plugin(3)