1 #!@sbindir@/nbdkit perl
8 nbdkit-example4-plugin - example nbdkit plugin written in Perl
12 nbdkit example4 size=<N>
16 nbdkit example4 size=1048576
17 guestfish -a nbd://localhost
21 C<nbdkit-example4-plugin> is an example L<nbdkit(1)> plugin
24 The C<size=N> parameter is required. It specifies the disk size in
25 bytes. A single disk image, initially all zeroes, is created and can
26 be read and written by all clients. When nbdkit shuts down the disk
29 Mainly this is useful for testing and as an example of nbdkit plugins
30 written in Perl. You should also read L<nbdkit-plugin(3)> and
31 L<nbdkit-perl-plugin(3)>. Note that nbdkit plugins may be written in
32 C or other scripting languages.
36 C<nbdkit-example4-plugin> first appeared in nbdkit 1.2.
40 L<https://gitlab.com/nbdkit/nbdkit/blob/master/plugins/example4/example4.pl>,
43 L<nbdkit-perl-plugin(3)>.
60 # size=<N> (in bytes) is required on the command line.
61 # This example could be improved by parsing strings such as "1M".
71 die "unknown parameter $k";
75 # After all the parameters have been parsed, this can be used to check
76 # for invalid or missing parameters.
79 die "size was not set" unless defined $size;
82 # This is called just before forking into the background and is the
83 # last opportunity for the plugin to print an error message that the
84 # user can see (without digging through log files). Here we allocate
91 # Accept a connection from a client, create and return the handle
92 # which is passed back to other calls.
96 my $h = { readonly
=> $readonly };
100 # Close the connection.
110 return length ($disk);
119 return substr ($disk, $offset, $count);
127 my $count = length ($buf);
129 substr ($disk, $offset, $count) = $buf;
132 # If you want to display extra information about the plugin when
133 # the user does ‘nbdkit example4 --dump-plugin’ then you can print
134 # ‘key=value’ lines here.
137 print "example4_extra=hello\n";