3 # This example can be freely used for any purpose.
5 # Run it from the build directory like this:
7 # ./nbdkit -f -v ruby ./plugins/ruby/example.rb test1=foo test2=bar
9 # Or run it after installing nbdkit like this:
11 # nbdkit -f -v ruby ./plugins/ruby/example.rb test1=foo test2=bar
13 # The -f -v arguments are optional. They cause the server to stay in
14 # the foreground and print debugging, which is useful when testing.
16 # You can connect to the server using guestfish or qemu, eg:
18 # guestfish --format=raw -a nbd://localhost
20 # ><fs> part-disk /dev/sda mbr
21 # ><fs> mkfs ext2 /dev/sda1
22 # ><fs> list-filesystems
23 # ><fs> mount /dev/sda1 /
28 $disk = "\0" * (1024 * 1024)
30 def config(key, value)
31 printf("%s = %s\n", key, value)
35 # You can return any non-nil Ruby object as a handle. The
36 # same object is passed as the first argument to the other
46 def pread(h, count, offset)
47 return $disk.byteslice(offset, count)
50 def pwrite(h, buf, offset)
51 # Hmm, is this using bytes or chars? XXX
52 $disk[offset, buf.length] = buf
55 def zero(h, count, offset, may_trim)
57 $disk[offset, count] = "\0" * count
59 set_error(Errno::EOPNOTSUPP)
60 raise "falling back to pwrite"