Skip to contentSkip to conversation

qemu-nbd: inetd support

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qemu-nbd: inetd support

This patch allows to put qemu-nbd in inetd.conf by using stdin/stdout instead of a socket.

This behavior is enabled by specifying "--port=0".

example:

# add in /etc/services.conf

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 /ISO/ubuntu-7.10-desktop-amd64.iso

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
---
 qemu-nbd.c |   25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
qemu-nbd.c +20 -5
Index: qemu/qemu-nbd.c
===================================================================
@@ -242,7 +242,7 @@ int main(int argc, char **argv)
             if (*end) {
                 errx(EINVAL, "Invalid port `%s'", optarg);
             }
-            if (li < 1 || li > 65535) {
+            if (li < 0 || li > 65535) {
                 errx(EINVAL, "Port out of range `%s'", optarg);
             }
             port = (uint16_t)li;
@@ -326,6 +326,10 @@ int main(int argc, char **argv)
 	return 0;
     }
 
+    if (port == 0 && shared != 1) {
+        errx(EINVAL, "You cannot set port to 0 and use shared");
+    }
+
     bdrv_init();
 
     bs = bdrv_new("hda");
@@ -412,9 +416,24 @@ int main(int argc, char **argv)
     if (sharing_fds == NULL)
         errx(ENOMEM, "Cannot allocate sharing fds");
 
+    data = qemu_memalign(512, NBD_BUFFER_SIZE);
+    if (data == NULL)
+        errx(ENOMEM, "Cannot allocate data buffer");
+
     if (socket) {
         sharing_fds[0] = unix_socket_incoming(socket);
     } else {
+        if (port == 0) {
+            /* read and write on stdin/stdout */
+            ret = nbd_negotiate(STDIN_FILENO, fd_size);
+            while (ret != -1) {
+                ret = nbd_trip(bs, STDIN_FILENO, fd_size, dev_offset,
+                               &offset, readonly, data, NBD_BUFFER_SIZE);
+            }
+            qemu_free(data);
+            bdrv_close(bs);
+            return 0;
+        }
         sharing_fds[0] = tcp_socket_incoming(bindto, port);
     }
 
@@ -423,10 +442,6 @@ int main(int argc, char **argv)
     max_fd = sharing_fds[0];
     nb_fds++;
 
-    data = qemu_memalign(512, NBD_BUFFER_SIZE);
-    if (data == NULL)
-        errx(ENOMEM, "Cannot allocate data buffer");
-
     do {
 
         FD_ZERO(&fds);
#1 lore raw
Quoted 5 lines · Laurent Vivier

This patch allows to put qemu-nbd in inetd.conf by using stdin/stdout instead of a socket.

This behavior is enabled by specifying "--port=0".

That's kind of non-intuitive. Why not just add a --inetd option or a flag that explicitly means read from stdio?

Regards,

Anthony Liguori

Quoted 12 lines

example:

# add in /etc/services.conf

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 /ISO/ubuntu-7.10-desktop-amd64.iso

#2 lore raw
Quoted 9 lines · Anthony Liguori
Quoted 5 lines · Laurent Vivier

This patch allows to put qemu-nbd in inetd.conf by using stdin/stdout instead of a socket.

This behavior is enabled by specifying "--port=0".

That's kind of non-intuitive. Why not just add a --inetd option or a flag that explicitly means read from stdio?

Or --port=-, probably?

Thiemo

#3 lore raw
In reply to Anthony Liguori
Quoted 8 lines · Anthony Liguori
Quoted 5 lines · Laurent Vivier

This patch allows to put qemu-nbd in inetd.conf by using stdin/stdout instead of a socket.

This behavior is enabled by specifying "--port=0".

That's kind of non-intuitive. Why not just add a --inetd option or a flag that explicitly means read from stdio?

just because nbd-server acts like this...

But I can add an "--inetd" option.

Regards,
Laurent
----------------------- Laurent Vivier ---------------------- "The best way to predict the future is to invent it."
- Alan Kay