File: example.lua

package info (click to toggle)
nbdkit 1.42.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,696 kB
  • sloc: ansic: 59,224; sh: 16,793; makefile: 6,463; python: 1,837; cpp: 1,116; ml: 504; perl: 502; tcl: 62
file content (82 lines) | stat: -rw-r--r-- 1,933 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
-- Example Lua plugin.
--
-- This example can be freely used for any purpose.

-- Run it from the build directory like this:
--
--   ./nbdkit -f -v lua ./plugins/lua/example.lua file=disk.img
--
-- Or run it after installing nbdkit like this:
--
--   nbdkit -f -v lua ./plugins/lua/example.lua file=disk.img
--
-- The -f -v arguments are optional.  They cause the server to stay in
-- the foreground and print debugging, which is useful when testing.
--
-- You can connect to the server using guestfish or qemu, eg:
--
--   guestfish --format=raw -a nbd://localhost
--   ><fs> run
--   ><fs> part-disk /dev/sda mbr
--   ><fs> mkfs ext2 /dev/sda1
--   ><fs> list-filesystems
--   ><fs> mount /dev/sda1 /
--   ><fs> [etc]

-- This is called from: nbdkit lua example.lua --dump-plugin
function dump_plugin ()
   print ("example_lua=1")
end

-- We expect a file=... parameter pointing to the file to serve.
function config (key, value)
   if key == "file" then
      file = value
   else
      error ("unknown parameter " .. key .. "=" .. value)
   end
end

-- Check the file parameter was passed.
function config_complete ()
   if not file then
      error ("file parameter missing")
   end
end

-- Open a new client connection.
function open (readonly)
   -- Open the file.
   local flags
   if readonly then
      flags="rb"
   else
      flags="r+b"
   end
   local fh = assert (io.open (file, flags))

    -- We can return any Lua object as the handle.  In this
    -- plugin it's convenient to return the file handle.
   return fh
end

-- Close a client connection.
function close (fh)
   assert (fh:close())
end

function get_size (fh)
   local size = assert (fh:seek ("end"))
   return size
end

function pread (fh, count, offset)
   assert (fh:seek ("set", offset))
   local data = assert (fh:read (count))
   return data
end

function pwrite (fh, buf, offset)
   assert (fh:seek ("set", offset))
   assert (fh:write (buf))
end