[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 10/31] docs: show how to spawn qemu-storage-daemon with fd passing
From: |
Kevin Wolf |
Subject: |
[PULL 10/31] docs: show how to spawn qemu-storage-daemon with fd passing |
Date: |
Fri, 5 Mar 2021 17:54:33 +0100 |
From: Stefan Hajnoczi <stefanha@redhat.com>
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 <rjones@redhat.com>:
https://gitlab.com/nbdkit/libnbd/-/commit/89113f484effb0e6c322314ba75c1cbe07a04543
Thanks to Daniel P. Berrangé <berrange@redhat.com> for suggestions on
how to get this working. Now let's document it!
Reported-by: Richard W.M. Jones <rjones@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>
Cc: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20210301172728.135331-2-stefanha@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
docs/tools/qemu-storage-daemon.rst | 42 ++++++++++++++++++++++++++++--
1 file changed, 40 insertions(+), 2 deletions(-)
diff --git a/docs/tools/qemu-storage-daemon.rst
b/docs/tools/qemu-storage-daemon.rst
index 6ce85f2f7d..5714794775 100644
--- a/docs/tools/qemu-storage-daemon.rst
+++ b/docs/tools/qemu-storage-daemon.rst
@@ -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``::
@@ -141,6 +143,42 @@ QMP commands::
--chardev socket,path=qmp.sock,server=on,wait=off,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 subprocess
+ import socket
+
+ sock_path = '/var/run/qmp.sock'
+
+ 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],
+ )
+
+ # listen_sock was automatically closed when leaving the 'with' statement
+ # body. If the daemon process terminated early then the following connect()
+ # will fail with "Connection refused" because no process has the listen
+ # socket open anymore. Launch errors can be detected this way.
+
+ 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 \
--
2.29.2
- [PULL 15/31] libqtest: add qtest_remove_abrt_handler(), (continued)
- [PULL 15/31] libqtest: add qtest_remove_abrt_handler(), Kevin Wolf, 2021/03/05
- [PULL 13/31] libqtest: add qtest_socket_server(), Kevin Wolf, 2021/03/05
- [PULL 08/31] storage-daemon: include current command line option in the errors, Kevin Wolf, 2021/03/05
- [PULL 05/31] iotests: Fix up python style in 300, Kevin Wolf, 2021/03/05
- [PULL 09/31] qemu-storage-daemon: add --pidfile option, Kevin Wolf, 2021/03/05
- [PULL 02/31] backup: Remove nodes from job in .clean(), Kevin Wolf, 2021/03/05
- [PULL 26/31] block/parallels: BDRVParallelsState: add cluster_size field, Kevin Wolf, 2021/03/05
- [PULL 11/31] docs: replace insecure /tmp examples in qsd docs, Kevin Wolf, 2021/03/05
- [PULL 19/31] block/export: use VIRTIO_BLK_SECTOR_BITS, Kevin Wolf, 2021/03/05
- [PULL 25/31] parallels.txt: fix bitmap L1 table description, Kevin Wolf, 2021/03/05
- [PULL 10/31] docs: show how to spawn qemu-storage-daemon with fd passing,
Kevin Wolf <=
- [PULL 20/31] block/export: fix vhost-user-blk export sector number calculation, Kevin Wolf, 2021/03/05
- [PULL 17/31] tests/qtest: add multi-queue test case to vhost-user-blk-test, Kevin Wolf, 2021/03/05
- [PULL 21/31] block/export: port virtio-blk discard/write zeroes input validation, Kevin Wolf, 2021/03/05
- [PULL 23/31] block/export: port virtio-blk read/write range check, Kevin Wolf, 2021/03/05
- [PULL 03/31] backup-top: Refuse I/O in inactive state, Kevin Wolf, 2021/03/05
- [PULL 30/31] MAINTAINERS: update parallels block driver, Kevin Wolf, 2021/03/05
- [PULL 31/31] docs: qsd: Explain --export nbd,name=... default, Kevin Wolf, 2021/03/05
- [PULL 28/31] iotests.py: add unarchive_sample_image() helper, Kevin Wolf, 2021/03/05
- [PULL 24/31] qcow2-bitmap: make bytes_covered_by_bitmap_cluster() public, Kevin Wolf, 2021/03/05
- [PULL 07/31] storage-daemon: report unexpected arguments on the fly, Kevin Wolf, 2021/03/05