qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [patch 5/5] Allow QEMU to connect directly to an NBD server


From: Laurent . Vivier
Subject: [Qemu-devel] [patch 5/5] Allow QEMU to connect directly to an NBD server
Date: Fri, 27 Jun 2008 13:02:09 +0200
User-agent: quilt/0.45-1

This patch add a new protocol to QEMU block interface: "nbd".

It allows to QEMU to connect to an NBD server without using kernel support
(/dev/nbd*).

example:
qemu linux.img -hdb nbd:my_nbd_server.mydomain.org:1024

If the NBD server is located on the same host, you can use an unix socket
instead of an inet socket:
qemu linux.img -hdb nbd:unix:/tmp/my_socket

In this case, the block device must be exported using qemu-nbd:
qemu-nbd --socket=/tmp/my_socket my_disk.qcow2

The use of qemu-nbd allows to share a disk between several guests:

qemu-nbd --socket=/tmp/my_socket --share=2 my_disk.qcow2

and then you can use it with two guests:

qemu linux1.img -hdb nbd:unix:/tmp/my_socket
qemu linux2.img -hdb nbd:unix:/tmp/my_socket
---
 Makefile      |    8 +-
 block-nbd.c   |  194 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block.c       |    1 
 block.h       |    1 
 nbd.c         |  196 ++++++++++++++++++++++++++++++++++++++++------------------
 nbd.h         |   22 ++++++
 qemu-doc.texi |   35 ++++++++++
 qemu-nbd.c    |   10 ++
 qemu-nbd.texi |    2 
 9 files changed, 403 insertions(+), 66 deletions(-)

Index: qemu/block-nbd.c
===================================================================
--- /dev/null   1970-01-01 00:00:00.000000000 +0000
+++ qemu/block-nbd.c    2008-06-27 11:35:14.000000000 +0200
@@ -0,0 +1,194 @@
+/*
+ * QEMU Block driver for  NBD
+ *
+ * Copyright (C) 2008 Bull S.A.S.
+ *     Author: Laurent Vivier <address@hidden;net>
+ *
+ * Some parts:
+ *    Copyright (C) 2007 Anthony Liguori <address@hidden>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu-common.h"
+#include "nbd.h"
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <pthread.h>
+
+typedef struct BDRVNBDState {
+    int sock;
+    off_t size;
+    size_t blocksize;
+} BDRVNBDState;
+
+static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
+{
+    BDRVNBDState *s = bs->opaque;
+    const char *host;
+    const char *unixpath;
+    int sock;
+    off_t size;
+    size_t blocksize;
+    int ret;
+
+    if ((flags & BDRV_O_CREAT))
+        return -EINVAL;
+
+    if (!strstart(filename, "nbd:", &host))
+        return -EINVAL;
+
+    if (strstart(host, "unix:", &unixpath)) {
+
+        if (unixpath[0] != '/')
+            return -EINVAL;
+
+        sock = unix_socket_outgoing(unixpath);
+
+    } else {
+        uint16_t port;
+        char *p, *r;
+        char hostname[128];
+
+        pstrcpy(hostname, 128, host);
+
+        p = strchr(hostname, ':');
+        if (p == NULL)
+            return -EINVAL;
+
+        *p = '\0';
+        p++;
+
+        port = strtol(p, &r, 0);
+        if (r == p)
+            return -EINVAL;
+        sock = tcp_socket_outgoing(hostname, port);
+    }
+
+    if (sock == -1)
+        return -errno;
+
+    ret = nbd_receive_negotiate(sock, &size, &blocksize);
+    if (ret == -1)
+        return -errno;
+
+    s->sock = sock;
+    s->size = size;
+    s->blocksize = blocksize;
+
+    return 0;
+}
+
+static int nbd_read(BlockDriverState *bs, int64_t sector_num,
+                    uint8_t *buf, int nb_sectors)
+{
+    BDRVNBDState *s = bs->opaque;
+    struct nbd_request request;
+    struct nbd_reply reply;
+
+    request.type = NBD_CMD_READ;
+    request.handle = (uint64_t)bs;
+    request.from = sector_num * 512;;
+    request.len = nb_sectors * 512;
+
+    if (nbd_send_request(s->sock, &request) == -1)
+        return -errno;
+
+    if (nbd_receive_reply(s->sock, &reply) == -1)
+        return -errno;
+
+    if (reply.error !=0)
+        return -reply.error;
+
+    if (reply.handle != request.handle)
+        return -EIO;
+
+    if (nbd_wr_sync(s->sock, buf, request.len, 1) != request.len)
+        return -EIO;
+
+    return 0;
+}
+
+static int nbd_write(BlockDriverState *bs, int64_t sector_num,
+                     const uint8_t *buf, int nb_sectors)
+{
+    BDRVNBDState *s = bs->opaque;
+    struct nbd_request request;
+    struct nbd_reply reply;
+
+    request.type = NBD_CMD_WRITE;
+    request.handle = (uint64_t)bs;
+    request.from = sector_num * 512;;
+    request.len = nb_sectors * 512;
+
+    if (nbd_send_request(s->sock, &request) == -1)
+        return -errno;
+
+    if (nbd_wr_sync(s->sock, (uint8_t*)buf, request.len, 0) != request.len)
+        return -EIO;
+
+    if (nbd_receive_reply(s->sock, &reply) == -1)
+        return -errno;
+
+    if (reply.error !=0)
+        return -reply.error;
+
+    if (reply.handle != request.handle)
+        return -EIO;
+
+    return 0;
+}
+
+static void nbd_close(BlockDriverState *bs)
+{
+    BDRVNBDState *s = bs->opaque;
+    struct nbd_request request;
+
+    request.type = NBD_CMD_DISC;
+    request.handle = (uint64_t)bs;
+    request.from = 0;
+    request.len = 0;
+    nbd_send_request(s->sock, &request);
+
+    close(s->sock);
+}
+
+static int64_t  nbd_getlength(BlockDriverState *bs)
+{
+    BDRVNBDState *s = bs->opaque;
+
+    return s->size;
+}
+
+BlockDriver bdrv_nbd = {
+    "nbd",
+    sizeof(BDRVNBDState),
+    NULL, /* no probe for protocols */
+    nbd_open,
+    nbd_read,
+    nbd_write,
+    nbd_close,
+    .bdrv_getlength = nbd_getlength,
+    .protocol_name = "nbd",
+};
Index: qemu/Makefile
===================================================================
--- qemu.orig/Makefile  2008-06-27 11:35:11.000000000 +0200
+++ qemu/Makefile       2008-06-27 11:35:14.000000000 +0200
@@ -42,7 +42,7 @@ recurse-all: $(SUBDIR_RULES)
 BLOCK_OBJS=cutils.o qemu-malloc.o
 BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
 BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
