5 # This example can be freely used for any purpose.
7 # Run it from the build directory like this:
9 # ./src/nbdkit -f -v ./plugins/perl/.libs/nbdkit-perl-plugin.so \
10 # script=./plugins/perl/example.pl test1=foo test2=bar
12 # Or run it after installing nbdkit like this:
14 # nbdkit -f -v perl script=./plugins/perl/example.pl test1=foo test2=bar
16 # The -f -v arguments are optional. They cause the server to stay in
17 # the foreground and print debugging, which is useful when testing.
19 # You can connect to the server using guestfish or qemu, eg:
21 # guestfish --format=raw -a nbd://localhost
23 # ><fs> part-disk /dev/sda mbr
24 # ><fs> mkfs ext2 /dev/sda1
25 # ><fs> list-filesystems
26 # ><fs> mount /dev/sda1 /
29 # This is the string used to store the emulated disk (initially all
30 # zero bytes). There is one disk per nbdkit instance, so if you
31 # reconnect to the same server you should see the same disk. You
32 # could also put this into the handle, so there would be a fresh disk
34 my $disk = "\0" x
(1024*1024);
36 # This just prints the extra command line parameters, but real plugins
37 # should parse them and reject any unknown parameters.
43 print "$0: ignored parameter $key=$value\n";
50 printf ("$0: open: readonly=%d\n", $readonly);
52 # You can return any Perl value from open, and the same Perl value
53 # will be passed as the first arg to the other callbacks [in the
54 # client connected phase]. In most cases it's convenient to use a
56 my $h = { readonly
=> $readonly };
65 return length ($disk);
74 return substr ($disk, $offset, $count);
81 my $count = length ($buf);
84 substr ($disk, $offset, $count) = $buf;