diff mbox

[Bug,1217339,v2] Unix signal to send ACPI-shutdown to Guest

Message ID 14585f602461dbefb3adaf16591ce5e5@whitewinterwolf.com (mailing list archive)
State New, archived
Headers show

Commit Message

Simon March 18, 2017, 1:41 p.m. UTC
Hi,

The patch below adds the new command-line option `-powerdown' which
changes the behavior for both SIGHUP and SIGINT signals to cause a clean
power down of the guest using an ACPI shutdown request.

If this option is not used, the original behavior is kept.

Regards,
Simon.

Signed-off-by: Simon Geusebroek <qemu.bugs@whitewinterwolf.com>
---
--

Comments

Daniel P. Berrangé March 20, 2017, 9:55 a.m. UTC | #1
On Sat, Mar 18, 2017 at 02:41:16PM +0100, Simon wrote:
> Hi,
> 
> The patch below adds the new command-line option `-powerdown' which
> changes the behavior for both SIGHUP and SIGINT signals to cause a clean
> power down of the guest using an ACPI shutdown request.

If you need to add command line arguments IMHO this feature becomes
much less interesting - just use the QEMU monitor instead of inventing
an adhoc way to achieve something the monitor already does. The value of
using a signal handler comes from the fact that it would be unconditionally
available no matter what command line args were added.


Regards,
Daniel
Simon March 21, 2017, 12:30 p.m. UTC | #2
Daniel P. Berrange:
> On Sat, Mar 18, 2017 at 02:41:16PM +0100, Simon wrote:
>> Hi,
>> 
>> The patch below adds the new command-line option `-powerdown' which
>> changes the behavior for both SIGHUP and SIGINT signals to cause a 
>> clean
>> power down of the guest using an ACPI shutdown request.
> 
> If you need to add command line arguments IMHO this feature becomes
> much less interesting - just use the QEMU monitor instead of inventing
> an adhoc way to achieve something the monitor already does. The value 
> of
> using a signal handler comes from the fact that it would be 
> unconditionally
> available no matter what command line args were added.

Hi Daniel,

Thank you for taking the time to answer me.

I am currently using the monitor to achieve this functionality, and this
boils down into doing something similar to this:

--------- 8< ----------------
if ! type 'socat' >/dev/null 2>&1
then
     echo "You must install 'socat'." >&2
     exit 1
fi
files=$( ps -e -o args | awk '$1 ~ /qemu-system-/' \
     | sed 's/.* -monitor unix:\([^,]*\).*/\1/;t;s/.*//' )
IFS='
'
for f in $files
do
     echo 'system_powerdown' | socat "UNIX-CONNECT:${f}" -
done
--------- 8< ----------------

In other words I check that required software is properly installed,
parse `ps' output to extract the path to the monitor socket file when it
is available, and pipe the `system_powerdown' command into it to
cleanly shut the VM down.

As a matter of comparison, here the same functionality implemented using
Unix signals instead of the monitor socket files:

--------- 8< ----------------
pkill -HUP qemu-system-
--------- 8< ----------------

Maybe it is just my own opinion, but I find the second approach cleaner
and more reliable.

Regarding the fact that it would require a specific parameter to be
enabled, I already use several parameters on a systematic basis with
Qemu (enable KVM, the USB tablet, set the keyboard layout, etc.).
I don't see the fact of adding a supplementary one to this set as an
annoyance.

On the contrary, I love Qemu's versatility and all these parameters are
here just to let users personalize Qemu behavior to best suit the way
they use it.

Regards,
Simon.
diff mbox

Patch

diff -ru a/qemu-options.hx b/qemu-options.hx
--- a/qemu-options.hx	2016-12-20 21:16:49.000000000 +0100
+++ b/qemu-options.hx	2017-03-18 14:17:35.759915414 +0100
@@ -3322,6 +3322,19 @@ 
  disk image.
  ETEXI

+DEF("powerdown", 0, QEMU_OPTION_powerdown, \
+    "-powerdown    Cleanly power down the guest on SIGINT or SIGHUP\n", 
\
+    QEMU_ARCH_ALL)
+STEXI
+@item -powerdown
+@findex -powerdown
+Don't forcefully shutdown the guest upon SIGINT (Ctrl-C) or SIGHUP 
(terminal
+closure) signals, but instead attempt to cleanly stop them using an 
ACPI
+shutdown request.
+Non ACPI-aware and stuck guests will remain up, the SIGTERM signal will 
still
+forcefully shutdown them.
+ETEXI
+
  DEF("loadvm", HAS_ARG, QEMU_OPTION_loadvm, \
      "-loadvm [tag|id]\n" \
      "                start right away with a saved state (loadvm in 
monitor)\n",
diff -ru a/vl.c b/vl.c
--- a/vl.c	2016-12-20 21:16:54.000000000 +0100
+++ b/vl.c	2017-03-18 13:48:21.227967255 +0100
@@ -164,6 +164,7 @@ 
  int fd_bootchk = 1;
  static int no_reboot;
  int no_shutdown = 0;
+int powerdown = 0;
  int cursor_hide = 1;
  int graphic_rotate = 0;
  const char *watchdog;
@@ -1871,7 +1872,11 @@ 
      /* Cannot call qemu_system_shutdown_request directly because
       * we are in a signal handler.
       */
-    shutdown_requested = 1;
+    if (powerdown == 1 && (signal == SIGINT || signal == SIGHUP)) {
+        powerdown_requested = 1;
+    } else {
+        shutdown_requested = 1;
+    }
      qemu_notify_event();
  }

@@ -3794,6 +3799,9 @@ 
              case QEMU_OPTION_no_shutdown:
                  no_shutdown = 1;
                  break;
+            case QEMU_OPTION_powerdown:
+                powerdown = 1;
+                break;
              case QEMU_OPTION_show_cursor:
                  cursor_hide = 0;
                  break;