-BLOCK_OBJS+=block-qcow2.o block-parallels.o
+BLOCK_OBJS+=block-qcow2.o block-parallels.o block-nbd.o
 
 ######################################################################
 # libqemu_common.a: Target independent part of system emulation. The
@@ -50,7 +50,7 @@ BLOCK_OBJS+=block-qcow2.o block-parallel
 # system emulation, i.e. a single QEMU executable should support all
 # CPUs and machines.
 
-OBJS=$(BLOCK_OBJS)
+OBJS=nbd.o $(BLOCK_OBJS)
 OBJS+=readline.o console.o
 OBJS+=block.o
 
@@ -154,7 +154,7 @@ libqemu_user.a: $(USER_OBJS)
        rm -f $@ 
        $(AR) rcs $@ $(USER_OBJS)
 
-QEMU_IMG_BLOCK_OBJS = $(BLOCK_OBJS)
+QEMU_IMG_BLOCK_OBJS = nbd.o $(BLOCK_OBJS)
 ifdef CONFIG_WIN32
 QEMU_IMG_BLOCK_OBJS += qemu-img-block-raw-win32.o
 else
@@ -175,7 +175,7 @@ qemu-img-%.o: %.c
 qemu-nbd-%.o: %.c
        $(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_NBD -c -o $@ $<
 
-qemu-nbd$(EXESUF):  qemu-nbd.o nbd.o qemu-img-block.o \
+qemu-nbd$(EXESUF):  qemu-nbd.o qemu-nbd-nbd.o qemu-img-block.o \
                    osdep.o qemu-nbd-block-raw-posix.o $(BLOCK_OBJS)
        $(CC) $(LDFLAGS) -o $@ $^ -lz $(LIBS)
 
Index: qemu/nbd.c
===================================================================
--- qemu.orig/nbd.c     2008-06-27 11:35:11.000000000 +0200
+++ qemu/nbd.c  2008-06-27 11:35:14.000000000 +0200
@@ -31,17 +31,21 @@
 #include <arpa/inet.h>
 #include <netdb.h>
 
+#if defined(QEMU_NBD)
 extern int verbose;
+#else
+static int verbose = 0;
+#endif
+
+#define TRACE(msg, ...) do { \
+    if (verbose) LOG(msg, ## __VA_ARGS__); \
+} while(0)
 
 #define LOG(msg, ...) do { \
     fprintf(stderr, "%s:%s():L%d: " msg "\n", \
             __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
 } while(0)
 
-#define TRACE(msg, ...) do { \
-    if (verbose) LOG(msg, ## __VA_ARGS__); \
-} while(0)
-
 /* This is all part of the "official" NBD API */
 
 #define NBD_REQUEST_MAGIC       0x25609513
@@ -59,10 +63,10 @@ extern int verbose;
 
 /* That's all folks */
 
-#define read_sync(fd, buffer, size) wr_sync(fd, buffer, size, true)
-#define write_sync(fd, buffer, size) wr_sync(fd, buffer, size, false)
+#define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
+#define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
 
-static size_t wr_sync(int fd, void *buffer, size_t size, bool do_read)
+size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
 {
     size_t offset = 0;
 
@@ -76,7 +80,7 @@ static size_t wr_sync(int fd, void *buff
         }
 
         /* recoverable error */
-        if (len == -1 && errno == EAGAIN) {
+        if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
             continue;
         }
 
@@ -96,7 +100,7 @@ static size_t wr_sync(int fd, void *buff
     return offset;
 }
 
-static int tcp_socket_outgoing(const char *address, uint16_t port)
+int tcp_socket_outgoing(const char *address, uint16_t port)
 {
     int s;
     struct in_addr in;
@@ -404,15 +408,31 @@ int nbd_client(int fd, int csock)
        return ret;
 }
 
-int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, 
off_t *offset, bool readonly, uint8_t *data, int data_size)
+int nbd_send_request(int csock, struct nbd_request *request)
 {
        uint8_t buf[4 + 4 + 8 + 8 + 4];
-       uint32_t magic;
-       uint32_t type;
-       uint64_t from;
-       uint32_t len;
 
-       TRACE("Reading request.");
+       cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
+       cpu_to_be32w((uint32_t*)(buf + 4), request->type);
+       cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
+       cpu_to_be64w((uint64_t*)(buf + 16), request->from);
+       cpu_to_be32w((uint32_t*)(buf + 24), request->len);
+
+       TRACE("Sending request to client");
+
+       if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
+               LOG("writing to socket failed");
+               errno = EINVAL;
+               return -1;
+       }
+       return 0;
+}
+
+
+static int nbd_receive_request(int csock, struct nbd_request *request)
+{
+       uint8_t buf[4 + 4 + 8 + 8 + 4];
+       uint32_t magic;
 
        if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
                LOG("read failed");
@@ -429,89 +449,150 @@ int nbd_trip(BlockDriverState *bs, int c
         */
 
        magic = be32_to_cpup((uint32_t*)buf);
-       type  = be32_to_cpup((uint32_t*)(buf + 4));
-       from  = be64_to_cpup((uint64_t*)(buf + 16));
-       len   = be32_to_cpup((uint32_t*)(buf + 24));
+       request->type  = be32_to_cpup((uint32_t*)(buf + 4));
+       request->handle = be64_to_cpup((uint64_t*)(buf + 8));
+       request->from  = be64_to_cpup((uint64_t*)(buf + 16));
+       request->len   = be32_to_cpup((uint32_t*)(buf + 24));
 
        TRACE("Got request: "
              "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
-             magic, type, from, len);
-
+             magic, request->type, request->from, request->len);
 
        if (magic != NBD_REQUEST_MAGIC) {
                LOG("invalid magic (got 0x%x)", magic);
                errno = EINVAL;
                return -1;
        }
+}
+
+int nbd_receive_reply(int csock, struct nbd_reply *reply)
+{
+       uint8_t buf[4 + 4 + 8];
+       uint32_t magic;
+int i;
+
+for (i = 0; i < sizeof(buf); i++) buf[i] = 0xAA;
+
+       if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
+               LOG("read failed");
+               errno = EINVAL;
+               return -1;
+       }
 
