Skip to contentSkip to conversation

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

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

v8 of 9 Applied Latest is v9 →

lore
Share & tools
lore
#0 lore raw

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

Changes since v7:

- fsync (ie. bdrv_co_flush_to_disk) is now supported, *if* you have the following patches to libssh2 and OpenSSH:

https://bugzilla.mindrot.org/show_bug.cgi?id=1798
(OpenSSH: accepted, but not upstream)

http://www.libssh2.org/mail/libssh2-devel-archive-2013-04/0006.shtml
(libssh2: posted)

I tested 3 combinations (without libssh2 patch; with libssh2 patch and without OpenSSH support; with libssh2 patch and with OpenSSH support), and all work.

Rich.

#1 lore raw

PATCH v8 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         | 868 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure           |  48 +++
 qemu-doc.texi       |  40 +++
 qemu-options.hx     |  12 +
 5 files changed, 969 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 +868 -0

868 lines not on this page · raw message

configure +48 -0

84 lines not on this page · raw message

qemu-doc.texi +40 -0
index af84bef..7a0f373 100644
@@ -423,6 +423,7 @@ snapshots.
 * disk_images_sheepdog::      Sheepdog disk images
 * disk_images_iscsi::         iSCSI LUNs
 * disk_images_gluster::       GlusterFS disk images
+* disk_images_ssh::           Secure Shell (ssh) disk images
 @end menu
 
 @node disk_images_quickstart
@@ -1038,6 +1039,45 @@ qemu-system-x86_64 -drive file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glu
 qemu-system-x86_64 -drive file=gluster+rdma://1.2.3.4:24007/testvol/a.img
 @end example
 
+@node disk_images_ssh
+@subsection Secure Shell (ssh) disk images
+
+You can access disk images located on a remote ssh server
+by using the ssh protocol:
+
+@example
+qemu-system-x86_64 -drive file=ssh://[@var{user}@@]@var{server}[:@var{port}]/@var{path}
+@end example
+
+Alternative syntax using properties:
+
+@example
+qemu-system-x86_64 -drive file.driver=ssh[,file.user=@var{user}],file.host=@var{server}[,file.port=@var{port}],file.path=@var{path}
+@end example
+
+@var{ssh} is the protocol.
+
+@var{user} is the remote user.  If not specified, then the local
+username is tried.
+
+@var{server} specifies the remote ssh server.  Any ssh server can be
+used, but it must implement the sftp-server protocol.  Most Unix/Linux
+systems should work without requiring any extra configuration.
+
+@var{port} is the port number on which sshd is listening.  By default
+the standard ssh port (22) is used.
+
+@var{path} is the path to the disk image.
+
+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.
+
 @node pcsys_network
 @section Network emulation
 
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 v8 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   | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 configure     | 25 +++++++++++++++++++++++++
 qemu-doc.texi |  6 ------
 3 files changed, 72 insertions(+), 6 deletions(-)
block/ssh.c +47 -0
index e1322cb..b83c239 100644
@@ -826,6 +826,50 @@ static coroutine_fn int ssh_co_writev(BlockDriverState *bs,
     return ret;
 }
 
