Why Use NBDs?
Why would you want to use an NBD rather than a more common file-sharing protocol? Several scenarios come to mind:fsck
, for instance) than the server, providing NBD access may make sense.Obtaining NBD Software
NBD software for Linux includes three components:nbd
package includes both client and server software. Some distributions provide NBD packages; for instance, Ubuntu calls them nbd-client
and nbd-server
.
It’s usually easiest to install a ready-made package, but if you can’t
find one for your distribution, the installation process is
conventional:cd
into the resulting directory../configure
make
make install
nbd-server
and nbd-client
programs, along with associated man pages. As of version 2.99, the NBD source package includes a subdirectory called winnbd, which includes a Windows NBD server. I didn’t test it for this column, though.Preparing an NBD Server
The first step in preparing an NBD server is to set aside a block device for use by the server. Typically, this will be a hard disk partition, although it could be a logical volume manager (LVM) volume, redundant array of independent disks (RAID) device, CD-ROM/DVD-ROM drive, or some other block device. You may also create a regular file and export it as a block device. This approach is similar to using a loopback device locally, but you give a remote system access to the file as if it were a block device. This can be handy if you want to provide clients with access to a variety of image files (CD-ROM images, say).# nbd-server 2000 /dev/sdb1This command exports /dev/sdb1 using port 2000. Note that the device filename must use an absolute path, not a relative path. The version of NBD I tested issues a warning message about being unable to open its config file. This message seems to be harmless, so you can ignore it. In principle, you can run
nbd-server
as a non-root
user; however, the user must have read (and perhaps write) access to
whatever file or device you export. Note that both the port number and
block device are required parameters to nbd-server
. You can read the nbd-server
man page to learn about its options; some highlights include:-r
exports the device in read-only mode, which is a
highly recommended precaution if clients should not be able to write to
the device.-c
creates a copy-on-write export, meaning that client
write operations are performed on a temporary file, rather than on the
original file. The temporary file is discarded when the client
disconnects.-a timeout
specifies a timeout period (in seconds), after which the NBD server terminates the connection.-l host_list_file
specifies a file that includes the IP addresses of hosts that may connect to the NBD server. The default value is nbd_server.allow. If the file is missing, any host may connect. This tool is obviously useful for controlling access to your NBD server.-C config_file
tells nbd-server
where to find its configuration file (described shortly).-c
(copy-on-write) option deserves a few more comments. Because most modern Linux distributions use udev to create a dynamic /dev directory tree on a virtual filesystem, -c
can produce write errors after a while. To avoid this problem, try
creating symbolic links on a conventional filesystem to point to the
real device file and then export the symbolic links rather than the
device files to which they point. The -c
option also has the drawback of reducing performance. nbd-server
’s options in action, consider the following extended command:# nbd-server 2000 /dev/sdb1 -r -l /etc/nbd.allowThis command provides read-only access to /dev/sdb1 to those clients whose IP addresses appear in /etc/nbd.allow.
Preparing a Server Configuration File
Instead of specifying options on the command line, you can create an NBD server configuration file. This file consists of named sections, each section name enclosed in square brackets ([]
). Within each section, lines contain parameter/value pairs, with parameters and values separated by equal signs (=
). The [generic]
section is required and sets global options. Each subsequent named section sets options for particular devices. Listing One presents an example, which demonstrates two NBD exports, one for /dev/sdb1 and one for /dev/fd0.[generic]The easiest way to create a configuration file is to specify the options you want and use the
listenaddr = 0.0.0.0
authfile = /etc/nbd-server/allow
[diskfile]
exportname = /dev/sdb1
copyonwrite = true
port = 2000
[floppy]
exportname = /dev/fd0
port = 2001
-o section_name
option to nbd-server
, where section_name
is the name you want to give to the section. You can then cut-and-paste
the output into your configuration file. Be sure each section specifies
a unique port number! You may also want to peruse the README
file that comes with the NBD source package; it provides examples of a
few additional options that the configuration file supports.nbd-server
via its -C
option. The server will then share all the listed block devices, each
on its specified port and with its specified options, with just one
call to nbd-server
.Preparing an NBD Client
With an NBD-enabled kernel running, you can use thenbd-client
program on the client computer to map a remote NBD to a local device file:# nbd-client nbdserver 2000 /dev/nbd0In this example,
nbdserver
is the hostname or IP address of the server computer, 2000
is the port number associated with the device you want to use, and /dev/nbd0
is the local device filename you want to link to the remote server. A few caveats and tricks require attention:nbd
module before running nbd-client
.nbd
module is loaded, some systems (including Fedora and Ubuntu systems I’ve checked) automatically create /dev/nbd# devices, where # is a number from 0 up. Upon creation, these device files are not assigned to any server, but you should specify one of these devices in your nbd-client
command line.nbd-client
program fails silently if it can’t find
an NBD server on the specified computer and port. If it does find an
NBD server, it displays a couple of lines of text summarizing the size
of the device it finds. If something goes wrong during this process, it
may hang with the word Negotiation
on the screen. This
looks like a prompt, but it isn’t. If you see this, chances are there’s
something wrong with your NBD server configuration.nbd-client
program must be run as root.nbd-client
command specified earlier works fine in many cases, the program supports some additional options, including:-swap
tells the system that the device should be used as swap space. This helps prevent deadlocks.timeout=seconds
specifies the timeout period for NBD operations.Using NBDs
With your NBD client pointing to a network-accessible NBD server, you can begin using NBD. You can treat your /dev/nbd# device just as you would any local block device. Typically, you’ll mount it as a filesystem (perhaps first creating a filesystem on it) withmount
:# mount /dev/nbd0 /mnt/nbdThis command mounts the device at /mnt/nbd. If the device was exported for read/write operations, you’ll be able to write to it, with the caveat that two clients should not connect to the same read/write device simultaneously unless you use the
-c
option on the server!mkfs
to create a filesystem, check a filesystem with fsck
, and so on. Many of these actions require that you have read/write access to the device, though.umount
, just as you would a local filesystem. You can then type nbd-client -d /dev/nbd0
(substituting the correct device filename) to terminate the
client/server NBD network link. Although this last step may not always
be strictly necessary, it can help you avoid confusion over what
devices are active, and it causes the server to delete diff files it
created if you used the -c
option on the server.