Skip to content

Commit

Permalink
Add support for an nbdtab file
Browse files Browse the repository at this point in the history
This file, the format of which is modeled after fstab, will allow
configuring an NBD device by way of a simple

  nbd-client nbdX

rather than having to specify everything on the command line.

Also add documentation for the file format.
  • Loading branch information
yoe committed Oct 9, 2015
1 parent 42422d7 commit 950f7c3
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 8 deletions.
2 changes: 1 addition & 1 deletion autogen.sh
@@ -1,4 +1,4 @@
#!/bin/sh
set -ex
make -C man -f Makefile.am nbd-server.1.sh.in nbd-server.5.sh.in nbd-client.8.sh.in nbd-trdump.1.sh.in
make -C man -f Makefile.am nbd-server.1.sh.in nbd-server.5.sh.in nbd-client.8.sh.in nbd-trdump.1.sh.in nbdtab.5.sh.in
exec autoreconf -f -i
3 changes: 2 additions & 1 deletion configure.ac
Expand Up @@ -170,6 +170,7 @@ AC_CONFIG_FILES([Makefile
man/nbd-client.8.sh
man/nbd-server.5.sh
man/nbd-server.1.sh
man/nbd-trdump.1.sh])
man/nbd-trdump.1.sh
man/nbdtab.5.sh])
AC_OUTPUT

16 changes: 12 additions & 4 deletions man/Makefile.am
@@ -1,8 +1,8 @@
man_MANS = nbd-server.1 nbd-server.5 nbd-client.8 nbd-trdump.1
man_MANS = nbd-server.1 nbd-server.5 nbd-client.8 nbd-trdump.1 nbdtab.5
CLEANFILES = manpage.links manpage.refs
DISTCLEANFILES = nbd-server.1 nbd-client.8 nbd-server.5 nbd-trdump.1
MAINTAINERCLEANFILES = nbd-server.1.sh.in nbd-client.8.sh.in nbd-server.5.sh.in nbd-trdump.1.sh.in
EXTRA_DIST = nbd-server.1.in.sgml nbd-client.8.in.sgml nbd-server.5.in.sgml nbd-trdump.1.in.sgml nbd-server.1.sh.in nbd-server.5.sh.in nbd-client.8.sh.in nbd-trdump.1.sh.in sh.tmpl
DISTCLEANFILES = nbd-server.1 nbd-client.8 nbd-server.5 nbd-trdump.1 nbdtab.5
MAINTAINERCLEANFILES = nbd-server.1.sh.in nbd-client.8.sh.in nbd-server.5.sh.in nbd-trdump.1.sh.in nbdtab.5.sh.in
EXTRA_DIST = nbd-server.1.in.sgml nbd-client.8.in.sgml nbd-server.5.in.sgml nbd-trdump.1.in.sgml nbd-server.1.sh.in nbd-server.5.sh.in nbd-client.8.sh.in nbd-trdump.1.sh.in nbdtab.5.sh.in sh.tmpl

