Skip to contentSkip to conversation

block: Add support for Secure Shell (ssh) block device.

PATCH Applied Richard W.M. Jones 2 participants 6 messages Apr 9, 2013 – Apr 11, 2013 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH v9 1/3 block: Add support for Secure Shell (ssh) block device.

Changes since v8:

- Implement the suggestions of Stefan Hajnoczi.

- Add host_key_check parameter, which you can use like this:

ssh://[...]?host_key_check=no # skip the check entirely
ssh://[...]?host_key_check=yes # the default, uses known_hosts

  # or check for a specific host key fingerprint:
  ssh://[...]?host_key_check=md5:<usual SSH host key fingerprint>
  ssh://[...]?host_key_check=sha1:<SHA1 fingerprint>

Note that I cannot persuade OpenSSH to display the SHA1 fingerprint of a host key, so sha1: is not tested.

A possible extension to this would be to check for the exact key (not the fingerprint, which is just a hash of the key), although in this case the URI would be very long indeed, possibly longer than URIs are supposed to be in some cases.

Rich.

#1 lore raw

PATCH v9 1/3 block: Add support for Secure Shell (ssh) block device.

From: "Richard W.M. Jones" <[email protected]>

qemu-system-x86_64 -drive file=ssh://hostname/some/image

QEMU will ssh into 'hostname' and open '/some/image' which is made available as a standard block device.

