Your company here — click to reach over 10,000 unique daily visitors

nbd_create - Man Page

create libnbd handles and fetch errors


 #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);


struct nbd_handle is an opaque structure which describes an NBD client handle and the connection to an NBD server.

Creating a libnbd handle

nbd_create creates a new handle.  It returns a pointer to the opaque handle structure.

On error this returns NULL.  See "ERROR HANDLING" in libnbd(3) for how to get further details of the error.

Closing a handle

nbd_close closes the handle, closing and freeing any associated resources.

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.

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)).

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.

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.  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.


 #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);


These functions were all present in libnbd 1.0.

See Also



Eric Blake

Richard W.M. Jones


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

Referenced By

libnbd(3), libnbd-ocaml(3), NBD(3), nbd_add_meta_context(3), nbd_aio_block_status(3), nbd_aio_block_status_64(3), nbd_aio_block_status_filter(3), nbd_aio_cache(3), nbd_aio_command_completed(3), nbd_aio_connect(3), nbd_aio_connect_command(3), nbd_aio_connect_socket(3), nbd_aio_connect_systemd_socket_activation(3), nbd_aio_connect_tcp(3), nbd_aio_connect_unix(3), nbd_aio_connect_uri(3), nbd_aio_connect_vsock(3), nbd_aio_disconnect(3), nbd_aio_flush(3), nbd_aio_get_direction(3), nbd_aio_get_fd(3), nbd_aio_in_flight(3), nbd_aio_is_closed(3), nbd_aio_is_connecting(3), nbd_aio_is_created(3), nbd_aio_is_dead(3), nbd_aio_is_negotiating(3), nbd_aio_is_processing(3), nbd_aio_is_ready(3), nbd_aio_notify_read(3), nbd_aio_notify_write(3), nbd_aio_opt_abort(3), nbd_aio_opt_extended_headers(3), nbd_aio_opt_go(3), nbd_aio_opt_info(3), nbd_aio_opt_list(3), nbd_aio_opt_list_meta_context(3), nbd_aio_opt_list_meta_context_queries(3), nbd_aio_opt_set_meta_context(3), nbd_aio_opt_set_meta_context_queries(3), nbd_aio_opt_starttls(3), nbd_aio_opt_structured_reply(3), nbd_aio_peek_command_completed(3), nbd_aio_pread(3), nbd_aio_pread_structured(3), nbd_aio_pwrite(3), nbd_aio_trim(3), nbd_aio_zero(3), nbd_block_status(3), nbd_block_status_64(3), nbd_block_status_filter(3), nbd_cache(3), nbd_can_block_status_payload(3), nbd_can_cache(3), nbd_can_df(3), nbd_can_fast_zero(3), nbd_can_flush(3), nbd_can_fua(3), nbd_can_meta_context(3), nbd_can_multi_conn(3), nbd_can_trim(3), nbd_can_zero(3), nbd_clear_debug_callback(3), nbd_clear_meta_contexts(3), nbd_connect_command(3), nbd_connection_state(3), nbd_connect_socket(3), nbd_connect_systemd_socket_activation(3), nbd_connect_tcp(3), nbd_connect_unix(3), nbd_connect_uri(3), nbd_connect_vsock(3), nbd_flush(3), nbd_get_block_size(3), nbd_get_canonical_export_name(3), nbd_get_debug(3), nbd_get_export_description(3), nbd_get_export_name(3), nbd_get_extended_headers_negotiated(3), nbd_get_full_info(3), nbd_get_handle_name(3), nbd_get_handshake_flags(3), nbd_get_meta_context(3), nbd_get_nr_meta_contexts(3), nbd_get_opt_mode(3), nbd_get_package_name(3), nbd_get_pread_initialize(3), nbd_get_private_data(3), nbd_get_protocol(3), nbd_get_request_block_size(3), nbd_get_request_extended_headers(3), nbd_get_request_meta_context(3), nbd_get_request_structured_replies(3), nbd_get_size(3), nbd_get_socket_activation_name(3), nbd_get_strict_mode(3), nbd_get_structured_replies_negotiated(3), nbd_get_tls(3), nbd_get_tls_hostname(3), nbd_get_tls_negotiated(3), nbd_get_tls_username(3), nbd_get_tls_verify_peer(3), nbd_get_uri(3), nbd_get_version(3), nbd_is_read_only(3), nbd_is_rotational(3), nbd_kill_subprocess(3), nbd_opt_abort(3), nbd_opt_extended_headers(3), nbd_opt_go(3), nbd_opt_info(3), nbd_opt_list(3), nbd_opt_list_meta_context(3), nbd_opt_list_meta_context_queries(3), nbd_opt_set_meta_context(3), nbd_opt_set_meta_context_queries(3), nbd_opt_starttls(3), nbd_opt_structured_reply(3), nbd_poll(3), nbd_poll2(3), nbd_pread(3), nbd_pread_structured(3), nbd_pwrite(3), nbd_set_debug(3), nbd_set_debug_callback(3), nbd_set_export_name(3), nbd_set_full_info(3), nbd_set_handle_name(3), nbd_set_handshake_flags(3), nbd_set_opt_mode(3), nbd_set_pread_initialize(3), nbd_set_private_data(3), nbd_set_request_block_size(3), nbd_set_request_extended_headers(3), nbd_set_request_meta_context(3), nbd_set_request_structured_replies(3), nbd_set_socket_activation_name(3), nbd_set_strict_mode(3), nbd_set_tls(3), nbd_set_tls_certificates(3), nbd_set_tls_hostname(3), nbd_set_tls_psk_file(3), nbd_set_tls_username(3), nbd_set_tls_verify_peer(3), nbd_set_uri_allow_local_file(3), nbd_set_uri_allow_tls(3), nbd_set_uri_allow_transports(3), nbd_shutdown(3), nbd_stats_bytes_received(3), nbd_stats_bytes_sent(3), nbd_stats_chunks_received(3), nbd_stats_chunks_sent(3), nbd_supports_tls(3), nbd_supports_uri(3), nbd_supports_vsock(3), nbd_trim(3), nbd_zero(3).

The man pages nbd_close(3), nbd_get_errno(3) and nbd_get_error(3) are aliases of nbd_create(3).

2024-06-25 libnbd-1.21.1