主机关闭时正常关闭 QEMU/KVM

主机关闭时正常关闭 QEMU/KVM

我在启动时启动 QEMU/KVM Ubuntu 15.10 虚拟机并让它在后台运行(作为 Web 服务器)。

如果我关闭主机(也是 15.10)会发生什么?
它会杀死虚拟机并导致虚拟断电甚至更糟吗?
还是它会在虚拟机中触发“按下电源按钮”事件并等待它完全关闭?

客户系统设置为在发生此类“按下电源按钮”事件时正常关闭。通常在 5-10 秒内关闭。

如果主机关闭时的默认行为是终止虚拟机,我该如何将其更改为干净关闭客户机并等待其关闭?

答案1

在...的帮助下 @塞尔格回答我编写了这组三个脚本(Python 3 和 Bash),用于监听 Unity 关机/注销对话框、检查正在运行的虚拟机、阻止 Unity 对话框、显示一个漂亮的进度条、等待所有虚拟机关闭或达到超时、询问是否应强制终止剩余的虚拟机,最后显示自定义关机/注销对话框。

以下是脚本。将它们放在变量中包含的位置$PATH,例如/usr/local/bin/。确保它们由 root 拥有并设置了所有执行位(chmod +x)。

vm-terminator(在 Bash 中,GUI):

#! /bin/bash

# Use first command-line argument as timeout, if given and numeric, else 30 sec
if [ "$1" -eq "$1" ] 2> /dev/null
    then timeout=$1
    else timeout=30
fi


# Define function to ask whether to shut down / log out / reboot later.
function end_session () {
    action=$(zenity --list --title="VM Terminator" --text="All VMs are shut down. What to do now?" --radiolist --hide-header --column="" --column="" TRUE "Log out" FALSE "Reboot" FALSE "Shut down")

    case $action in
        "Log out")
          gnome-session-quit --logout --no-prompt
          ;;
        "Reboot")
          systemctl reboot
          ;;
        "Shut down")
          systemctl poweroff
          ;;
        *)
          echo "Not ending current session."
          ;;
    esac
}


# Try to shut down VMs with
(
    set -o pipefail
    shutdown-all-vms -i 0.5 -t $timeout -z |
      zenity --progress --title="VM Terminator" --auto-close --auto-kill --width=400
) &> /dev/null
succeeded=$?

# Evaluate whether the task was successful and show host shutdown/logout dialog or kill/manual dialog or error message.
case $succeeded in
    0)
      end_session
      ;;
    1)
      zenity --question --title="VM Terminator" --text="The timeout was reached.\n\nWould you like to forcibly power off all remaining VMs\nor abort and take care of them yourself?" --ok-label="Kill them!" --cancel-label="I'll do it myself" --default-cancel
      if [ $? == 0 ]
        then shutdown-all-vms -t 0 -k
            end_session
        else exit 1
      fi
      ;;
    129)
      zenity --question --title="VM Terminator" --text="You cancelled the timeout.\n\nWould you like to forcibly power off all remaining VMs\nor abort and take care of them yourself?" --ok-label="Kill them!" --cancel-label="I'll do it myself" --default-cancel
      if [ $? == 0 ]
        then shutdown-all-vms -t 0 -k
            end_session
        else exit 1
      fi
      ;;
    *)
      zenity --error --title="VM Terminator" --text="An error occured while trying to shut down some VMs. Please review them manualy!"
      exit 2
      ;;
esac

shutdown-all-vms(在 Python 3 中,核心):

#! /usr/bin/env python3

# Script to gracefully shut down all running virtual machines accessible to the 'virtsh' command.
# It was initially designed for QEMU/KVM machines, but might work with more hypervisors.

# The VMs are tried to be shut down by triggering a "power-button-pressed" event in each machine.
# Each guest OS is responsible to shut down when detecting one. By default, some systems may just show
# an user dialog prompt instead and do nothing. If configured, this script can turn them off forcibly.
# That would be similar to holding the power button or pulling the AC plug on a real machine.

