ruby: New plugin that lets you write plugins as Ruby scripts.
[nbd.git] / plugins / ruby / nbdkit-ruby-plugin.pod
blob85114790a7af75d0882e6431b61619fd86795582
1 =encoding utf8
3 =head1 NAME
5 nbdkit-ruby-plugin - nbdkit ruby plugin
7 =head1 SYNOPSIS
9  nbdkit ruby script=/path/to/plugin.rb [arguments...]
11 =head1 DESCRIPTION
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
22 like this:
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:
38  def open(readonly)
39    # see below
40  end
41  def get_size(h)
42    # see below
43  end
44  def pread(h, count, offset)
45    # see below
46  end
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
57 work.
59 =head2 EXCEPTIONS
61 Ruby callbacks should throw exceptions to indicate errors.
63 =head2 RUBY CALLBACKS
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
68 L<nbdkit-plugin(3)>.
70 =over 4
72 =item C<config>
74 (Optional)
76  def config(key, value)
77    # no return value
78  end
80 =item C<config_complete>
82 (Optional)
84 There are no arguments or return value.
86 =item C<open>
88 (Required)
90  def open(readonly)
91    # return handle
92  end
94 You can return any non-nil Ruby value as the handle.  It is passed
95 back in subsequent calls.
97 =item C<close>
99 (Optional)
101  def close(h)
102    # no return value
103  end
105 =item C<get_size>
107 (Required)
109  def get_size(h)
110    # return the size of the disk
111  end
113 =item C<can_write>
115 (Optional)
117  def can_write(h)
118    # return a boolean
119  end
121 =item C<can_flush>
123 (Optional)
125  def can_flush(h)
126    # return a boolean
127  end
129 =item C<is_rotational>
131 (Optional)
133  def is_rotational(h)
134    # return a boolean
135  end
137 =item C<can_trim>
139 (Optional)
141  def can_trim(h)
142    # return a boolean
143  end
145 =item C<pread>
147 (Required)
149  def pread(h, count, offset)
150    # construct a string of length count bytes and return it
151  end
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.
161 =item C<pwrite>
163 (Optional)
165  def pwrite(h, buf, offset)
166    length = buf.length
167    # no return value
168  end
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
172 C<offset>.
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.
178 =item C<flush>
180 (Optional)
182  def flush(h)
183    # no return value
184  end
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.
189 =item C<trim>
191 (Optional)
193  def trim(h, count, offset)
194    # no return value
195  end
197 The body of your C<trim> function should "punch a hole" in the
198 backing store.
200 =back
202 =head2 MISSING CALLBACKS
204 =over 4
206 =item Missing: C<load> and C<unload>
208 These are not needed because you can just use ordinary Ruby
209 constructs.
211 =item Missing: C<name>, C<version>, C<longname>, C<description>, C<config_help>
213 These are not yet supported.
215 =back
217 =head2 THREADS
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
222 settable in future.
224 =head1 SEE ALSO
226 L<nbdkit(1)>,
227 L<nbdkit-plugin(3)>,
228 L<ruby(1)>.
230 =head1 AUTHORS
232 Richard W.M. Jones
234 =head1 COPYRIGHT
236 Copyright (C) 2013-2016 Red Hat Inc.
238 =head1 LICENSE
240 Redistribution and use in source and binary forms, with or without
241 modification, are permitted provided that the following conditions are
242 met:
244 =over 4
246 =item *
248 Redistributions of source code must retain the above copyright
249 notice, this list of conditions and the following disclaimer.
251 =item *
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.
257 =item *
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.
263 =back
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
276 SUCH DAMAGE.