qvm-shutdown 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 ("--all", action="store_true", dest="shutdown_all", default=False,
  37. help="Shutdown all running VMs")
  38. parser.add_option ("--exclude", action="append", dest="exclude_list",
  39. help="When --all is used: exclude this VM name (may be "
  40. "repeated)")
  41. (options, args) = parser.parse_args ()
  42. if not options.shutdown_all and (len (args) != 1):
  43. parser.error ("You must specify VM name!")
  44. qvm_collection = QubesVmCollection()
  45. qvm_collection.lock_db_for_reading()
  46. qvm_collection.load()
  47. qvm_collection.unlock_db()
  48. vms_list = []
  49. if options.shutdown_all:
  50. all_vms = [vm for vm in qvm_collection.values()]
  51. for vm in all_vms:
  52. if options.exclude_list is not None and vm.name in options.exclude_list:
  53. continue
  54. if vm.qid == 0:
  55. continue
  56. if vm.is_running():
  57. vms_list.append (vm)
  58. else:
  59. vmname = args[0]
  60. vm = qvm_collection.get_vm_by_name(vmname)
  61. if vm is None:
  62. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system!".format(vmname)
  63. exit(1)
  64. vms_list.append(vm)
  65. # Check VMs network dependencies
  66. if not options.force:
  67. for vm in vms_list:
  68. if vm.is_netvm():
  69. for connected_vm in vm.connected_vms.values():
  70. if connected_vm.is_running() and not connected_vm in vms_list:
  71. print >> sys.stderr, "ERROR: There are other VMs connected to VM '%s'" % vm.name
  72. exit(1)
  73. for vm in vms_list:
  74. try:
  75. if options.verbose:
  76. print >> sys.stderr, "Shutting down VM: '{0}'...".format(vm.name)
  77. # Dependencies already checked above
  78. vm.shutdown(force=True)
  79. except (IOError, OSError, QubesException) as err:
  80. print >> sys.stderr, "ERROR: {0}".format(err)
  81. exit (1)
  82. if options.wait_for_shutdown:
  83. if options.verbose:
  84. print >> sys.stderr, "Waiting for the VM(s) to shutdown..."
  85. shutdown_counter = 0
  86. halting_vms = []
  87. while len (vms_list):
  88. if options.verbose:
  89. print >> sys.stderr, "Waiting for VMs: ", [vm.name for vm in vms_list]
  90. for vm in vms_list:
  91. if not vm.is_running():
  92. vms_list.remove (vm)
  93. continue
  94. if vm.get_power_state() == "Halting":
  95. if vm in halting_vms:
  96. vm.force_shutdown()
  97. continue
  98. else:
  99. halting_vms.append(vm)
  100. if shutdown_counter > defaults["shutdown_counter_max"]:
  101. # kill the VM
  102. if options.verbose:
  103. print >> sys.stderr, "Killing the (apparently hanging) VM '{0}'...".format(vm.name)
  104. vm.force_shutdown()
  105. #vms_list.remove(vm)
  106. shutdown_counter += 1
  107. time.sleep (1)
  108. main()