Skip to contentSkip to conversation

NBD updates for 2012-11-12

GIT PULL New Paolo Bonzini 1 participant 9 messages Nov 12, 2012 – Nov 13, 2012 Block layer

lore
Share & tools
lore
#0 lore raw

Anthony,

The following changes since commit 3c5645fab3c4b65d0cffbe1aaafc787e4be63d0f:

tcg: properly check that op's output needs to be synced to memory (2012-11-11 16:06:46 +0100)

are available in the git repository at:

git://github.com/bonzini/qemu.git nbd-next

for you to fetch changes up to 4057725f35abe00ea371f85c6e27dd25eafd9ddf:

hmp: add NBD server commands (2012-11-12 14:38:29 +0100)

The main change is making the NBD server available via HMP. This prompted a few minor usability improvements in the QMP interface too. Also, NBD is made accessible via URIs, similar to Gluster (the change is backwards-compatible).

----------------------------------------------------------------
Paolo Bonzini (7):
qemu-nbd: initialize main loop before block layer
nbd: accept relative path to Unix socket
nbd: accept URIs
nbd: fix nbd_server_stop crash when no server was running
nbd: force read-only export for read-only devices
nbd: disallow nbd-server-add before nbd-server-start
hmp: add NBD server commands

 block/nbd.c     | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 blockdev-nbd.c  |  20 ++++++++--
 hmp-commands.hx |  45 ++++++++++++++++++++++
 hmp.c           |  76 +++++++++++++++++++++++++++++++++++++
 hmp.h           |   3 ++
 qemu-doc.texi   |  25 ++++++++----
 qemu-nbd.c      |   2 +-

7 file modificati, 263 inserzioni(+), 23 rimozioni(-)

Signature · 1 line
1.7.12.1
#1 lore raw

PATCH 1/7 qemu-nbd: initialize main loop before block layer

qemu-nbd was broken because they initialized the block layer while qemu_aio_context was still NULL.

---
 qemu-nbd.c | 2 +-

1 file modificato, 1 inserzione(+). 1 rimozione(-)

qemu-nbd.c +1 -1
index 15bcd08..80f08d8 100644
@@ -539,6 +539,7 @@ int main(int argc, char **argv)
         snprintf(sockpath, 128, SOCKET_PATH, basename(device));
     }
 
+    qemu_init_main_loop();
     bdrv_init();
     atexit(bdrv_close_all);
 
@@ -584,7 +585,6 @@ int main(int argc, char **argv)
         memset(&client_thread, 0, sizeof(client_thread));
     }
 
-    qemu_init_main_loop();
     qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
                          (void *)(uintptr_t)fd);
 
Signature · 1 line
1.7.12.1
#2 lore raw

PATCH 2/7 nbd: accept relative path to Unix socket

In reply to Paolo Bonzini

Adding the "is_unix" member now will simplify the parsing of NBD URIs.

---
 block/nbd.c | 17 +++++++----------

1 file modificato, 7 inserzioni(+), 10 rimozioni(-)

