Skip to contentSkip to conversation

Add support for Secure Shell (ssh) block device.

PATCH New Richard W.M. Jones 3 participants 14 messages Mar 21, 2013 – Mar 26, 2013 Block layer

lore
Share & tools
lore
#0 lore raw

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

This is a fun little patch which adds an ssh-based block device. Nearly every Unix/Linux server runs an ssh daemon. This new block device lets qemu ssh into those servers and use files on those servers as disks:

 qemu-system-x86_64 -m 512 \
    -drive file=ssh://rjones@onuma/mnt/scratch/f15x32.img,if=virtio

If you want to test this, note that you will need to have ssh-agent set up so there is passwordless access from your local machine to the remote account. Krb5 and other authentication methods (probably) won't work.

I have tested with a couple of Windows and Linux guest images, successfully booting and using those disks which are located on a remote RHEL server over 100 Mbps ethernet. Disk speed is reasonable though not exactly fast.

It's not ready to be applied as it would be nice to fix the "easy to fix" problems noted in the commit message. Nevertheless I'd appreciate an initial review.

Also: Is there any documentation on how coroutines / AIO work? (apart from reading the code, which I've been doing)

Rich.

#1 lore raw

PATCH 1/1 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/...).

Current limitations:

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

- Does not check host key. (*)

- New remote files cannot be created. (*)

- Uses coroutine read/write, instead of true AIO. (libssh2 supports non-blocking access, so this could be fixed with some effort).

- Blocks during connection and authentication.

(*) = potentially easy fix

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.

---
 block/Makefile.objs |   1 +
 block/ssh.c         | 514 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure           |  47 +++++
 qemu-doc.texi       |  28 +++
 4 files changed, 590 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 +514 -0

514 lines not on this page · raw message

configure +47 -0

83 lines not on this page · raw message

qemu-doc.texi +28 -0
index af84bef..004b2ff 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,33 @@ 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
+
+@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.
+
+Authentication must currently be done without requiring a password or
+passphrase.  Set up ssh-agent with authorized keys, or Kerberos or
+similar.
+
 @node pcsys_network
 @section Network emulation
 
Signature · 1 line
1.8.1.4
#2 lore raw

Re: [Qemu-devel] [PATCH] block: Add support for Secure Shell (ssh) block device.

Quoted 9 lines · Richard W.M. Jones

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/...).

I can see this being handy for qemu-img since it gives you the ability to work with remote image files.

Quoted 8 lines

Current limitations:

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

- Does not check host key. (*)

- New remote files cannot be created. (*)

Would be important to fix these limitations. Authentication methods to make this more usable. Host key check for security. File creation for qemu-img.

Quoted 2 lines

- Uses coroutine read/write, instead of true AIO. (libssh2 supports non-blocking access, so this could be fixed with some effort).

This patch does not really use coroutines - the SSH I/O is blocking!

Coroutines must submit the SSH I/O and then yield so the QEMU event loop can get on with other work. When SSH I/O finishes the request's coroutine is re-entered and the request gets completed.

Quoted 1 line

- Blocks during connection and authentication.

Right now the code also blocks while SSH I/O takes place.

Quoted 5 lines

(*) = potentially easy fix

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.

How much of a win over sshfs is this?

sshfs can be mounted by unprivileged users and QEMU accesses it like a regular file. So the sshfs approach already works today.

Stefan

#3 lore raw

Re: [Qemu-devel] [PATCH] block: Add support for Secure Shell (ssh) block device.

Quoted 19 lines · Stefan Hajnoczi
Quoted 9 lines · Richard W.M. Jones

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/...).

I can see this being handy for qemu-img since it gives you the ability to work with remote image files.

Quoted 5 lines

Current limitations:

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

Quoted 7 lines
Quoted 3 lines

- Does not check host key. (*)

- New remote files cannot be created. (*)

Would be important to fix these limitations. Authentication methods to make this more usable. Host key check for security. File creation for qemu-img.

I agree.

Quoted 8 lines
Quoted 2 lines

