qvm_shutdown.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 sys
  27. import time
  28. import qubes.config
  29. import qubes.tools
  30. parser = qubes.tools.QubesArgumentParser(
  31. description='gracefully shut down a qube', vmname_nargs='+')
  32. parser.add_argument('--force',
  33. action='store_true', default=False,
  34. help='force operation, even if may damage other VMs (eg. shutdown of'
  35. ' network provider)')
  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=qubes.config.defaults['shutdown_counter_max'],
  42. help='timeout after which domains are killed when using --wait'
  43. ' (default: %d)')
  44. def main(args=None):
  45. args = parser.parse_args(args)
  46. for vm in args.domains:
  47. vm.shutdown(force=args.force)
  48. if not args.wait:
  49. return
  50. timeout = args.timeout
  51. current_vms = list(sorted(args.domains))
  52. while timeout >= 0:
  53. current_vms = [vm for vm in current_vms
  54. if vm.get_power_state() != 'Halted']
  55. args.app.log.info('Waiting for shutdown ({}): {}'.format(
  56. timeout, ', '.join(map(str, current_vms))))
  57. time.sleep(1)
  58. timeout -= 1
  59. args.app.log.notice(
  60. 'Killing remaining qubes: {}'.format(', '.join(map(str, current_vms))))
  61. for vm in current_vms:
  62. vm.force_shutdown()
  63. if __name__ == '__main__':
  64. sys.exit(main())