# This script exits with code 0 when all VMs could be shut down or were forced off at timeout.
# If the 'virsh shutdown VM_NAME' command returned an error, this script will exit with error code 1.
# On timeout with KILL_ON_TIMEOUT set to False, the script will exit with error code 2.
# If KILL_ON_TIMEOUT is active and the timeout was reached, but one of the 'virsh destroy VM_NAME' commands
# returned an error, this script exits with error code 3.


import subprocess
import time
from optparse import OptionParser

# Function to get a list of running VM names:
def list_running_vms():
    as_string = subprocess.check_output(["virsh", "list", "--state-running", "--name"], universal_newlines=True).strip()
    return [] if not as_string else as_string.split("\n")

# Evaluate command-line arguments:
parser = OptionParser(version="%prog 1.0")
parser.add_option("-i", "--interval", type="float", dest="interval", default=1,
                  help="Interval to use for polling the VM state after sending the shutdown command. (default: %default)")
parser.add_option("-t", "--timeout", type="float", dest="timeout", default=30,
                  help="Time to wait for all VMs to shut down. (default: %default)")
parser.add_option("-k", "--kill-on-timeout", action="store_true", dest="kill", default=False,
                  help="Kill (power cut) all remaining VMs when the timeout is reached. "
                       "Otherwise exit with error code 1. (default: %default)")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                  help="Print verbose status output. (default: %default)")
parser.add_option("-z", "--zenity", action="store_true", dest="zenity", default=False,
                  help="Print progress lines for 'zenity --progress' GUI progress dialog. (default: %default)")
(options, args) = parser.parse_args()

# List all running VMs:
running_vms = list_running_vms()

# Print summary of what will happen:
print("Shutting down all running VMs (currently {}) within {} seconds. {} remaining VMs.".format(
       len(running_vms), options.timeout, "Kill all" if options.kill else "Do not kill any"))

# Send shutdown command ("power-button-pressed" event) to all running VMs:
any_errors = False
if options.zenity:
    print("# Sending shutdown signals...", flush=True)