nbd-server.1: nbd-server.1.sh
sh nbd-server.1.sh > nbd-server.1
Expand All @@ -12,6 +12,8 @@ nbd-client.8: nbd-client.8.sh
sh nbd-client.8.sh > nbd-client.8
nbd-trdump.1: nbd-trdump.1.sh
sh nbd-trdump.1.sh > nbd-trdump.1
nbdtab.5: nbdtab.5.sh
sh nbdtab.5.sh > nbdtab.5
nbd-server.1.sh.in: nbd-server.1.in.sgml sh.tmpl
LC_ALL=C docbook2man nbd-server.1.in.sgml
cat sh.tmpl > nbd-server.1.sh.in
Expand All @@ -36,3 +38,9 @@ nbd-trdump.1.sh.in: nbd-trdump.1.in.sgml sh.tmpl
cat NBD-TRDUMP.1 >> nbd-trdump.1.sh.in
echo "EOF" >> nbd-trdump.1.sh.in
rm NBD-TRDUMP.1
nbdtab.5.sh.in: nbdtab.5.in.sgml sh.tmpl
LC_ALL=C docbook2man nbdtab.5.in.sgml
cat sh.tmpl > nbdtab.5.sh.in
cat NBDTAB.5 >> nbdtab.5.sh.in
echo "EOF" >> nbdtab.5.sh.in
rm NBDTAB.5
216 changes: 216 additions & 0 deletions man/nbdtab.5.in.sgml
@@ -0,0 +1,216 @@
<!doctype refentry PUBLIC "-//OASIS//DTD DocBook V4.1//EN" [

<!-- Process this file with docbook-to-man to generate an nroff manual
page: `docbook-to-man manpage.sgml > manpage.1'. You may view
the manual page with: `docbook-to-man manpage.sgml | nroff -man |
less'. A typical entry in a Makefile or Makefile.am is:

manpage.1: manpage.sgml
docbook-to-man $< > $@
-->

<!-- Fill in your name for FIRSTNAME and SURNAME. -->
<!ENTITY dhfirstname "<firstname>Wouter</firstname>">
<!ENTITY dhsurname "<surname>Verhelst</surname>">
<!-- Please adjust the date whenever revising the manpage. -->
<!ENTITY dhdate "<date>$Date: 2006-10-18 15:01:57 +0200 (wo, 18 okt 2006) $</date>">
<!-- SECTION should be 1-8, maybe w/ subsection other parameters are
allowed: see man(7), man(1). -->
<!ENTITY dhsection "<manvolnum>5</manvolnum>">
<!ENTITY dhemail "<email>wouter@debian.org</email>">
<!ENTITY dhusername "Wouter Verhelst">
<!ENTITY dhucpackage "<refentrytitle>NBDTAB</refentrytitle>">
<!ENTITY dhpackage "$sysconfdir/nbdtab">

<!ENTITY debian "<productname>Debian GNU/Linux</productname>">
<!ENTITY gnu "<acronym>GNU</acronym>">
]>

<refentry>
<refentryinfo>
<address>
&dhemail;
</address>
<author>
&dhfirstname;
&dhsurname;
</author>
<copyright>
<year>2015</year>
<holder>&dhusername;</holder>
</copyright>
&dhdate;
</refentryinfo>
<refmeta>
&dhucpackage;

&dhsection;
</refmeta>
<refnamediv>
<refname>&dhpackage;</refname>

<refpurpose>configuration file for nbd-client</refpurpose>
</refnamediv>
<refsynopsisdiv>
<cmdsynopsis>
<command>&dhpackage; </command>

</cmdsynopsis>
</refsynopsisdiv>
<refsect1>
<title>DESCRIPTION</title>

<para>This file allows to configure predefined connections for
nbd-client. It may contain multiple definitions, one per line,
each of which contains four space-separated fields.</para>

<para>To connect a device specified in the nbdtab file,
run <command>nbd-client</command>(8) with the short name of that
device as the sole argument. It will then look up the required
information in <filename>nbdtab</filename>, and make the
connection.</para>

<para>Fields are separated from one another by any number of space
or tab characters; records are separated from one another by
newline characters. The file may also contain any number of
comments, which start with a '#' character and continue until the
end of the line or the end of the file, whichever is first.</para>
<refsect2>
<title>Fields</title>
<para>The file contains the following fields:</para>
<orderedlist>
<listitem>
<para>The short name of the device file. That is, it should
contain the name of the device without the leading
<filename>/dev/</filename> part; e.g., it could say
<filename>nbd0</filename>.</para>
</listitem>
<listitem>
<para>The hostname (in case of a TCP socket) or filename (in
case of a unix domain socket) on which the server is
listening.</para>
</listitem>
<listitem>
<para>The name of the export as exported by
<command>nbd-server</command>.</para>
</listitem>
<listitem>
<para>Any extra options. This field is optional (no pun
intended), and need not appear in a file if no options are
necessary. The options recognized by
<command>nbd-client</command>(8) are specified below, in the
section "Options". Any unknown options in
this field will produce a warning by
<command>nbd-client</command>, unless they are prepended by
an underscore ('_') character; the underscore is
specifically reserved for local use, or for distribution
customization.</para>
</listitem>
</orderedlist>
</refsect2>
<refsect2>
<title>Options</title>
<para>Every command-line <command>nbd-client</command> option
has a corresponding option in the <filename>nbdtab</filename> file,
and vice versa; where this isn't the case, that is a
bug.</para>
<para>Individual options in this field should be separated from
one another by the comma character.</para>
<variablelist>
<varlistentry>
<term><option>bs=<replaceable>block size</replaceable></option></term>
<listitem>
<para>The block size for this export. If this option is
not used, the kernel's default will be used
instead.</para>
<para>Corresponds to the <option>-b</option> option on the
command line.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>port=<replaceable>port number</replaceable></option></term>
<listitem>
<para>The port on which to communicate with the
<command>nbd-server</command>. Defaults to the
IANA-assigned port for NBD, 10809.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>timeout=<replaceable>timeout</replaceable></option></term>
<listitem>
<para>The timeout. If this option is not specified, no
timeout is configured.</para>
<para>Corresponds to the <option>-t</option> option on the
command line.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>persist</option></term>
<listitem>
<para>Persist the connection, using the semantics of the
<option>-p</option> command-line option.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>swap</option></term>
<listitem>
<para>Optimize for swap; <option>-s</option>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>sdp</option></term>
<listitem>
<para>Use the Socket Direct protocol; <option>-S</option>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>unix</option></term>
<listitem>
<para>Use a Unix Domain socket to connect to the server;
<option>-u</option>.</para>
</listitem>
</varlistentry>
</variablelist>
</refsect2>
</refsect1>
<refsect1>
<title>SEE ALSO</title>

