qvm_shutdown.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if have_events:
  47. loop = asyncio.get_event_loop()
  48. remaining_domains = args.domains
  49. for _ in range(len(args.domains)):
  50. this_round_domains = set(remaining_domains)
  51. if not this_round_domains:
  52. break
  53. remaining_domains = set()
  54. for vm in this_round_domains:
  55. try:
  56. vm.shutdown()
  57. except qubesadmin.exc.QubesVMNotRunningError:
  58. pass
  59. except qubesadmin.exc.QubesException as e:
  60. if not args.wait:
  61. vm.log.error('Shutdown error: {}'.format(e))
  62. else:
  63. remaining_domains.add(vm)
  64. if not args.wait:
  65. return len(remaining_domains)
  66. this_round_domains.difference_update(remaining_domains)
  67. if not this_round_domains:
  68. # no VM shutdown request succeed, no sense to try again
  69. break
  70. if have_events:
  71. try:
  72. # pylint: disable=no-member
  73. loop.run_until_complete(asyncio.wait_for(
  74. qubesadmin.events.utils.wait_for_domain_shutdown(
  75. this_round_domains),
  76. args.timeout))
  77. except asyncio.TimeoutError:
  78. for vm in this_round_domains:
  79. try:
  80. vm.kill()
  81. except qubesadmin.exc.QubesVMNotStartedError:
  82. # already shut down
  83. pass
  84. except qubesadmin.exc.QubesException as e:
  85. parser.error_runtime(e)
  86. else:
  87. timeout = args.timeout
  88. current_vms = list(sorted(this_round_domains))
  89. while timeout >= 0:
  90. current_vms = [vm for vm in current_vms
  91. if vm.get_power_state() != 'Halted']
  92. if not current_vms:
  93. break
  94. args.app.log.info('Waiting for shutdown ({}): {}'.format(
  95. timeout, ', '.join([str(vm) for vm in current_vms])))
  96. time.sleep(1)
  97. timeout -= 1
  98. if current_vms:
  99. args.app.log.info(
  100. 'Killing remaining qubes: {}'
  101. .format(', '.join([str(vm) for vm in current_vms])))
  102. for vm in current_vms:
  103. try:
  104. vm.kill()
  105. except qubesadmin.exc.QubesVMNotStartedError:
  106. # already shut down
  107. pass
  108. except qubesadmin.exc.QubesException as e:
  109. parser.error_runtime(e)
  110. if args.wait:
  111. if have_events:
  112. loop.close()
  113. return len([vm for vm in args.domains
  114. if vm.get_power_state() != 'Halted'])
  115. if __name__ == '__main__':
  116. sys.exit(main())