Skip to contentSkip to conversation

block: add native support for NFS

PATCH Superseded Peter Lieven 2 participants 3 messages Dec 20, 2013 Block layer

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 block: add native support for NFS

This patch adds native support for accessing images on NFS shares without the requirement to actually mount the entire NFS share on the host.

NFS Images can simply be specified by an url of the form:
nfs://<host>/<export>/<filename>

For example:
qemu-img create -f qcow2 nfs://10.0.0.1/qemu-images/test.qcow2

You need libnfs from Ronnie Sahlberg available at:
git://github.com/sahlberg/libnfs.git
for this to work.

During configure it is automatically probed for libnfs and support is enabled on-the-fly. You can forbid or enforce libnfs support with --disable-libnfs or --enable-libnfs respectively.

Due to NFS restrictions you might need to execute your binaries as root, allow them to open priviledged ports (<1024) or specify insecure option on the NFS server.

LibNFS currently support NFS version 3 only.

---
v2->v3:
- rebased the stefanha/block
- use pkg_config to check for libnfs (ignoring cflags which are broken in 1.8.0) [Stefan]
- fixed NFSClient declaration [Stefan]
- renamed Task variables to task [Stefan]
- renamed NFSTask to NFSRPC [Ronnie]
- do not update bs->total_sectors in nfs_co_writev [Stefan]
- return -ENOMEM on all async call failures [Stefan,Ronnie]
- fully implement ftruncate
- use util/uri.c for URL parsing [Stefan]
- reworked nfs_file_open_common to nfs_client_open which works on NFSClient [Stefan]
- added a comment ot the connect message that libnfs support NFSv3 only at the moment.
- DID NOT add full implementation of bdrv_get_allocated_file_size because we are not in a coroutine context and I cannot do an async call here.
I could do a sync call if there would be a guarantee that no requests
are in flight. [Stefan]

v1->v2:
- fixed block/Makefile.objs [Ronnie]
- do not always register a read handler [Ronnie]
- add support for reading beyond EOF [Fam]
- fixed struct and paramter naming [Fam]
- fixed overlong lines and whitespace errors [Fam]
- return return status from libnfs whereever possible [Fam]
- added comment why we set allocated_file_size to -ENOTSUP after write [Fam]
- avoid segfault when parsing filname [Fam]
- remove unused close_bh from NFSClient [Fam]
- avoid dividing and mutliplying total_size by BDRV_SECTOR_SIZE in nfs_file_create [Fam]

 MAINTAINERS         |    5 +
 block/Makefile.objs |    1 +
 block/nfs.c         |  402 +++++++++++++++++++++++++++++++++++++++++++++++++++
 configure           |   26 ++++
 4 files changed, 434 insertions(+)
 create mode 100644 block/nfs.c
MAINTAINERS +5 -0
index a5ab8f8..09996ab 100644
@@ -935,6 +935,11 @@ M: Peter Lieven <[email protected]>
 S: Supported
 F: block/iscsi.c
 
+NFS
+M: Peter Lieven <[email protected]>
+S: Maintained
+F: block/nfs.c
+
 SSH
 M: Richard W.M. Jones <[email protected]>
 S: Supported
block/Makefile.objs +1 -0
index 4e8c91e..e254a21 100644
@@ -12,6 +12,7 @@ block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 ifeq ($(CONFIG_POSIX),y)
 block-obj-y += nbd.o nbd-client.o sheepdog.o
 block-obj-$(CONFIG_LIBISCSI) += iscsi.o
+block-obj-$(CONFIG_LIBNFS) += nfs.o
 block-obj-$(CONFIG_CURL) += curl.o
 block-obj-$(CONFIG_RBD) += rbd.o
 block-obj-$(CONFIG_GLUSTERFS) += gluster.o
block/nfs.c new file +402 -0

402 lines not on this page · raw message

configure +26 -0
index d97556c..bae1d2b 100755
@@ -251,6 +251,7 @@ vss_win32_sdk=""
 win_sdk="no"
 want_tools="yes"
 libiscsi=""
+libnfs=""
 coroutine=""
 coroutine_pool=""
 seccomp=""
@@ -839,6 +840,10 @@ for opt do
   ;;
   --enable-libiscsi) libiscsi="yes"
   ;;
+  --disable-libnfs) libnfs="no"
+  ;;
+  --enable-libnfs) libnfs="yes"
+  ;;
   --enable-profiler) profiler="yes"
   ;;
   --disable-cocoa) cocoa="no"
