Skip to contentSkip to conversation

qemu-nbd:multiple files server

RFC PATCH In review Laurent Vivier 2 participants 2 messages Sep 8, 2008 – Sep 10, 2008

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 qemu-nbd:multiple files server

This patch allows the NBD client to select the file to use.

It modifies the NBD protocol to allow the client to provide a filename if the NBD server reports a disk size of 0.

Example:

# add in /etc/services

nbd 1024/tcp

# add in /etc/inetd.conf

nbd stream tcp nowait root /usr/local/bin/qemu-nbd /usr/local/bin/qemu-nbd --nocache --read-only --port 0 --directory /ISO

In /ISO, there are:

debian-31r0a-amd64-netinst.iso   openSUSE-10.3-GM-x86_64-mini.iso
debian-40r0-i386-netinst.iso     ubuntu-6.06-desktop-amd64.iso
debian-40r0-i386-xfce-CD-1.iso   ubuntu-7.10-desktop-amd64.iso
debian-40r1-amd64-netinst.iso    ubuntu-8.04-desktop-i386.iso
debian-40r1-powerpc-netinst.iso  winxp_64.iso

Then I can start qemu with:

x86_64-softmmu/qemu-system-x86_64 -net nic -net tap -hda new_disk.qcow2 -cdrom nbd:my_nbd_server:1024:ubuntu-6.06-desktop-amd64.iso -boot d

or

x86_64-softmmu/qemu-system-x86_64 -net nic -net tap -hda new_disk.qcow2 -cdrom nbd:my_nbd_server:1024:winxp_64.iso -boot d

My goal is to be able to provide an install CD server, without configuring BOOTD/DHCP/TFTP server.

Signature · 3 lines
----------------- [email protected]  ------------------
  "La perfection est atteinte non quand il ne reste rien à
ajouter mais quand il ne reste rien à enlever." Saint Exupéry
---
 block-nbd.c |   16 ++++++++++++++
 nbd.c       |   47 +++++++++++++++++++++++++++++++++++++++++
 nbd.h       |    2 +
 qemu-nbd.c  |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 4 files changed, 124 insertions(+), 9 deletions(-)
nbd.c +47 -0
Index: qemu/nbd.c
===================================================================
@@ -337,6 +337,53 @@ int nbd_receive_negotiate(int csock, off
         return 0;
 }
 
+int nbd_filename(int csock, const char *filename)
+{
+    uint8_t len;
+
+    if (!filename || strlen(filename) > 254) {
+        errno = EINVAL;
+        return -1;
+    }
+
+    len = strlen(filename) + 1;
+    if (write_sync(csock, &len, sizeof(uint8_t)) !=
+                    sizeof(uint8_t)) {
+        LOG("read failed");
+        errno = EINVAL;
+        return -1;
+    }
+    if (len && write_sync(csock, (char*)filename, len) != len) {
+        LOG("read failed");
+        errno = EINVAL;
+        return -1;
+    }
+    return 0;
+}
+
+int nbd_receive_filename(int csock, char *filename)
+{
+    uint8_t len;
+    filename[0] = '\0';
+    if (read_sync(csock, &len, sizeof(uint8_t)) != sizeof(uint8_t)) {
+        LOG("read failed");
+        errno = EINVAL;
+        return -1;
+    }
+    if (len == 0) {
+        LOG("read failed");
+        errno = EINVAL;
+        return -1;
+    }
+    if (read_sync(csock, filename, len) != len) {
+        LOG("read failed");
+        errno = EINVAL;
+        return -1;
+    }
+    filename[len] = 0;
+    return 0;
+}
+
 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
 {
 	TRACE("Setting block size to %lu", (unsigned long)blocksize);
nbd.h +2 -0
Index: qemu/nbd.h
===================================================================
@@ -52,6 +52,8 @@ int unix_socket_incoming(const char *pat
 
 int nbd_negotiate(int csock, off_t size);
 int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize);
+int nbd_filename(int csock, const char *filename);
+int nbd_receive_filename(int csock, char *filename);
 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);
block-nbd.c +16 -0
Index: qemu/block-nbd.c
===================================================================
@@ -47,6 +47,7 @@ static int nbd_open(BlockDriverState *bs
     off_t size;
     size_t blocksize;
     int ret;
+    const char *name = NULL;
 
     if ((flags & BDRV_O_CREAT))
         return -EINVAL;
@@ -79,6 +80,10 @@ static int nbd_open(BlockDriverState *bs
         if (r == p)
             return -EINVAL;
         sock = tcp_socket_outgoing(hostname, port);
+        if (*r == ':') {
+            r++;
+            name = r;
+        }
     }
 
     if (sock == -1)
@@ -88,6 +93,17 @@ static int nbd_open(BlockDriverState *bs
     if (ret == -1)
         return -errno;
 
+    if (size == 0) {
+        if (name == NULL)
+            return -EINVAL;
+        ret = nbd_filename(sock, name);
+        if (ret == -1)
+            return -errno;
+        ret = nbd_receive_negotiate(sock, &size, &blocksize);
+        if (ret == -1)
+            return -errno;
+    }
+
     s->sock = sock;
     s->size = size;
     s->blocksize = blocksize;
qemu-nbd.c +59 -9

119 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Laurent Vivier

This patch allows the NBD client to select the file to use.

It modifies the NBD protocol to allow the client to provide a filename if the NBD server reports a disk size of 0.

I don't have a problem with it from a client/server perspective, but we need to make sure that we're extending the NBD protocol in a way that's compatible with other implementations.

So please make sure Paul is on board with how you're extending the protocol.

Regards,

Anthony Liguori

Quoted 31 lines

Example:

# add in /etc/services

nbd 1024/tcp

# add in /etc/inetd.conf

nbd stream tcp nowait root /usr/local/bin/qemu-nbd /usr/local/bin/qemu-nbd --nocache --read-only --port 0 --directory /ISO

In /ISO, there are:

debian-31r0a-amd64-netinst.iso   openSUSE-10.3-GM-x86_64-mini.iso
debian-40r0-i386-netinst.iso     ubuntu-6.06-desktop-amd64.iso
debian-40r0-i386-xfce-CD-1.iso   ubuntu-7.10-desktop-amd64.iso
debian-40r1-amd64-netinst.iso    ubuntu-8.04-desktop-i386.iso
debian-40r1-powerpc-netinst.iso  winxp_64.iso

Then I can start qemu with:

x86_64-softmmu/qemu-system-x86_64 -net nic -net tap -hda new_disk.qcow2 -cdrom nbd:my_nbd_server:1024:ubuntu-6.06-desktop-amd64.iso -boot d

or

x86_64-softmmu/qemu-system-x86_64 -net nic -net tap -hda new_disk.qcow2 -cdrom nbd:my_nbd_server:1024:winxp_64.iso -boot d

My goal is to be able to provide an install CD server, without configuring BOOTD/DHCP/TFTP server.