Repository navigation
NBD
NBD
(Network Block Device) makes a disk on another machine look like a local one.
The TinyNBD library implements an
NBD server and client for Arduino. NBD is a block protocol, and so is this
library's USBMscBlockDevice interface, so the two fit together directly.
No NBD code is built into usb-msc; the adapter is a small optional header.
NBD server (Linux, qemu, nbdkit, TinyNBD)
| TCP
nbd::NBDClient -> USBMscNbdBlockDevice -> USBMscFatFS / USBMscExt4FS / USBMscAutoFS (files)
-> USBDeviceMSC (USB drive)
USBMscNbdBlockDevice (#include "USBMscNbdBlockDevice.h") wraps a connected
nbd::NBDClient as a USBMscBlockDevice with 512-byte blocks.
WiFiClient wifi;
nbd::NBDClient nbd_client(wifi);
USBMscNbdBlockDevice remote(nbd_client);
nbd_client.connect("192.168.1.10", nbd::NBD_DEFAULT_PORT, "disk");- The header is not included by any other file of this library. TinyNBD is only
needed by sketches that include it, and is not listed in
depends=. - The client must be connected before use and must outlive the adapter. The
adapter never connects or reconnects; after a lost connection every call
fails until you call
connect()again. - Requests are split to the server's maximum payload.
- Writes fail on a read-only export (
remote.isReadOnly()).flush()is forwarded to the server when it supports it. - The block size is fixed at 512, which is what the filesystem backends need. The server's own preferred block size (4096 by default) is not used.
Every filesystem class has a begin(USBMscBlockDevice &) overload next to the
USB one, so any block device can be mounted: an NBD export, but also a RAM
disk, an SD card or your own subclass.
| Class | Mounts |
|---|---|
USBMscFatFS |
FAT12/16/32, exFAT |
USBMscExt4FS, USBMscExt4FSReadOnly
|
ext2/3/4 |
USBMscAutoFS<> |
whichever of the above is found |
USBMscExt4FSReadOnly extFs;
...
if (extFs.begin(remote)) {
File f = extFs.fs().open("/hello.txt", "r"); // USBMscFile on STM32
}
...
extFs.end(); // unmountAs with USB sticks, the first partition of the right type in an MBR is used;
an export without a partition table is mounted whole. GPT is not supported.
Unmount with end() before you destroy or disconnect the block device. See
examples/NBDMountFilesystem.
- The filesystem classes are part of the host side of this library, so the
sketch must be built where the USB host code compiles (the same board and
USB-stack setting as the
USBHostMSC_*examples). No USB device has to be attached. On a board without a USB host stack, such as a classic ESP32, use TinyFATFS or TinyEXT4 directly instead (next section). - Block devices must use 512-byte blocks.
- Reads and writes are synchronous TCP round trips. Expect much lower speed than local storage, and prefer a read-only export when you can.
USBDeviceMSC::begin() takes any USBMscBlockDevice, so a remote disk can be
presented to a PC as a USB drive:
USBDeviceMSC msc;
nbd_client.connect(...); // before USB starts
msc.begin(remote);The PC caches aggressively, so do not mount the same export on the board at the same time. Several writers on one export corrupt it; use a read-only export if several clients need it.
TinyFATFS and TinyEXT4 ship their own NBDClientIO driver that mounts an
export directly with their own SD-style API, and TinyNBD documents
the same for LittleFS. They don't need the USB host code, but they don't parse
the MBR or offer fs::FS/USBMscFile. Use this library's route when you want
the same API as for USB sticks, partition support, or USBMscAutoFS.
- NBD has no authentication or encryption: use it on a trusted network.
- Only one client may mount an export with write access.
- Flash and SD wear: see TinyNBD's
docs/notes.mdbefore exporting flash storage with write access.