[Nbd] How to find a free NBD device?
is it possible to implement something like losetup --find with NBD?
i.e. is there a safe method to grab the next free /dev/nbdX without
disturbing anything. (in your own program that makes use of NBD)
The nbd-client leaves it to the user to provide a free /dev/nbdX
on the command line. If the user gets it wrong, and tries to start two
clients on the same device, both clients fail and any further requests
to that device result in I/O errors.
My attempt was to call NBD_SET_SOCK ioctl first, which only succeeds
when the device was free. However doing so, subsequent ioctl (like
BLKROSET) seem to fail, and in nbd-client itself, the SET_SOCK is the
last ioctl before DO_IT, rather than the first, so I guess it's not
supposed to be used this way?
Checking /sys/block/nbd*/pid does not seem to be entirely reliable
either, as it seems to be only set on the DO_IT, so if two programs
search for a free device at the same time, they might race.
Is there something I'm missing?
Are there any NBD programs that do not ask the user for the /dev/nbdX
so I might have a look at what they've done about it?