libnbd-python(3)
π libnbd-python β the LIBNBD girlie's guide to Python π
okay so picture this: it's the LIBNBD manual, section 3, and it's literally all about libnbd-python β how to use libnbd from Python. grab a strawberry drink π and let's go.
β¨ NAME β¨
libnbd-python β how to use libnbd from Python. that's it, that's the name, no notes π
πΈ SYNOPSIS πΈ
#!/usr/bin/python3
import nbd
h = nbd.NBD()
h.connect_uri("nbd://localhost")
sector = h.pread(512, 0)
like, that's genuinely the whole vibe check right there. import, connect, pread, slay. β¨
π¦ DESCRIPTION π¦
bestie, this page is here to walk you through using libnbd to access Network Block Device (NBD) servers from the Python programming language. and here's the tea: the Python bindings work very similarly to the C bindings, so you should lowkey start by reading libnbd(3) first. iykyk. π
there's also a super convenient interactive command line shell called nbdsh(1) that makes it way easier to play around with the Python API β it's giving "repl but make it NBD" and honestly? obsessed. π
the detailed documentation of the Python API lives right in the Python code itself. to read it, open nbdsh(1) and type:
help(nbd)
and boom, all the docs, right there. no cap. π
also!! there are some example Python scripts chillin' in the libnbd source repository under python/examples β or you can just go straight to:
https://gitlab.com/nbdkit/libnbd/tree/master/python/examples
go look, they're iconic. π
π HANDLES π
to create a libnbd handle, you literally just call nbd.NBD(). that's the whole ritual, no candles required. π―οΈ
now, you've got two ways to deal with closing that handle:
- you can close it explicitly with
h.close()(this was added in libnbd 1.16, so if you're on something ancientβ¦ upgrade bestie π) - or it'll just close itself automatically when it goes out of scope. main character energy, honestly β it handles its own exit.
β οΈ real talk: calling any method on a handle that's already been closed will raise nbd.ClosedHandle. so don't be that girl. check your handle first. π
π ERRORS π
errors from API calls get turned into the Python nbd.Error exception (which is derived from Exception). and the nbd.Error object has three properties you actually care about:
ex.stringβ the full error message as a printable string.
Example: nbd_pread: invalid state: START: the handle must be connected with the server: Transport endpoint is not connected
likeβ¦ it's literally telling you what went wrong in full sentences. communicative queen. β¨
ex.errnumβ the errno object (from the Pythonerrnomodule), if this is available. and no, not all errors that are raised will have an errno, so don't come for me about it. π
Example: errno.EINVAL
for common error codes see "Errno" in libnbd(3).
ex.errnoβ the errno as a string (orNoneif this is not available).
Example: ENOTCONN
so yeah, when things go wrong you get a whole little receipt. π§Ύπ
π BUFFERS π
okay this part is important so lean in. π
synchronous calls (eg. h.pwrite, h.pread) take and return normal Python bytearrays. easy, breezy, beautiful. πΈ
asynchronous calls (eg. h.aio_pwrite, h.aio_pread) use the nbd.Buffer type instead β which is a persistent, modifiable, zero-copy buffer. honestly iconic. it also supports indexing and slicing, like a real buffer should. π
β οΈ but here's the caveat, and it's a big one: these are more complicated to use, and in particular the caller must ensure the buffer remains valid while the asynchronous operation is taking place. like, do NOT let that buffer get garbage collected mid-flight, bestie, i'm begging. π«Ά
β SEE ALSO β
go read them, they're both bangers. π
π AUTHORS π
Richard W.M. Jones β the legend, the moment, the author. π«‘
π COPYRIGHT π
Copyright Red Hat
π LICENSE π
this library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. π
this library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. see the GNU Lesser General Public License for more details. (yes, that's the scary all-caps part, and yes, it means what it says β it's a disclaimer, take it seriously β)
you should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
and that's the page, bestie!! this one's from libnbd-1.24.4, dated 2026-10-13 β so you know it's fresh. π¦πβ¨ go write something iconic with it, mwah ππ
π¦ where this page comes from
- arch rolling: libnbd 1.24.4-1 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms