[Libguestfs] [PATCH nbdkit 1/3] sh: Implement inline scripts.
Eric Blake
eblake at redhat.com
Fri Dec 14 22:31:32 UTC 2018
On 12/14/18 4:16 PM, Richard W.M. Jones wrote:
> This implements something like a readonly 1MB disk reading as zeroes:
>
> nbdkit sh script=- <<'EOF'
> case "$1" in
> get_size) echo 1M ;;
> pread) dd if=/dev/zero count=$3 iflag=count_bytes ;;
> *) exit 2 ;;
> esac
> EOF
>
> Use of "-" is analogous to reading passwords from stdin.
> ---
> @@ -26,6 +30,19 @@ like this:
> You may have to add further C<key=value> arguments to the command
> line.
>
> +=head2 Inline shell scripts
> +
> +It is also possible to write a shell script plugin "inline" using C<->
> +as the name of the script, like this:
> +
> + nbdkit sh script=- <<'EOF'
> + case "$1" in
> + get_size) echo 1M ;;
> + pread) dd if=/dev/zero count=$3 iflag=count_bytes ;;
> + *) exit 2 ;;
> + esac
> + EOF
> +
Worth a specific mention that 'script=' is required, since otherwise the
'-' looks like an option to nbdkit proper rather than a script parameter?
Probably not worth a mention that a local file named '-' can still be
invoked via [script=]./- (there, script is optional because the leading
./ avoids the leading -).
> +static char *
> +inline_script (void)
> +{
> + const char scriptname[] = "inline-script.sh";
> + char *filename = NULL;
> + char *cmd = NULL;
> +
> + if (asprintf (&filename, "%s/%s", tmpdir, scriptname) == -1) {
> + nbdkit_error ("asprintf: %m");
> + goto err;
> + }
> +
> + /* Safe because both the tmpdir and script name are controlled by us
> + * and don't contain characters that need quoting.
> + */
> + if (asprintf (&cmd, "cat > %s", filename) == -1) {
Useful comment :) And I verified it is true.
Looks good.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
More information about the Libguestfs
mailing list