qvm-shutdown 4.3 KB

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