Skip to contentSkip to conversation

docs: show how to spawn qemu-storage-daemon with fd passing

PATCH Superseded Stefan Hajnoczi 5 participants 12 messages Mar 1, 2021 – Mar 2, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 docs: show how to spawn qemu-storage-daemon with fd passing

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

---
 docs/tools/qemu-storage-daemon.rst | 38 ++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
docs/tools/qemu-storage-daemon.rst +36 -2
index f63627eaf6..45854c131e 100644
@@ -101,10 +101,12 @@ Standard options:
 
 .. option:: --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
   --nbd-server addr.type=unix,addr.path=<path>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
+  --nbd-server addr.type=fd,addr.str=<fd>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
 
   is a server for NBD exports. Both TCP and UNIX domain sockets are supported.
-  TLS encryption can be configured using ``--object`` tls-creds-* and authz-*
-  secrets (see below).
+  A listen socket can be provided via file descriptor passing (see Examples
+  below). TLS encryption can be configured using ``--object`` tls-creds-* and
+  authz-* secrets (see below).
 
   To configure an NBD server on UNIX domain socket path ``/tmp/nbd.sock``::
 
@@ -127,6 +129,38 @@ QMP commands::
       --chardev socket,path=qmp.sock,server,nowait,id=char1 \
       --monitor chardev=char1
 
+Launch the daemon from Python with a QMP monitor socket using file descriptor
+passing so there is no need to busy wait for the QMP monitor to become
+available::
+
+  #!/usr/bin/env python3
+  import os
+  import subprocess
+  import socket
+
+  sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())
+
+  with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listen_sock:
+      listen_sock.bind(sock_path)
+      listen_sock.listen()
+
+      fd = listen_sock.fileno()
+
+      subprocess.Popen(
+          ['qemu-storage-daemon',
+           '--chardev', f'socket,fd={fd},server=on,id=char1',
+           '--monitor', 'chardev=char1'],
+          pass_fds=[fd],
+      )
+
+  qmp_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+  qmp_sock.connect(sock_path)
+  ...QMP interaction...
+
+The same socket spawning approach also works with the ``--nbd-server
+addr.type=fd,addr.str=<fd>`` and ``--export
+type=vhost-user-blk,addr.type=fd,addr.str=<fd>`` options.
+
 Export raw image file ``disk.img`` over NBD UNIX domain socket ``nbd.sock``::
 
   $ qemu-storage-daemon \
Signature · 1 line
2.29.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 43 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

---
 docs/tools/qemu-storage-daemon.rst | 38 ++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
docs/tools/qemu-storage-daemon.rst +4 -2
index f63627eaf6..45854c131e 100644
@@ -101,10 +101,12 @@ Standard options:
 
 .. option:: --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
   --nbd-server addr.type=unix,addr.path=<path>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
+  --nbd-server addr.type=fd,addr.str=<fd>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
 
   is a server for NBD exports. Both TCP and UNIX domain sockets are supported.
-  TLS encryption can be configured using ``--object`` tls-creds-* and authz-*
-  secrets (see below).
+  A listen socket can be provided via file descriptor passing (see Examples
+  below). TLS encryption can be configured using ``--object`` tls-creds-* and
+  authz-* secrets (see below).
 
   To configure an NBD server on UNIX domain socket path ``/tmp/nbd.sock``::
 
@@ -127,6 +129,38 @@ QMP commands::
       --chardev socket,path=qmp.sock,server,nowait,id=char1 \
       --monitor chardev=char1
 

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 141

Quoted 10 lines
docs/tools/qemu-storage-daemon.rst +10 -0
+Launch the daemon from Python with a QMP monitor socket using file descriptor
+passing so there is no need to busy wait for the QMP monitor to become
+available::
+
+  #!/usr/bin/env python3
+  import os
+  import subprocess
+  import socket
+
+  sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Not sure how much you worry about the insecure / easily guessable tmp path here. I notice that there's already one in the surrounding documentation (/tmp/nbd.sock) so maybe it's not a problem :-)

Quoted 5 lines
docs/tools/qemu-storage-daemon.rst +5 -0
+  with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listen_sock:
+      listen_sock.bind(sock_path)
+      listen_sock.listen()
+
+      fd = listen_sock.fileno()

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 157

Quoted 10 lines
docs/tools/qemu-storage-daemon.rst +10 -0
+
+      subprocess.Popen(
+          ['qemu-storage-daemon',
+           '--chardev', f'socket,fd={fd},server=on,id=char1',
+           '--monitor', 'chardev=char1'],
+          pass_fds=[fd],
+      )
+
+  qmp_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+  qmp_sock.connect(sock_path)

A note that the order of opening the sockets is slightly different from how I did it in the interop test. But I believe it makes no difference, as long as you don't connect to the socket until it's in the listening state, which is what you're doing here. So it should be fine.

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 164

Quoted 7 lines
docs/tools/qemu-storage-daemon.rst +6 -0
+  ...QMP interaction...
+
+The same socket spawning approach also works with the ``--nbd-server
+addr.type=fd,addr.str=<fd>`` and ``--export
+type=vhost-user-blk,addr.type=fd,addr.str=<fd>`` options.
+
 Export raw image file ``disk.img`` over NBD UNIX domain socket ``nbd.sock``::

The patch looks fine in general:

Rich.

Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW
#2 lore raw
Quoted 72 lines · Richard W.M. Jones
Quoted 43 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

---
 docs/tools/qemu-storage-daemon.rst | 38 ++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
docs/tools/qemu-storage-daemon.rst +4 -2
index f63627eaf6..45854c131e 100644
@@ -101,10 +101,12 @@ Standard options:
 
 .. option:: --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
   --nbd-server addr.type=unix,addr.path=<path>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
+  --nbd-server addr.type=fd,addr.str=<fd>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
 
   is a server for NBD exports. Both TCP and UNIX domain sockets are supported.
-  TLS encryption can be configured using ``--object`` tls-creds-* and authz-*
-  secrets (see below).
+  A listen socket can be provided via file descriptor passing (see Examples
+  below). TLS encryption can be configured using ``--object`` tls-creds-* and
+  authz-* secrets (see below).
 
   To configure an NBD server on UNIX domain socket path ``/tmp/nbd.sock``::
 
@@ -127,6 +129,38 @@ QMP commands::
       --chardev socket,path=qmp.sock,server,nowait,id=char1 \
       --monitor chardev=char1
 

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 141

Quoted 10 lines
docs/tools/qemu-storage-daemon.rst +10 -0
+Launch the daemon from Python with a QMP monitor socket using file descriptor
+passing so there is no need to busy wait for the QMP monitor to become
+available::
+
+  #!/usr/bin/env python3
+  import os
+  import subprocess
+  import socket
+
+  sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Not sure how much you worry about the insecure / easily guessable tmp path here. I notice that there's already one in the surrounding documentation (/tmp/nbd.sock) so maybe it's not a problem :-)

Quoted 13 lines
docs/tools/qemu-storage-daemon.rst +13 -0
+  with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listen_sock:
+      listen_sock.bind(sock_path)
+      listen_sock.listen()
+
+      fd = listen_sock.fileno()
+
+      subprocess.Popen(
+          ['qemu-storage-daemon',
+           '--chardev', f'socket,fd={fd},server=on,id=char1',
+           '--monitor', 'chardev=char1'],
+          pass_fds=[fd],
+      )
+
Quoted 8 lines

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 157

Quoted 2 lines
docs/tools/qemu-storage-daemon.rst +2 -0
+  qmp_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+  qmp_sock.connect(sock_path)

A note that the order of opening the sockets is slightly different from how I did it in the interop test. But I believe it makes no difference, as long as you don't connect to the socket until it's in the listening state, which is what you're doing here. So it should be fine.

Nothing here is closing listen_sock in the parent though.

The trick of passing the listener FD into the child relies on the listener being closed in the parent, so that the parent can get a socket error if the child exits abnormally during startup. Keeping the listen socket open means the parent will wait forever for an accept() that never comes.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 82 lines · Daniel P. Berrangé
Quoted 72 lines · Richard W.M. Jones
Quoted 43 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

---
 docs/tools/qemu-storage-daemon.rst | 38 ++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
docs/tools/qemu-storage-daemon.rst +4 -2
index f63627eaf6..45854c131e 100644
@@ -101,10 +101,12 @@ Standard options:
 
 .. option:: --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
   --nbd-server addr.type=unix,addr.path=<path>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
