From c43a62e0bbc64103fb33d2fb0f447f3f9a891819 Mon Sep 17 00:00:00 2001 From: Marek Marczykowski Date: Mon, 17 Oct 2011 23:08:53 +0200 Subject: [PATCH] dom0/qvm-shutdown: implement --wait in qvm-shutdown --- dom0/qvm-core/qubes.py | 4 ++++ dom0/qvm-tools/qvm-run | 2 +- dom0/qvm-tools/qvm-shutdown | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/dom0/qvm-core/qubes.py b/dom0/qvm-core/qubes.py index 8f1ef813..f18d13aa 100755 --- a/dom0/qvm-core/qubes.py +++ b/dom0/qvm-core/qubes.py @@ -81,6 +81,10 @@ qubes_whitelisted_appmenus = 'whitelisted-appmenus.list' dom0_update_check_interval = 6*3600 +# how long (in sec) to wait for VMs to shutdown +# before killing them (when used qvm-run with --wait option) +shutdown_counter_max = 30 + # do not allow to start a new AppVM if Dom0 mem was to be less than this dom0_min_memory = 700*1024*1024 diff --git a/dom0/qvm-tools/qvm-run b/dom0/qvm-tools/qvm-run index 7de43703..9ae6f538 100755 --- a/dom0/qvm-tools/qvm-run +++ b/dom0/qvm-tools/qvm-run @@ -40,7 +40,7 @@ notify_object = None # how long (in sec) to wait for VMs to shutdown # before killing them (when used with --wait option) -shutdown_counter_max = 30 +from qubes.qubes import shutdown_counter_max def tray_notify(str, label, timeout = 3000): notify_object.Notify("Qubes", 0, label.icon, "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications") diff --git a/dom0/qvm-tools/qvm-shutdown b/dom0/qvm-tools/qvm-shutdown index 226f97f7..c19c165d 100755 --- a/dom0/qvm-tools/qvm-shutdown +++ b/dom0/qvm-tools/qvm-shutdown @@ -21,15 +21,20 @@ # from qubes.qubes import QubesVmCollection,QubesException +from qubes.qubes import shutdown_counter_max from optparse import OptionParser; import sys +import time def main(): usage = "usage: %prog [options] " parser = OptionParser (usage) + parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True) parser.add_option ("--force", action="store_true", dest="force", default=False, help="Force operation, even if may damage other VMs (eg shutdown of NetVM)") + parser.add_option ("--wait", action="store_true", dest="wait_for_shutdown", default=False, + help="Wait for the VM(s) to shutdown") (options, args) = parser.parse_args () if (len (args) != 1): @@ -52,5 +57,19 @@ def main(): print >> sys.stderr, "ERROR: {0}".format(err) exit (1) + if options.wait_for_shutdown: + if options.verbose: + print >> sys.stderr, "Waiting for the VM(s) to shutdown..." + shutdown_counter = 0 + + while vm.is_running(): + if shutdown_counter > shutdown_counter_max: + # kill the VM + if options.verbose: + print >> sys.stderr, "Killing the (apparently hanging) VM '{0}'...".format(vm.name) + vm.force_shutdown() + + shutdown_counter += 1 + time.sleep (1) main()