Implement -run-with exit-with-parent=on
PATCH 1/1 Implement -run-with exit-with-parent=on
Libguestfs wants to use qemu to run a captive appliance. When the program linked to libguestfs exits, we want qemu to be cleaned up. Libguestfs goes to great lengths to do this at the moment: it either forks a separate process to ensure clean-up is done, or it asks libvirt to clean up the qemu process. However this is complicated and not totally reliable.
On Linux, FreeBSD and macOS, there are mechanisms to ensure a signal or message is delivered to a process when its parent process goes away. The qemu test suite even uses this mechanism on Linux (see PR_SET_PDEATHSIG in tests/qtest/libqtest.c).
In nbdkit we have long had the concept of running nbdkit captively, and we have the nbdkit --exit-with-parent flag to help
(https://libguestfs.org/nbdkit-captive.1.html#EXIT-WITH-PARENT)
This commit adds the same mechanism that the qemu test suite uses, and that nbdkit has long implemented, to allow this feature to be used by all qemu users. The syntax is:
qemu -run-with exit-with-parent=on [...]
This is not a feature that most typical users of qemu (for running general purpose, long-lived VMs) should use, so it defaults to off.
The exit-with-parent.[ch] files are copied from nbdkit, where they have a 3-clause BSD license which is compatible with qemu:
https://gitlab.com/nbdkit/nbdkit/-/tree/master/common/utils?ref_type=heads
--- include/qemu/exit-with-parent.h | 49 +++++++++ qemu-options.hx | 13 ++- system/exit-with-parent.c | 171 ++++++++++++++++++++++++++++++++ system/meson.build | 1 + system/vl.c | 13 +++ 5 files changed, 245 insertions(+), 2 deletions(-)
include/qemu/exit-with-parent.h new file +49 -0
@@ -0,0 +1,49 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause +* nbdkit +* Copyright Red Hat +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* * Neither the name of Red Hat nor the names of its contributors may be +* used to endorse or promote products derived from this software without +* specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND +* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR +* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +* SUCH DAMAGE. +*/ + +#ifndef NBDKIT_EXIT_WITH_PARENT_H +#define NBDKIT_EXIT_WITH_PARENT_H + +#include <stdbool.h> + +/* Test if the feature is available on the platform. */ +extern bool can_exit_with_parent (void ); + +/* +* --exit-with-parent: kill the current process if the parent exits. +* This may return -1 on error. +*/ +extern int set_exit_with_parent (void ); + +#endif /* NBDKIT_EXIT_WITH_PARENT_H */
qemu-options.hx +11 -2
@@ -5437,15 +5437,18 @@ DEF("qtest-log", HAS_ARG, QEMU_OPTION_qtest_log, "", QEMU_ARCH_ALL) #if defined(CONFIG_POSIX) && !defined(EMSCRIPTEN) DEF("run-with", HAS_ARG, QEMU_OPTION_run_with, - "-run-with [async-teardown=on|off][,chroot=dir][user=username|uid:gid]\n" + "-run-with [async-teardown=on|off][,chroot=dir]\n" \ + " [,exit-with-parent=on|off][,user=username|uid:gid]\n" " Set miscellaneous QEMU process lifecycle options:\n" " async-teardown=on enables asynchronous teardown (Linux only)\n" + " exit-with-parent=on causes QEMU to exit if the parent\n" + " process of QEMU exits (Linux, FreeBSD, macOS only)\n" " chroot=dir chroot to dir just before starting the VM\n" " user=username switch to the specified user before starting the VM\n" " user=uid:gid ditto, but use specified user-ID and group-ID instead\n", QEMU_ARCH_ALL) SRST -``-run-with [async-teardown=on|off][,chroot=dir][user=username|uid:gid]`` +``-run-with [async-teardown=on|off][,chroot=dir][,exit-with-parent=on|off][,user=username|uid:gid]`` Set QEMU process lifecycle options. ``async-teardown=on`` enables asynchronous teardown. A new process called
@@ -5463,6 +5466,12 @@ SRST immediately before starting the guest execution. This is especially useful in combination with ``user=...``. + ``exit-with-parent=on`` causes QEMU to exit if the parent process of + QEMU exits. This can be used when QEMU runs a captive appliance, + where the lifetime of the appliance is scoped to the parent process. + In case the parent process crashes, QEMU is still cleaned up. + This only works on Linux, FreeBSD and macOS platforms. + ``user=username`` or ``user=uid:gid`` can be used to drop root privileges before starting guest execution. QEMU will use the ``setuid`` and ``setgid`` system calls to switch to the specified identity. Note that the
system/exit-with-parent.c new file +171 -0
171 lines not on this page · raw message
system/meson.build +1 -0
@@ -15,6 +15,7 @@ system_ss.add(files( 'datadir.c', 'dirtylimit.c', 'dma-helpers.c', + 'exit-with-parent.c', 'globals.c', 'ioport.c', 'ram-block-attributes.c',
system/vl.c +13 -0
@@ -53,6 +53,7 @@#include "qemu/sockets.h" #include "qemu/accel.h" #include "qemu/async-teardown.h" +#include "qemu/exit-with-parent.h" #include "hw/usb.h" #include "hw/isa/isa.h" #include "hw/scsi/scsi.h"
@@ -783,6 +784,10 @@ static QemuOptsList qemu_run_with_opts = { .name = "chroot", .type = QEMU_OPT_STRING, }, + { + .name = "exit-with-parent", + .type = QEMU_OPT_BOOL, + }, { .name = "user", .type = QEMU_OPT_STRING,
@@ -3690,6 +3695,14 @@ void qemu_init(int argc, char **argv)if (str) {os_set_chroot (str); } +if (qemu_opt_get_bool (opts,"exit-with-parent" , false)) { +if (!can_exit_with_parent ()) { +error_report ("exit-with-parent is not available" +" on this platform" ); +exit (1 ); + } +set_exit_with_parent (); + } str =qemu_opt_get (opts,"user" );if (str) {if (!os_set_runas (str)) {
Signature · 1 line
2.50.1
Quoted 45 lines · Richard W.M. Jones
Libguestfs wants to use qemu to run a captive appliance. When the program linked to libguestfs exits, we want qemu to be cleaned up. Libguestfs goes to great lengths to do this at the moment: it either forks a separate process to ensure clean-up is done, or it asks libvirt to clean up the qemu process. However this is complicated and not totally reliable.
On Linux, FreeBSD and macOS, there are mechanisms to ensure a signal or message is delivered to a process when its parent process goes away. The qemu test suite even uses this mechanism on Linux (see
PR_SET_PDEATHSIGintests/qtest/libqtest.c).In nbdkit we have long had the concept of running nbdkit captively, and we have the nbdkit --exit-with-parent flag to help
(https://libguestfs.org/nbdkit-captive.1.html#EXIT-WITH-PARENT)This commit adds the same mechanism that the qemu test suite uses, and that nbdkit has long implemented, to allow this feature to be used by all qemu users. The syntax is:
qemu -run-with exit-with-parent=on [...]
This is not a feature that most typical users of qemu (for running general purpose, long-lived VMs) should use, so it defaults to off.
The exit-with-parent.[ch] files are copied from nbdkit, where they have a 3-clause BSD license which is compatible with qemu:
https://gitlab.com/nbdkit/nbdkit/-/tree/master/common/utils?ref_type=heads
Signed-off-by Richard W.M. Jones <[email protected]>--- include/qemu/exit-with-parent.h | 49 +++++++++ qemu-options.hx | 13 ++- system/exit-with-parent.c | 171 ++++++++++++++++++++++++++++++++ system/meson.build | 1 + system/vl.c | 13 +++ 5 files changed, 245 insertions(+), 2 deletions(-)include/qemu/exit-with-parent.h new file +0
-0
include/qemu/exit-with-parent.h line 3
Quoted 3 lines
include/qemu/exit-with-parent.h new file +3
-0@@ -0,0 +1,49 @@ +/* +* SPDX-License-Identifier: BSD-3-Clause +* nbdkit
This 'nbdkit' line can be dropped presumably, unless you want to refer back to the original source ? Same question for the .c
Quoted 20 lines
include/qemu/exit-with-parent.h new file +20
-0+* Copyright Red Hat +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* * Neither the name of Red Hat nor the names of its contributors may be +* used to endorse or promote products derived from this software without +* specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND +* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
include/qemu/exit-with-parent.h line 33
Quoted 10 lines
include/qemu/exit-with-parent.h new file +10
-0+* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR +* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +* SUCH DAMAGE. +*/
IIUC, from the nbdkit history exit-with-parent.{ch} only ever had 3 contributors, all from Red Hat. So if Red Hat is exclusive copyright holder, I think you're be justified in removing the license boilerplate here & below, as a representative of RH.
include/qemu/exit-with-parent.h line 38
Quoted 5 lines
include/qemu/exit-with-parent.h new file +5
-0+ +#ifndef NBDKIT_EXIT_WITH_PARENT_H +#define NBDKIT_EXIT_WITH_PARENT_H + +#include <stdbool.h>
This is redundant in QEMU since it is guaranteed by osdep.h
Quoted 28 lines
include/qemu/exit-with-parent.h new file +11
-0+ +/* Test if the feature is available on the platform. */ +extern bool can_exit_with_parent (void ); + +/* +* --exit-with-parent: kill the current process if the parent exits. +* This may return -1 on error. +*/ +extern int set_exit_with_parent (void ); + +#endif /* NBDKIT_EXIT_WITH_PARENT_H */ system/exit-with-parent.c new file +10
-0@@ -0,0 +1,171 @@ + +/* +* Implement the --exit-with-parent feature on operating systems which +* support it. +*/ + +#include "qemu/osdep.h" +#include "qemu/exit-with-parent.h" + +#ifdef CONFIG_LINUX
system/exit-with-parent.c line 20
Quoted 10 lines
system/exit-with-parent.c new file +10
-0+#include <sys/prctl.h> +#endif + +#ifdef __FreeBSD__ +#include <sys/procctl.h> +#endif + +#if defined(CONFIG_LINUX ) && defined(PR_SET_PDEATHSIG ) + +/* For Linux >= 2.1.57. */
I don't think we need the PR_SET_PDEATHSIG check
since the oldest Linux platform QEMU targets will
always be newer than 2.1.57 and so can assume to
have PR_SET_PDEATHSIG defined.
Quoted 6 lines
system/exit-with-parent.c new file +6
-0+ +int +set_exit_with_parent (void ) +{ +return prctl (PR_SET_PDEATHSIG ,SIGTERM ); +}
system/exit-with-parent.c line 69
Quoted 10 lines
system/exit-with-parent.c new file +10
-0+ +bool +can_exit_with_parent (void ) +{ +return true; +} + +#elif defined(__FreeBSD__) && defined(PROC_PDEATHSIG_CTL ) + +/* For FreeBSD >= 11.2 */
Also don't need the PROC_PDEATHSIG_CTL check either, since with current FreeBSD being 14.0, the oldest QEMU targets is now 13.0
Quoted 12 lines
system/exit-with-parent.c new file +12
-0+ +#include <sys/procctl.h> + +int +set_exit_with_parent (void ) +{ +const int sig =SIGTERM ; +return procctl (P_PID ,0 ,PROC_PDEATHSIG_CTL , (void *) &sig); +} + +bool +can_exit_with_parent (void )
system/exit-with-parent.c line 91
Quoted 10 lines
system/exit-with-parent.c new file +10
-0+{ +return true; +} + +#elif defined(__APPLE__) + +/* For macOS. */ + +#include <unistd.h> +#include <errno.h>
These two are guaranteed by osdep.h
system/exit-with-parent.c line 93
Quoted 2 lines
system/exit-with-parent.c new file +2
-0+#include <sys/event.h> +#include <pthread.h>
Use "qemu/thread.h" instead
Quoted 37 lines
system/exit-with-parent.c new file +37
-0+ +static void * +exit_with_parent_loop (void *vp) +{ +const pid_t ppid =getppid (); +int fd; +struct kevent kev, res[1 ]; +int r; + +/* Register the kevent to wait for ppid to exit. */ + fd =kqueue (); +if (fd ==-1 ) { +error_report ("exit_with_parent_loop: kqueue: %m" ); +return NULL ; + } +EV_SET (&kev, ppid,EVFILT_PROC ,EV_ADD |EV_ENABLE ,NOTE_EXIT ,0 ,NULL ); +if (kevent (fd, &kev,1 ,NULL ,0 ,NULL ) ==-1 ) { +error_report ("exit_with_parent_loop: kevent: %m" ); +close (fd); +return NULL ; + } + +/* Wait for the kevent to happen. */ + r =kevent (fd,0 ,0 , res,1 ,NULL ); +if (r ==1 && res[0 ].ident == ppid) { +/* Shut down the whole process when the parent dies. */ +exit (0 ); + } + +return NULL ; +} + +int +set_exit_with_parent (void ) +{ +int r; +pthread_attr_t attrs;
system/exit-with-parent.c line 140
Quoted 10 lines
system/exit-with-parent.c new file +10
-0+pthread_t exit_with_parent_thread; + +/* +* We have to block waiting for kevent, so that requires that we +* start a background thread. +*/ +pthread_attr_init (&attrs); +pthread_attr_setdetachstate (&attrs,PTHREAD_CREATE_DETACHED ); + r =pthread_create (&exit_with_parent_thread,NULL , + exit_with_parent_loop,NULL );
QemuThread exit_with_parent_thread;
qemu_thread_create(&exit_with_parent_thread,
"exit-parent",
exit_with_parent_loop,
QEMU_THREAD_DETACHED);mostly just so we get the thread name set.
Quoted 7 lines
system/exit-with-parent.c new file +7
-0+if (r !=0 ) { + errno = r; +return -1 ; + } + +return 0 ; +}
system/exit-with-parent.c line 157
Quoted 10 lines
system/exit-with-parent.c new file +10
-0+ +bool +can_exit_with_parent (void ) +{ +return true; +} + +#else /* any platform that doesn't support this function */ + +#include <stdlib.h>
Can be assumed with osdep.h
system/exit-with-parent.c line 162
Quoted 5 lines
system/exit-with-parent.c new file +5
-0+ +int +set_exit_with_parent (void ) +{ +abort ();
Slightly better to use g_assert_not_reached()
Quoted 38 lines
system/exit-with-parent.c new file +9
-0+} + +bool +can_exit_with_parent (void ) +{ +return false; +} + +#endif system/vl.c +6
-0@@ -53,6 +53,7 @@#include "qemu/sockets.h" #include "qemu/accel.h" #include "qemu/async-teardown.h" +#include "qemu/exit-with-parent.h" #include "hw/usb.h" #include "hw/isa/isa.h" #include "hw/scsi/scsi.h" @@ -783,6 +784,10 @@ static QemuOptsList qemu_run_with_opts = { .name = "chroot", .type = QEMU_OPT_STRING, }, + { + .name = "exit-with-parent", + .type = QEMU_OPT_BOOL, + }, { .name = "user", .type = QEMU_OPT_STRING,@@ -3690,6 +3695,14 @@ void qemu_init(int argc, char **argv)if (str) {os_set_chroot (str); } +if (qemu_opt_get_bool (opts,"exit-with-parent" , false)) {
system/vl.c void qemu_init(int argc, char **argv) · line 3708
Quoted 10 lines
system/vl.c +7
-0+if (!can_exit_with_parent ()) { +error_report ("exit-with-parent is not available" +" on this platform" ); +exit (1 ); + } +set_exit_with_parent (); + } str =qemu_opt_get (opts,"user" );if (str) {if (!os_set_runas (str)) {
Broadly looks good to me.
With 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 :|
Quoted 19 lines
Quoted 5 lines
system/exit-with-parent.c new file +5
-0+int +set_exit_with_parent (void ) +{ +int r; +pthread_attr_t attrs;system/exit-with-parent.c line 140
Quoted 10 lines
system/exit-with-parent.c new file +10
-0+pthread_t exit_with_parent_thread; + +/* +* We have to block waiting for kevent, so that requires that we +* start a background thread. +*/ +pthread_attr_init (&attrs); +pthread_attr_setdetachstate (&attrs,PTHREAD_CREATE_DETACHED ); + r =pthread_create (&exit_with_parent_thread,NULL , + exit_with_parent_loop,NULL );QemuThread
exit_with_parent_thread;
qemu_thread_create(&exit_with_parent_thread,
Quoted 5 lines
"exit-parent", exit_with_parent_loop, QEMU_THREAD_DETACHED);mostly just so we get the thread name set.
Right, this (macOS) is the part I didn't even compile test yet, hence why the patch is WIP. We do compile the same code in nbdkit, but I could be sure it'll work in macOS. I'll make sure to fix this to make it qemu-native, and test it before posting the real patch :-)
Quoted 1 line
Broadly looks good to me.
Thanks, I'll incorporate all those suggestions into the real version.
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 virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v