- Uses coroutine read/write, instead of true AIO. (libssh2 supports non-blocking access, so this could be fixed with some effort).

This patch does not really use coroutines - the SSH I/O is blocking!

Coroutines must submit the SSH I/O and then yield so the QEMU event loop can get on with other work. When SSH I/O finishes the request's coroutine is re-entered and the request gets completed.

Hmm OK. Is there any documentation at all on how coroutines are supposed to work? Or AIO for that matter? For example, do coroutines really replace all the read/write syscalls deep inside a library (libssh2) so that these calls context switch, or am I missing the point of how this works entirely?

[...]

Quoted 8 lines
Quoted 3 lines

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.

How much of a win over sshfs is this?

sshfs can be mounted by unprivileged users and QEMU accesses it like a regular file. So the sshfs approach already works today.

Sure, but compared to having to install and set up sshfs and FUSE, using this is a lot simpler. It's also potentially faster since it cuts out FUSE and context switching to the sshfs process.

BTW I looked into the implementation of sshfs before starting this, and what it does is to run an 'ssh' client subprocess, then implements the sftp protocol by hand on top. So using libssh2 cuts out *two* external processes (plus FUSE).

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
#4 lore raw

Re: [Qemu-devel] [PATCH] block: Add support for Secure Shell (ssh) block device.

Quoted 39 lines · Richard W.M. Jones
Quoted 19 lines · Stefan Hajnoczi
Quoted 9 lines · Richard W.M. Jones

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/...).

I can see this being handy for qemu-img since it gives you the ability to work with remote image files.

Quoted 5 lines

Current limitations:

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

Quoted 7 lines
Quoted 3 lines

- Does not check host key. (*)

- New remote files cannot be created. (*)

Would be important to fix these limitations. Authentication methods to make this more usable. Host key check for security. File creation for qemu-img.

I agree.

Quoted 8 lines
Quoted 2 lines

- Uses coroutine read/write, instead of true AIO. (libssh2 supports non-blocking access, so this could be fixed with some effort).

This patch does not really use coroutines - the SSH I/O is blocking!

Coroutines must submit the SSH I/O and then yield so the QEMU event loop can get on with other work. When SSH I/O finishes the request's coroutine is re-entered and the request gets completed.

Quoted 5 lines

Hmm OK. Is there any documentation at all on how coroutines are supposed to work? Or AIO for that matter? For example, do coroutines really replace all the read/write syscalls deep inside a library (libssh2) so that these calls context switch, or am I missing the point of how this works entirely?

Before we go into coroutines, take a look at AIO since it may fit libssh2's non-blocking mode of operation better.

Doing AIO means implementing .bdrv_aio_readv()/.bdrv_aio_writev(). These functions cannot block so you need to use non-blocking libssh2 interfaces and let QEMU's event loop notify us of readability/writability (see qemu_aio_set_fd_handler()).

Look at block/iscsi.c:iscsi_aio_readv() for an example of how to interface with an asynchronous library.

Back to coroutines. Coroutines are just a primitive, like threads, that your code can use. They aren't a framework for how to do block I/O.

If you want an example, take a look at block/sheepdog.c:

static coroutine_fn void do_co_req(void *opaque)

