qvm_shutdown.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # encoding=utf-8
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2011-2016 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. # Copyright (C) 2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU Lesser General Public License as published by
  12. # the Free Software Foundation; either version 2.1 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU Lesser General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU Lesser General Public License along
  21. # with this program; if not, see <http://www.gnu.org/licenses/>.
  22. ''' Shutdown a qube '''
  23. from __future__ import print_function
  24. import sys
  25. import time
  26. import asyncio
  27. try:
  28. import qubesadmin.events.utils
  29. have_events = True
  30. except ImportError:
  31. have_events = False
  32. import qubesadmin.tools
  33. import qubesadmin.exc
  34. parser = qubesadmin.tools.QubesArgumentParser(
  35. description=__doc__, vmname_nargs='+')
  36. parser.add_argument('--wait',
  37. action='store_true', default=False,
  38. help='wait for the VMs to shut down')
  39. parser.add_argument('--timeout',
  40. action='store', type=float,
  41. default=60,
  42. help='timeout after which domains are killed when using --wait'
  43. ' (default: %(default)d)')
  44. parser.add_argument(
  45. '--force',
  46. action='store_true', default=False,
  47. help='force shutdown regardless of connected domains; use with caution')
  48. def main(args=None, app=None): # pylint: disable=missing-docstring
  49. args = parser.parse_args(args, app=app)
  50. force = args.force or bool(args.all_domains)
  51. if have_events:
  52. loop = asyncio.get_event_loop()
  53. remaining_domains = args.domains
  54. for _ in range(len(args.domains)):
  55. this_round_domains = set(remaining_domains)
  56. if not this_round_domains:
  57. break
  58. remaining_domains = set()
  59. for vm in this_round_domains:
  60. try:
  61. vm.shutdown(force=force)
  62. except qubesadmin.exc.QubesVMNotRunningError:
  63. pass
  64. except qubesadmin.exc.QubesException as e:
  65. if not args.wait:
  66. vm.log.error('Shutdown error: {}'.format(e))
  67. else:
  68. remaining_domains.add(vm)
  69. if not args.wait:
  70. if remaining_domains:
  71. parser.error_runtime(
  72. 'Failed to shut down: ' +
  73. ', '.join(vm.name for vm in remaining_domains),
  74. len(remaining_domains))
  75. return
  76. this_round_domains.difference_update(remaining_domains)
  77. if not this_round_domains:
  78. # no VM shutdown request succeed, no sense to try again
  79. break
  80. if have_events:
  81. try:
  82. # pylint: disable=no-member
  83. loop.run_until_complete(asyncio.wait_for(
  84. qubesadmin.events.utils.wait_for_domain_shutdown(
  85. this_round_domains),
  86. args.timeout))
  87. except asyncio.TimeoutError:
  88. for vm in this_round_domains:
  89. try:
  90. vm.kill()
  91. except qubesadmin.exc.QubesVMNotStartedError:
  92. # already shut down
  93. pass
  94. except qubesadmin.exc.QubesException as e:
  95. parser.error_runtime(e)
  96. else:
  97. timeout = args.timeout
  98. current_vms = list(sorted(this_round_domains))
  99. while timeout >= 0:
  100. current_vms = [vm for vm in current_vms
  101. if vm.get_power_state() != 'Halted']
  102. if not current_vms:
  103. break
  104. args.app.log.info('Waiting for shutdown ({}): {}'.format(
  105. timeout, ', '.join([str(vm) for vm in current_vms])))
  106. time.sleep(1)
  107. timeout -= 1
  108. if current_vms:
  109. args.app.log.info(
  110. 'Killing remaining qubes: {}'
  111. .format(', '.join([str(vm) for vm in current_vms])))
  112. for vm in current_vms:
  113. try:
  114. vm.kill()
  115. except qubesadmin.exc.QubesVMNotStartedError:
  116. # already shut down
  117. pass
  118. except qubesadmin.exc.QubesException as e:
  119. parser.error_runtime(e)
  120. if args.wait:
  121. if have_events:
  122. loop.close()
  123. failed = []
  124. for vm in args.domains:
  125. power_state = vm.get_power_state()
  126. # DispVM might have been deleted before we check them,
  127. # so NA is acceptable.
  128. if not (power_state == 'Halted' or
  129. (vm.klass == 'DispVM' and power_state == 'NA')):
  130. failed.append(vm)
  131. if failed:
  132. parser.error_runtime(
  133. 'Failed to shut down: ' +
  134. ', '.join(vm.name for vm in failed),
  135. len(failed))
  136. if __name__ == '__main__':
  137. sys.exit(main())