You can specify a username (ssh://user@host/...) and/or a port number (ssh://host:port/...). You can also use an alternate syntax using properties (file.user, file.host, file.port, file.path).

Current limitations:

- Authentication must be done without passwords or passphrases, using ssh-agent. Other authentication methods are not supported.

- Uses a single connection, instead of concurrent AIO with multiple SSH connections.

This is implemented using libssh2 on the client side. The server just requires a regular ssh daemon with sftp-server support. Most ssh daemons on Unix/Linux systems will work out of the box.

Cc Stefan Hajnoczi <[email protected]>
Cc Kevin Wolf <[email protected]>
---
 block/Makefile.objs |   1 +
 block/ssh.c         | 995 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure           |  48 +++
 qemu-doc.texi       |  49 +++
 qemu-options.hx     |  12 +
 5 files changed, 1105 insertions(+)
 create mode 100644 block/ssh.c
block/Makefile.objs +1 -0
index c067f38..6c4b5bc 100644
@@ -13,6 +13,7 @@ block-obj-$(CONFIG_LIBISCSI) += iscsi.o
 block-obj-$(CONFIG_CURL) += curl.o
 block-obj-$(CONFIG_RBD) += rbd.o
 block-obj-$(CONFIG_GLUSTERFS) += gluster.o
+block-obj-$(CONFIG_LIBSSH2) += ssh.o
 endif
 
 common-obj-y += stream.o
block/ssh.c new file +995 -0

995 lines not on this page · raw message

configure +48 -0

84 lines not on this page · raw message

qemu-doc.texi +49 -0

61 lines not on this page · raw message

qemu-options.hx +12 -0
index 7cd6002..5c115d1 100644
@@ -2107,6 +2107,18 @@ Example for Unix Domain Sockets
 qemu-system-i386 --drive file=nbd:unix:/tmp/nbd-socket
 @end example
 
+@item SSH
+QEMU supports SSH (Secure Shell) access to remote disks.
+
+Examples:
+@example
+qemu-system-i386 -drive file=ssh://user@@host/path/to/disk.img
+qemu-system-i386 -drive file.driver=ssh,file.user=user,file.host=host,file.port=22,file.path=/path/to/disk.img
+@end example
+
+Currently authentication must be done using ssh-agent.  Other
+authentication methods may be supported in future.
+
 @item Sheepdog
 Sheepdog is a distributed storage system for QEMU.
 QEMU supports using either local sheepdog devices or remote networked
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v9 2/3 block: ssh: Use libssh2_sftp_fsync (if supported by libssh2) to flush to disk.

In reply to Richard W.M. Jones

From: "Richard W.M. Jones" <[email protected]>

libssh2_sftp_fsync is an extension to libssh2 to support fsync(2) over sftp, which is itself an extension of OpenSSH.

If both libssh2 and the ssh daemon support it, this will allow bdrv_flush_to_disk to commit changes through to disk on the remote server.

---
 block/ssh.c   | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 configure     | 25 +++++++++++++++++++
 qemu-doc.texi | 15 ++++++++----
 3 files changed, 108 insertions(+), 10 deletions(-)
block/ssh.c +73 -5
index 89a9017..8f78e2e 100644
@@ -72,6 +72,10 @@ typedef struct BDRVSSHState {
      * updated if it changes (eg by writing at the end of the file).
      */
     LIBSSH2_SFTP_ATTRIBUTES attrs;
+
+    /* Used to warn if 'flush' is not supported. */
+    char *hostport;
+    bool unsafe_flush_warning;
 } BDRVSSHState;
 
 static void ssh_state_init(BDRVSSHState *s)
@@ -84,6 +88,7 @@ static void ssh_state_init(BDRVSSHState *s)
 
 static void ssh_state_free(BDRVSSHState *s)
 {
+    g_free(s->hostport);
     if (s->sftp_handle) {
         libssh2_sftp_close(s->sftp_handle);
     }
@@ -479,7 +484,6 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
     Error *err = NULL;
     const char *host, *user, *path, *host_key_check;
     int port;
-    char *hostport = NULL;
 
     host = qdict_get_str(options, "host");
 
@@ -507,9 +511,12 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
         host_key_check = "yes";
     }
 
+    /* Construct the host:port name for inet_connect. */
+    g_free(s->hostport);
+    s->hostport = g_strdup_printf("%s:%d", host, port);
+
     /* Open the socket and connect. */
-    hostport = g_strdup_printf("%s:%d", host, port);
-    s->sock = inet_connect(hostport, &err);
+    s->sock = inet_connect(s->hostport, &err);
     if (err != NULL) {
         ret = -errno;
         qerror_report_err(err);
@@ -581,7 +588,6 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
     qdict_del(options, "path");
     qdict_del(options, "host_key_check");
 
-    g_free(hostport);
     return 0;
 
  err:
@@ -600,7 +606,6 @@ static int connect_to_ssh(BDRVSSHState *s, QDict *options,
         libssh2_session_free(s->session);
     }
     s->session = NULL;
-    g_free(hostport);
 
     return ret;
 }
@@ -953,6 +958,68 @@ static coroutine_fn int ssh_co_writev(BlockDriverState *bs,
     return ret;
 }
 
+static void unsafe_flush_warning(BDRVSSHState *s, const char *what)
+{
+    if (!s->unsafe_flush_warning) {
+        error_report("warning: ssh server %s does not support fsync",
+                     s->hostport);
+        if (what) {
+            error_report("to support fsync, you need %s", what);
+        }
+        s->unsafe_flush_warning = true;
+    }
+}
+
+#ifdef HAS_LIBSSH2_SFTP_FSYNC
+
+static coroutine_fn int ssh_flush(BDRVSSHState *s)
+{
+    int r;
+
+    DPRINTF("fsync");
+ again:
+    r = libssh2_sftp_fsync(s->sftp_handle);
+    if (r == LIBSSH2_ERROR_EAGAIN || r == LIBSSH2_ERROR_TIMEOUT) {
+        co_yield(s);
+        goto again;
+    }
+    if (r == LIBSSH2_ERROR_SFTP_PROTOCOL &&
+        libssh2_sftp_last_error(s->sftp) == LIBSSH2_FX_OP_UNSUPPORTED) {
+        unsafe_flush_warning(s, "OpenSSH >= 6.3");
+        return 0;
+    }
+    if (r < 0) {
+        sftp_error_report(s, "fsync failed");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
+{
+    BDRVSSHState *s = bs->opaque;
+    int ret;
+
+    qemu_co_mutex_lock(&s->lock);
+    ret = ssh_flush(s);
+    qemu_co_mutex_unlock(&s->lock);
+
+    return ret;
+}
+
+#else /* !HAS_LIBSSH2_SFTP_FSYNC */
+
+static coroutine_fn int ssh_co_flush(BlockDriverState *bs)
+{
+    BDRVSSHState *s = bs->opaque;
+
+    unsafe_flush_warning(s, "libssh2 >= 1.4.4");
+    return 0;
+}
+
+#endif /* !HAS_LIBSSH2_SFTP_FSYNC */
+
 static int64_t ssh_getlength(BlockDriverState *bs)
 {
     BDRVSSHState *s = bs->opaque;
@@ -976,6 +1043,7 @@ static BlockDriver bdrv_ssh = {
     .bdrv_co_readv                = ssh_co_readv,
     .bdrv_co_writev               = ssh_co_writev,
     .bdrv_getlength               = ssh_getlength,
+    .bdrv_co_flush_to_disk        = ssh_co_flush,
     .create_options               = ssh_create_options,
 };
 
configure +25 -0
index 4dd65b0..fa79588 100755
@@ -2357,6 +2357,31 @@ EOF
 fi
 
 ##########################################
+# libssh2_sftp_fsync probe
+
+if test "$libssh2" = "yes"; then
+  cat > $TMPC <<EOF
+#include <stdio.h>
+#include <libssh2.h>
+#include <libssh2_sftp.h>
+int main(void) {
+    LIBSSH2_SESSION *session;
+    LIBSSH2_SFTP *sftp;
+    LIBSSH2_SFTP_HANDLE *sftp_handle;
+    session = libssh2_session_init ();
+    sftp = libssh2_sftp_init (session);
+    sftp_handle = libssh2_sftp_open (sftp, "/", 0, 0);
+    libssh2_sftp_fsync (sftp_handle);
+    return 0;
+}
+EOF
+  # libssh2_cflags/libssh2_libs defined in previous test.
+  if compile_prog "$libssh2_cflags" "$libssh2_libs" ; then
+    QEMU_CFLAGS="-DHAS_LIBSSH2_SFTP_FSYNC $QEMU_CFLAGS"
+  fi
+fi
+
+##########################################
 # linux-aio probe
 
 if test "$linux_aio" != "no" ; then
qemu-doc.texi +10 -5
index 5b36004..dfea4d3 100644
@@ -1081,11 +1081,16 @@ tools only use MD5 to print fingerprints).
 Currently authentication must be done using ssh-agent.  Other
 authentication methods may be supported in future.
 
-Note: The ssh driver does not obey disk flush requests (ie. to commit
-data to the backing disk when the guest requests it).  This is because
-the underlying protocol (SFTP) does not support this.  Thus there is a
-risk of guest disk corruption if the remote server or network goes
-down during writes.
+Note: Many ssh servers do not support an @code{fsync}-style operation.
+The ssh driver cannot guarantee that disk flush requests are
+obeyed, and this causes a risk of disk corruption if the remote
+server or network goes down during writes.  The driver will
+print a warning when @code{fsync} is not supported:
+
+warning: ssh server @code{ssh.example.com:22} does not support fsync
+
+With sufficiently new versions of libssh2 and OpenSSH, @code{fsync} is
+supported.
 
 @node pcsys_network
 @section Network emulation
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH v9 3/3 iotests: Add 'check -ssh' option to test Secure Shell block device.

In reply to Richard W.M. Jones

From: "Richard W.M. Jones" <[email protected]>

Note in order to run these tests on ssh, you must be running a local ssh daemon, and that daemon must accept loopback connections, and ssh-agent has to be set up to allow logins on the local daemon. In other words, the following command should just work without demanding any passphrase:

ssh localhost

---
 tests/qemu-iotests/common    | 5 +++++
 tests/qemu-iotests/common.rc | 3 +++
 2 files changed, 8 insertions(+)
tests/qemu-iotests/common +5 -0
index b3aad89..6826ea7 100644
@@ -137,6 +137,7 @@ check options
     -rbd                test rbd
     -sheepdog           test sheepdog
     -nbd                test nbd
+    -ssh                test ssh
     -xdiff		graphical mode diff
     -nocache		use O_DIRECT on backing file
     -misalign		misalign memory allocations
@@ -206,6 +207,10 @@ testlist options
 	    IMGPROTO=nbd
 	    xpand=false
 	    ;;
+        -ssh)
+            IMGPROTO=ssh
+            xpand=false
+            ;;
 	-nocache)
 	    QEMU_IO_OPTIONS="$QEMU_IO_OPTIONS --nocache"
 	    xpand=false
tests/qemu-iotests/common.rc +3 -0
index e522d61..a536bf7 100644
@@ -52,6 +52,9 @@ if [ "$IMGPROTO" = "file" ]; then
 elif [ "$IMGPROTO" = "nbd" ]; then
     TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
     TEST_IMG="nbd:127.0.0.1:10810"
+elif [ "$IMGPROTO" = "ssh" ]; then
+    TEST_IMG_FILE=$TEST_DIR/t.$IMGFMT
+    TEST_IMG="ssh://127.0.0.1$TEST_IMG_FILE"
 else
     TEST_IMG=$IMGPROTO:$TEST_DIR/t.$IMGFMT
 fi
Signature · 1 line
1.8.1.4
#4 lore raw
In reply to Richard W.M. Jones
Quoted 16 lines · Richard W.M. Jones

Changes since v8:

- Implement the suggestions of Stefan Hajnoczi.

- Add host_key_check parameter, which you can use like this:

ssh://[...]?host_key_check=no # skip the check entirely
ssh://[...]?host_key_check=yes # the default, uses known_hosts

  # or check for a specific host key fingerprint:
  ssh://[...]?host_key_check=md5:<usual SSH host key fingerprint>
  ssh://[...]?host_key_check=sha1:<SHA1 fingerprint>

Note that I cannot persuade OpenSSH to display the SHA1 fingerprint of a host key, so sha1: is not tested.

Quoted 6 lines

A possible extension to this would be to check for the exact key (not the fingerprint, which is just a hash of the key), although in this case the URI would be very long indeed, possibly longer than URIs are supposed to be in some cases.

Rich.

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

libssh2.git now includes fsync support so we can take the fsync patch.

Stefan

#5 lore raw

Thanks.

I've also developed patches to incorporate this feature into libvirt and libguestfs. Here are the latest versions of these:

https://www.redhat.com/archives/libvir-list/2013-April/thread.html#00803
https://www.redhat.com/archives/libguestfs/2013-April/thread.html#00021

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v