-       if (len > data_size) {
+       /* Reply
+        [ 0 ..  3]    magic   (NBD_REPLY_MAGIC)
+        [ 4 ..  7]    error   (0 == no error)
+         [ 7 .. 15]    handle
+        */
+
+       magic = be32_to_cpup((uint32_t*)buf);
+       reply->error  = be32_to_cpup((uint32_t*)(buf + 4));
+       reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
+
+       TRACE("Got reply: "
+             "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
+             magic, reply->error, reply->handle);
+
+       if (magic != NBD_REPLY_MAGIC) {
+               LOG("invalid magic (got 0x%x)", magic);
+               errno = EINVAL;
+               return -1;
+       }
+       return 0;
+}
+
+static int nbd_send_reply(int csock, struct nbd_reply *reply)
+{
+       uint8_t buf[4 + 4 + 8];
+
+       /* Reply
+        [ 0 ..  3]    magic   (NBD_REPLY_MAGIC)
+        [ 4 ..  7]    error   (0 == no error)
+         [ 7 .. 15]    handle
+        */
+       cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
+       cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
+       cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
+
+       TRACE("Sending response to client");
+
+       if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
+               LOG("writing to socket failed");
+               errno = EINVAL;
+               return -1;
+       }
+       return 0;
+}
+
+int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, 
off_t *offset, bool readonly, uint8_t *data, int data_size)
+{
+       struct nbd_request request;
+       struct nbd_reply reply;
+
+       TRACE("Reading request.");
+
+       if (nbd_receive_request(csock, &request) == -1)
+               return -1;
+
+       if (request.len > data_size) {
                LOG("len (%u) is larger than max len (%u)",
-                   len, data_size);
+                   request.len, data_size);
                errno = EINVAL;
                return -1;
        }
 