block/nbd.c +7 -10
index 2bce47b..48bbeca 100644
@@ -55,7 +55,6 @@ typedef struct BDRVNBDState {
     uint32_t nbdflags;
     off_t size;
     size_t blocksize;
-    char *export_name; /* An NBD server may export several devices */
 
     CoMutex send_mutex;
     CoMutex free_sema;
@@ -65,13 +64,12 @@ typedef struct BDRVNBDState {
     Coroutine *recv_coroutine[MAX_NBD_REQUESTS];
     struct nbd_reply reply;
 
-    /* If it begins with  '/', this is a UNIX domain socket. Otherwise,
-     * it's a string of the form <hostname|ip4|\[ip6\]>:port
-     */
+    int is_unix;
     char *host_spec;
+    char *export_name; /* An NBD server may export several devices */
 } BDRVNBDState;
 
-static int nbd_config(BDRVNBDState *s, const char *filename, int flags)
+static int nbd_config(BDRVNBDState *s, const char *filename)
 {
     char *file;
     char *export_name;
@@ -98,11 +96,10 @@ static int nbd_config(BDRVNBDState *s, const char *filename, int flags)
 
     /* are we a UNIX or TCP socket? */
     if (strstart(host_spec, "unix:", &unixpath)) {
-        if (unixpath[0] != '/') { /* We demand  an absolute path*/
-            goto out;
-        }
+        s->is_unix = true;
         s->host_spec = g_strdup(unixpath);
     } else {
+        s->is_unix = false;
         s->host_spec = g_strdup(host_spec);
     }
 
@@ -262,7 +259,7 @@ static int nbd_establish_connection(BlockDriverState *bs)
     off_t size;
     size_t blocksize;
 
-    if (s->host_spec[0] == '/') {
+    if (s->is_unix) {
         sock = unix_socket_outgoing(s->host_spec);
     } else {
         sock = tcp_socket_outgoing_spec(s->host_spec);
@@ -320,7 +317,7 @@ static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
     qemu_co_mutex_init(&s->free_sema);
 
     /* Pop the config into our state object. Exit if invalid. */
-    result = nbd_config(s, filename, flags);
+    result = nbd_config(s, filename);
     if (result != 0) {
         return result;
     }
Signature · 1 line
1.7.12.1
#3 lore raw

PATCH 3/7 nbd: accept URIs

In reply to Paolo Bonzini

The URI syntax is consistent with the Gluster syntax. Export names are specified in the path, preceded by one or more (otherwise unused) slashes.

---
 block/nbd.c   | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 qemu-doc.texi | 25 ++++++++++-----

2 file modificati, 114 inserzioni(+), 9 rimozioni(-)

block/nbd.c +97 -1
index 48bbeca..e87c248 100644
@@ -28,6 +28,7 @@
 
 #include "qemu-common.h"
 #include "nbd.h"
+#include "uri.h"
 #include "block_int.h"
 #include "module.h"
 #include "qemu_socket.h"
@@ -69,6 +70,69 @@ typedef struct BDRVNBDState {
     char *export_name; /* An NBD server may export several devices */
 } BDRVNBDState;
 
+static int nbd_parse_uri(BDRVNBDState *s, const char *filename)
+{
+    URI *uri;
+    const char *p;
+    QueryParams *qp = NULL;
+    int ret = 0;
+
+    uri = uri_parse(filename);
+    if (!uri) {
+        return -EINVAL;
+    }
+
+    /* transport */
+    if (!strcmp(uri->scheme, "nbd")) {
+        s->is_unix = false;
+    } else if (!strcmp(uri->scheme, "nbd+tcp")) {
+        s->is_unix = false;
+    } else if (!strcmp(uri->scheme, "nbd+unix")) {
+        s->is_unix = true;
+    } else {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    p = uri->path ? uri->path : "/";
+    p += strspn(p, "/");
+    if (p[0]) {
+        s->export_name = g_strdup(p);
+    }
+
+    qp = query_params_parse(uri->query);
+    if (qp->n > 1 || (s->is_unix && !qp->n) || (!s->is_unix && qp->n)) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    if (s->is_unix) {
+        /* nbd+unix:///export?socket=path */
+        if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
+            ret = -EINVAL;
+            goto out;
+        }
+        s->host_spec = g_strdup(qp->p[0].value);
+    } else {
+        /* nbd[+tcp]://host:port/export */
+        if (!uri->server) {
+            ret = -EINVAL;
+            goto out;
+        }
+        if (!uri->port) {
+            uri->port = NBD_DEFAULT_PORT;
+        }
+        s->host_spec = g_strdup_printf("%s:%d", uri->server, uri->port);
+    }
+
+out:
+    if (qp) {
+        query_params_free(qp);
+    }
+    uri_free(uri);
+    return ret;
+}
+
 static int nbd_config(BDRVNBDState *s, const char *filename)
 {
     char *file;
@@ -77,6 +141,10 @@ static int nbd_config(BDRVNBDState *s, const char *filename)
     const char *unixpath;
     int err = -EINVAL;
 
+    if (strstr(filename, "://")) {
+        return nbd_parse_uri(s, filename);
+    }
+
     file = g_strdup(filename);
 
     export_name = strstr(file, EN_OPTSTR);
@@ -495,6 +563,33 @@ static int64_t nbd_getlength(BlockDriverState *bs)
 
 static BlockDriver bdrv_nbd = {
     .format_name         = "nbd",
+    .protocol_name       = "nbd",
+    .instance_size       = sizeof(BDRVNBDState),
+    .bdrv_file_open      = nbd_open,
+    .bdrv_co_readv       = nbd_co_readv,
+    .bdrv_co_writev      = nbd_co_writev,
+    .bdrv_close          = nbd_close,
+    .bdrv_co_flush_to_os = nbd_co_flush,
+    .bdrv_co_discard     = nbd_co_discard,
+    .bdrv_getlength      = nbd_getlength,
+};
+
+static BlockDriver bdrv_nbd_tcp = {
+    .format_name         = "nbd",
+    .protocol_name       = "nbd+tcp",
+    .instance_size       = sizeof(BDRVNBDState),
+    .bdrv_file_open      = nbd_open,
+    .bdrv_co_readv       = nbd_co_readv,
+    .bdrv_co_writev      = nbd_co_writev,
+    .bdrv_close          = nbd_close,
+    .bdrv_co_flush_to_os = nbd_co_flush,
+    .bdrv_co_discard     = nbd_co_discard,
+    .bdrv_getlength      = nbd_getlength,
+};
+
+static BlockDriver bdrv_nbd_unix = {
+    .format_name         = "nbd",
+    .protocol_name       = "nbd+unix",
     .instance_size       = sizeof(BDRVNBDState),
     .bdrv_file_open      = nbd_open,
     .bdrv_co_readv       = nbd_co_readv,
@@ -503,12 +598,13 @@ static BlockDriver bdrv_nbd = {
     .bdrv_co_flush_to_os = nbd_co_flush,
     .bdrv_co_discard     = nbd_co_discard,
     .bdrv_getlength      = nbd_getlength,
-    .protocol_name       = "nbd",
 };
 
 static void bdrv_nbd_init(void)
 {
     bdrv_register(&bdrv_nbd);
+    bdrv_register(&bdrv_nbd_tcp);
+    bdrv_register(&bdrv_nbd_unix);
 }
 
 block_init(bdrv_nbd_init);
qemu-doc.texi +17 -8
index 35cabbc..d8fb2de 100644
@@ -610,14 +610,14 @@ QEMU can access directly to block device exported using the Network Block Device
 protocol.
 
 @example
-qemu-system-i386 linux.img -hdb nbd:my_nbd_server.mydomain.org:1024
+qemu-system-i386 linux.img -hdb nbd://my_nbd_server.mydomain.org:1024/
 @end example
 
 If the NBD server is located on the same host, you can use an unix socket instead
 of an inet socket:
 
 @example
-qemu-system-i386 linux.img -hdb nbd:unix:/tmp/my_socket
+qemu-system-i386 linux.img -hdb nbd+unix://?socket=/tmp/my_socket
 @end example
 
 In this case, the block device must be exported using qemu-nbd:
@@ -631,17 +631,26 @@ The use of qemu-nbd allows to share a disk between several guests:
 qemu-nbd --socket=/tmp/my_socket --share=2 my_disk.qcow2
 @end example
 
+@noindent
 and then you can use it with two guests:
 @example
-qemu-system-i386 linux1.img -hdb nbd:unix:/tmp/my_socket
-qemu-system-i386 linux2.img -hdb nbd:unix:/tmp/my_socket
+qemu-system-i386 linux1.img -hdb nbd+unix://?socket=/tmp/my_socket
+qemu-system-i386 linux2.img -hdb nbd+unix://?socket=/tmp/my_socket
 @end example
 
-If the nbd-server uses named exports (since NBD 2.9.18), you must use the
-"exportname" option:
+If the nbd-server uses named exports (supported since NBD 2.9.18, or with QEMU's
+own embedded NBD server), you must specify an export name in the URI:
 @example
-qemu-system-i386 -cdrom nbd:localhost:exportname=debian-500-ppc-netinst
-qemu-system-i386 -cdrom nbd:localhost:exportname=openSUSE-11.1-ppc-netinst
+qemu-system-i386 -cdrom nbd://localhost/debian-500-ppc-netinst
+qemu-system-i386 -cdrom nbd://localhost/openSUSE-11.1-ppc-netinst
+@end example
+
+The URI syntax for NBD is supported since QEMU 1.3.  An alternative syntax is
+also available.  Here are some example of the older syntax:
+@example
+qemu-system-i386 linux.img -hdb nbd:my_nbd_server.mydomain.org:1024
+qemu-system-i386 linux2.img -hdb nbd:unix:/tmp/my_socket
+qemu-system-i386 -cdrom nbd:localhost:10809:exportname=debian-500-ppc-netinst
 @end example
 
 @node disk_images_sheepdog
Signature · 1 line
1.7.12.1
#4 lore raw

PATCH 4/7 nbd: fix nbd_server_stop crash when no server was running

In reply to Paolo Bonzini

This failed on the new assertion of qemu_set_fd_handler2:

qemu-system-x86_64: /home/pbonzini/work/upstream/qemu/iohandler.c:60: qemu_set_fd_handler2: Assertion `fd >= 0' failed.

---
 blockdev-nbd.c | 8 +++++---

1 file modificato, 5 inserzioni(+), 3 rimozioni(-)

blockdev-nbd.c +5 -3
index 8031813..274fba6 100644
@@ -113,7 +113,9 @@ void qmp_nbd_server_stop(Error **errp)
         nbd_close_notifier(&cn->n, nbd_export_get_blockdev(cn->exp));
     }
 
-    qemu_set_fd_handler2(server_fd, NULL, NULL, NULL, NULL);
-    close(server_fd);
-    server_fd = -1;
+    if (server_fd != -1) {
+        qemu_set_fd_handler2(server_fd, NULL, NULL, NULL, NULL);
+        close(server_fd);
+        server_fd = -1;
+    }
 }
Signature · 1 line
1.7.12.1
#5 lore raw

PATCH 5/7 nbd: force read-only export for read-only devices

In reply to Paolo Bonzini

This is the desired behavior for HMP, but it is a better choice for QMP as well.

---
 blockdev-nbd.c | 7 +++++++

1 file modificato, 7 inserzioni(+)

blockdev-nbd.c +7 -0
index 274fba6..e362572 100644
@@ -93,6 +93,13 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
         return;
     }
 
+    if (!has_writable) {
+        writable = true;
+    }
+    if (bdrv_is_read_only(bs)) {
+        writable = false;
+    }
+
     exp = nbd_export_new(bs, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY,
                          nbd_server_put_ref);
 
Signature · 1 line
1.7.12.1
#6 lore raw

PATCH 6/7 nbd: disallow nbd-server-add before nbd-server-start

In reply to Paolo Bonzini

It works nicely with the QMP commands, but it adds useless complication with HMP. In particular, see the following:

(qemu) nbd_server_add -w scsi0-hd0
(qemu) nbd_server_start -a localhost:10809
NBD server already exporting device scsi0-hd0

---
 blockdev-nbd.c | 5 +++++

1 file modificato, 5 inserzioni(+)

blockdev-nbd.c +5 -0
index e362572..d1721a3 100644
@@ -82,6 +82,11 @@ void qmp_nbd_server_add(const char *device, bool has_writable, bool writable,
     NBDExport *exp;
     NBDCloseNotifier *n;
 
+    if (server_fd == -1) {
+        error_setg(errp, "NBD server not running");
+        return;
+    }
+
     if (nbd_export_find(device)) {
         error_setg(errp, "NBD server already exporting device '%s'", device);
         return;
Signature · 1 line
1.7.12.1
#7 lore raw

PATCH 7/7 hmp: add NBD server commands

In reply to Paolo Bonzini
---
 hmp-commands.hx | 45 ++++++++++++++++++++++++++++++++++
 hmp.c           | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hmp.h           |  3 +++

3 file modificati, 124 inserzioni(+)

hmp-commands.hx +45 -0
index f916385..b74ef75 100644
@@ -1310,6 +1310,51 @@ Remove all matches from the access control list, and set the default
 policy back to @code{deny}.
 ETEXI
 
+    {
+        .name       = "nbd_server_start",
+        .args_type  = "all:-a,writable:-w,uri:s",
+        .params     = "nbd_server_start [-a] [-w] host:port",
+        .help       = "serve block devices on the given host and port",
+        .mhandler.cmd = hmp_nbd_server_start,
+    },
+STEXI
+@item nbd_server_start @var{host}:@var{port}
+@findex nbd_server_start
+Start an NBD server on the given host and/or port.  If the @option{-a}
+option is included, all of the virtual machine's block devices that
+have an inserted media on them are automatically exported; in this case,
+the @option{-w} option makes the devices writable too.
+ETEXI
+
+    {
+        .name       = "nbd_server_add",
+        .args_type  = "writable:-w,device:B",
+        .params     = "nbd_server_add [-w] device",
+        .help       = "export a block device via NBD",
+        .mhandler.cmd = hmp_nbd_server_add,
+    },
+STEXI
+@item nbd_server_add @var{device}
+@findex nbd_server_add
+Export a block device through QEMU's NBD server, which must be started
+beforehand with @command{nbd_server_start}.  The @option{-w} option makes the
+exported device writable too.
+ETEXI
+
+    {
+        .name       = "nbd_server_stop",
+        .args_type  = "",
+        .params     = "nbd_server_stop",
+        .help       = "stop serving block devices using the NBD protocol",
+        .mhandler.cmd = hmp_nbd_server_stop,
+    },
+STEXI
+@item nbd_server_stop
+@findex nbd_server_stop
+Stop the QEMU embedded NBD server.
+ETEXI
+
+
 #if defined(TARGET_I386)
 
     {
hmp.c +76 -0

85 lines not on this page · raw message

hmp.h +3 -0
index 34eb2b3..0ab03be 100644
@@ -77,5 +77,8 @@ void hmp_getfd(Monitor *mon, const QDict *qdict);
 void hmp_closefd(Monitor *mon, const QDict *qdict);
 void hmp_send_key(Monitor *mon, const QDict *qdict);
 void hmp_screen_dump(Monitor *mon, const QDict *qdict);
+void hmp_nbd_server_start(Monitor *mon, const QDict *qdict);
+void hmp_nbd_server_add(Monitor *mon, const QDict *qdict);
+void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict);
 
 #endif
Signature · 1 line
1.7.12.1
#8 lore raw
In reply to Paolo Bonzini

Il 12/11/2012 15:22, Paolo Bonzini ha scritto:

Quoted 10 lines

Anthony,

The following changes since commit 3c5645fab3c4b65d0cffbe1aaafc787e4be63d0f:

tcg: properly check that op's output needs to be synced to memory (2012-11-11 16:06:46 +0100)

are available in the git repository at:

git://github.com/bonzini/qemu.git nbd-next

Quoted 8 lines

for you to fetch changes up to 4057725f35abe00ea371f85c6e27dd25eafd9ddf:

hmp: add NBD server commands (2012-11-12 14:38:29 +0100)

The main change is making the NBD server available via HMP. This prompted a few minor usability improvements in the QMP interface too. Also, NBD is made accessible via URIs, similar to Gluster (the change is backwards-compatible).

And I added "nbd: fixes to read-only handling" on top of this branch.

Paolo

Quoted 19 lines

----------------------------------------------------------------
Paolo Bonzini (7):
qemu-nbd: initialize main loop before block layer
nbd: accept relative path to Unix socket
nbd: accept URIs
nbd: fix nbd_server_stop crash when no server was running
nbd: force read-only export for read-only devices
nbd: disallow nbd-server-add before nbd-server-start
hmp: add NBD server commands

 block/nbd.c     | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 blockdev-nbd.c  |  20 ++++++++--
 hmp-commands.hx |  45 ++++++++++++++++++++++
 hmp.c           |  76 +++++++++++++++++++++++++++++++++++++
 hmp.h           |   3 ++
 qemu-doc.texi   |  25 ++++++++----
 qemu-nbd.c      |   2 +-

7 file modificati, 263 inserzioni(+), 23 rimozioni(-)