3 # This example can be freely used for any purpose.
5 # Run it from the build directory like this:
7 # ./nbdkit -f -v tcl ./plugins/tcl/example.tcl file=disk.img
9 # Or run it after installing nbdkit like this:
11 # nbdkit -f -v tcl ./plugins/tcl/example.tcl file=disk.img
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 /
26 # This is called from: nbdkit tcl example.tcl --dump-plugin
31 # We expect a file=... parameter pointing to the file to serve.
32 proc config
{key value
} {
35 if { $key == "file" } {
38 error "unknown parameter $key=$value"
42 # Check the file parameter was passed.
43 proc config_complete
{} {
46 if { ![info exists
file] } {
47 error "file parameter missing"
51 # Open a new client connection.
52 proc plugin_open
{readonly
} {
61 set fh
[open $file $flags]
63 # Stop Tcl from trying to convert to and from UTF-8.
64 fconfigure $fh -translation binary
66 # We can return any Tcl object as the handle. In this
67 # plugin it's convenient to return the file handle.
71 # Close a client connection.
72 proc plugin_close
{fh
} {
79 return [file size
$file]
82 proc pread
{fh count offset
} {
84 return [read $fh $count]
87 proc pwrite
{fh buf offset
} {
89 puts -nonewline $fh $buf