for vm in running_vms:
    if options.verbose:
        ok = subprocess.call(["virsh", "shutdown", vm])
    else:
        ok = subprocess.call(["virsh", "shutdown", vm], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    if ok != 0:
        print("Error trying to shut down VM '{}' (code {})!".format(vm, ok))
        any_errors = True

# Don't start waiting if there was any error sending the shutdown command, exit with error:
if any_errors:
    print("ERROR: could not successfully send all shutdown commands!")
    exit(3)

# Wait for all VMs to shut down, but at most MAX_WAIT seconds. Poll every INTERVAL seconds::
t0 = time.time()
while running_vms:
    num_of_vms = len(running_vms)
    t = time.time() - t0
    if options.zenity:
        print("# Waiting for {} VM{} to shut down... ({} seconds left)".format(
               num_of_vms, "" if num_of_vms == 1 else "s", int(options.timeout - t)), flush=True)
        print(int(100 * t/options.timeout) if t < options.timeout else 99, flush=True)
    if options.verbose or t > options.timeout:
        print("\n[{:5.1f}s] Still waiting for {} VMs to shut down:".format(t, num_of_vms))
        print(" > " + "\n > ".join(running_vms))
    if t > options.timeout:
        if options.kill:
            print("\nTimeout of {} seconds reached! Killing all remaining VMs now!".format(options.timeout))
            if options.zenity:
                print("# Timeout reached! Have to kill the remaining {}.".format(
                       "VM" if num_of_vms == 1 else "{} VMs".format(num_of_vms)), flush=True)
            for vm in running_vms:
                if options.verbose:
                    ok = subprocess.call(["virsh", "destroy", vm])
                else:
                    ok = subprocess.call(["virsh", "destroy", vm], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
                if ok != 0:
                    if options.verbose:
                        print("Error trying to forcibly kill VM '{}' (code {})!".format(vm, ok))
                    any_errors = True
            if any_errors:
                print("ERROR: could not successfully send all destroy commands!")
                exit(3)
        else:
            print("ERROR: Timeout of {} seconds reached!".format(options.timeout))
            exit(1)
        break
    time.sleep(options.interval)
    running_vms = list_running_vms()

print("#" if options.zenity else "" + " All VMs were shut down successfully.", flush=True)
if options.zenity:
    print(100, flush=True)
exit(0)

shutdown-dialog-listener(在 Bash 中,Unity 关机/注销看门狗):

#!/bin/bash

DISPLAY=:0
dbus-monitor --session "interface='com.canonical.Unity.Session'" | \
  while read LINE;do \
  if grep -qi 'reboot\|shutdown\|logout' <<< "$LINE" ;then \
    VAR="$(virsh list --state-running --name)"
    if [ $(wc -w <<<$VAR) -gt 0 ]; then
      qdbus com.canonical.Unity /org/gnome/SessionManager/EndSessionDialog \
      org.gnome.SessionManager.EndSessionDialog.Close

      vm-terminator
    fi
  fi ;done

所有三个脚本都可以直接调用,核心脚本shutdown-all-vms甚至有一个很好的命令行帮助:

$ shutdown-all-vms --help
Usage: shutdown-all-vms [options]

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -i INTERVAL, --interval=INTERVAL
                        Interval to use for polling the VM state after sending
                        the shutdown command. (default: 1)
  -t TIMEOUT, --timeout=TIMEOUT
                        Time to wait for all VMs to shut down. (default: 30)
  -k, --kill-on-timeout
                        Kill (power cut) all remaining VMs when the timeout is
                        reached. Otherwise exit with error code 1. (default:
                        False)
  -v, --verbose         Print verbose status output. (default: False)
  -z, --zenity          Print progress lines for 'zenity --progress' GUI
                        progress dialog. (default: False)

此外,您还可以将其放入shutdown-dialog-listener用户帐户的启动应用程序中。

答案2

下面是一个小脚本,应该作为自动启动项运行或手动运行(如果用户愿意的话)。基本思路是这样的:继续轮询dbus会话总线,如果我们重新启动、关闭或注销,那么我们可以检查 QEMU 是否正在运行;如果正在运行,则终止关闭对话框,运行命令关闭虚拟机,然后调用 dbus 进行关闭,或者甚至使用以下命令调用单独的脚本script-name.sh &

下面的示例仅用于示例目的,已使用 Firefox 进行了测试(因为我没有 QEMU),但可以轻松进行调整。包含注释以供参考

#!/bin/bash

# You will need the DISPLAY variable, if you
# are running the script as an autostart entry
# DISPLAY=:0
dbus-monitor --session "interface='com.canonical.Unity.Session'" | \
  while read LINE;do \
  if grep -qi 'reboot\|shutdown\|logout' <<< "$LINE" ;then \
    # This part could be either pgrep , or 
    # VAR="$(virsh list --state-running --name)"
    # And then you can test whether or not variable is empty to see
    # if there are running processes
    PID="$(pgrep firefox)"
    if [ ! -z $PID  ]; then
      # This is where you can take action
      # For instance the qdbus lengthy command closes the End Session dialog
      # which effectively prevents user from clicking shutdown
      # You can append another command, such as
      # virsh shutdown VMNAME or run an external script that does it.
      # Since this script is constantly polling dbus-monitor, we need to avoid
      # it's better to call external , in my opinion.

      notify-send "GOTCHA $PID";
      qdbus com.canonical.Unity /org/gnome/SessionManager/EndSessionDialog \
      org.gnome.SessionManager.EndSessionDialog.Close
    # After the action part is done, one could call
    # dbus to shutdown
    # qdbus  com.canonical.Unity /com/canonical/Unity/Session com.canonical.Unity.Session.Shutdown 
    fi
  fi ;done

相关内容