@@ -1210,6 +1215,8 @@ echo "  --enable-spice           enable spice"
 echo "  --enable-rbd             enable building the rados block device (rbd)"
 echo "  --disable-libiscsi       disable iscsi support"
 echo "  --enable-libiscsi        enable iscsi support"
+echo "  --disable-libnfs         disable nfs support"
+echo "  --enable-libnfs          enable nfs support"
 echo "  --disable-smartcard-nss  disable smartcard nss support"
 echo "  --enable-smartcard-nss   enable smartcard nss support"
 echo "  --disable-libusb         disable libusb (for usb passthrough)"
@@ -3596,6 +3603,20 @@ elif test "$debug" = "no" ; then
   CFLAGS="-O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 $CFLAGS"
 fi
 
+##########################################
+# Do we have libnfs
+if test "$libnfs" != "no" ; then
+  if $pkg_config --atleast-version=1.8.0 libnfs; then
+    libnfs="yes"
+    libnfs_libs=$($pkg_config --libs libnfs)
+    LIBS="$LIBS $libnfs_libs"
+  else
+    if test "$libnfs" = "yes" ; then
+      feature_not_found "libnfs"
+    fi
+    libnfs="no"
+  fi
+fi
 
 # Disable zero malloc errors for official releases unless explicitly told to
 # enable/disable
@@ -3825,6 +3846,7 @@ echo "libiscsi support  $libiscsi (1.4.0)"
 else
 echo "libiscsi support  $libiscsi"
 fi
+echo "libnfs support    $libnfs"
 echo "build guest agent $guest_agent"
 echo "QGA VSS support   $guest_agent_with_vss"
 echo "seccomp support   $seccomp"
@@ -4161,6 +4183,10 @@ if test "$libiscsi" = "yes" ; then
   fi
 fi
 
+if test "$libnfs" = "yes" ; then
+  echo "CONFIG_LIBNFS=y" >> $config_host_mak
+fi
+
 if test "$seccomp" = "yes"; then
   echo "CONFIG_SECCOMP=y" >> $config_host_mak
 fi
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 4 lines · Peter Lieven

- DID NOT add full implementation of bdrv_get_allocated_file_size because we are not in a coroutine context and I cannot do an async call here. I could do a sync call if there would be a guarantee that no requests are in flight. [Stefan]

Let's discuss in the other email thread where we started.

block/nfs.c line 251

Quoted 11 lines
block/nfs.c new file +11 -0
+static int64_t nfs_client_open(NFSClient *client, const char *filename,
+                               int flags, Error **errp)
+{
+    int ret = -EINVAL;
+    URI *uri;
+    char *file = NULL, *strp = NULL;
+    struct stat st;
+
+    uri = uri_parse(filename);
+    if (!uri) {
+        error_setg(errp, "Invalid URL specified.\n");

\n should not be included in error_setg() messages.

block/nfs.c line 256

Quoted 5 lines
block/nfs.c new file +5 -0
+        goto fail;
+    }
+    strp = strrchr(uri->path, '/');
+    if (strp == NULL) {
+        error_setg(errp, "Invalid URL specified.\n");

\n should not be included in error_setg() messages.

#2 lore raw
Quoted 6 lines · Stefan Hajnoczi
Quoted 4 lines · Peter Lieven

- DID NOT add full implementation of bdrv_get_allocated_file_size because we are not in a coroutine context and I cannot do an async call here. I could do a sync call if there would be a guarantee that no requests are in flight. [Stefan]

Let's discuss in the other email thread where we started.

Okay.

Quoted 14 lines

block/nfs.c line 251

Quoted 12 lines
block/nfs.c new file +11 -0
+static int64_t nfs_client_open(NFSClient *client, const char *filename,
+                               int flags, Error **errp)
+{
+    int ret = -EINVAL;
+    URI *uri;
+    char *file = NULL, *strp = NULL;
+    struct stat st;
+
+    uri = uri_parse(filename);
+    if (!uri) {
+        error_setg(errp, "Invalid URL specified.\n");

\n should not be included in error_setg() messages.

Quoted 6 lines

block/nfs.c line 256

Quoted 5 lines
block/nfs.c new file +5 -0
+        goto fail;
+    }
+    strp = strrchr(uri->path, '/');
+    if (strp == NULL) {
+        error_setg(errp, "Invalid URL specified.\n");

\n should not be included in error_setg() messages.

If thats the only objection you have, please remove the \n when picking.

Thanks,
Peter