-1

According to this blog post, it should be easy:

# nbd-server MyFile.dat
# nbd-client localhost /dev/nbd0

Trouble is... that doesn't actually work.

First of all, nbd-server whinges and whines about "no configured exports", unless I explicitly specify a port number and the absolute path to the file. But OK, whatever. (It still complains that this is "deprecated" somehow and that you should really edit the config file instead — which I definitely don't want to do!) It appears to now be serving the block device. (At least, there's now an nbd-server process running, which has TCP/10809 open for listening.)

Trying to get nbd-client to connect, however, is impossible. No matter what arguments I give it, it moans and complains and refuses to do anything! It keeps saying

 ERROR: not enough information specified

followed by the usage synopsis. But it won't actually tell me what it's unhappy about. Either with or without a port number specified, I get the same error message. How do I make it actually connect?

(Obviously I'm connecting to the same machine. Eventually I want to do this between machines on the network. But I can't even get it to work locally yet!)

PS. Some examples talk about modprobe nbd-client and modprobe nbd-server. My OpenSUSE machine appears to only have nbd.ko, so I've inserted that. Before that, /dev/nbd0 doesn't even exist. After that, it exists just fine.

MathematicalOrchid
  • 5,664
  • 7
  • 35
  • 62
  • @cas Does it matter which blog post? Either the commands are correct or they aren't. As I explained, `nbd-server MyData.dat` doesn't work, whereas `nbd-server 10809 /home/fred/MyData.dat` appears to work. I can't get `nbd-client` to work at all; `nbd-client localhost /dev/nbd0` doesn't work. `nbd-client localhost 10809 /dev/nbd0` doesn't work. Not sure what else to try. – MathematicalOrchid Jul 12 '21 at 16:01
  • @cas OK. Just for you, I edited in a link to the blog post. – MathematicalOrchid Jul 12 '21 at 16:05
  • It's not just for cas. They have a valid point that we need information to be able to evaluate the situation appropriately – roaima Jul 12 '21 at 16:15
  • The blog post looks a bit lightweight and vague, but there are no obvious errors. Is your ndb server listening on your loopback interface (i.e. localhost, 127.0.0.1) or only on your NIC's interface? (the ndb-server man page says it listens on all interfaces by default, but man pages aren't always accurate). Try specifying the host and port explicitly. e.g. `nbd-server localhost:10809 MyFile.dat` and `nbd-client localhost 10809 /dev/ndb0`. – cas Jul 12 '21 at 16:19
  • The blog is out of date. I'm looking for something newer before writing an answer for you. https://github.com/NetworkBlockDevice/nbd looks plausible – roaima Jul 12 '21 at 16:25
  • @cas `lsof` indicates the server is listening on `TCP *:10809`, which I *think* means all interfaces. Regardless, I tried setting it explicitly (`lsof` now says `TCP localhost:10809`). It doesn't seem to make any difference to `nbd-client`. It still says "not enough information specified", whatever that means. – MathematicalOrchid Jul 12 '21 at 16:25
  • I spent some time experimenting with nbd-server and nbd-client yesterday, and I think the only way you're going to get this working is to use a config file - at the very least, an authorisation file to list IP addresses allowed to connect). You can use `-C` to specify your own config file instead of the default (dunno what distro you're using, but on debian the default is `/etc/nbd-server/config`). Also, I don't know what your distro does, but debian runs nbd-server itself as user `nbd` so you'll need to be careful about ownership and permissions of files. – cas Jul 14 '21 at 04:40
  • also, avoid making the same mistake I made repeatedly, mistyping `nbd` as `ndb` - this is especially annoying when you mistype the device name as `/dev/ndb0`. – cas Jul 14 '21 at 04:42
  • If these comments help you figure it out, please write it up as an answer to help the next person with a similar question. You can accept your own answer. – cas Jul 14 '21 at 04:44

2 Answers2

2

This took some figuring out.

It seems that as well as the NBD server and port number, you also need to specify an "export name", which the documentation doesn't really explain very well.

I also had to resort to using a config file [which I desparately wanted to avoid]. Fortunately, you don't actually have to put it in any special location or anything.

I was able to actually get the following to work:

  1. Create an empty disk image.

  2. Create a file looking something like this:

    [generic]
    # Must always exist.
    
    [Mu]
    exportname=/path/to/image/file
    
  3. Start the NBD server:

    nbd-server -C /path/to/config/file
    

    This seems to generate /var/run/nbd-server.pid, which holds the PID of the NBD server process (i.e., so you can kill it). There doesn't seem to be any other stop command.

  4. Start the NBD client:

    nbd-client localhost -N Mu /dev/nbd0
    

    The Mu part has to match whatever random name you chose in the config file. It's perfectly possible for a single server to serve multiple files as once, so you need to specify which one you want. (This is presumably the “not enough information” that the client was whining about before.)

At this point, /dev/nbd0 should function as a normal block device.

So far, I haven't found any way to disconnect, other than killing the client/server processes (e.g., pkill nbd).

I have also tested; this works remotely, if you replace localhost with the appropriate hostname.

MathematicalOrchid
  • 5,664
  • 7
  • 35
  • 62
  • To disconnect use `nbd-client -d /dev/nbd0`. To check if connected use `nbd-client -c /dev/nbd0` – Rich Sep 14 '21 at 18:47
0

I would suggest using nbdkit instead of nbd-server. (Note: I wrote it). It's a more modern and capable server, and by default it doesn't care about export names. Here's an example of using it to create a 1 terabyte RAM disk, and connecting to that using nbd-client:

$ nbdkit memory 1T
$ sudo nbd-client localhost /dev/nbd0
Negotiation: ..size = 1048576MB
Connected /dev/nbd0
$ sudo blockdev --getsize64 /dev/nbd0
1099511627776

Another tool that we wrote is nbdinfo which can be used to query NBD servers for their properties, including listing export names that they serve:

$ nbdinfo nbd://localhost
protocol: newstyle-fixed without TLS
export="":
    export-size: 1099511627776
    content: data
    uri: nbd://localhost:10809/
[more output clipped]

To clean up:

$ sudo nbd-client -d /dev/nbd0
$ killall nbdkit

(If you were doing this properly there are better ways than killall. You could have nbdkit write a PID file and then kill that PID. Or you could use captive nbdkit)

Rich
  • 303
  • 3
  • 6