<para>nbd-server (1), nbd-client (8), nbd-trdump (8)</para>


</refsect1>
<refsect1>
<title>AUTHOR</title>
<para>The NBD kernel module and the NBD tools were originally
written by Pavel Machek (pavel@ucw.cz)</para>

<para>The Linux kernel module is now maintained by Paul Clements
(Paul.Clements@steeleye.com), while the userland tools are
maintained by &dhusername; (&dhemail;)</para>

<para>On The Hurd there is a regular translator available to perform the
client side of the protocol, and the use of
<command>nbd-client</command> is not required. Please see the
relevant documentation for more information.</para>

<para>This manual page was written by &dhusername; (&dhemail;).
Permission is granted to copy, distribute and/or modify this
document under the terms of the <acronym>GNU</acronym> General
Public License, version 2, as published by the Free Software
Foundation.</para>

</refsect1>
<refsect1>
<title>EXAMPLES</title>
<para>A simple <filename>nbdtab</filename> file could look like
this:</para>
<programlisting>
# swap space, called "swapexport" on the server
# optimize for swap, and try to reconnect upon disconnect.
nbd0 nbdserver.example.com swapexport swap,persist
# other export, called "data" on the server. No options for this one.
nbd1 nbdserver.example.com data
</programlisting>
</refsect1>
</refentry>
8 changes: 6 additions & 2 deletions nbd-client.c
Expand Up @@ -352,7 +352,7 @@ void negotiate(int sock, u64 *rsize64, uint16_t *flags, char* name, uint32_t nee
*rsize64 = size64;
}

bool get_from_config(char* cfgname, char** name_ptr, char** dev_ptr, char** hostn_ptr, int* bs, int* timeout, int* persist, int* swap, int* sdp, int* b_unix) {
bool get_from_config(char* cfgname, char** name_ptr, char** dev_ptr, char** hostn_ptr, int* bs, int* timeout, int* persist, int* swap, int* sdp, int* b_unix, char**port) {
int fd = open(SYSCONFDIR "/nbdtab", O_RDONLY);
if(fd < 0) {
fprintf(stderr, "while opening %s: ", SYSCONFDIR "/nbdtab");
Expand Down Expand Up @@ -407,6 +407,10 @@ bool get_from_config(char* cfgname, char** name_ptr, char** dev_ptr, char** host
*timeout = (int)strtol(loc+8, &loc, 0);
goto next;
}
if(!strncmp(loc, "port=", 5)) {
*port = strndup(loc+5, strcspn(loc+5, ","));
goto next;
}
if(!strncmp(loc, "persist", 7)) {
loc += 7;
*persist = 1;
Expand Down Expand Up @@ -680,7 +684,7 @@ int main(int argc, char *argv[]) {
if(hostname) {
if((!name || !nbddev) && !(opts & NBDC_DO_LIST)) {
if(!strncmp(hostname, "nbd", 3)) {
if(!get_from_config(hostname, &name, &nbddev, &hostname, &blocksize, &timeout, &cont, &swap, &sdp, &b_unix)) {
if(!get_from_config(hostname, &name, &nbddev, &hostname, &blocksize, &timeout, &cont, &swap, &sdp, &b_unix, &port)) {
usage("no valid configuration for specified device found", hostname);
exit(EXIT_FAILURE);
}
Expand Down

0 comments on commit 950f7c3

Please sign in to comment.