{
    int ret;
    Coroutine *co;
[...]
    co = qemu_coroutine_self();
    qemu_aio_set_fd_handler(sockfd, NULL, restart_co_req, have_co_req, co);

    ret = send_co_req(sockfd, hdr, data, wlen);

Using qemu_aio_set_fd_handler() we tell the event loop to call restart_co_req() when the file descriptor becomes writable.

Then we call send_co_req() which yields if send(2) returns EAGAIN.

This is an example of setting up an event handler, invoking a non-blocking syscall, and yielding on EAGAIN. Here is what restart_co_req() looks like:

static void restart_co_req(void *opaque)

{
    Coroutine *co = opaque;

    qemu_coroutine_enter(co, NULL);
}

If you want to make the code clean and reusable it's best to put the event handler, non-blocking I/O, and yield/re-enter into a function that can be reused. That way each callers don't duplicate this low-level code.

Hope this gives you an idea of how to use coroutines for block drivers.

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

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.

How much of a win over sshfs is this?

sshfs can be mounted by unprivileged users and QEMU accesses it like a regular file. So the sshfs approach already works today.

Quoted 8 lines

Sure, but compared to having to install and set up sshfs and FUSE, using this is a lot simpler. It's also potentially faster since it cuts out FUSE and context switching to the sshfs process.

BTW I looked into the implementation of sshfs before starting this, and what it does is to run an 'ssh' client subprocess, then implements the sftp protocol by hand on top. So using libssh2 cuts out *two* external processes (plus FUSE).

I see. Benchmarks would be interesting once AIO or coroutines is implemented.

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH] block: Add support for Secure Shell (ssh) block device.

In reply to Richard W.M. Jones
Quoted 29 lines · Richard W.M. Jones

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/...).

Current limitations:

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

- Does not check host key. (*)

- New remote files cannot be created. (*)

- Uses coroutine read/write, instead of true AIO. (libssh2 supports non-blocking access, so this could be fixed with some effort).

- Blocks during connection and authentication.

(*) = potentially easy fix

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.

Quoted 7 lines
---
 block/Makefile.objs |   1 +
 block/ssh.c         | 514 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure           |  47 +++++
 qemu-doc.texi       |  28 +++
 4 files changed, 590 insertions(+)
 create mode 100644 block/ssh.c

Just noticed that libcurl supports sftp.

Did you try enabling sftp support in block/curl.c? I think you just need to add CURLPROTO_SFTP to #define PROTOCOLS.

Stefan

#6 lore raw

Re: [Qemu-devel] [PATCH] block: Add support for Secure Shell (ssh) block device.

Quoted 38 lines · Stefan Hajnoczi
Quoted 29 lines · Richard W.M. Jones

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/...).

Current limitations:

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

- Does not check host key. (*)

- New remote files cannot be created. (*)

- Uses coroutine read/write, instead of true AIO. (libssh2 supports non-blocking access, so this could be fixed with some effort).

- Blocks during connection and authentication.

(*) = potentially easy fix

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.

Quoted 7 lines
---
 block/Makefile.objs |   1 +
 block/ssh.c         | 514 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure           |  47 +++++
 qemu-doc.texi       |  28 +++
 4 files changed, 590 insertions(+)
 create mode 100644 block/ssh.c
Quoted 4 lines

Just noticed that libcurl supports sftp.

Did you try enabling sftp support in block/curl.c? I think you just need to add CURLPROTO_SFTP to #define PROTOCOLS.

Interestingly curl's sftp support is implemented using libssh2. I'll take a look at how easy this will be.

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
#7 lore raw

PATCH 1/1 block/curl: Add support for Secure Shell (ssh/sftp) block device

In reply to Stefan Hajnoczi

I got it working with Curl, patch attached.

However there are multiple issues (these are mainly notes for myself):

(1) libcurl cannot read the size of the file. I had to hard-code this. This is probably just a shortcoming of libcurl (libssh2/sftp itself can read the size of files). Will try to work on a patch for upstream.

(2) Fedora's curl (which is heavily patched) is broken in some way and deadlocks itself. Upstream curl from git works better. I haven't yet identified which patch/commit is responsible.

(3) ssh-agent authentication doesn't work. It appears that either ssh-agent itself doesn't like multiple connections from a single process (qemu), or libcurl/libssh2 is having a problem with making multiple connections out to ssh-agent. If I disable ssh-agent auth, it works. Still investigating this.

(4) You must specify a user@ in the URL, else libcurl tries to authenticate as user "". I will see if I can send a fix for this upstream.

(5) Although it gets much of the way through a boot of a guest, it eventually segfaults. Still investigating this.

(6) There are several more issues marked by XXX's in the code.

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
Quoted 1 line

