qvm-shutdown 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2011 Marek Marczykowski <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. from qubes.qubes import QubesVmCollection,QubesException
  24. from qubes.qubes import defaults
  25. from optparse import OptionParser;
  26. import sys
  27. import time
  28. def main():
  29. usage = "usage: %prog [options] <vm-name>"
  30. parser = OptionParser (usage)
  31. parser.add_option ("-q", "--quiet", action="store_false", dest="verbose", default=True)
  32. parser.add_option ("--force", action="store_true", dest="force", default=False,
  33. help="Force operation, even if may damage other VMs (eg shutdown of NetVM)")
  34. parser.add_option ("--wait", action="store_true", dest="wait_for_shutdown", default=False,
  35. help="Wait for the VM(s) to shutdown")
  36. parser.add_option("--wait-time", action="store", dest="wait_time",
  37. default=defaults["shutdown_counter_max"],
  38. help="Timout after which VM will be killed when --wait "
  39. "is used")
  40. parser.add_option ("--all", action="store_true", dest="shutdown_all", default=False,
  41. help="Shutdown all running VMs")
  42. parser.add_option ("--exclude", action="append", dest="exclude_list",
  43. help="When --all is used: exclude this VM name (may be "
  44. "repeated)")
  45. (options, args) = parser.parse_args ()
  46. if not options.shutdown_all and (len (args) != 1):
  47. parser.error ("You must specify VM name!")
  48. qvm_collection = QubesVmCollection()
  49. qvm_collection.lock_db_for_reading()
  50. qvm_collection.load()
  51. qvm_collection.unlock_db()
  52. vms_list = []
  53. if options.shutdown_all:
  54. all_vms = [vm for vm in qvm_collection.values()]
  55. for vm in all_vms:
  56. if options.exclude_list is not None and vm.name in options.exclude_list:
  57. continue
  58. if vm.qid == 0:
  59. continue
  60. if vm.is_running():
  61. vms_list.append (vm)
  62. else:
  63. vmname = args[0]
  64. vm = qvm_collection.get_vm_by_name(vmname)
  65. if vm is None:
  66. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system!".format(vmname)
  67. exit(1)
  68. vms_list.append(vm)
  69. # Check VMs network dependencies
  70. if not options.force:
  71. for vm in vms_list:
  72. if vm.is_netvm():
  73. for connected_vm in vm.connected_vms.values():
  74. if connected_vm.is_running() and not connected_vm in vms_list:
  75. print >> sys.stderr, "ERROR: There are other VMs connected to VM '%s'" % vm.name
  76. exit(1)
  77. for vm in vms_list:
  78. try:
  79. if options.verbose:
  80. print >> sys.stderr, "Shutting down VM: '{0}'...".format(vm.name)
  81. # Dependencies already checked above
  82. vm.shutdown(force=True)
  83. except (IOError, OSError, QubesException) as err:
  84. print >> sys.stderr, "ERROR: {0}".format(err)
  85. exit (1)
  86. if options.wait_for_shutdown:
  87. if options.verbose:
  88. print >> sys.stderr, "Waiting for the VM(s) to shutdown..."
  89. shutdown_counter = 0
  90. halting_vms = []
  91. while len (vms_list):
  92. if options.verbose:
  93. print >> sys.stderr, "Waiting for VMs: ", [vm.name for vm in vms_list]
  94. for vm in vms_list:
  95. if not vm.is_running():
  96. vms_list.remove (vm)
  97. continue
  98. if vm.get_power_state() == "Halting":
  99. if vm in halting_vms:
  100. vm.force_shutdown()
  101. continue
  102. else:
  103. halting_vms.append(vm)
  104. if shutdown_counter > int(options.wait_time):
  105. # kill the VM
  106. if options.verbose:
  107. print >> sys.stderr, "Killing the (apparently hanging) VM '{0}'...".format(vm.name)
  108. vm.force_shutdown()
  109. #vms_list.remove(vm)
  110. shutdown_counter += 1
  111. time.sleep (1)
  112. main()