# dnbd3 - distributed network block device (version 3)
The distributed network block device in version 3 (dnbd3) is a network protocol similar to [nbd](https://github.com/NetworkBlockDevice/nbd) to implement a distributed block-based storage system. Such a distributed block-based storage system consists of dnbd3 components, namly one or more servers and several clients. Servers are meant to expose virtual disk images as block devices to clients using dnbd3. Clients request data blocks from servers and can implement a load balancing mechanism to connect to the fastest available server for data exchange.
This repository contains the source code for the following dnbd3 components:
- **dnbd3**: Linux kernel module client for dnbd3
- **dnbd3-bench**: Benchmark utility to test dnbd3
- **dnbd3-fuse**: Fuse client for dnbd3
- **dnbd3-server**: Server to serve virtual disk images for dnbd3
The dnbd3 components can be built for the following Linux kernel versions and Unix distributions:
- Archlinux with **Linux kernel 5.10.x** or **5.4.x**
- Raspberry Pi OS with **Linux kernel 5.4.x**
- Ubuntu 20.04 with **Linux kernel 5.4.x**
- Ubuntu 18.04 with **Linux kernel 4.19.x**
- CentOS 8 with **Linux kernel 4.18.x**
- AlmaLinux 8 with **Linux kernel 4.18.x**
- FreeBSD 12.x and 13.x (only user space programs, eg. dnbd3-server)
## Build
### Preliminaries
A build of the dnbd3 components requires the installation of the following build tools and libraries under your supported Unix distribution.
#### Archlinux with Linux kernel 5.10.x or 5.4.x
```shell
pacman -S git \
make \
cmake \
gcc \
linux-headers \ # or linux-lts-headers
fuse2 \
jansson \
afl \
dpkg \
rpm-tools
```
#### Raspberry Pi OS with Linux kernel 5.4.x
```shell
apt-get install git \
make \
cmake \
gcc \
raspberrypi-kernel-headers \
libfuse-dev \
libjansson-dev \
afl \
rpm
```
#### Ubuntu 20.04 with Linux kernel 5.4.x
```shell
apt-get install git \
make \
cmake \
gcc \
linux-headers-generic \
libfuse-dev \
libjansson-dev \
rpm
```
Note that `afl` is not available on Ubuntu 20.04 and should be built from the [original sources](https://github.com/google/AFL).
#### Ubuntu 18.04 with Linux kernel 4.19.x
```shell
apt-get install git \
make \
cmake \
gcc \
linux-headers-generic \
libfuse-dev \
libjansson-dev \
afl \
rpm
```
#### CentOS 8 with Linux kernel 4.18.x
```shell
yum install git \
make \
cmake \
gcc \
kernel-devel \
elfutils-libelf-devel \
fuse-devel \
jansson-devel \
rpm-build
```
Note that `afl` is not available on CentOS 8 and should be built from the [original sources](https://github.com/google/AFL).
#### AlmaLinux 8 with Linux kernel 4.18.x
```shell
yum install git \
make \
cmake \
gcc \
kernel-devel \
elfutils-libelf-devel \
fuse-devel \
jansson-devel \
libatomic \
rpm-build
```
Note that `afl` is not available on AlmaLinux 8 and should be built from the [original sources](https://github.com/google/AFL).
#### FreeBSD 12.x and 13.x
```shell
pkg install git \
cmake \
gcc \
pkgconf \
fusefs-libs \
jansson \
afl \
rpm4
```
### Preparation
Before a build takes place, you should create a `build` directory inside the root folder of the repository. After that, change your working directory to that new directory as follows:
```shell
mkdir build
cd build
```
### Configuration
A build of the dnbd3 components can be configured and customized by the following configuration variables (CMake cache entries):
| Variable | Type | Values | Default value | Description |
|:-----------------------------|:-------|:----------------------------------------|:------------------------------|----------------------------------------------------------------------|
| `CMAKE_BUILD_TYPE` | STRING | {`Debug`, `Release`} | `Debug` | Build configuration of the dnbd3 project. |
| `KERNEL_BUILD_DIR` | PATH | {`a` .. `z`, `A` .. `Z`, `/`, `_`, `-`} | /lib/modules/`uname -r`/build | Path to Linux kernel modules to compile against. |
| `KERNEL_INSTALL_DIR` | PATH | {`a` .. `z`, `A` .. `Z`, `/`, `_`, `-`} | /lib/modules/`uname -r`/extra | Path to install Linux kernel modules. |
| `DNBD3_KERNEL_MODULE` | OPTION | {`ON`, `OFF`} | `ON` | Build the dnbd3 Linux kernel module. |
| `DNBD3_CLIENT_FUSE` | OPTION | {`ON`, `OFF`} | `ON` | Enable build of dnbd3-fuse. |
| `DNBD3_SERVER` | OPTION | {`ON`, `OFF`} | `ON` | Enable build of dnbd3-server. |
| `DNBD3_SERVER_FUSE` | OPTION | {`ON`, `OFF`} | `OFF` | Enable FUSE-Integration for dnbd3-server. |
| `DNBD3_SERVER_AFL` | OPTION | {`ON`, `OFF`} | `OFF` | Build dnbd3-server for usage with afl-fuzz. |
| `DNBD3_SERVER_DEBUG_LOCKS` | OPTION | {`ON`, `OFF`} | `OFF` | Add lock debugging code to dnbd3-server. |
| `DNBD3_SERVER_DEBUG_THREADS` | OPTION | {`ON`, `OFF`} | `OFF` | Add thread debugging code to dnbd3-server. |
| `DNBD3_RELEASE_HARDEN` | OPTION | {`ON`, `OFF`} | `OFF` | Compile dnbd3 programs in Release build with code hardening options. |
| `DNBD3_PACKAGE_DOCKER` | OPTION | {`ON`, `OFF`} | `OFF` | Enable packaging of Docker image. |
A value from the range of appropriate values can be assigend to each configuration variable by executing CMake once with the following command pattern:
```shell
cmake -D<VARIABLE>=<VALUE> [-D ...] ../.
```
> **Note that the default compiler on FreeBSD 12.x and 13.x is clang/llvm and should be changed to gcc by appending the set CMake compiler configuration variable -DCMAKE_C_COMPILER=gcc to the CMake configuration command.**
### Cross-Compiling
With the help of CMake, it is also possible to cross-compile the dnbd3 components for a Linux target architecture other than the compiling Linux host architecture. This repository is shipped with two CMake toolchain files to cross-compile all components for the following two Linux target architectures if necessary.
> **Note that all used header files (eg. Linux kernel headers) and libraries (eg. jansson, fuse) for the target architecture are installed and set up properly, so that the cross-compiler can find and use them.**
#### Cross-Compiling for _powerpc_ Target
If you want to cross-compile all dnbd3 components for the _powerpc_ Linux target architecture (eg. for a Mac G5), make sure that the `powerpc-linux-gnu-gcc`