NBD (Network Block Device)

Linux kernel module and userspace tools to expose a block device over the network. The server exports a disk image or block device; the client sees it as a local block device (e.g. /dev/nbd0) that can be formatted, mounted, or used as-is.

Packages

# server + client
nix-env -iA nixos.nbd          # NixOS
apt install nbd-server nbd-client  # Debian/Ubuntu

Server

# Serve a file as a block device
nbd-server 10809 /path/to/disk.img
 
# Config file: /etc/nbd-server/config
[generic]
  port = 10809
 
[myexport]
  exportname = /var/lib/nbd/disk.img
  readonly = false

Client

modprobe nbd                        # load kernel module (if not auto-loaded)
nbd-client server.host 10809 /dev/nbd0
# Now /dev/nbd0 is available as a local block device
 
mount /dev/nbd0 /mnt
# ... use it ...
 
umount /mnt
nbd-client -d /dev/nbd0             # disconnect

NixOS server config

services.nbd = {
  server = {
    enable = true;
    exports.myimage = {
      path = "/var/lib/nbd/disk.img";
      allowAddresses = [ "192.168.1.0/24" ];
    };
  };
};

Use cases

  • Diskless/netboot clients with a shared root image
  • Live migration of block devices
  • Remote disk images for VMs
  • Thin provisioning (expose a sparse file as a block device)

Comparison

NBDiSCSI
ProtocolSimple, Linux-nativeIndustry standard
AuthIP-based onlyCHAP, mutual CHAP
MultipathNoYes
OverheadLowHigher
Use caseSimple LAN setupsEnterprise SAN