-       if ((from + len) < from) {
+       if ((request.from + request.len) < request.from) {
                LOG("integer overflow detected! "
                    "you're probably being attacked");
                errno = EINVAL;
                return -1;
        }
 
-       if ((from + len) > size) {
+       if ((request.from + request.len) > size) {
                LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
                    ", Offset: %" PRIu64 "\n",
-                    from, len, size, dev_offset);
+                    request.from, request.len, size, dev_offset);
                LOG("requested operation past EOF--bad client?");
                errno = EINVAL;
                return -1;
        }
 
-       /* Reply
-        [ 0 ..  3]    magic   (NBD_REPLY_MAGIC)
-        [ 4 ..  7]    error   (0 == no error)
-         [ 7 .. 15]    handle
-        */
-       cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
-       cpu_to_be32w((uint32_t*)(buf + 4), 0);
-
        TRACE("Decoding type");
 
-       switch (type) {
-       case 0:
+       reply.handle = request.handle;
+       reply.error = 0;
+
+       switch (request.type) {
+       case NBD_CMD_READ:
                TRACE("Request type is READ");
 
-               if (bdrv_read(bs, (from + dev_offset) / 512, data, len / 512) 
== -1) {
+               if (bdrv_read(bs, (request.from + dev_offset) / 512, data,
+                             request.len / 512) == -1) {
                        LOG("reading from file failed");
                        errno = EINVAL;
                        return -1;
                }
-               *offset += len;
-
-               TRACE("Read %u byte(s)", len);
+               *offset += request.len;
 
-               TRACE("Sending OK response");
+               TRACE("Read %u byte(s)", request.len);
 
-               if (write_sync(csock, buf, 16) != 16) {
-                       LOG("writing to socket failed");
-                       errno = EINVAL;
+               if (nbd_send_reply(csock, &reply) == -1)
                        return -1;
-               }
 
                TRACE("Sending data to client");
 
-               if (write_sync(csock, data, len) != len) {
+               if (write_sync(csock, data, request.len) != request.len) {
                        LOG("writing to socket failed");
                        errno = EINVAL;
                        return -1;
                }
                break;
-       case 1:
+       case NBD_CMD_WRITE:
                TRACE("Request type is WRITE");
 
-               TRACE("Reading %u byte(s)", len);
+               TRACE("Reading %u byte(s)", request.len);
 
-               if (read_sync(csock, data, len) != len) {
+               if (read_sync(csock, data, request.len) != request.len) {
                        LOG("reading from socket failed");
                        errno = EINVAL;
                        return -1;
@@ -519,34 +600,29 @@ int nbd_trip(BlockDriverState *bs, int c
 
                if (readonly) {
                        TRACE("Server is read-only, return error");
-
-                       cpu_to_be32w((uint32_t*)(buf + 4), 1);
+                       reply.error = 1;
                } else {
                        TRACE("Writing to device");
 
-                       if (bdrv_write(bs, (from + dev_offset) / 512, data, len 
/ 512) == -1) {
+                       if (bdrv_write(bs, (request.from + dev_offset) / 512,
+                                      data, request.len / 512) == -1) {
                                LOG("writing to file failed");
                                errno = EINVAL;
                                return -1;
                        }
 
-                       *offset += len;
+                       *offset += request.len;
                }
 
-               TRACE("Sending response to client");
-
-               if (write_sync(csock, buf, 16) != 16) {
-                       LOG("writing to socket failed");
-                       errno = EINVAL;
+               if (nbd_send_reply(csock, &reply) == -1)
                        return -1;
-               }
                break;
-       case 2:
+       case NBD_CMD_DISC:
                TRACE("Request type is DISCONNECT");
                errno = 0;
                return 1;
        default:
-               LOG("invalid request type (%u) received", type);
+               LOG("invalid request type (%u) received", request.type);
                errno = EINVAL;
                return -1;
        }
Index: qemu/nbd.h
===================================================================
--- qemu.orig/nbd.h     2008-06-27 11:35:11.000000000 +0200
+++ qemu/nbd.h  2008-06-27 11:35:14.000000000 +0200
@@ -26,6 +26,26 @@
 #include <qemu-common.h>
 #include "block_int.h"
 
+struct nbd_request {
+    uint32_t type;
+    uint64_t handle;
+    uint64_t from;
+    uint32_t len;
+};
+
+struct nbd_reply {
+    uint32_t error;
+    uint64_t handle;
+};
+
+enum {
+    NBD_CMD_READ = 0,
+    NBD_CMD_WRITE = 1,
+    NBD_CMD_DISC = 2
+};
+
+size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read);
+int tcp_socket_outgoing(const char *address, uint16_t port);
 int tcp_socket_incoming(const char *address, uint16_t port);
 int unix_socket_outgoing(const char *path);
 int unix_socket_incoming(const char *path);
@@ -33,6 +53,8 @@ int unix_socket_incoming(const char *pat
 int nbd_negotiate(BlockDriverState *bs, int csock, off_t size);
 int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize);
 int nbd_init(int fd, int csock, off_t size, size_t blocksize);
+int nbd_send_request(int csock, struct nbd_request *request);
+int nbd_receive_reply(int csock, struct nbd_reply *reply);
 int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
              off_t *offset, bool readonly, uint8_t *data, int data_size);
 int nbd_client(int fd, int csock);
Index: qemu/block.c
===================================================================
--- qemu.orig/block.c   2008-06-27 11:35:11.000000000 +0200
+++ qemu/block.c        2008-06-27 11:35:14.000000000 +0200
@@ -1332,6 +1332,7 @@ void bdrv_init(void)
     bdrv_register(&bdrv_vvfat);
     bdrv_register(&bdrv_qcow2);
     bdrv_register(&bdrv_parallels);
+    bdrv_register(&bdrv_nbd);
 }
 
 void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
Index: qemu/block.h
===================================================================
--- qemu.orig/block.h   2008-06-27 11:35:11.000000000 +0200
+++ qemu/block.h        2008-06-27 11:35:14.000000000 +0200
@@ -16,6 +16,7 @@ extern BlockDriver bdrv_vpc;
 extern BlockDriver bdrv_vvfat;
 extern BlockDriver bdrv_qcow2;
 extern BlockDriver bdrv_parallels;
+extern BlockDriver bdrv_nbd;
 
 typedef struct BlockDriverInfo {
     /* in bytes, 0 if irrelevant */
Index: qemu/qemu-nbd.c
===================================================================
--- qemu.orig/qemu-nbd.c        2008-06-27 11:35:11.000000000 +0200
+++ qemu/qemu-nbd.c     2008-06-27 11:35:14.000000000 +0200
@@ -56,6 +56,7 @@ static void usage(const char *name)
 "  -c, --connect=DEV    connect FILE to the local NBD device DEV\n"
 "  -d, --disconnect     disconnect the specified device\n"
 "  -e, --shared=NUM     device can be shared by NUM clients (default '1')\n"
+"  -t, --persistent     don't exit on the last connection\n"
 "  -v, --verbose        display extra debugging information\n"
 "  -h, --help           display this help and exit\n"
 "  -V, --version        output version information and exit\n"
@@ -189,7 +190,7 @@ int main(int argc, char **argv)
     char *device = NULL;
     char *socket = NULL;
     char sockpath[128];
-    const char *sopt = "hVbo:p:rsnP:c:dvk:e:";
+    const char *sopt = "hVbo:p:rsnP:c:dvk:e:t";
     struct option lopt[] = {
         { "help", 0, 0, 'h' },
         { "version", 0, 0, 'V' },
@@ -204,6 +205,7 @@ int main(int argc, char **argv)
         { "snapshot", 0, 0, 's' },
         { "nocache", 0, 0, 'n' },
         { "shared", 1, 0, 'e' },
+        { "persistent", 0, 0, 't' },
         { "verbose", 0, 0, 'v' },
         { NULL, 0, 0, 0 }
     };
@@ -222,6 +224,7 @@ int main(int argc, char **argv)
     int i;
     int nb_fds = 0;
     int max_fd;
+    int persistent = 0;
 
     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
         switch (ch) {
@@ -283,6 +286,9 @@ int main(int argc, char **argv)
                 errx(EINVAL, "Shared device number must be greater than 0\n");
             }
             break;
+       case 't':
+           persistent = 1;
+           break;
         case 'v':
             verbose = 1;
             break;
@@ -459,7 +465,7 @@ exit:
                 }
             }
         }
-    } while (nb_fds > 1);
+    } while (persistent || nb_fds > 1);
     qemu_free(data);
 
     close(sharing_fds[0]);