+#ifdef HAS_LIBSSH2_SFTP_FSYNC
+
+static coroutine_fn int ssh_flush(BDRVSSHState *s)
+{
+    int r;
+    static int warned = 0;
+
+    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) {
+        if (!warned) {
+            error_report("warning: remote ssh server does not support fsync");
+            warned = 1;
+        }
+        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;
+}
+
+#endif /* HAS_LIBSSH2_SFTP_FSYNC */
+
 static int64_t ssh_getlength(BlockDriverState *bs)
 {
     BDRVSSHState *s = bs->opaque;
@@ -849,6 +893,9 @@ static BlockDriver bdrv_ssh = {
     .bdrv_co_readv                = ssh_co_readv,
     .bdrv_co_writev               = ssh_co_writev,
     .bdrv_getlength               = ssh_getlength,
+#ifdef HAS_LIBSSH2_SFTP_FSYNC
+    .bdrv_co_flush_to_disk        = ssh_co_flush,
+#endif
     .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 +0 -6
index 7a0f373..9e30667 100644
@@ -1072,12 +1072,6 @@ the standard ssh port (22) is used.
 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.
-
 @node pcsys_network
 @section Network emulation
 
Signature · 1 line
1.8.1.4
#3 lore raw

Re: [Qemu-devel] [PATCH v8 2/3] block: ssh: Use libssh2_sftp_fsync (if supported by libssh2) to flush to disk.

On Tue, Apr 09, 2013 at 10:56:32AM +0100, Richard W.M. Jones wrote:

These are both not worth respinning for, but just in case you send another version.

Quoted 6 lines
block/ssh.c +6 -0
+static coroutine_fn int ssh_flush(BDRVSSHState *s)
+{
+    int r;
+    static int warned = 0;
+
+    DPRINTF("fsync");

block/ssh.c static coroutine_fn int ssh_co_writev(BlockDriverState *bs, · line 846

Quoted 10 lines
block/ssh.c +10 -0
+ 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) {
+        if (!warned) {
+            error_report("warning: remote ssh server does not support fsync");

Perhaps not worth fixing but this error message has two properties which could be improved:

1. When multiple -drive file=ssh:// are used it is unclear which SSH server lacks fsync support since the error message does not say.

2. When multiple -drive file=ssh:// are used only the first server lacking support will print a warning since warned is static.

qemu-doc.texi the standard ssh port (22) is used. · line 1079

Quoted 13 lines
qemu-doc.texi +0 -5
index 7a0f373..9e30667 100644
@@ -1072,12 +1072,6 @@ the standard ssh port (22) is used.
 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.

We can expect many SSH servers not to support the fsync extension. It would be best to include a note explaining that fsync is not available on older ssh servers and therefore data integrity cannot be guaranteed in those cases.

Stefan

#4 lore raw

Re: [Qemu-devel] [PATCH v8 2/3] block: ssh: Use libssh2_sftp_fsync (if supported by libssh2) to flush to disk.

Quoted 39 lines · Stefan Hajnoczi

On Tue, Apr 09, 2013 at 10:56:32AM +0100, Richard W.M. Jones wrote:

These are both not worth respinning for, but just in case you send another version.

Quoted 6 lines
block/ssh.c +6 -0
+static coroutine_fn int ssh_flush(BDRVSSHState *s)
+{
+    int r;
+    static int warned = 0;
+
+    DPRINTF("fsync");

block/ssh.c static coroutine_fn int ssh_co_writev(BlockDriverState *bs, · line 846

Quoted 10 lines
block/ssh.c +10 -0
+ 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) {
+        if (!warned) {
+            error_report("warning: remote ssh server does not support fsync");

Perhaps not worth fixing but this error message has two properties which could be improved:

1. When multiple -drive file=ssh:// are used it is unclear which SSH server lacks fsync support since the error message does not say.

2. When multiple -drive file=ssh:// are used only the first server lacking support will print a warning since warned is static.

Quoted 8 lines
qemu-doc.texi +0 -0
index 7a0f373..9e30667 100644
@@ -1072,12 +1072,6 @@ the standard ssh port (22) is used.
 Currently authentication must be done using ssh-agent.  Other
 authentication methods may be supported in future.
 
Quoted 10 lines

qemu-doc.texi the standard ssh port (22) is used. · line 1079

Quoted 5 lines
qemu-doc.texi +0 -5
-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.

We can expect many SSH servers not to support the fsync extension. It would be best to include a note explaining that fsync is not available on older ssh servers and therefore data integrity cannot be guaranteed in those cases.

Thanks -- will fix both in the next version.

Also I have (in my copy) implemented host_key_check parameter. Fun fact: OpenSSH uses MD5 to print SSH host key fingerprints.

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://people.redhat.com/~rjones/virt-df/
#5 lore raw

PATCH v8 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
#6 lore raw
In reply to Richard W.M. Jones
Quoted 8 lines · Richard W.M. Jones

Changes since v7:

- fsync (ie. bdrv_co_flush_to_disk) is now supported, *if* you have the following patches to libssh2 and OpenSSH:

https://bugzilla.mindrot.org/show_bug.cgi?id=1798
(OpenSSH: accepted, but not upstream)

Quoted 8 lines

http://www.libssh2.org/mail/libssh2-devel-archive-2013-04/0006.shtml
(libssh2: posted)

I tested 3 combinations (without libssh2 patch; with libssh2 patch and without OpenSSH support; with libssh2 patch and with OpenSSH support), and all work.

Rich.

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

I did not merge Patch 2 since it is an out-of-tree libssh2 feature. Let's wait until libssh2.git has merged the fsync patch.

Stefan

#7 lore raw
In reply to Richard W.M. Jones
Quoted 7 lines · Richard W.M. Jones

Changes since v7:

- fsync (ie. bdrv_co_flush_to_disk) is now supported, *if* you have the following patches to libssh2 and OpenSSH:

https://bugzilla.mindrot.org/show_bug.cgi?id=1798
(OpenSSH: accepted, but not upstream)

FYI: OpenSSH 6.4 has finally added fsync support upstream!

Rich.

Signature · 3 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org
#8 lore raw
Quoted 10 lines · Richard W.M. Jones
Quoted 7 lines · Richard W.M. Jones

Changes since v7:

- fsync (ie. bdrv_co_flush_to_disk) is now supported, *if* you have the following patches to libssh2 and OpenSSH:

https://bugzilla.mindrot.org/show_bug.cgi?id=1798
(OpenSSH: accepted, but not upstream)

FYI: OpenSSH 6.4 has finally added fsync support upstream!

Thanks for the update. This was an impressive effort across layers: you extended libssh2, openssh, and QEMU to add a missing feature!

Stefan