nbdkit-ruby-plugin man page

nbdkit-ruby-plugin — nbdkit ruby plugin

Synopsis

nbdkit ruby script=/path/to/plugin.rb [arguments...]

Description

"nbdkit-ruby-plugin" is an embedded Ruby interpreter for nbdkit(1), allowing you to write nbdkit plugins in Ruby.

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

Using a Ruby Nbdkit Plugin

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

nbdkit ruby script=/path/to/ruby.rb

You may have to add further "key=value" arguments to the command line. Read the Ruby script to see if it requires any. "script=..." must come first on the command line.

Writing a Ruby Nbdkit Plugin

There is an example Ruby nbdkit plugin called "example.rb" which ships with the nbdkit source.

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

def open(readonly)
  # see below
end
def get_size(h)
  # see below
end
def pread(h, count, offset)
  # see below
end

Note that the subroutines must have those literal names (like "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). Any other top level statements are run when the script is loaded, just like ordinary Ruby.

The file does not need to include a "#!" (hash-bang) at the top, and does not need to be executable. In fact it's a good idea not to do that, because running the plugin directly as a Ruby script won't work.

Exceptions

Ruby callbacks should throw exceptions to indicate errors.

Ruby Callbacks

This just documents the arguments to the callbacks in Ruby, 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).

"config"

(Optional)

def config(key, value)
  # no return value
end
"config_complete"
(Optional)

There are no arguments or return value.
"open"

(Required)

def open(readonly)
  # return handle
end

You can return any non-nil Ruby value as the handle. It is passed back in subsequent calls.

"close"

(Optional)

def close(h)
  # no return value
end
"get_size"

(Required)

def get_size(h)
  # return the size of the disk
end
"can_write"

(Optional)

def can_write(h)
  # return a boolean
end
"can_flush"

(Optional)

def can_flush(h)
  # return a boolean
end
"is_rotational"

(Optional)

def is_rotational(h)
  # return a boolean
end
"can_trim"

(Optional)

def can_trim(h)
  # return a boolean
end
"pread"

(Required)

def pread(h, count, offset)
  # construct a string of length count bytes and return it
end

The body of your "pread" function should construct a string of length (at least) "count" bytes. You should read "count" bytes from the disk starting at "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 throw an exception.

"pwrite"

(Optional)

def pwrite(h, buf, offset)
  length = buf.length
  # no return value
end

The body of your "pwrite" function should write the "buf" string to the disk. You should write "count" bytes to the disk starting at "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 throw an exception.

"flush"

(Optional)

def flush(h)
  # no return value
end

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

"trim"

(Optional)

def trim(h, count, offset)
  # no return value
end

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

Missing Callbacks

Missing: "load" and "unload"
These are not needed because you can just use ordinary Ruby constructs.
Missing: "name", "version", "longname", "description", "config_help"
These are not yet supported.

Threads

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

See Also

nbdkit(1), nbdkit-plugin(3), ruby(1).

Authors

Richard W.M. Jones

License

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

·
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
·
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
·
Neither the name of Red Hat nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

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.

Referenced By

nbdkit-plugin(3).

2016-06-07 nbdkit nbdkit