Index: qemu/qemu-nbd.texi
===================================================================
--- qemu.orig/qemu-nbd.texi     2008-06-27 11:35:11.000000000 +0200
+++ qemu/qemu-nbd.texi  2008-06-27 11:35:14.000000000 +0200
@@ -36,6 +36,8 @@ Export Qemu disk image using NBD protoco
   disconnect the specified device
 @item -e, --shared=NUM
   device can be shared by NUM clients (default '1')
address@hidden -t, --persistent
+  don't exit on the last connection
 @item -v, --verbose
   display extra debugging information
 @item -h, --help
Index: qemu/qemu-doc.texi
===================================================================
--- qemu.orig/qemu-doc.texi     2008-06-27 11:35:11.000000000 +0200
+++ qemu/qemu-doc.texi  2008-06-27 11:35:14.000000000 +0200
@@ -1310,6 +1310,7 @@ snapshots.
 * qemu_nbd_invocation::       qemu-nbd Invocation
 * host_drives::               Using host drives
 * disk_images_fat_images::    Virtual FAT disk images
+* disk_images_nbd::           NBD access
 @end menu
 
 @node disk_images_quickstart
@@ -1492,6 +1493,40 @@ What you should @emph{never} do:
 @item write to the FAT directory on the host system while accessing it with 
the guest system.
 @end itemize
 
address@hidden disk_images_nbd
address@hidden NBD access
+
+QEMU can access directly to block device exported using the Network Block 
Device
+protocol.
+
address@hidden
+qemu linux.img -hdb nbd:my_nbd_server.mydomain.org:1024
address@hidden example
+
+If the NBD server is located on the same host, you can use an unix socket 
instead
+of an inet socket:
+
address@hidden
+qemu linux.img -hdb nbd:unix:/tmp/my_socket
address@hidden example
+
+In this case, the block device must be exported using qemu-nbd:
+
address@hidden
+qemu-nbd --socket=/tmp/my_socket my_disk.qcow2
address@hidden example
+
+The use of qemu-nbd allows to share a disk between several guests:
address@hidden
+qemu-nbd --socket=/tmp/my_socket --share=2 my_disk.qcow2
address@hidden example
+
+and then you can use it with two guests:
address@hidden
+qemu linux1.img -hdb nbd:unix:/tmp/my_socket
+qemu linux2.img -hdb nbd:unix:/tmp/my_socket
address@hidden example
+
 @node pcsys_network
 @section Network emulation
 

--




reply via email to

[Prev in Thread] Current Thread [Next in Thread]