nbdkit-service(1)

πŸ’– nbdkit-service(1) πŸ’–

okay bestie, so this page is all about nbdkit-service β€” yes it lives in the nbdkit manual fam, and it's the one you read when you wanna run nbdkit as a whole service instead of just poking it from your shell. it's giving "background character who's actually the main character energy" 🫢


✨ NAME ✨

nbdkit-service β€” running nbdkit as a service, and systemd socket activation πŸ’…


🌸 DESCRIPTION 🌸

okay so most people just start nbdkit straight from the command line, or they run it from inside another program (peep nbdkit-captive(1) for that whole situation). but plot twist: you can ALSO run nbdkit as a standalone service, and that's literally what this entire page is about. no cap. 🌈


πŸ¦‹ SOCKET ACTIVATION πŸ¦‹

nbdkit supports socket activation (some people call it systemd socket activation). it's a super simple protocol, and here's the tea: instead of nbdkit opening the listening socket(s) itself, the parent process (usually systemd or libnbd) just hands over pre-opened file descriptors. iconic behavior honestly. ✨

one reason you'd wanna do this: serving infrequent NBD requests through a superserver, without needing nbdkit to be sitting there running the whole time. πŸ’€ another reason: running nbdkit from a controlling libnbd process. both valid, both slay.

socket activation kicks in when both the LISTEN_FDS and LISTEN_PID environment variables are set. πŸ‘€

and when you're in this mode? using -i, -p, --run, -s or -U flags on the command line will cause an error. also, in this mode nbdkit does not fork into the background (so like, -f is implied, periodt).


πŸŽ€ Using socket activation with systemd πŸŽ€

to do socket activation with systemd, you make a unit file ending in .socket (for example /etc/systemd/system/nbdkit.socket) that contains:

[Unit]
Description=NBDKit Network Block Device server

[Socket]
ListenStream=10809
# Optional settings to detect dead clients:
#KeepAlive=true
#KeepAliveTimeSec=60
#KeepAliveIntervalSec=10
#KeepAliveProbes=5

[Install]
WantedBy=sockets.target

there are various formats for the ListenStream key btw β€” go read systemd.socket(5) for more info. πŸ“–

then also make a service unit (for example /etc/systemd/system/nbdkit.service) containing:

[Service]
ExecStart=/usr/sbin/nbdkit file /path/to/serve
# Optional settings to run as non-root:
#User=nbd
#Group=nbd

and for even more on systemd + socket activation, look at http://0pointer.de/blog/projects/socket-activation.html 🌐


πŸ’œ Using socket activation with libnbd πŸ’œ

libnbd(3) nbd_connect_systemd_socket_activation(3) lets you run nbdkit from a program that uses libnbd. here's an example, bestie:

struct nbd_handle *nbd = nbd_create ();
if (nbd == NULL) {
  fprintf (stderr, "%s\n", nbd_get_error ());
  exit (EXIT_FAILURE);
}
char *argv[] = {
  "nbdkit", "--exit-with-parent", "memory", "1G", NULL
};
if (nbd_connect_systemd_socket_activation (nbd,
                                           args) == -1) {
  fprintf (stderr, "%s\n", nbd_get_error ());
  exit (EXIT_FAILURE);
}

🌟 INETD AND XINETD 🌟

you can also run nbdkit from inetd(8) or xinetd(8). for inetd, use:

nbd stream tcp nowait root /usr/sbin/nbdkit nbdkit -s file /tmp/disk.img

for xinetd, create a file /etc/xinetd.d/nbdkit containing:

service nbd
{
    socket_type = stream
    wait = no
    user = root
    server = /usr/sbin/nbdkit
    server_args = -s file /tmp/disk.img
}

🧚 AF_VSOCK 🧚

on Linux, nbdkit supports the AF_VSOCK address family / protocol. this lets you serve NBD devices into virtual machines without needing a regular network connection at all. it's giving ✨direct line✨.

not me clarifying this: this is different from the usual case where you present NBD as a virtual block device to a guest (which the guest sees as something like a SATA or virtio-scsi disk). with AF_VSOCK the virtual machine sees a raw NBD socket which it can connect to by opening an AF_VSOCK connection. for more about this protocol, see https://wiki.qemu.org/Features/VirtioVsock πŸ¦‹

⭐ AF_VSOCK example ⭐

to set up an AF_VSOCK server, use for example:

nbdkit --vsock [--port PORT] memory 1G

the optional -p/--port argument is what you use to change the AF_VSOCK port number. heads up: these port numbers live in a different namespace from TCP/IP port numbers. also, unlike TCP, the port numbers are 32 bit. the default port is 10809. πŸ’…

the guest that wants to access nbdkit has to be configured for virtio-vsock. on the qemu command line, use:

qemu ... -device vhost-vsock-pci,id=vhost-vsock-pci0

for libvirt, add this element to the <devices> section:

<vsock/>

if you see the error "unable to open vhost-vsock device" then you might have to unload the VMCI transport on the host:

modprobe -r vmw_vsock_vmci_transport

once nbdkit and the guest are both running, from inside the guest you can connect to nbdkit on the host using libnbd:

nbdinfo nbd+vsock:///

nbdsh -c 'h.connect_vsock(2, 10809)' -c 'print(h.get_size())'

ate that. πŸ”₯


🌈 ENVIRONMENT VARIABLES 🌈

  • LISTEN_FDS
  • LISTEN_PID

if these are present in the environment when nbdkit starts up, they trigger SOCKET ACTIVATION. that's the whole job, no notes. ✨


πŸ‘‘ SEE ALSO πŸ‘‘

nbdkit(1), nbdkit-client(1), nbdkit-exitlast-filter(1), nbdkit-exitwhen-filter(1), nbdkit-ip-filter(1), nbdkit-limit-filter(1), systemd(1), systemd.socket(5), inetd(8), xinetd(8), syslog(3), rsyslogd(8), journalctl(1), libnbd(3), nbd_connect_systemd_socket_activation(3), nbdsh(1).

go read them, bestie, i believe in you 🫢


πŸ’ AUTHORS πŸ’

  • Eric Blake
  • Richard W.M. Jones
  • Pino Toscano

the icons who wrote this, we stan πŸ˜­πŸ’–


Copyright Red Hat


πŸ“œ LICENSE πŸ“œ

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.

like, read it, it's literally the rules. πŸ™ˆ


and that's the whole page!! this was nbdkit-service(1) from nbdkit-1.48.1, dated 2026-08-29 β€” ty for coming to my ted talk, mwah πŸ’‹βœ¨πŸŽ€

πŸ“¦ where this page comes from