qvm_shutdown.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python2
  2. # vim: fileencoding=utf8
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2010-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2011-2016 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. # Copyright (C) 2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. from __future__ import print_function
  26. import qubes.config
  27. import qubes.tools
  28. import sys
  29. import time
  30. parser = qubes.tools.QubesArgumentParser(
  31. description='gracefully shut down a qube',
  32. want_vm=True,
  33. want_vm_all=True)
  34. parser.add_argument('--force',
  35. action='store_true', default=False,
  36. help='force operation, even if may damage other VMs (eg. shutdown of'
  37. ' network provider)')
  38. parser.add_argument('--wait',
  39. action='store_true', default=False,
  40. help='wait for the VMs to shut down')
  41. parser.add_argument('--timeout',
  42. action='store', type=float,
  43. default=qubes.config.defaults['shutdown_counter_max'],
  44. help='timeout after which domains are killed when using --wait'
  45. ' (default: %d)')
  46. def main(args=None):
  47. args = parser.parse_args(args)
  48. for vm in args.vm:
  49. vm.shutdown(force=args.force)
  50. if not args.wait:
  51. return
  52. timeout = args.timeout
  53. current_vms = list(sorted(args.vm))
  54. while timeout >= 0:
  55. current_vms = [vm for vm in current_vms
  56. if vm.get_power_state() != 'Halted']
  57. args.app.log.info('Waiting for shutdown ({}): {}'.format(
  58. timeout, ', '.join(map(str, current_vms))))
  59. time.sleep(1)
  60. timeout -= 1
  61. args.app.log.notice(
  62. 'Killing remaining qubes: {}'.format(', '.join(map(str, current_vms))))
  63. for vm in current_vms:
  64. vm.force_shutdown()
  65. if __name__ == '__main__':
  66. sys.exit(main())