qvm-shutdown 4.3 KB

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