5 nbdkit-ruby-plugin - nbdkit ruby plugin
9 nbdkit ruby script=/path/to/plugin.rb [arguments...]
13 C<nbdkit-ruby-plugin> is an embedded Ruby interpreter for
14 L<nbdkit(1)>, allowing you to write nbdkit plugins in Ruby.
16 Broadly speaking, Ruby nbdkit plugins work like C ones, so you should
17 read L<nbdkit-plugin(3)> first.
19 =head2 USING A RUBY NBDKIT PLUGIN
21 Assuming you have a Ruby script which is an nbdkit plugin, you run it
24 nbdkit ruby script=/path/to/ruby.rb
26 You may have to add further C<key=value> arguments to the command
27 line. Read the Ruby script to see if it requires any. C<script=...>
28 I<must> come first on the command line.
30 =head1 WRITING A RUBY NBDKIT PLUGIN
32 There is an example Ruby nbdkit plugin called C<example.rb> which
33 ships with the nbdkit source.
35 To write a Ruby nbdkit plugin, you create a Ruby file which
36 contains at least the following required functions:
44 def pread(h, count, offset)
48 Note that the subroutines must have those literal names (like C<open>),
49 because the C part looks up and calls those functions directly. You
50 may want to include documentation and globals (eg. for storing global
51 state). Any other top level statements are run when the script is
52 loaded, just like ordinary Ruby.
54 The file does not need to include a C<#!> (hash-bang) at the top, and
55 does not need to be executable. In fact it's a good idea not to do
56 that, because running the plugin directly as a Ruby script won't
61 Ruby callbacks should throw exceptions to indicate errors.
65 This just documents the arguments to the callbacks in Ruby, and any
66 way that they differ from the C callbacks. In all other respects they
67 work the same way as the C callbacks, so you should go and read
76 def config(key, value)
80 =item C<config_complete>
84 There are no arguments or return value.
94 You can return any non-nil Ruby value as the handle. It is passed
95 back in subsequent calls.
110 # return the size of the disk
129 =item C<is_rotational>
149 def pread(h, count, offset)
150 # construct a string of length count bytes and return it
153 The body of your C<pread> function should construct a string of length
154 (at least) C<count> bytes. You should read C<count> bytes from the
155 disk starting at C<offset>.
157 NBD only supports whole reads, so your function should try to read
158 the whole region (perhaps requiring a loop). If the read fails or
159 is partial, your function should throw an exception.
165 def pwrite(h, buf, offset)
170 The body of your C<pwrite> function should write the C<buf> string to
171 the disk. You should write C<count> bytes to the disk starting at
174 NBD only supports whole writes, so your function should try to
175 write the whole region (perhaps requiring a loop). If the write
176 fails or is partial, your function should throw an exception.
186 The body of your C<flush> function should do a L<sync(2)> or
187 L<fdatasync(2)> or equivalent on the backing store.
193 def trim(h, count, offset)
197 The body of your C<trim> function should "punch a hole" in the
202 =head2 MISSING CALLBACKS
206 =item Missing: C<load> and C<unload>
208 These are not needed because you can just use ordinary Ruby
211 =item Missing: C<name>, C<version>, C<longname>, C<description>, C<config_help>
213 These are not yet supported.
219 The thread model for Ruby callbacks currently cannot be set from Ruby.
220 It is hard-coded in the C part to
221 C<NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS>. This may change or be
236 Copyright (C) 2013-2016 Red Hat Inc.
240 Redistribution and use in source and binary forms, with or without
241 modification, are permitted provided that the following conditions are
248 Redistributions of source code must retain the above copyright
249 notice, this list of conditions and the following disclaimer.
253 Redistributions in binary form must reproduce the above copyright
254 notice, this list of conditions and the following disclaimer in the
255 documentation and/or other materials provided with the distribution.
259 Neither the name of Red Hat nor the names of its contributors may be
260 used to endorse or promote products derived from this software without
261 specific prior written permission.
265 THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
266 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
267 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
268 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
269 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
270 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
271 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
272 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
273 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
274 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
275 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF