qvm_shutdown.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. def main(args=None, app=None): # pylint: disable=missing-docstring
  45. args = parser.parse_args(args, app=app)
  46. force = bool(args.all_domains)
  47. if have_events:
  48. loop = asyncio.get_event_loop()
  49. remaining_domains = args.domains
  50. for _ in range(len(args.domains)):
  51. this_round_domains = set(remaining_domains)
  52. if not this_round_domains:
  53. break
  54. remaining_domains = set()
  55. for vm in this_round_domains:
  56. try:
  57. vm.shutdown(force=force)
  58. except qubesadmin.exc.QubesVMNotRunningError:
  59. pass
  60. except qubesadmin.exc.QubesException as e:
  61. if not args.wait:
  62. vm.log.error('Shutdown error: {}'.format(e))
  63. else:
  64. remaining_domains.add(vm)
  65. if not args.wait:
  66. if remaining_domains:
  67. parser.error_runtime(
  68. 'Failed to shut down: ' +
  69. ', '.join(vm.name for vm in remaining_domains),
  70. len(remaining_domains))
  71. return
  72. this_round_domains.difference_update(remaining_domains)
  73. if not this_round_domains:
  74. # no VM shutdown request succeed, no sense to try again
  75. break
  76. if have_events:
  77. try:
  78. # pylint: disable=no-member
  79. loop.run_until_complete(asyncio.wait_for(
  80. qubesadmin.events.utils.wait_for_domain_shutdown(
  81. this_round_domains),
  82. args.timeout))
  83. except asyncio.TimeoutError:
  84. for vm in this_round_domains:
  85. try:
  86. vm.kill()
  87. except qubesadmin.exc.QubesVMNotStartedError:
  88. # already shut down
  89. pass
  90. except qubesadmin.exc.QubesException as e:
  91. parser.error_runtime(e)
  92. else:
  93. timeout = args.timeout
  94. current_vms = list(sorted(this_round_domains))
  95. while timeout >= 0:
  96. current_vms = [vm for vm in current_vms
  97. if vm.get_power_state() != 'Halted']
  98. if not current_vms:
  99. break
  100. args.app.log.info('Waiting for shutdown ({}): {}'.format(
  101. timeout, ', '.join([str(vm) for vm in current_vms])))
  102. time.sleep(1)
  103. timeout -= 1
  104. if current_vms:
  105. args.app.log.info(
  106. 'Killing remaining qubes: {}'
  107. .format(', '.join([str(vm) for vm in current_vms])))
  108. for vm in current_vms:
  109. try:
  110. vm.kill()
  111. except qubesadmin.exc.QubesVMNotStartedError:
  112. # already shut down
  113. pass
  114. except qubesadmin.exc.QubesException as e:
  115. parser.error_runtime(e)
  116. if args.wait:
  117. if have_events:
  118. loop.close()
  119. failed = []
  120. for vm in args.domains:
  121. power_state = vm.get_power_state()
  122. # DispVM might have been deleted before we check them,
  123. # so NA is acceptable.
  124. if not (power_state == 'Halted' or
  125. (vm.klass == 'DispVM' and power_state == 'NA')):
  126. failed.append(vm)
  127. if failed:
  128. parser.error_runtime(
  129. 'Failed to shut down: ' +
  130. ', '.join(vm.name for vm in failed),
  131. len(failed))
  132. if __name__ == '__main__':
  133. sys.exit(main())