NBD

From Segfault
Jump to navigation Jump to search

The network block device can be used to export a block device over the network. Several client & server implementations exist[1].

NBD

Server

Install the server:

sudo apt-get install nbd-server                                  # For Debian, Ubuntu

Adjust the configuration file as needed:

$ cat /etc/nbd-server/config
[generic]
   user = nbd
   group = nbd
   listenaddr = 127.0.0.1
   includedir = /etc/nbd-server/conf.d
$ cat /etc/nbd-server/conf.d/local.conf
[testdisk]
   exportname = /dev/loop1
   flush = true
#  copyonwrite = true

Note: since all (local) block devices are likely only to be accessible by root:disk, we may have to grant access to the nbd user:

$ ls -l /dev/loop1
brw-rw---- 1 root disk 7, 1 Mar 27 14:20 /dev/loop1

$ sudo setfacl -m u:nbd:rw /dev/loop1
$ getfacl /dev/loop1
[...]
user:nbd:rw-

Start the server, it should listen on port 10809/tcp[2]

Client

On the client node, install the nbd-client package:

sudo apt-get install nbd-client

Notes:

  • The client needs nbd kernel support[3] is needed as well (usually the nbd.ko kernel module).
  • nbd-client prior to version 3.11 have a bug[4], rendering exported devices as readonly.

Adjust the configuration as needed:

$ grep -v ^\# /etc/nbd-client
KILLALL="false"
NBD_DEVICE[0]=/dev/nbd0
NBD_TYPE[0]=r
NBD_HOST[0]=localhost
NBD_PORT[0]=10809
NBD_NAME[0]=testdisk
NBD_EXTRA[0]=

Or, for newer version:

$ grep ^nbd /etc/nbdtab 
nbd0 localhost testdisk port=10809,timeout=30,persist

After starting the client, we should see something like this:

$ ps xf | grep nb[d]
2043 ?        S<     0:00  \_ [nbd0]
2041 ?        Ss     0:00 /sbin/nbd-client localhost -N testdisk 3001 /dev/nbd0

xNBD

xNBD is another NBD server program; its server version is compatible with the stock nbd server.

Server

Install the server:

sudo apt-get install xnbd-server

As the xnbd package has been orphaned[5], we will have to build it from source:

sudo apt-get -V install gcc autoconf automake libtool mercurial libglib2.0-dev libjansson-dev
hg clone https://bitbucket.org/hirofuchi/xnbd xnbd-hg
cd xnbd-hg/trunk

autoreconf -i && ./configure --prefix=/opt/xnbd
make && sudo make install                                     # Add -ki to ignore the documentation build errors

Start with:

export PATH=$PATH:/opt/xnbd/sbin
sudo chown nbd:nbd /dev/loop1                                 # A setfacl rule doesn't seem to be enough here.
sudo -u nbd xnbd-server --target --lport 8520 /dev/loop1      # Add --daemonize after testing is done.

There doesn't seem to be an option to bind xnbd-server to a certain IP address, so make sure to filter out access from the internet to that port.

Client

Install the client:

sudo apt-get install xnbd-client

The client part of xnbd has been orphaned[5] as well, so we have to use the version we just built from source.

export PATH=$PATH:/opt/xnbd/sbin
xnbd-client --retry 600 --connect /dev/nbd0 localhost 8520 

The --retry parameter may be useful on spotty or slow connections - when all retries fail, the NBD socket will be torn down:

block nbd0: Connection timed out, shutting down connection
blk_update_request: I/O error, dev nbd0, sector 18388096
[...]
XFS (dm-5): metadata I/O error: block 0x9ffae0 ("xlog_iodone") error -1 numblks 64
XFS (dm-5): Log I/O Error Detected.  Shutting down filesystem
XFS (dm-5): Please umount the filesystem and rectify the problem(s)
block nbd0: Receive control failed (result -32)
block nbd0: shutting down socket

Links

References