From c2ae4abf29973b8b8bc490b84fb761a8a4053fbf Mon Sep 17 00:00:00 2001

From: "Richard W.M. Jones" <[email protected]>
Date: Thu, 21 Mar 2013 21:38:10 +0000
Subject: [PATCH] block/curl: Add support for Secure Shell (ssh/sftp) block
device.

qemu-system-x86_64 -drive file=sftp://user@hostname/some/image

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

You must specify a 'user@' in the URL.

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.

This is implemented using curl (using libssh2 underneath).

Thanks: Stefan Hajnoczi for pointing out that this could be done much more easily using curl instead of using libssh2 directly.

---
 block/curl.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
block/curl.c +29 -1
index 98947da..b236f7f 100644
@@ -36,7 +36,7 @@
 
 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
                    CURLPROTO_FTP | CURLPROTO_FTPS | \
-                   CURLPROTO_TFTP)
+                   CURLPROTO_TFTP | CURLPROTO_SFTP)
 
 #define CURL_NUM_STATES 8
 #define CURL_NUM_ACB    8
@@ -305,6 +305,17 @@ static CURLState *curl_init_state(BDRVCURLState *s)
     curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
     curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
     curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
+#if defined(CURLSSH_AUTH_ANY)
+    curl_easy_setopt(state->curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_ANY);
+#endif
+
+    /* XXX */
+    curl_easy_setopt (state->curl, CURLOPT_SSH_PRIVATE_KEYFILE,
+                      "/home/rjones/.ssh/id_rsa"); /* XXX */
+    curl_easy_setopt (state->curl, CURLOPT_SSH_PUBLIC_KEYFILE,
+                      "/home/rjones/.ssh/id_rsa.pub"); /* XXX */
+    curl_easy_setopt (state->curl, CURLOPT_KEYPASSWD,
+                      "XXX PUT YOUR PASSPHRASE HERE XXX"); /* XXX */
 
     /* Restrict supported protocols to avoid security issues in the more
      * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
@@ -406,8 +417,12 @@ static int curl_open(BlockDriverState *bs, const char *filename, int flags)
     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
     if (d)
         s->len = (size_t)d;
+#if 0
     else if(!s->len)
         goto out;
+#else
+    s->len = 8589934592; /* XXX */
+#endif
     DPRINTF("CURL: Size = %zd\n", s->len);
 
     curl_clean_state(state);
@@ -626,6 +641,18 @@ static BlockDriver bdrv_tftp = {
     .bdrv_aio_readv  = curl_aio_readv,
 };
 
+static BlockDriver bdrv_sftp = {
+    .format_name     = "sftp",
+    .protocol_name   = "sftp",
+
+    .instance_size   = sizeof(BDRVCURLState),
+    .bdrv_file_open  = curl_open,
+    .bdrv_close      = curl_close,
+    .bdrv_getlength  = curl_getlength,
+
+    .bdrv_aio_readv  = curl_aio_readv,
+};
+
 static void curl_block_init(void)
 {
     bdrv_register(&bdrv_http);
@@ -633,6 +660,7 @@ static void curl_block_init(void)
     bdrv_register(&bdrv_ftp);
     bdrv_register(&bdrv_ftps);
     bdrv_register(&bdrv_tftp);
+    bdrv_register(&bdrv_sftp);
 }
 
 block_init(curl_block_init);
Signature · 1 line
1.8.1.4
#8 lore raw

Re: [Qemu-devel] [PATCH] block/curl: Add support for Secure Shell (ssh/sftp) block device

Quoted 20 lines · Richard W.M. Jones

I got it working with Curl, patch attached.

However there are multiple issues (these are mainly notes for myself):

(1) libcurl cannot read the size of the file. I had to hard-code this. This is probably just a shortcoming of libcurl (libssh2/sftp itself can read the size of files). Will try to work on a patch for upstream.

(2) Fedora's curl (which is heavily patched) is broken in some way and deadlocks itself. Upstream curl from git works better. I haven't yet identified which patch/commit is responsible.

