nbdkit-sh-plugin(3) | NBDKIT | nbdkit-sh-plugin(3) |
nbdkit-sh-plugin - nbdkit shell, script or executable plugin
nbdkit sh /path/to/script [arguments...] nbdkit sh - <<'EOF' ... shell script ... EOF
"nbdkit-sh-plugin" allows you to write plugins for nbdkit(1) using arbitrary scripting languages, including shells like bash(1), dash(1), csh(1), zsh(1) etc., other scripting environments, or any executable. Note if you want to use an established scripting language like Perl or Python, then nbdkit has specific plugins to handle those languages and those will be more efficient (see nbdkit(1) for a complete list).
Assuming you have a shell script which is an nbdkit plugin, you run it like this:
nbdkit sh /path/to/script
You may have to add further "key=value" arguments to the command line. The script must be executable ("chmod +x").
It is also possible to write a shell script plugin "inline" using "-" as the name of the script, like this:
nbdkit sh - <<'EOF' case "$1" in get_size) echo 1M ;; pread) dd if=/dev/zero count=$3 iflag=count_bytes ;; *) exit 2 ;; esac EOF
By default the inline script runs under /bin/sh. You can add a shebang ("#!") to use other scripting languages.
For an example plugin written in Bash, see: https://github.com/libguestfs/nbdkit/blob/master/plugins/sh/example.sh
Broadly speaking, nbdkit shell plugins work like C ones, so you should read nbdkit-plugin(3) first.
This plugin has a simple programming model: For every plugin method that needs to be called, the external script is invoked with parameters describing the method and its arguments. The first parameter is always the method name. For example:
/path/to/script config file disk.img │ │ │ │ │ └─ value ($3) │ └── key ($2) method ($1) /path/to/script pread <handle> <count> <offset> │ │ │ │ │ │ │ └─ offset in bytes ($4) │ │ └── request size in bytes ($3) method ($1) └── handle ($2) ─ see "Handles" below
The script should exit with specific exit codes:
ENOSPC Out of space
If the script doesn't print anything or the output cannot be parsed then nbdkit assumes error "EIO".
A fresh script is invoked for each method call (ie. scripts are stateless), so if the script needs to store state it has to store it somewhere in the filesystem in a format and location which is left up to the author of the script.
However nbdkit helps by creating a randomly named, empty directory for the script. This directory persists for the lifetime of nbdkit and is deleted when nbdkit exits. The name of the directory is passed to each script invocation in the $tmpdir environment variable.
Handles are arbitrary strings, but it is best to limit them to short alphanumeric strings.
Per-connection state
The temporary directory described above can be used for state for the lifetime of the nbdkit instance (across multiple connections). If you want to store state per connection then one way to do it is to create a randomly named subdirectory under the temporary directory:
case "$1" in ... open) mktemp -d $tmpdir/handle-XXXXXX ;;
The handle will be the subdirectory name, returned to the script as $2 in all connected calls (eg. "pread", "get_size"). You can delete the subdirectory explicitly in "close":
case "$1" in ... close) rm -rf "$2" ;;
or rely on nbdkit deleting the whole temporary directory including all per-handle subdirectories when it exits.
This just documents the arguments to the script corresponding to each plugin method, 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 nbdkit-plugin(3).
/path/to/script load
/path/to/script unload
This is called just before nbdkit exits. Errors from this method are ignored.
/path/to/script dump_plugin
/path/to/script config <key> <value>
/path/to/script config_complete
/path/to/script open <readonly>
The "readonly" parameter will be "true" or "false".
On success this should print the handle (any string) on stdout and exit with code 0. If the handle ends with a newline character then the newline is removed.
Unlike C plugins, this method is not required. If omitted then the handle will be "" (empty string).
/path/to/script close <handle>
/path/to/script get_size <handle>
The script should print the size of the disk image on stdout. You can print the size in bytes, or use any format understood by "nbdkit_parse_size" such as "1M" (see "PARSING SIZE PARAMETERS" in nbdkit-plugin(3)).
This method is required.
/path/to/script can_write <handle> /path/to/script can_flush <handle> /path/to/script can_trim <handle> /path/to/script can_zero <handle>
The script should exit with code 0 for true or code 3 for false.
/path/to/script is_rotational <handle>
The script should exit with code 0 for true or code 3 for false.
/path/to/script can_fua <handle>
This controls Forced Unit Access (FUA) behaviour of the core server.
Unlike the other "can_*" callbacks, this one is not a boolean. It must print either "none", "emulate" or "native" to stdout. The meaning of these is described in nbdkit-plugin(3).
/path/to/script can_multi_conn <handle>
The script should exit with code 0 for true or code 3 for false.
/path/to/script pread <handle> <count> <offset>
The script should print the requested binary data on stdout. Exactly "count" bytes must be printed.
This method is required.
/path/to/script pwrite <handle> <count> <offset> <flags>
The script should read the binary data to be written from stdin.
The "flags" parameter can be an empty string or "fua". In the future, a comma-separated list of flags may be present.
Unlike in other languages, if you provide a "pwrite" method you must also provide a "can_write" method which exits with code 0 (true).
/path/to/script flush <handle>
Unlike in other languages, if you provide a "flush" method you must also provide a "can_flush" method which exits with code 0 (true).
/path/to/script trim <handle> <count> <offset> <flags>
The "flags" parameter can be an empty string or "fua". In the future, a comma-separated list of flags may be present.
Unlike in other languages, if you provide a "trim" method you must also provide a "can_trim" method which exits with code 0 (true).
/path/to/script zero <handle> <count> <offset> <flags>
The "flags" parameter can be an empty string or a comma-separated list of the flags: "fua" and "may_trim" (eg. "", "fua", "fua,may_trim" are all possible values).
Unlike in other languages, if you provide a "zero" method you must also provide a "can_zero" method which exits with code 0 (true).
The thread model for scripts currently cannot be set from this plugin. It is hard-coded in the C part to "NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS". This may change or be settable in future.
Richard W.M. Jones
Copyright (C) 2018 Red Hat Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2019-01-26 | nbdkit-1.10.3 |