nbdkit-python-plugin - nbdkit python plugin
nbdkit python script=/path/to/plugin.py [arguments...]
"nbdkit-python-plugin" is an embedded Python interpreter for nbdkit(1), allowing you to write nbdkit plugins in Python. Broadly speaking, Python nbdkit plugins work like C ones, so you should read nbdkit-plugin(3) first. USING A PYTHON NBDKIT PLUGIN Assuming you have a Python script which is an nbdkit plugin, you run it like this: nbdkit python script=/path/to/plugin.py You may have to add further "key=value" arguments to the command line. Read the Python script to see if it requires any. "script=..." must come first on the command line.
There is an example Python nbdkit plugin called "example.py" which
ships with the nbdkit source.
To write a Python nbdkit plugin, you create a Python file which
contains at least the following required functions (in the top level
"__main__" module):
def open(readonly):
# see below
def get_size(h):
# see below
def pread(h, count, offset):
# see below
Note that the subroutines must have those literal names (like "open"),
because the C part looks up and calls those functions directly. You
may want to include documentation and globals (eg. for storing global
state). Any other top level statements are run when the script is
loaded, just like ordinary Python.
The file does not need to include a "#!" (hash-bang) at the top, and
does not need to be executable. In fact it's a good idea not to do
that, because running the plugin directly as a Python script won't
work.
EXCEPTIONS
Python callbacks should throw exceptions to indicate errors.
PYTHON CALLBACKS
This just documents the arguments to the callbacks in Python, and any
way that they differ from the C callbacks. In all other respects they
work the same way as the C callbacks, so you should go and read
nbdkit-plugin(3).
"config"
(Optional)
def config(key, value):
# no return value
"config_complete"
(Optional)
There are no arguments or return value.
"open"
(Required)
def open(readonly):
# return handle
You can return any non-NULL Python value as the handle. It is
passed back in subsequent calls.
"close"
(Optional)
def close(h):
# no return value
After "close" returns, the reference count of the handle is
decremented in the C part, which usually means that the handle and
its contents will be garbage collected.
"get_size"
(Required)
def get_size(h):
# return the size of the disk
"can_write"
(Optional)
def can_write(h):
# return a boolean
"can_flush"
(Optional)
def can_flush(h):
# return a boolean
"is_rotational"
(Optional)
def is_rotational(h):
# return a boolean
"can_trim"
(Optional)
def can_trim(h):
# return a boolean
"pread"
(Required)
def pread(h, count, offset):
# construct a bytearray of length count bytes and return it
The body of your "pread" function should construct a buffer of
length (at least) "count" bytes. You should read "count" bytes
from the disk starting at "offset".
NBD only supports whole reads, so your function should try to read
the whole region (perhaps requiring a loop). If the read fails or
is partial, your function should throw an exception.
"pwrite"
(Optional)
def pwrite(h, buf, offset):
length = len (buf)
# no return value
The body of your "pwrite" function should write the "buf" string to
the disk. You should write "count" bytes to the disk starting at
"offset".
NBD only supports whole writes, so your function should try to
write the whole region (perhaps requiring a loop). If the write
fails or is partial, your function should throw an exception.
"flush"
(Optional)
def flush(h):
# no return value
The body of your "flush" function should do a sync(2) or
fdatasync(2) or equivalent on the backing store.
"trim"
(Optional)
def trim(h, count, offset):
# no return value
The body of your "trim" function should "punch a hole" in the
backing store.
MISSING CALLBACKS
Missing: "load" and "unload"
These are not needed because you can just use ordinary Python
constructs.
Missing: "name", "version", "longname", "description", "config_help"
These are not yet supported.
THREADS
The thread model for Python callbacks currently cannot be set from
Python. It is hard-coded in the C part to
"NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS". This may change or be
settable in future.
nbdkit(1), nbdkit-plugin(3), python(1).
Richard W.M. Jones
Copyright (C) 2013-2014 Red Hat Inc.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
* Neither the name of Red Hat nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.
Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.
Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.
Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.
The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.
Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.
Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.
Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.