(3) ssh-agent authentication doesn't work. It appears that either ssh-agent itself doesn't like multiple connections from a single process (qemu), or libcurl/libssh2 is having a problem with making multiple connections out to ssh-agent. If I disable ssh-agent auth, it works. Still investigating this.

Quoted 8 lines

(4) You must specify a user@ in the URL, else libcurl tries to authenticate as user "". I will see if I can send a fix for this upstream.

(5) Although it gets much of the way through a boot of a guest, it eventually segfaults. Still investigating this.

(6) There are several more issues marked by XXX's in the code.

Thank you for improving libcurl! You're making it better for everybody.

A lot of people go back to NIH when they hit limitations in existing software.

Stefan

#9 lore raw

Re: [Qemu-devel] [PATCH] block/curl: Add support for Secure Shell (ssh/sftp) block device

In reply to Richard W.M. Jones
Quoted 8 lines · Richard W.M. Jones

I got it working with Curl, patch attached.

However there are multiple issues (these are mainly notes for myself):

(1) libcurl cannot read the size of the file. I had to hard-code this. This is probably just a shortcoming of libcurl (libssh2/sftp itself can read the size of files). Will try to work on a patch for upstream.

After my holiday and in the cold of day I've had a long look at the SFTP implementation in libcurl.

https://github.com/bagder/curl/blob/master/lib/ssh.c

It's implemented as a huge state machine and simply implementing (1) above is problematic (I believe we would have to reopen the connection after our call to curl_easy_perform).

