nbd_create(3)

πŸ’… nbd_create(3) β€” the libnbd handle glow-up page

okay so bestie, welcome to nbd_create(3) from the LIBNBD manual, section 3. this is the page where you meet your handle, learn to let it go, and find out how to actually read the error messages when things go sideways. let's get into it 🫢


✨ NAME ✨

nbd_create, nbd_close, nbd_get_error, nbd_get_errno β€” literally the four functions that create libnbd handles and fetch errors. the whole friend group πŸ’–


🌸 SYNOPSIS 🌸

#include <libnbd.h>

struct nbd_handle *nbd;

struct nbd_handle *nbd_create (void);

void nbd_close (struct nbd_handle *nbd);

const char *nbd_get_error (void);

int nbd_get_errno (void);

look at that. gorgeous. no cap ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

struct nbd_handle is an opaque structure which describes an NBD client handle and the connection to an NBD server. opaque means you don't get to peek inside β€” it's giving main character with a private account, iykyk πŸ™ˆ. for an overview of the NBD client and NBD handles, see libnbd(3).

πŸŽ€ Creating a libnbd handle πŸŽ€

nbd_create creates a new handle. it returns a pointer to the opaque handle structure. that's it, that's the whole vibe β€” you get a handle, you slay πŸ’…

On error this returns "NULL". See "ERROR HANDLING" in libnbd(3) for how to get further details of the error. (yes bestie, NULL check is mandatory, that's not a drill 😭)

🌈 Closing a handle 🌈

nbd_close closes the handle, closing and freeing any associated resources. clean up after yourself, the way a real one does πŸ’œ

The final status of any command that has not been retired (whether by nbd_aio_command_completed(3) or by a low-level completion callback returning 1) is lost. so like... poof. gone. it's giving "you left it on read" πŸ’€

This function is not safe to call while any other thread is still using any nbd_* API on the same handle. This function can block in the case where we wait for a subprocess (eg. one created with nbd_connect_command(3)).

so fr fr, no closing a handle while another thread is touching it. and heads up, it can block if we're waiting on a subprocess (like one you made with nbd_connect_command(3)). don't say i didn't warn you 🫢

🧚 Getting the latest error message in the thread 🧚

nbd_get_error returns the most recent error message in the current thread. The error message is only valid if called immediately after the failing call, from the same thread. The error string returned will be freed up next time any libnbd API is called from the same thread, so if you need to keep it you must make a copy. like, it's on a timer bestie β€” screenshot it or it's gone πŸ“Έ

This should never return "NULL" provided there was an error returned from the immediately preceding libnbd call in the current thread.

nbd_get_errno returns the most recent errno in the current thread. Not all errors have corresponding errnos, so even if there has been an error this may return 0. so 0 doesn't mean "we're fine", it means "no errno to give you, don't @ me". Error codes are the standard ones from <errno.h> ⭐

See "ERROR HANDLING" in libnbd(3) for more discussion of how error handling works in libnbd.


πŸ”₯ EXAMPLE πŸ”₯

here's the whole create-check-close ritual, in code:

#include <libnbd.h>

main ()
{
  struct nbd_handle *nbd;

  nbd = nbd_create ();
  if (nbd == NULL) {
    fprintf (stderr, "%s\n", nbd_get_error ());
    nbd_close (nbd);
    exit (EXIT_FAILURE);
  }
  nbd_close (nbd);
  exit (EXIT_SUCCESS);
}

see how it calls nbd_get_error () right after the failure, in the same thread? that's the whole point, bestie. iconic behaviour πŸ‘‘


⭐ VERSION ⭐

These functions were all present in libnbd 1.0. day one legends, for real πŸŽ€


πŸ¦‹ SEE ALSO πŸ¦‹

go read those next, i'll wait πŸ’œ


πŸ‘‘ AUTHORS πŸ‘‘

  • Eric Blake
  • Richard W.M. Jones

the architects. we stan 🫢


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.

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

yes, that's ALL CAPS "WITHOUT ANY WARRANTY" β€” the license is yelling and honestly? valid πŸ’€

okay that's the whole page, bestie! this lil masterpiece came to you from libnbd-1.25.8, dated 2026-10-13, and it's called nbd_create(3) β€” now go create a handle and close it properly, mwah πŸ˜˜πŸ’…βœ¨

πŸ“¦ where this page comes from