+  --nbd-server addr.type=fd,addr.str=<fd>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
 
   is a server for NBD exports. Both TCP and UNIX domain sockets are supported.
-  TLS encryption can be configured using ``--object`` tls-creds-* and authz-*
-  secrets (see below).
+  A listen socket can be provided via file descriptor passing (see Examples
+  below). TLS encryption can be configured using ``--object`` tls-creds-* and
+  authz-* secrets (see below).
 
   To configure an NBD server on UNIX domain socket path ``/tmp/nbd.sock``::
 
@@ -127,6 +129,38 @@ QMP commands::
       --chardev socket,path=qmp.sock,server,nowait,id=char1 \
       --monitor chardev=char1
 

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 141

Quoted 10 lines
docs/tools/qemu-storage-daemon.rst +10 -0
+Launch the daemon from Python with a QMP monitor socket using file descriptor
+passing so there is no need to busy wait for the QMP monitor to become
+available::
+
+  #!/usr/bin/env python3
+  import os
+  import subprocess
+  import socket
+
+  sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Not sure how much you worry about the insecure / easily guessable tmp path here. I notice that there's already one in the surrounding documentation (/tmp/nbd.sock) so maybe it's not a problem :-)

Quoted 13 lines
docs/tools/qemu-storage-daemon.rst +13 -0
+  with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listen_sock:
+      listen_sock.bind(sock_path)
+      listen_sock.listen()
+
+      fd = listen_sock.fileno()
+
+      subprocess.Popen(
+          ['qemu-storage-daemon',
+           '--chardev', f'socket,fd={fd},server=on,id=char1',
+           '--monitor', 'chardev=char1'],
+          pass_fds=[fd],
+      )
+
Quoted 8 lines

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 157

Quoted 2 lines
docs/tools/qemu-storage-daemon.rst +2 -0
+  qmp_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+  qmp_sock.connect(sock_path)

A note that the order of opening the sockets is slightly different from how I did it in the interop test. But I believe it makes no difference, as long as you don't connect to the socket until it's in the listening state, which is what you're doing here. So it should be fine.

Quoted 7 lines

Nothing here is closing listen_sock in the parent though.

The trick of passing the listener FD into the child relies on the listener being closed in the parent, so that the parent can get a socket error if the child exits abnormally during startup. Keeping the listen socket open means the parent will wait forever for an accept() that never comes.

The listen socket is closed by the context manager at the end of the 'with' statement. This is the modern Python approach for resource acquisition that also handles exceptions automatically. It's like RAII in C++.

https://www.python.org/dev/peps/pep-0343/#specification-the-with-statement

Stefan

#4 lore raw
Quoted 91 lines · Stefan Hajnoczi
Quoted 82 lines · Daniel P. Berrangé
Quoted 72 lines · Richard W.M. Jones
Quoted 43 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

---
 docs/tools/qemu-storage-daemon.rst | 38 ++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
docs/tools/qemu-storage-daemon.rst +4 -2
index f63627eaf6..45854c131e 100644
@@ -101,10 +101,12 @@ Standard options:
 
 .. option:: --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
   --nbd-server addr.type=unix,addr.path=<path>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
+  --nbd-server addr.type=fd,addr.str=<fd>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
 
   is a server for NBD exports. Both TCP and UNIX domain sockets are supported.
-  TLS encryption can be configured using ``--object`` tls-creds-* and authz-*
-  secrets (see below).
+  A listen socket can be provided via file descriptor passing (see Examples
+  below). TLS encryption can be configured using ``--object`` tls-creds-* and
+  authz-* secrets (see below).
 
   To configure an NBD server on UNIX domain socket path ``/tmp/nbd.sock``::
 
@@ -127,6 +129,38 @@ QMP commands::
       --chardev socket,path=qmp.sock,server,nowait,id=char1 \
       --monitor chardev=char1
 

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 141

Quoted 10 lines
docs/tools/qemu-storage-daemon.rst +10 -0
+Launch the daemon from Python with a QMP monitor socket using file descriptor
+passing so there is no need to busy wait for the QMP monitor to become
+available::
+
+  #!/usr/bin/env python3
+  import os
+  import subprocess
+  import socket
+
+  sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Not sure how much you worry about the insecure / easily guessable tmp path here. I notice that there's already one in the surrounding documentation (/tmp/nbd.sock) so maybe it's not a problem :-)

Quoted 13 lines
docs/tools/qemu-storage-daemon.rst +13 -0
+  with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listen_sock:
+      listen_sock.bind(sock_path)
+      listen_sock.listen()
+
+      fd = listen_sock.fileno()
+
+      subprocess.Popen(
+          ['qemu-storage-daemon',
+           '--chardev', f'socket,fd={fd},server=on,id=char1',
+           '--monitor', 'chardev=char1'],
+          pass_fds=[fd],
+      )
+
Quoted 8 lines

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 157

Quoted 2 lines
docs/tools/qemu-storage-daemon.rst +2 -0
+  qmp_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+  qmp_sock.connect(sock_path)

A note that the order of opening the sockets is slightly different from how I did it in the interop test. But I believe it makes no difference, as long as you don't connect to the socket until it's in the listening state, which is what you're doing here. So it should be fine.

Quoted 7 lines

Nothing here is closing listen_sock in the parent though.

The trick of passing the listener FD into the child relies on the listener being closed in the parent, so that the parent can get a socket error if the child exits abnormally during startup. Keeping the listen socket open means the parent will wait forever for an accept() that never comes.

Quoted 4 lines

The listen socket is closed by the context manager at the end of the 'with' statement. This is the modern Python approach for resource acquisition that also handles exceptions automatically. It's like RAII in C++.

Hmm, yes, I didn't remember that at first. I'm not sure that is a good idea as an example code, because people mapping this example into other languages are likely to miss that critical detail.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw
Quoted 92 lines · Daniel P. Berrangé
Quoted 91 lines · Stefan Hajnoczi
Quoted 89 lines · Daniel P. Berrangé
Quoted 72 lines · Richard W.M. Jones
Quoted 43 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

---
 docs/tools/qemu-storage-daemon.rst | 38 ++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
docs/tools/qemu-storage-daemon.rst +4 -2
index f63627eaf6..45854c131e 100644
@@ -101,10 +101,12 @@ Standard options:
 
 .. option:: --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
   --nbd-server addr.type=unix,addr.path=<path>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
+  --nbd-server addr.type=fd,addr.str=<fd>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
 
   is a server for NBD exports. Both TCP and UNIX domain sockets are supported.
-  TLS encryption can be configured using ``--object`` tls-creds-* and authz-*
-  secrets (see below).
+  A listen socket can be provided via file descriptor passing (see Examples
+  below). TLS encryption can be configured using ``--object`` tls-creds-* and
+  authz-* secrets (see below).
 
   To configure an NBD server on UNIX domain socket path ``/tmp/nbd.sock``::
 
@@ -127,6 +129,38 @@ QMP commands::
       --chardev socket,path=qmp.sock,server,nowait,id=char1 \
       --monitor chardev=char1
 

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 141

Quoted 10 lines
docs/tools/qemu-storage-daemon.rst +10 -0
+Launch the daemon from Python with a QMP monitor socket using file descriptor
+passing so there is no need to busy wait for the QMP monitor to become
+available::
+
+  #!/usr/bin/env python3
+  import os
+  import subprocess
+  import socket
+
+  sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Not sure how much you worry about the insecure / easily guessable tmp path here. I notice that there's already one in the surrounding documentation (/tmp/nbd.sock) so maybe it's not a problem :-)

Quoted 13 lines
docs/tools/qemu-storage-daemon.rst +13 -0
+  with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listen_sock:
+      listen_sock.bind(sock_path)
+      listen_sock.listen()
+
+      fd = listen_sock.fileno()
+
+      subprocess.Popen(
+          ['qemu-storage-daemon',
+           '--chardev', f'socket,fd={fd},server=on,id=char1',
+           '--monitor', 'chardev=char1'],
+          pass_fds=[fd],
+      )
+
Quoted 8 lines

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 157

Quoted 2 lines
docs/tools/qemu-storage-daemon.rst +2 -0
+  qmp_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+  qmp_sock.connect(sock_path)

A note that the order of opening the sockets is slightly different from how I did it in the interop test. But I believe it makes no difference, as long as you don't connect to the socket until it's in the listening state, which is what you're doing here. So it should be fine.

Nothing here is closing listen_sock in the parent though.

The trick of passing the listener FD into the child relies on the listener being closed in the parent, so that the parent can get a socket error if the child exits abnormally during startup. Keeping the listen socket open means the parent will wait forever for an accept() that never comes.

Quoted 8 lines
Quoted 4 lines

The listen socket is closed by the context manager at the end of the 'with' statement. This is the modern Python approach for resource acquisition that also handles exceptions automatically. It's like RAII in C++.

Hmm, yes, I didn't remember that at first. I'm not sure that is a good idea as an example code, because people mapping this example into other languages are likely to miss that critical detail.

Let's keep the Pythonic code but add a comment that makes this clear.

Stefan

#6 lore raw
In reply to Richard W.M. Jones
Quoted 44 lines · Richard W.M. Jones
Quoted 43 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

---
 docs/tools/qemu-storage-daemon.rst | 38 ++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
docs/tools/qemu-storage-daemon.rst +4 -2
index f63627eaf6..45854c131e 100644
@@ -101,10 +101,12 @@ Standard options:
 
 .. option:: --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
   --nbd-server addr.type=unix,addr.path=<path>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
+  --nbd-server addr.type=fd,addr.str=<fd>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
 
   is a server for NBD exports. Both TCP and UNIX domain sockets are supported.
-  TLS encryption can be configured using ``--object`` tls-creds-* and authz-*
-  secrets (see below).
+  A listen socket can be provided via file descriptor passing (see Examples
+  below). TLS encryption can be configured using ``--object`` tls-creds-* and
+  authz-* secrets (see below).
 
   To configure an NBD server on UNIX domain socket path ``/tmp/nbd.sock``::
 
@@ -127,6 +129,38 @@ QMP commands::
       --chardev socket,path=qmp.sock,server,nowait,id=char1 \
       --monitor chardev=char1
 
Quoted 14 lines

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 141

Quoted 10 lines
docs/tools/qemu-storage-daemon.rst +10 -0
+Launch the daemon from Python with a QMP monitor socket using file descriptor
+passing so there is no need to busy wait for the QMP monitor to become
+available::
+
+  #!/usr/bin/env python3
+  import os
+  import subprocess
+  import socket
+
+  sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Not sure how much you worry about the insecure / easily guessable tmp path here. I notice that there's already one in the surrounding documentation (/tmp/nbd.sock) so maybe it's not a problem :-)

Yes, the documentation doesn't address those issues. If I respin I'll change the path to something that's less likely to be a globally writeable directory (/var/run/... where the pid files usually go).

Stefan

#7 lore raw
In reply to Stefan Hajnoczi
Quoted 43 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

---
 docs/tools/qemu-storage-daemon.rst | 38 ++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
docs/tools/qemu-storage-daemon.rst +4 -2
index f63627eaf6..45854c131e 100644
@@ -101,10 +101,12 @@ Standard options:
 
 .. option:: --nbd-server addr.type=inet,addr.host=<host>,addr.port=<port>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
   --nbd-server addr.type=unix,addr.path=<path>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
+  --nbd-server addr.type=fd,addr.str=<fd>[,tls-creds=<id>][,tls-authz=<id>][,max-connections=<n>]
 
   is a server for NBD exports. Both TCP and UNIX domain sockets are supported.
-  TLS encryption can be configured using ``--object`` tls-creds-* and authz-*
-  secrets (see below).
+  A listen socket can be provided via file descriptor passing (see Examples
+  below). TLS encryption can be configured using ``--object`` tls-creds-* and
+  authz-* secrets (see below).
 
   To configure an NBD server on UNIX domain socket path ``/tmp/nbd.sock``::
 
@@ -127,6 +129,38 @@ QMP commands::
       --chardev socket,path=qmp.sock,server,nowait,id=char1 \
       --monitor chardev=char1
 

docs/tools/qemu-storage-daemon.rst QMP commands:: · line 141

Quoted 10 lines
docs/tools/qemu-storage-daemon.rst +10 -0
+Launch the daemon from Python with a QMP monitor socket using file descriptor
+passing so there is no need to busy wait for the QMP monitor to become
+available::
+
+  #!/usr/bin/env python3
+  import os
+  import subprocess
+  import socket
+
+  sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Example code inevitably gets cut+paste into real world apps, and this example is a tmpfile CVE flaw. At least put it in $CWD instead.

Quoted 28 lines
docs/tools/qemu-storage-daemon.rst +22 -0
+
+  with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as listen_sock:
+      listen_sock.bind(sock_path)
+      listen_sock.listen()
+
+      fd = listen_sock.fileno()
+
+      subprocess.Popen(
+          ['qemu-storage-daemon',
+           '--chardev', f'socket,fd={fd},server=on,id=char1',
+           '--monitor', 'chardev=char1'],
+          pass_fds=[fd],
+      )
+
+  qmp_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+  qmp_sock.connect(sock_path)
+  ...QMP interaction...
+
+The same socket spawning approach also works with the ``--nbd-server
+addr.type=fd,addr.str=<fd>`` and ``--export
+type=vhost-user-blk,addr.type=fd,addr.str=<fd>`` options.
+
 Export raw image file ``disk.img`` over NBD UNIX domain socket ``nbd.sock``::
 
   $ qemu-storage-daemon \
Signature · 1 line
2.29.2

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#8 lore raw
Quoted 10 lines · Daniel P. Berrangé
Quoted 9 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Quoted 8 lines
Quoted 5 lines

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

+ sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Example code inevitably gets cut+paste into real world apps, and this example is a tmpfile CVE flaw. At least put it in $CWD instead.

Except $CWD may be too long for a sock file name to be created. Creating the sock in a securely-created subdirectory of /tmp is more reliable.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#9 lore raw
Quoted 15 lines · Eric Blake
Quoted 13 lines · Daniel P. Berrangé
Quoted 12 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

+ sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Example code inevitably gets cut+paste into real world apps, and this example is a tmpfile CVE flaw. At least put it in $CWD instead.

Except $CWD may be too long for a sock file name to be created. Creating the sock in a securely-created subdirectory of /tmp is more reliable.

$XDG_RUNTIME_DIR then, which is /run/user/$UID, so safely per user on all modern OS.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#10 lore raw
Quoted 21 lines · Daniel P. Berrangé
Quoted 20 lines · Eric Blake
Quoted 18 lines · Daniel P. Berrangé
Quoted 9 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Quoted 5 lines

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

+ sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Example code inevitably gets cut+paste into real world apps, and this example is a tmpfile CVE flaw. At least put it in $CWD instead.

Quoted 6 lines
Quoted 3 lines

Except $CWD may be too long for a sock file name to be created. Creating the sock in a securely-created subdirectory of /tmp is more reliable.

$XDG_RUNTIME_DIR then, which is /run/user/$UID, so safely per user on all modern OS.

Both you and Rich mentioned this. I'll change it to /var/run/qsd.pid.

I'll also update the /tmp/nbd.sock example in the documentation.

Stefan

#11 lore raw
In reply to Daniel P. Berrangé
Quoted 21 lines · Daniel P. Berrangé
Quoted 20 lines · Eric Blake
Quoted 18 lines · Daniel P. Berrangé
Quoted 9 lines · Stefan Hajnoczi

The QMP monitor, NBD server, and vhost-user-blk export all support file descriptor passing. This is a useful technique because it allows the parent process to spawn and wait for qemu-storage-daemon without busy waiting, which may delay startup due to arbitrary sleep() calls.

This Python example is inspired by the test case written for libnbd by Richard W.M. Jones <[email protected]>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543

Quoted 5 lines

Thanks to Daniel P. Berrangé <[email protected]> for suggestions on how to get this working. Now let's document it!

+ sock_path = '/tmp/qmp-{}.sock'.format(os.getpid())

Example code inevitably gets cut+paste into real world apps, and this example is a tmpfile CVE flaw. At least put it in $CWD instead.

Quoted 6 lines
Quoted 3 lines

Except $CWD may be too long for a sock file name to be created. Creating the sock in a securely-created subdirectory of /tmp is more reliable.

$XDG_RUNTIME_DIR then, which is /run/user/$UID, so safely per user on all modern OS.

Reach under your pillow and check the standard library:

import tempfile

with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)

https://docs.python.org/3.6/library/tempfile.html#tempfile.TemporaryDirectory