[Libguestfs] [PATCH libnbd] ublk: Add new nbdublk program

Richard W.M. Jones
Thu, 25 Aug 2022 12:10:55 -0000

This patch adds simple support for a ublk-based NBD client.
It is also available here:
https://gitlab.com/rwmjones/libnbd/-/tree/nbdublk/ublk

ublk is a way to write Linux block device drivers in userspace:
https://lwn.net/Articles/903855/

For simplicity of implementation and because I don't currently
understand the thread model of ublksrv, this only implements
synchronous calls for now.  It should be possible to extend this to a
fully asynchronous client without too much difficulty.

It does appear to work, at least for simple cases.  I have created
filesystems, files, etc on a ublk device backed by an nbdkit RAM disk,
eg: On one machine do:

  $ nbdkit memory 1G

On the client machine with the right kernel etc [see below] do:

  # modprobe ublk_drv
  # nbdublk /dev/ublkb0 nbd://remote
  # ublk list

  # blockdev --getsize64 /dev/ublkb0
  # mke2fs /dev/ublkb0
  # ...

  # ublk del -n 0

Testing this is not for the fainthearted.  I would start with a
throwaway Fedora Rawhide virtual machine, fully upgraded.  You will
need to recompile the kernel with CONFIG_BLK_DEV_UBLK=m

You will need to upgrade to liburing 2.2 (I pushed this to Rawhide a
few days ago).

You will need to download & compile: https://github.com/ming1/ubdsrv

Apply this patch to libnbd and compile it with:

  export PKG_CONFIG_PATH=$HOME/ubdsrv
  export CFLAGS="$CFLAGS -I$HOME/ubdsrv/include"
  export CXXFLAGS="$CXXFLAGS -I$HOME/ubdsrv/include"
  export LDFLAGS="$LDFLAGS -L$HOME/ubdsrv/lib"
  ./configure
  make

(Check that ublk dependencies are found and nbdublk is compiled)

You will then be able to run nbdublk from the compile directory using:

  sudo ./run nbdublk --help

Rich.