Network Block Device (NBD) server and client written in Rust
Find a file
2024-12-04 15:01:30 -06:00
.github/workflows Fix ci config 2024-10-12 18:02:39 -05:00
src Bump dependencies 2024-10-12 17:55:36 -05:00
tests Fix clippy warning in nightly 2024-10-12 18:08:30 -05:00
.editorconfig Add a skeleton of option parsing 2022-06-07 08:58:12 -04:00
.gitignore Add a CLI 2022-06-07 21:36:25 -04:00
Cargo.lock Bump dependencies 2024-12-04 15:01:30 -06:00
Cargo.toml Bump version number 2024-10-12 17:59:26 -05:00
LICENSE Add some metadata 2022-06-09 17:46:48 -04:00
README.md Clarify situation with macOS a bit better 2024-10-12 17:57:29 -05:00

rust-nbd

CI

Implementation of a basic Network Block Device (NBD) server and client written in Rust. NBD is a Linux kernel feature that exports a block device over the network, with commands for reading and writing blocks of the device by offset. The kernel natively supports NBD, but you do need sudo modprobe nbd to initialize the kernel module.

This code implements:

  • Rust modules that implement the client and server parts of the NBD protocol.
  • A userspace NBD server that is compatible with Linux.
  • A Rust re-implementation of the nbd-client utility (from the standard userland tools). This avoids needing to install anything extra to use NBD.

All of the interactions with the kernel are very Linux-specific.

macOS does not provide an nbd kernel component, but it can run the server. There is also a Rust library to interact with the server that would work if you wanted to use nbd from userspace.

Here's a quick demo of running the server and connecting with the client:

$ cargo run --release -- --size 1000 disk.img &
$ sudo modprobe nbd
$ cargo run --bin client -- /dev/nbd0

The client automatically escalates to root with sudo in order to have the necessary privilege to set up the block device. Now we can interact with /dev/nbd0 as with any other block device, for example with dd (more interestingly, you can use mkfs.ext to create a file system there and then mount it):

$ sudo chown $USER /dev/nbd0
$ dd if=/dev/zero of=/dev/nbd0 bs=4096

Finally, make sure to disconnect before running again:

$ cargo run --bin client -- --disconnect /dev/nbd0