The larger issue is that the qemu curl block driver doesn't support writes. Now these could in theory be added. Indeed curl does support "random access" writes, although AFAICT you have to open a new connection each time you want to seek backwards, and you can't read and write over the same connection (so you'd need >= 1 connections for reading and another >= 1 connections for writing).

I think I will continue with the pure libssh2-based block driver, making it support all the missing features discussed earlier:

http://www.mail-archive.com/[email protected]/msg161997.html

plus of course non-blocking AIO.

I think this way we'll end up with a much more robust, reliable and easier to debug ssh implementation in qemu.

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
#10 lore raw

Re: [Qemu-devel] [PATCH] block/curl: Add support for Secure Shell (ssh/sftp) block device

Quoted 29 lines · Richard W.M. Jones
Quoted 8 lines · Richard W.M. Jones

I got it working with Curl, patch attached.

However there are multiple issues (these are mainly notes for myself):

(1) libcurl cannot read the size of the file. I had to hard-code this. This is probably just a shortcoming of libcurl (libssh2/sftp itself can read the size of files). Will try to work on a patch for upstream.

After my holiday and in the cold of day I've had a long look at the SFTP implementation in libcurl.

https://github.com/bagder/curl/blob/master/lib/ssh.c

It's implemented as a huge state machine and simply implementing (1) above is problematic (I believe we would have to reopen the connection after our call to curl_easy_perform).

The larger issue is that the qemu curl block driver doesn't support writes. Now these could in theory be added. Indeed curl does support "random access" writes, although AFAICT you have to open a new connection each time you want to seek backwards, and you can't read and write over the same connection (so you'd need >= 1 connections for reading and another >= 1 connections for writing).

I think I will continue with the pure libssh2-based block driver, making it support all the missing features discussed earlier:

Quoted 6 lines

http://www.mail-archive.com/[email protected]/msg161997.html

plus of course non-blocking AIO.

I think this way we'll end up with a much more robust, reliable and easier to debug ssh implementation in qemu.

Fair enough.

Stefan

#11 lore raw

Re: [Qemu-devel] [PATCH] block: Add support for Secure Shell (ssh) block device.

In reply to Richard W.M. Jones

Am 21.03.2013 um 14:38 hat Richard W.M. Jones geschrieben:

Quoted 7 lines

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.

Quoted 7 lines

You can specify a username (ssh://user@host/...) and/or a port number (ssh://host:port/...).

Current limitations:

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

Maybe you can just expose it as an encrypted image in order to allow password authentication?

Quoted 18 lines

- Does not check host key. (*)

- New remote files cannot be created. (*)

- Uses coroutine read/write, instead of true AIO. (libssh2 supports non-blocking access, so this could be fixed with some effort).

- Blocks during connection and authentication.

(*) = potentially easy fix

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.

block/ssh.c line 8

Quoted 8 lines
block/ssh.c new file +8 -0
@@ -0,0 +1,514 @@
+/*
+ * Secure Shell (ssh) backend for QEMU.
+ *
+ * Copyright (C) 2013 Red Hat Inc., Richard W.M. Jones <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */

Please consider the MIT license or LGPL for block layer code; otherwise we may have to compile the driver out for a future libqblock.

block/ssh.c line 10

Quoted 2 lines
block/ssh.c new file +2 -0
+
+#define SECTOR_SIZE 512

BDRV_SECTOR_SIZE exists.

block/ssh.c line 36

Quoted 11 lines
block/ssh.c new file +11 -0
+
+static void __attribute__((constructor)) ssh_init(void)
+{
+    int r;
+
+    r = libssh2_init(0);
+    if (r != 0) {
+        fprintf(stderr, "libssh2 initialization failed, %d\n", r);
+        exit(EXIT_FAILURE);
+    }
+}

Why don't you include this in bdrv_ssh_init()?

Quoted 35 lines
block/ssh.c new file +35 -0
+static int parse_uri(BDRVSSHState *s, const char *filename)
+{
+    URI *uri;
+
+    uri = uri_parse(filename);
+    if (!uri)
+        return -EINVAL;
+
+    if (strcmp(uri->scheme, "ssh") != 0) {
+        error_report("URI scheme must be 'ssh'");
+        goto err;
+    }
+
+    if (!uri->server || strcmp(uri->server, "") == 0) {
+        error_report("missing hostname in URI");
+        goto err;
+    }
+
+    if (!uri->path || strcmp(uri->path, "") == 0) {
+        error_report("missing remote path in URI");
+        goto err;
+    }
+
+    /* If user is defined in the URI, use it.  Else use local username. */
+    if(uri->user && strcmp(uri->user, "") != 0)
+        s->user = g_strdup(uri->user);
+    else {
+        s->user = get_username();
+        if (!s->user)
+            goto err;
+    }
+
+    /* Construct the hostname:port string for inet_connect. */
+    if(uri->port == 0)
+        uri->port = 22;

block/ssh.c line 183

Quoted 10 lines
block/ssh.c new file +10 -0
+    s->host = g_strdup_printf("%s:%d", uri->server, uri->port);
+
+    s->path = g_strdup(uri->path);
+
+    return 0;
+
+ err:
+    uri_free(uri);
+    return -EINVAL;
+}

You could implement this similar to what I just changed in NBD, using the new .bdrv_parse_filename callback and driver-specific options, so that instead of specifying a URL you can use something like:

-drive file.driver=ssh,file.host=localhost,file.user=test

This should turn out much more elegant than management tools encoding everything in a single filename string and qemu parsing it again, especially once QMP support passing this as JSON objects.

block/ssh.c line 317

Quoted 2 lines
block/ssh.c new file +2 -0
+static int ssh_file_open(BlockDriverState *bs, const char *filename,
+                         int bdrv_flags)

After rebasing, this will have a new parameter for driver-specific options.

block/ssh.c line 327

Quoted 3 lines
block/ssh.c new file +3 -0
+    ret = parse_uri(s, filename);
+    if (ret < 0)
+        goto err;

I only see it now, but it seems to be repeated all over the place: The qemu coding style requires braces here.

Quoted 25 lines
block/ssh.c new file +25 -0
+
+    /* Open the socket and connect. */
+    s->sock = inet_connect(s->host, &err);
+    if (err != NULL) {
+        ret = -errno;
+        qerror_report_err(err);
+        error_free(err);
+        goto err;
+    }
+
+    /* Start up SSH. */
+    ret = connect_to_ssh(s);
+    if (ret < 0)
+        goto err;
+
+#if 0 /* Enable this when AIO callbacks are implemented. */
+    /* Go non-blocking. */
+    libssh2_session_set_blocking(s->session, 0);
+#endif
+
+    qemu_co_mutex_init(&s->lock);
+
+    return 0;
+
+ err:

block/ssh.c line 362

Quoted 10 lines
block/ssh.c new file +10 -0
+    if (s->sock >= 0)
+        close(s->sock);
+    s->sock = -1;
+
+    g_free(s->path);
+    s->path = NULL;
+    g_free(s->host);
+    s->host = NULL;
+    g_free(s->user);
+    s->user = NULL;

Resetting the fields isn't really necessary. s will be freed anyway.

Quoted 5 lines
block/ssh.c new file +5 -0
+static BlockDriver bdrv_ssh = {
+    .format_name                  = "ssh",
+    .protocol_name                = "ssh",
+    .instance_size                = sizeof(BDRVSSHState),
+    .bdrv_file_open               = ssh_file_open,

block/ssh.c line 507

Quoted 10 lines
block/ssh.c new file +10 -0
+    .bdrv_close                   = ssh_close,
+    .bdrv_read                    = ssh_co_read,
+    .bdrv_write                   = ssh_co_write,
+    .bdrv_getlength               = ssh_getlength,
+#if 0
+    .bdrv_aio_readv               = ssh_aio_readv,
+    .bdrv_aio_writev              = ssh_aio_writev,
+    .bdrv_aio_flush               = ssh_aio_flush,
+#endif
+};

You don't have a bdrv_co_flush_to_disk, what does this mean? Is every write immediately flushed to the disk, is the driver unsafe by design, or is this just a missing implementation detail?

Kevin

#12 lore raw

Re: [Qemu-devel] [PATCH] block: Add support for Secure Shell (ssh) block device.

Quoted 3 lines · Kevin Wolf

You don't have a bdrv_co_flush_to_disk, what does this mean? Is every write immediately flushed to the disk, is the driver unsafe by design, or is this just a missing implementation detail?

Initially there is no .bdrv_co_flush_to_disk because this patch is just a proof of concept.

However it does seem likely that a true flush-to-disk operation is not possible with sftp. Assuming this is supposed to guarantee that the bits have been committed to disk, I don't see anything in the sftp protocol that supports that guarantee. It seems to be left up to the implementation to do whatever it wants.

Here's the protocol v3 as implemented by OpenSSH:

http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02

and here's the latest version v6 (not implemented by anyone AFAIK):

http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13

if you think you can see anything that I've missed ...

Also I grepped over the source of OpenSSH and there is no call to sync(2), fsync(2) or fdatasync(2).

Rich.

Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
#13 lore raw

Re: [Qemu-devel] [PATCH] block: Add support for Secure Shell (ssh) block device.

Quoted 18 lines · Richard W.M. Jones
Quoted 3 lines · Kevin Wolf

You don't have a bdrv_co_flush_to_disk, what does this mean? Is every write immediately flushed to the disk, is the driver unsafe by design, or is this just a missing implementation detail?

Initially there is no .bdrv_co_flush_to_disk because this patch is just a proof of concept.

However it does seem likely that a true flush-to-disk operation is not possible with sftp. Assuming this is supposed to guarantee that the bits have been committed to disk, I don't see anything in the sftp protocol that supports that guarantee. It seems to be left up to the implementation to do whatever it wants.

Here's the protocol v3 as implemented by OpenSSH:

http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02

Quoted 8 lines

and here's the latest version v6 (not implemented by anyone AFAIK):

http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13

if you think you can see anything that I've missed ...

Also I grepped over the source of OpenSSH and there is no call to sync(2), fsync(2) or fdatasync(2).

I looked at OpenSSH sftp-server.c to see if open, close, or write do anything special. They don't.

There's also no 'sync' command in the SFTP protocol.

So unless the underlying filesystem is mounted -o sync, there is no guarantee that data is safely on disk.

Stefan